[pypy-svn] r36024 - in pypy/dist/pypy/rlib/rctypes: . test

arigo at codespeak.net arigo at codespeak.net
Thu Dec 28 17:59:48 CET 2006


Author: arigo
Date: Thu Dec 28 17:59:44 2006
New Revision: 36024

Added:
   pypy/dist/pypy/rlib/rctypes/rbuiltin.py   (contents, props changed)
Modified:
   pypy/dist/pypy/rlib/rctypes/implementation.py
   pypy/dist/pypy/rlib/rctypes/rctypesobject.py
   pypy/dist/pypy/rlib/rctypes/rpointer.py
   pypy/dist/pypy/rlib/rctypes/rprimitive.py
   pypy/dist/pypy/rlib/rctypes/test/test_rprimitive.py
Log:
Implement sizeof().  All remaining rprimitive 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	Thu Dec 28 17:59:44 2006
@@ -122,17 +122,24 @@
     return hop2
 
 def register_function_impl(builtinfn, controllingfn,
-                           revealargs=[], revealresult=None):
+                           revealargs=[], revealresult=None,
+                           register=True):
 
     class Entry(extregistry.ExtRegistryEntry):
-        _about_ = builtinfn
+        if register:
+            _about_ = builtinfn
 
         def compute_result_annotation(self, *args_s):
             real_args_s = list(args_s)
-            for index in revealargs:
-                real_args_s[index] = args_s[index].s_real_obj
             if annmodel.s_ImpossibleValue in real_args_s:
                 return annmodel.s_ImpossibleValue   # temporarily hopefully
+            for index in revealargs:
+                s_controlled = args_s[index]
+                if not isinstance(s_controlled, SomeControlledInstance):
+                    raise TypeError("in call to %s:\nargs_s[%d] should be a "
+                                    "ControlledInstance,\ngot instead %s" % (
+                        builtinfn, index, s_controlled))
+                real_args_s[index] = s_controlled.s_real_obj
             s_result = controllerentry.delegate(controllingfn, *real_args_s)
             if revealresult:
                 result_ctype = revealresult(*args_s)
@@ -145,6 +152,8 @@
             from pypy.rpython.rcontrollerentry import rtypedelegate
             return rtypedelegate(controllingfn, hop, revealargs, revealresult)
 
+    return Entry
+
 # ____________________________________________________________
 #
 # Imports for side-effects
@@ -153,3 +162,4 @@
 import pypy.rlib.rctypes.rarray
 import pypy.rlib.rctypes.rpointer
 import pypy.rlib.rctypes.rstruct
+import pypy.rlib.rctypes.rbuiltin

Added: pypy/dist/pypy/rlib/rctypes/rbuiltin.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rlib/rctypes/rbuiltin.py	Thu Dec 28 17:59:44 2006
@@ -0,0 +1,46 @@
+from pypy.annotation import model as annmodel
+from pypy.rpython.lltypesystem import lltype
+from pypy.rpython.error import TyperError
+from pypy.rpython.controllerentry import SomeControlledInstance
+from pypy.rlib.rctypes.implementation import getcontroller
+from pypy.rlib.rctypes.implementation import register_function_impl
+from pypy.rlib.rctypes import rctypesobject
+
+import ctypes
+
+#
+# pointer()
+#
+register_function_impl(ctypes.pointer, rctypesobject.pointer,
+                       revealargs   = [0],
+                       revealresult = lambda s_obj: ctypes.POINTER(
+                                                       s_obj.controller.ctype))
+
+#
+# sizeof()
+#
+sizeof_base_entry = register_function_impl(ctypes.sizeof, rctypesobject.sizeof,
+                                           revealargs=[0], register=False)
+
+class Entry(sizeof_base_entry):
+    _about_ = ctypes.sizeof
+
+    def compute_result_annotation(self, s_arg):
+        return annmodel.SomeInteger(nonneg=True)
+
+    def specialize_call(self, hop):
+        s_arg = hop.args_s[0]
+        if isinstance(s_arg, SomeControlledInstance):
+            # sizeof(object)
+            return sizeof_base_entry.specialize_call(self, hop)
+        else:
+            # sizeof(type)
+            if not s_arg.is_constant():
+                raise TyperError("only supports sizeof(object) or "
+                                 "sizeof(constant-type)")
+            ctype = s_arg.const
+            sample = ctype()   # XXX can we always instantiate ctype like this?
+            controller = getcontroller(ctype)
+            real_obj = controller.convert(sample)
+            size = rctypesobject.sizeof(real_obj)
+            return hop.inputconst(lltype.Signed, size)

