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

arigo at codespeak.net arigo at codespeak.net
Sun Dec 31 14:48:37 CET 2006


Author: arigo
Date: Sun Dec 31 14:48:34 2006
New Revision: 36075

Modified:
   pypy/dist/pypy/rlib/rctypes/implementation.py
   pypy/dist/pypy/rlib/rctypes/rarray.py
   pypy/dist/pypy/rlib/rctypes/rpointer.py
   pypy/dist/pypy/rlib/rctypes/rprimitive.py
   pypy/dist/pypy/rlib/rctypes/rstruct.py
   pypy/dist/pypy/rlib/rctypes/test/test_rpointer.py
   pypy/dist/pypy/rpython/controllerentry.py
   pypy/dist/pypy/rpython/rcontrollerentry.py
Log:
The pointer tests pass.


Modified: pypy/dist/pypy/rlib/rctypes/implementation.py
==============================================================================
--- pypy/dist/pypy/rlib/rctypes/implementation.py	(original)
+++ pypy/dist/pypy/rlib/rctypes/implementation.py	Sun Dec 31 14:48:34 2006
@@ -55,6 +55,13 @@
 
     return_value = Controller.box
 
+    def store_box(self, obj, valuebox):
+        obj.copyfrom(valuebox)
+
+    def store_value(self, obj, value):
+        raise TypeError("cannot store a value into a non-primitive ctype")
+    store_value._annspecialcase_ = 'specialize:arg(0)'
+
     # extension to the setattr/setitem support: if the new value is actually
     # a CTypeControlled instance as well, reveal it automatically (i.e. turn
     # it into an rctypesobject) and call a method with a different name.

Modified: pypy/dist/pypy/rlib/rctypes/rarray.py
==============================================================================
--- pypy/dist/pypy/rlib/rctypes/rarray.py	(original)
+++ pypy/dist/pypy/rlib/rctypes/rarray.py	Sun Dec 31 14:48:34 2006
@@ -18,17 +18,17 @@
             self.itemcontroller.knowntype,
             self.length)
 
-    def new(self, *args):
-        obj = self.knowntype.allocate()
-        if args:
-            if len(args) > self.length:
-                raise ValueError("too many arguments for an array of "
-                                 "length %d" % (self.length,))
-            lst = list(args)
-            for i in range(len(args)):
-                self.setitem(obj, i, lst[i])
-        return obj
-    new._annspecialcase_ = 'specialize:arg(0)'
+        def arraynew(*args):
+            obj = self.knowntype.allocate()
+            if args:
+                if len(args) > self.length:
+                    raise ValueError("too many arguments for an array of "
+                                     "length %d" % (self.length,))
+                lst = list(args)
+                for i in range(len(args)):
+                    self.setitem(obj, i, lst[i])
+            return obj
+        self.new = arraynew
 
     def getitem(self, obj, i):
         itemobj = obj.ref(i)

Modified: pypy/dist/pypy/rlib/rctypes/rpointer.py
==============================================================================
--- pypy/dist/pypy/rlib/rctypes/rpointer.py	(original)
+++ pypy/dist/pypy/rlib/rctypes/rpointer.py	Sun Dec 31 14:48:34 2006
@@ -22,15 +22,14 @@
         if ptrto is not None:
             obj.set_contents(self.contentscontroller.unbox(ptrto))
         return obj
+    new._annspecialcase_ = 'specialize:arg(0)'
 
     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")
-        contentsobj = obj.get_contents()
+        contentsobj = obj.ref(index)
         return self.contentscontroller.return_value(contentsobj)
     getitem._annspecialcase_ = 'specialize:arg(0)'
 
@@ -58,4 +57,11 @@
         obj.set_contents(contentsbox)
     setbox_contents._annspecialcase_ = 'specialize:arg(0)'
 
+    def is_true(self, obj):
+        return not obj.is_null()
+    is_true._annspecialcase_ = 'specialize:arg(0)'
+
+    def store_box(self, obj, valuebox):
+        obj.set_contents(valuebox.ref(0))
+
 PointerCTypeController.register_for_metatype(PointerType)

