Author: Ronan Lamy <ronan.l...@gmail.com>
Branch: rffi-parser-2
Changeset: r89481:5ce867e5a994
Date: 2017-01-10 19:46 +0000
http://bitbucket.org/pypy/pypy/changeset/5ce867e5a994/

Log:    Parse function declarations

diff --git a/pypy/module/cpyext/cparser.py b/pypy/module/cpyext/cparser.py
--- a/pypy/module/cpyext/cparser.py
+++ b/pypy/module/cpyext/cparser.py
@@ -783,6 +783,16 @@
             result = result.TYPE
         return result
 
+    def parse_func(self, cdecl):
+        cdecl = cdecl.strip()
+        if cdecl[-1] != ';':
+            cdecl += ';'
+        ast, _, _ = self.ctx._parse(cdecl)
+        decl = ast.ext[-1]
+        tp, quals = self.ctx._get_type_and_quals(decl.type, name=decl.name)
+        FUNCP = self.convert_type(tp.as_function_pointer())
+        return decl.name, FUNCP.TO
+
 
 def parse_source(source, includes=None, eci=None, configure_now=False):
     ctx = Parser()
diff --git a/pypy/module/cpyext/test/test_cparser.py 
b/pypy/module/cpyext/test/test_cparser.py
--- a/pypy/module/cpyext/test/test_cparser.py
+++ b/pypy/module/cpyext/test/test_cparser.py
@@ -163,3 +163,28 @@
     res = parse_source(decl, eci=eci, configure_now=True)
     assert res.gettype('Py_ssize_t') == rffi.SSIZE_T
     assert res.gettype('TestFloatObject *').TO.c_ob_refcnt == rffi.SSIZE_T
+
+def test_parse_funcdecl(tmpdir):
+    decl = """
+    typedef ssize_t Py_ssize_t;
+
+    #define PyObject_HEAD  \
+        Py_ssize_t ob_refcnt;        \
+        Py_ssize_t ob_pypy_link;     \
+
+    typedef struct {
+        PyObject_HEAD
+        double ob_fval;
+    } TestFloatObject;
+
+    typedef TestFloatObject* (*func_t)(int, int);
+    """
+    hdr = tmpdir / 'header.h'
+    hdr.write(decl)
+    eci = ExternalCompilationInfo(
+        include_dirs=[str(tmpdir)], includes=['sys/types.h', 'header.h'])
+    res = parse_source(decl, eci=eci, configure_now=True)
+    name, FUNC = res.parse_func("func_t some_func(TestFloatObject*)")
+    assert name == 'some_func'
+    assert FUNC.RESULT == res.gettype('func_t')
+    assert FUNC.ARGS == (res.gettype('TestFloatObject *'),)
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to