[pypy-svn] r36027 - in pypy/dist/pypy: rlib/rctypes rlib/rctypes/test rpython

arigo at codespeak.net arigo at codespeak.net
Thu Dec 28 19:02:56 CET 2006


Author: arigo
Date: Thu Dec 28 19:02:53 2006
New Revision: 36027

Added:
   pypy/dist/pypy/rlib/rctypes/test/test_rpointer.py
      - copied, changed from r36026, pypy/dist/pypy/rpython/rctypes/test/test_rpointer.py
Modified:
   pypy/dist/pypy/rlib/rctypes/implementation.py
   pypy/dist/pypy/rlib/rctypes/rbuiltin.py
   pypy/dist/pypy/rlib/rctypes/rpointer.py
   pypy/dist/pypy/rlib/rctypes/test/test_rprimitive.py
   pypy/dist/pypy/rpython/controllerentry.py
Log:
Annotate ctypes.pointer tests.  Some hacking in controllerentry
to add an explicit way to "convert" between SomeControlledInstance
and its underlying, revealed object.


Modified: pypy/dist/pypy/rlib/rctypes/implementation.py
==============================================================================
--- pypy/dist/pypy/rlib/rctypes/implementation.py	(original)
+++ pypy/dist/pypy/rlib/rctypes/implementation.py	Thu Dec 28 19:02:53 2006
@@ -1,3 +1,4 @@
+import py
 from pypy.annotation import model as annmodel
 from pypy.rlib.rctypes import rctypesobject
 from pypy.rpython import extregistry, controllerentry
@@ -8,10 +9,13 @@
 from pypy.rpython.lltypesystem import lltype
 from pypy.rpython.rctypes import rcarithmetic as rcarith
 
-import ctypes
-if ctypes.__version__ < '0.9.9.6':  # string comparison... good enough?
-    raise ImportError("requires ctypes >= 0.9.9.6, got %s" % (
-        ctypes.__version__,))
+try:
+    import ctypes
+    if ctypes.__version__ < '0.9.9.6':  # string comparison... good enough?
+        raise ImportError("requires ctypes >= 0.9.9.6, got %s" % (
+            ctypes.__version__,))
+except ImportError, e:
+    py.test.skip(str(e))
 
 
 class CTypeController(Controller):
@@ -49,9 +53,7 @@
             self.initialize_prebuilt(obj, x)
             return obj
 
-    def return_value(self, obj):
-        return obj
-    return_value._annspecialcase_ = 'specialize:arg(0)'
+    return_value = Controller.box
 
     # extension to the setattr/setitem support: if the new value is actually
     # a CTypeControlled instance as well, reveal it automatically (i.e. turn

Modified: pypy/dist/pypy/rlib/rctypes/rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rlib/rctypes/rbuiltin.py	(original)
+++ pypy/dist/pypy/rlib/rctypes/rbuiltin.py	Thu Dec 28 19:02:53 2006
@@ -1,6 +1,7 @@
 from pypy.annotation import model as annmodel
 from pypy.rpython.lltypesystem import lltype
 from pypy.rpython.error import TyperError
+from pypy.rpython.extregistry import ExtRegistryEntry
 from pypy.rpython.controllerentry import SomeControlledInstance
 from pypy.rlib.rctypes.implementation import getcontroller
 from pypy.rlib.rctypes.implementation import register_function_impl
@@ -17,6 +18,32 @@
                                                        s_obj.controller.ctype))
 
 #
