Author: Ronan Lamy <[email protected]>
Branch: rffi-parser-2
Changeset: r89618:660f34981e06
Date: 2017-01-16 19:33 +0000
http://bitbucket.org/pypy/pypy/changeset/660f34981e06/

Log:    Make cts.gettype() RPython

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
@@ -861,6 +861,14 @@
                     "The first argument of cts.cast() must be a constant.")
             TP = self.gettype(v_decl.value)
             return ctx.appcall(rffi.cast, const(TP), v_arg)
+
+        @register_flow_sc(self.gettype)
+        def sc_gettype(ctx, v_decl):
+            if not isinstance(v_decl, Constant):
+                raise FlowingError(
+                    "The argument of cts.gettype() must be a constant.")
+            return const(self.gettype(v_decl.value))
+
         self._frozen = True
         return True
 
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
@@ -202,3 +202,17 @@
     op = graph.startblock.operations[0]
     assert op.args[0] == const(rffi.cast)
     assert op.args[1].value is cts.gettype('Py_ssize_t*')
+
+def test_translate_gettype():
+    cdef = "typedef ssize_t Py_ssize_t;"
+    cts = parse_source(cdef)
+
+    def f():
+        return cts.gettype('Py_ssize_t*')
+    graph = build_flow(f)
+    simplify_graph(graph)
+    # Check that the result is constant-folded
+    assert graph.startblock.operations == []
+    [link] = graph.startblock.exits
+    assert link.target is graph.returnblock
+    assert link.args[0] == const(rffi.CArrayPtr(rffi.SSIZE_T))
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to