[pypy-commit] pypy ffi-backend: Next test

arigo noreply at buildbot.pypy.org
Thu Jun 21 13:16:22 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: ffi-backend
Changeset: r55749:6c5effca84a2
Date: 2012-06-20 18:50 +0200
http://bitbucket.org/pypy/pypy/changeset/6c5effca84a2/

Log:	Next test

diff --git a/pypy/module/_ffi_backend/__init__.py b/pypy/module/_ffi_backend/__init__.py
--- a/pypy/module/_ffi_backend/__init__.py
+++ b/pypy/module/_ffi_backend/__init__.py
@@ -10,6 +10,7 @@
         'load_library': 'libraryobj.load_library',
 
         'new_primitive_type': 'newtype.new_primitive_type',
+        'new_pointer_type': 'newtype.new_pointer_type',
 
         'cast': 'func.cast',
         'sizeof': 'func.sizeof',
diff --git a/pypy/module/_ffi_backend/ctypeobj.py b/pypy/module/_ffi_backend/ctypeobj.py
--- a/pypy/module/_ffi_backend/ctypeobj.py
+++ b/pypy/module/_ffi_backend/ctypeobj.py
@@ -11,10 +11,13 @@
 class W_CType(Wrappable):
     _immutable_ = True
 
-    def __init__(self, space, name, size):
+    def __init__(self, space, size, name, name_position):
         self.space = space
-        self.name = name
         self.size = size     # size of instances, or -1 if unknown
+        self.name = name     # the name of the C type as a string
+        self.name_position = name_position
+        # 'name_position' is the index in 'name' where it must be extended,
+        # e.g. with a '*' or a variable name.
 
     def repr(self):
         space = self.space
@@ -39,6 +42,17 @@
     def try_str(self, cdata):
         return None
 
+    def insert_name(self, extra, extra_position):
+        name = '%s%s%s' % (self.name[:self.name_position],
+                           extra,
+                           self.name[self.name_position:])
+        name_position = self.name_position + extra_position
+        return name, name_position
+
+
+class W_CTypePointer(W_CType):
+    pass
+
 
 class W_CTypePrimitive(W_CType):
 
@@ -73,6 +87,10 @@
 
 class W_CTypePrimitiveSigned(W_CTypePrimitive):
 
+    def __init__(self, *args):
+        W_CTypePrimitive.__init__(self, *args)
+        self.value_fits_long = self.size <= rffi.sizeof(lltype.Signed)
+
     def int(self, cdata):
         if self.value_fits_long:
             # this case is to handle enums, but also serves as a slight
@@ -93,6 +111,10 @@
 
 class W_CTypePrimitiveUnsigned(W_CTypePrimitive):
 
+    def __init__(self, *args):
+        W_CTypePrimitive.__init__(self, *args)
+        self.value_fits_long = self.size < rffi.sizeof(lltype.Signed)
+
     def int(self, cdata):
         return self.convert_to_object(cdata)
 
diff --git a/pypy/module/_ffi_backend/newtype.py b/pypy/module/_ffi_backend/newtype.py
--- a/pypy/module/_ffi_backend/newtype.py
+++ b/pypy/module/_ffi_backend/newtype.py
@@ -11,14 +11,7 @@
 PRIMITIVE_TYPES = {}
 
 def eptype(name, TYPE, ctypecls):
-    size = rffi.sizeof(TYPE)
-    if ctypecls is ctypeobj.W_CTypePrimitiveSigned:
-        value_fits_long = size <= rffi.sizeof(lltype.Signed)
-    elif ctypecls is ctypeobj.W_CTypePrimitiveUnsigned:
-        value_fits_long = size < rffi.sizeof(lltype.Signed)
-    else:
-        value_fits_long = False
-    PRIMITIVE_TYPES[name] = ctypecls, size, value_fits_long
+    PRIMITIVE_TYPES[name] = ctypecls, rffi.sizeof(TYPE)
 
 eptype("char",        lltype.Char,     ctypeobj.W_CTypePrimitiveChar)
 eptype("signed char", rffi.SIGNEDCHAR, ctypeobj.W_CTypePrimitiveSigned)
@@ -37,9 +30,17 @@
 @unwrap_spec(name=str)
 def new_primitive_type(space, name):
     try:
-        ctypecls, size, value_fits_long = PRIMITIVE_TYPES[name]
+        ctypecls, size = PRIMITIVE_TYPES[name]
     except KeyError:
         raise OperationError(space.w_KeyError, space.wrap(name))
-    ctype = ctypecls(space, name, size)
-    ctype.value_fits_long = value_fits_long
+    ctype = ctypecls(space, size, name, len(name))
     return ctype
+
+# ____________________________________________________________
+
+ at unwrap_spec(ctype=ctypeobj.W_CType)
+def new_pointer_type(space, ctype):
+    name, name_position = ctype.insert_name(' *', 2)
+    size = rffi.sizeof(rffi.VOIDP)
+    ctype = ctypeobj.W_CTypePointer(space, size, name, name_position)
+    return ctype
diff --git a/pypy/module/_ffi_backend/test/test_c.py b/pypy/module/_ffi_backend/test/test_c.py
--- a/pypy/module/_ffi_backend/test/test_c.py
+++ b/pypy/module/_ffi_backend/test/test_c.py
@@ -136,6 +136,8 @@
         assert str(cast(p, 'A')) == 'A'
 
     def test_pointer_type(self):
+        new_primitive_type = self.b.new_primitive_type
+        new_pointer_type = self.b.new_pointer_type
         p = new_primitive_type("int")
         assert repr(p) == "<ctype 'int'>"
         p = new_pointer_type(p)


More information about the pypy-commit mailing list