+# POINTER()
+#
+class Entry(ExtRegistryEntry):
+    "Annotation and rtyping of calls to ctypes.POINTER(): constant-folded."
+    _about_ = ctypes.POINTER
+
+    def compute_result_annotation(self, s_arg):
+        # POINTER(constant_ctype) returns the constant annotation
+        # corresponding to the POINTER(ctype).
+        assert s_arg.is_constant(), (
+            "POINTER(%r): argument must be constant" % (s_arg,))
+        RESTYPE = ctypes.POINTER(s_arg.const)
+            # POINTER(varsized_array_type): given that rctypes performs
+            # no index checking, this pointer-to-array type is equivalent
+            # to a pointer to an array of whatever size.
+            # ('0' is a bad idea, though, as FixedSizeArrays of length 0
+            # tend to say they have impossible items.)
+            #XXX: RESTYPE = POINTER(s_arg.ctype_array._type_ * 1)
+        return self.bookkeeper.immutablevalue(RESTYPE)
+
+    def specialize_call(self, hop):
+        assert hop.s_result.is_constant()
+        hop.exception_cannot_occur()
+        return hop.inputconst(lltype.Void, hop.s_result.const)
+
+#
 # sizeof()
 #
 sizeof_base_entry = register_function_impl(ctypes.sizeof, rctypesobject.sizeof,
@@ -43,4 +70,5 @@
             controller = getcontroller(ctype)
             real_obj = controller.convert(sample)
             size = rctypesobject.sizeof(real_obj)
+            hop.exception_cannot_occur()
             return hop.inputconst(lltype.Signed, size)

Modified: pypy/dist/pypy/rlib/rctypes/rpointer.py
==============================================================================
--- pypy/dist/pypy/rlib/rctypes/rpointer.py	(original)
+++ pypy/dist/pypy/rlib/rctypes/rpointer.py	Thu Dec 28 19:02:53 2006
@@ -17,11 +17,16 @@
         self.knowntype = rctypesobject.RPointer(
             self.contentscontroller.knowntype)
 
-    def new(self):
+    def new(self, ptrto=None):
         obj = self.knowntype.allocate()
-        #...
+        if ptrto is not None:
+            obj.set_contents(self.contentscontroller.unbox(ptrto))
         return obj
 
+    def initialize_prebuilt(self, obj, x):
+        contentsbox = self.contentscontroller.convert(x.contents)
+        self.setbox_contents(obj, contentsbox)
+
     def getitem(self, obj, index):
         if index != 0:
             raise ValueError("can only access item 0 of pointers")
@@ -37,4 +42,20 @@
         self.contentscontroller.set_value(contentsobj, value)
     setitem._annspecialcase_ = 'specialize:arg(0)'
 
+    def setboxitem(self, obj, index, valuebox):
+        if index != 0:
+            raise ValueError("assignment to pointer[x] with x != 0")
+            # not supported by ctypes either
+        contentsobj = obj.get_contents()
+        contentsobj.copyfrom(valuebox)
+    setitem._annspecialcase_ = 'specialize:arg(0)'
+
+    def get_contents(self, obj):
+        return self.contentscontroller.box(obj.get_contents())
+    get_contents._annspecialcase_ = 'specialize:arg(0)'
+
+    def setbox_contents(self, obj, contentsbox):
+        obj.set_contents(contentsbox)
+    setbox_contents._annspecialcase_ = 'specialize:arg(0)'
+
 PointerCTypeController.register_for_metatype(PointerType)

Modified: pypy/dist/pypy/rlib/rctypes/test/test_rprimitive.py
==============================================================================
--- pypy/dist/pypy/rlib/rctypes/test/test_rprimitive.py	(original)
+++ pypy/dist/pypy/rlib/rctypes/test/test_rprimitive.py	Thu Dec 28 19:02:53 2006
@@ -6,12 +6,12 @@
 import pypy.rlib.rctypes.implementation
 from pypy.annotation.annrpython import RPythonAnnotator
 from pypy.annotation import policy
-from pypy.annotation.model import SomeCTypesObject, SomeObject
 from pypy import conftest
 import sys
 from pypy.rpython.test.test_llinterp import interpret
 from pypy.rlib.rarithmetic import r_longlong, r_ulonglong
 from pypy.rpython.lltypesystem import lltype
+
 from ctypes import c_char, c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint
 from ctypes import c_long, c_ulong, c_longlong, c_ulonglong, c_float
 from ctypes import c_double, c_wchar, c_char_p, pointer, sizeof
@@ -20,7 +20,7 @@
 test_c_compile = True
 test_llvm_compile = False
 
-class Test_annotation:
+class BaseTestAnnotation:
     def build_types(self, func, argtypes):
         P = policy.AnnotatorPolicy()
         P.allow_someobjects = False
@@ -30,6 +30,7 @@
             a.translator.view()
         return s
 
+class Test_annotation(BaseTestAnnotation):
     def test_simple(self):
         res = c_int(42)
         assert res.value == 42 

Modified: pypy/dist/pypy/rpython/controllerentry.py
==============================================================================
--- pypy/dist/pypy/rpython/controllerentry.py	(original)
+++ pypy/dist/pypy/rpython/controllerentry.py	Thu Dec 28 19:02:53 2006
@@ -42,6 +42,14 @@
     def _freeze_(self):
         return True
 
+    def box(self, obj):
+        return controlled_instance_box(self, obj)
+    box._annspecialcase_ = 'specialize:arg(0)'
+
+    def unbox(self, obj):
+        return controlled_instance_unbox(self, obj)
+    unbox._annspecialcase_ = 'specialize:arg(0)'
+
     def ctrl_new(self, *args_s):
         s_real_obj = delegate(self.new, *args_s)
         if s_real_obj == annmodel.s_ImpossibleValue:
@@ -103,6 +111,41 @@
     return bk.emulate_pbc_call(bk.position_key, s_meth, args_s,
                                callback = bk.position_key)
 
+def controlled_instance_box(controller, obj):
+    XXX
+
+def controlled_instance_unbox(controller, obj):
+    XXX
+
+class BoxEntry(ExtRegistryEntry):
+    _about_ = controlled_instance_box
+
+    def compute_result_annotation(self, s_controller, s_real_obj):
+        if s_real_obj == annmodel.s_ImpossibleValue:
+            return annmodel.s_ImpossibleValue
+        else:
+            assert s_controller.is_constant()
+            controller = s_controller.const
+            return SomeControlledInstance(s_real_obj, controller=controller)
+
+    def specialize_call(self, hop):
+        [v] = hop.inputargs(hop.r_result)
+        return v
+
+class UnboxEntry(ExtRegistryEntry):
+    _about_ = controlled_instance_unbox
+
+    def compute_result_annotation(self, s_controller, s_obj):
+        if s_obj == annmodel.s_ImpossibleValue:
+            return annmodel.s_ImpossibleValue
+        else:
+            assert isinstance(s_obj, SomeControlledInstance)
+            return s_obj.s_real_obj
+
+    def specialize_call(self, hop):
+        [v] = hop.inputargs(hop.r_result)
+        return v
+
 # ____________________________________________________________
 
 class SomeControlledInstance(annmodel.SomeObject):



More information about the Pypy-commit mailing list