[pypy-svn] r25847 - in pypy/dist/pypy/rpython/rctypes: . test

arigo at codespeak.net arigo at codespeak.net
Sat Apr 15 11:50:21 CEST 2006


Author: arigo
Date: Sat Apr 15 11:50:20 2006
New Revision: 25847

Modified:
   pypy/dist/pypy/rpython/rctypes/rpointer.py
   pypy/dist/pypy/rpython/rctypes/test/test_rpointer.py
Log:
Support for in-function POINTER() calls -- with a constant argument
only, as usual.


Modified: pypy/dist/pypy/rpython/rctypes/rpointer.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rpointer.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rpointer.py	Sat Apr 15 11:50:20 2006
@@ -146,3 +146,19 @@
 extregistry.register_value(byref,
         compute_result_annotation=pointerfn_compute_annotation,
         specialize_call=pointertype_specialize_call)
+
+# constant-fold POINTER(CONSTANT_CTYPE) calls
+def POINTER_compute_annotation(s_arg):
+    from pypy.annotation.bookkeeper import getbookkeeper
+    assert s_arg.is_constant(), "POINTER(%r): argument must be constant" % (
+        s_arg,)
+    RESTYPE = POINTER(s_arg.const)
+    return getbookkeeper().immutablevalue(RESTYPE)
+
+def POINTER_specialize_call(hop):
+    assert hop.s_result.is_constant()
+    return hop.inputconst(lltype.Void, hop.s_result.const)
+
+extregistry.register_value(POINTER,
+        compute_result_annotation=POINTER_compute_annotation,
+        specialize_call=POINTER_specialize_call)

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rpointer.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rpointer.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rpointer.py	Sat Apr 15 11:50:20 2006
@@ -114,6 +114,20 @@
         if conftest.option.view:
             t.view()
 
+    def test_annotate_POINTER(self):
+        def fn():
+            p = POINTER(c_float)()
+            p.contents = c_float(6.1)
+            return p
+
+        t = TranslationContext()
+        a = t.buildannotator()
+        s = a.build_types(fn, [])
+        assert s.knowntype == POINTER(c_float)
+
+        if conftest.option.view:
+            t.view()
+
 
 class Test_specialization:
     def test_specialize_c_int_ptr(self):
@@ -227,3 +241,13 @@
 
         res = interpret(fn, [])
         assert res == 12
+
+    def test_specialize_POINTER(self):
+        def fn():
+            p = POINTER(c_float)()
+            p.contents = c_float(6.25)
+            return p
+
+        res = interpret(fn, [])
+        float_c_data = res.c_data[0]
+        assert float_c_data[0] == 6.25



More information about the Pypy-commit mailing list