Modified: pypy/dist/pypy/rlib/rctypes/rctypesobject.py
==============================================================================
--- pypy/dist/pypy/rlib/rctypes/rctypesobject.py	(original)
+++ pypy/dist/pypy/rlib/rctypes/rctypesobject.py	Thu Dec 28 17:59:44 2006
@@ -99,6 +99,9 @@
     def sameaddr(self, otherbox):
         return self.addr == otherbox.addr
 
+    def sizeof(self):
+        return self.rawsize
+
     def _keepalivememblock(self, index, memblock):
         self.memblock.setkeepalive(index, memblock)
 
@@ -266,6 +269,9 @@
     return p
 pointer._annspecialcase_ = 'specialize:argtype(0)'
 
+def sizeof(x):
+    return x.sizeof()
+
 # ____________________________________________________________
 
 def RStruct(c_name, fields, c_external=False):
@@ -375,6 +381,10 @@
                 self.memblock = memblock
                 self.length = length
 
+            def sizeof(self):
+                rawsize = FIRSTITEMOFS + ITEMOFS * self.length
+                return rawsize
+
             def allocate(length):
                 rawsize = FIRSTITEMOFS + ITEMOFS * length
                 num_keepalives = itemcls.num_keepalives * length

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 17:59:44 2006
@@ -1,6 +1,5 @@
 from pypy.rpython.extregistry import ExtRegistryEntry
 from pypy.rlib.rctypes.implementation import CTypeController, getcontroller
-from pypy.rlib.rctypes.implementation import register_function_impl
 from pypy.rlib.rctypes import rctypesobject
 from pypy.rpython.lltypesystem import lltype
 
@@ -39,8 +38,3 @@
     setitem._annspecialcase_ = 'specialize:arg(0)'
 
 PointerCTypeController.register_for_metatype(PointerType)
-
-register_function_impl(pointer, rctypesobject.pointer,
-                       revealargs   = [0],
-                       revealresult = lambda s_obj: POINTER(s_obj.controller
-                                                            .ctype))

Modified: pypy/dist/pypy/rlib/rctypes/rprimitive.py
==============================================================================
--- pypy/dist/pypy/rlib/rctypes/rprimitive.py	(original)
+++ pypy/dist/pypy/rlib/rctypes/rprimitive.py	Thu Dec 28 17:59:44 2006
@@ -50,7 +50,14 @@
     new._annspecialcase_ = 'specialize:arg(0)'
 
     def initialize_prebuilt(self, obj, x):
-        self.set_value(obj, x.value)
+        value = x.value
+        # convert 'value' to anything that cast_primitive will be happy with
+        if type(value) is long:
+            if value >= 0:
+                value = rcarith.rculonglong(value)
+            else:
+                value = rcarith.rclonglong(value)
+        self.set_value(obj, value)
 
     def get_value(self, obj):
         llvalue = obj.get_value()

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 17:59:44 2006
@@ -379,7 +379,7 @@
         assert res == 19
 
         
-class INPROGRESS_Test_compilation:
+class Test_compilation:
     def setup_class(self):
         if not test_c_compile:
             py.test.skip("c compilation disabled")
@@ -527,7 +527,7 @@
         fn = self.compile(func, [int])
         assert fn(19) == func(19)
 
-class INPROGRESS_Test_compilation_llvm(INPROGRESS_Test_compilation):
+class Test_compilation_llvm(Test_compilation):
     def setup_class(self):
         if not test_llvm_compile:
             py.test.skip("llvm compilation disabled")



More information about the Pypy-commit mailing list