Modified: pypy/dist/pypy/rlib/rctypes/rprimitive.py
==============================================================================
--- pypy/dist/pypy/rlib/rctypes/rprimitive.py	(original)
+++ pypy/dist/pypy/rlib/rctypes/rprimitive.py	Sun Dec 31 14:48:34 2006
@@ -40,14 +40,14 @@
         self.is_char_type = self.VALUETYPE in (lltype.Char, lltype.UniChar)
         self.knowntype = rctypesobject.Primitive(self.VALUETYPE)
 
-    def new(self, *initialvalue):
-        obj = self.knowntype.allocate()
-        if len(initialvalue) > 0:
-            if len(initialvalue) > 1:
-                raise TypeError("at most 1 argument expected")
-            self.set_value(obj, initialvalue[0])
-        return obj
-    new._annspecialcase_ = 'specialize:arg(0)'
+        def primitivenew(*initialvalue):
+            obj = self.knowntype.allocate()
+            if len(initialvalue) > 0:
+                if len(initialvalue) > 1:
+                    raise TypeError("at most 1 argument expected")
+                self.set_value(obj, initialvalue[0])
+            return obj
+        self.new = primitivenew
 
     def initialize_prebuilt(self, obj, x):
         value = x.value
@@ -78,6 +78,7 @@
     # ctypes automatically unwraps the c_xxx() of primitive types when
     # they are returned by most operations
     return_value = get_value
+    store_value = set_value
 
     def is_true(self, obj):
         llvalue = self.get_value(obj)

Modified: pypy/dist/pypy/rlib/rctypes/rstruct.py
==============================================================================
--- pypy/dist/pypy/rlib/rctypes/rstruct.py	(original)
+++ pypy/dist/pypy/rlib/rctypes/rstruct.py	Sun Dec 31 14:48:34 2006
@@ -37,8 +37,10 @@
                 if args:
                     value = args[0]
                     args = args[1:]
-                    itemobj = getattr(obj, 'ref_' + name)()
-                    controller.set_value(itemobj, value)
+                    if controller.is_box(value):
+                        structsetboxattr(obj, name, value)
+                    else:
+                        structsetattr(obj, name, value)
             return obj
 
         self.new = structnew
@@ -53,11 +55,18 @@
         def structsetattr(obj, attr, value):
             controller = getattr(self, 'fieldcontroller_' + attr)
             itemobj = getattr(obj, 'ref_' + attr)()
-            controller.set_value(itemobj, value)
+            controller.store_value(itemobj, value)
         structsetattr._annspecialcase_ = 'specialize:arg(1)'
 
+        def structsetboxattr(obj, attr, valuebox):
+            controller = getattr(self, 'fieldcontroller_' + attr)
+            itemobj = getattr(obj, 'ref_' + attr)()
+            controller.store_box(itemobj, valuebox)
+        structsetboxattr._annspecialcase_ = 'specialize:arg(1)'
+
         self.getattr = structgetattr
         self.setattr = structsetattr
+        self.setboxattr = structsetboxattr
 
 
 StructCTypeController.register_for_metatype(StructType)

Modified: pypy/dist/pypy/rlib/rctypes/test/test_rpointer.py
==============================================================================
--- pypy/dist/pypy/rlib/rctypes/test/test_rpointer.py	(original)
+++ pypy/dist/pypy/rlib/rctypes/test/test_rpointer.py	Sun Dec 31 14:48:34 2006
@@ -104,7 +104,7 @@
 ##        assert not s.ownsmemory
 
 
-class INPROGRESS_Test_specialization:
+class Test_specialization:
     def test_specialize_c_int_ptr(self):
         ptrtype = POINTER(c_int)
         def func():
@@ -221,11 +221,10 @@
         def fn():
             p = POINTER(c_double)()
             p.contents = c_double(6.25)
-            return p
+            return p.contents.value
 
         res = interpret(fn, [])
-        float_c_data = res.c_data[0]
-        assert float_c_data[0] == 6.25
+        assert res == 6.25
 
     def test_specialize_getitem_nonzero_index(self):
         A = c_int * 10

