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

arigo at codespeak.net arigo at codespeak.net
Tue Apr 4 17:08:06 CEST 2006


Author: arigo
Date: Tue Apr  4 17:08:05 2006
New Revision: 25302

Modified:
   pypy/dist/pypy/rpython/rctypes/rpointer.py
   pypy/dist/pypy/rpython/rctypes/test/test_rpointer.py
Log:
(arre, arigo)

Support for calling pointer().


Modified: pypy/dist/pypy/rpython/rctypes/rpointer.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rpointer.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rpointer.py	Tue Apr  4 17:08:05 2006
@@ -4,7 +4,7 @@
 from pypy.annotation import model as annmodel
 from pypy.rpython.rctypes.rmodel import CTypesValueRepr
 
-from ctypes import POINTER, c_int
+from ctypes import POINTER, pointer, c_int
 
 class PointerRepr(CTypesValueRepr):
     def __init__(self, rtyper, s_pointer, s_contents):
@@ -150,3 +150,16 @@
         compute_annotation=pointerinstance_compute_annotation,
         get_repr=pointerinstance_get_repr)
 entry.get_field_annotation = pointerinstance_field_annotation
+
+def pointerfn_compute_annotation(s_arg):
+    assert isinstance(s_arg, annmodel.SomeCTypesObject)
+    ctype = s_arg.knowntype
+    result_ctype = POINTER(ctype)
+    return annmodel.SomeCTypesObject(result_ctype,
+                                     annmodel.SomeCTypesObject.OWNSMEMORY)
+
+extregistry.register_value(pointer,
+        compute_result_annotation=pointerfn_compute_annotation,
+        # same rtyping for calling pointer() or calling a specific instance
+        # of PointerType:
+        specialize_call=pointertype_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	Tue Apr  4 17:08:05 2006
@@ -7,7 +7,7 @@
 from pypy import conftest
 from pypy.rpython.test.test_llinterp import interpret
 
-from ctypes import c_int, c_float, POINTER
+from ctypes import c_int, c_float, POINTER, pointer
 
 import pypy.rpython.rctypes.implementation
 
@@ -49,6 +49,21 @@
 
         assert s.knowntype == float
 
+    def test_annotate_pointer_fn(self):
+        def func():
+            p = pointer(c_int(123))
+            return p.contents.value
+
+        t = TranslationContext()
+        a = t.buildannotator()
+        s = a.build_types(func, [])
+
+        if conftest.option.view:
+            t.view()
+
+        assert s.knowntype == int
+
+
 class Test_specialization:
     def test_specialize_c_int_ptr(self):
         ptrtype = POINTER(c_int)
@@ -94,19 +109,28 @@
         ptrptrtype = POINTER(ptrtype)
         def func(n):
             if n == 1:
-                x = c_int(6)
-                p1 = ptrtype(x)
+                p1 = ptrtype(c_int(6))
                 p2 = ptrptrtype(p1)
             else:
                 if n == 2:
-                    y = c_int(7)
-                    p1 = ptrtype(y)
+                    p1 = ptrtype(c_int(7))
                 else:
-                    z = c_int(8)
-                    p1 = ptrtype(z)
+                    p1 = ptrtype(c_int(8))
                 p2 = ptrptrtype(p1)
-            p2.contents.contents.value *= 6
+            del p1
+            p3 = ptrptrtype(p2.contents)
+            p3.contents.contents.value *= 6
             return p2.contents.contents.value
 
+        assert func(2) == 42
         res = interpret(func, [2])
         assert res == 42
+
+    def test_specialize_pointer_fn(self):
+        def func():
+            p = pointer(c_int(123))
+            return p.contents.value
+
+        assert func() == 123
+        res = interpret(func, [])
+        assert res == 123



More information about the Pypy-commit mailing list