Modified: pypy/dist/pypy/rpython/controllerentry.py
==============================================================================
--- pypy/dist/pypy/rpython/controllerentry.py	(original)
+++ pypy/dist/pypy/rpython/controllerentry.py	Sun Dec 31 14:48:34 2006
@@ -24,6 +24,17 @@
         return controller.rtype_new(hop)
 
 
+
+def controlled_instance_box(controller, obj):
+    XXX  # only for special-casing by ExtRegistryEntry below
+
+def controlled_instance_unbox(controller, obj):
+    XXX  # only for special-casing by ExtRegistryEntry below
+
+def controlled_instance_is_box(controller, obj):
+    XXX  # only for special-casing by ExtRegistryEntry below
+
+
 class ControllerEntryForPrebuilt(ExtRegistryEntry):
 
     def compute_annotation(self):
@@ -50,6 +61,10 @@
         return controlled_instance_unbox(self, obj)
     unbox._annspecialcase_ = 'specialize:arg(0)'
 
+    def is_box(self, obj):
+        return controlled_instance_is_box(self, obj)
+    is_box._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:
@@ -111,12 +126,6 @@
     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
 
@@ -129,8 +138,12 @@
             return SomeControlledInstance(s_real_obj, controller=controller)
 
     def specialize_call(self, hop):
-        [v] = hop.inputargs(hop.r_result)
-        return v
+        from pypy.rpython.rcontrollerentry import ControlledInstanceRepr
+        if not isinstance(hop.r_result, ControlledInstanceRepr):
+            raise TyperError("box() should return ControlledInstanceRepr,\n"
+                             "got %r" % (hop.r_result,))
+        hop.exception_cannot_occur()
+        return hop.inputarg(hop.r_result.r_real_obj, arg=1)
 
 class UnboxEntry(ExtRegistryEntry):
     _about_ = controlled_instance_unbox
@@ -143,8 +156,31 @@
             return s_obj.s_real_obj
 
     def specialize_call(self, hop):
-        [v] = hop.inputargs(hop.r_result)
-        return v
+        from pypy.rpython.rcontrollerentry import ControlledInstanceRepr
+        if not isinstance(hop.args_r[1], ControlledInstanceRepr):
+            raise TyperError("unbox() should take a ControlledInstanceRepr,\n"
+                             "got %r" % (hop.args_r[1],))
+        hop.exception_cannot_occur()
+        v = hop.inputarg(hop.args_r[1], arg=1)
+        return hop.llops.convertvar(v, hop.args_r[1].r_real_obj, hop.r_result)
+
+class IsBoxEntry(ExtRegistryEntry):
+    _about_ = controlled_instance_is_box
+
+    def compute_result_annotation(self, s_controller, s_obj):
+        if s_obj == annmodel.s_ImpossibleValue:
+            return annmodel.s_ImpossibleValue
+        else:
+            assert s_controller.is_constant()
+            controller = s_controller.const
+            result = (isinstance(s_obj, SomeControlledInstance) and
+                      s_obj.controller == controller)
+            return self.bookkeeper.immutablevalue(result)
+
+    def specialize_call(self, hop):
+        from pypy.rpython.lltypesystem import lltype
+        assert hop.s_result.is_constant()
+        return hop.inputconst(lltype.Bool, hop.s_result.const)
 
 # ____________________________________________________________
 

Modified: pypy/dist/pypy/rpython/rcontrollerentry.py
==============================================================================
--- pypy/dist/pypy/rpython/rcontrollerentry.py	(original)
+++ pypy/dist/pypy/rpython/rcontrollerentry.py	Sun Dec 31 14:48:34 2006
@@ -17,6 +17,11 @@
         real_value = self.controller.convert(value)
         return self.r_real_obj.convert_const(real_value)
 
+    def reveal(self, r):
+        if r is not self:
+            raise TyperError("expected %r, got %r" % (self, r))
+        return self.s_real_obj, self.r_real_obj
+
     def rtype_getattr(self, hop):
         return self.controller.rtype_getattr(hop)
 



More information about the Pypy-commit mailing list