[pypy-commit] cffi verifier2: hg merge default

arigo noreply at buildbot.pypy.org
Sat Jul 28 13:04:08 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: verifier2
Changeset: r733:31f46a250f51
Date: 2012-07-28 13:03 +0200
http://bitbucket.org/cffi/cffi/changeset/31f46a250f51/

Log:	hg merge default

diff --git a/cffi/api.py b/cffi/api.py
--- a/cffi/api.py
+++ b/cffi/api.py
@@ -94,23 +94,27 @@
         self._function_caches.append(function_cache)
         return lib
 
-    def typeof(self, cdecl, consider_function_as_funcptr=False):
+    def _typeof(self, cdecl, consider_function_as_funcptr=False):
+        # string -> ctype object
+        try:
+            btype, cfaf = self._parsed_types[cdecl]
+            if consider_function_as_funcptr and not cfaf:
+                raise KeyError
+        except KeyError:
+            cfaf = consider_function_as_funcptr
+            type = self._parser.parse_type(cdecl,
+                       consider_function_as_funcptr=cfaf)
+            btype = self._get_cached_btype(type)
+            self._parsed_types[cdecl] = btype, cfaf
+        return btype
+
+    def typeof(self, cdecl):
         """Parse the C type given as a string and return the
         corresponding Python type: <class 'ffi.CData<...>'>.
         It can also be used on 'cdata' instance to get its C type.
         """
         if isinstance(cdecl, basestring):
-            try:
-                btype, cfaf = self._parsed_types[cdecl]
-                if consider_function_as_funcptr and not cfaf:
-                    raise KeyError
-            except KeyError:
-                cfaf = consider_function_as_funcptr
-                type = self._parser.parse_type(cdecl,
-                           consider_function_as_funcptr=cfaf)
-                btype = self._get_cached_btype(type)
-                self._parsed_types[cdecl] = btype, cfaf
-            return btype
+            return self._typeof(cdecl)
         else:
             return self._backend.typeof(cdecl)
 
@@ -119,7 +123,7 @@
         string naming a C type, or a 'cdata' instance.
         """
         if isinstance(cdecl, basestring):
-            BType = self.typeof(cdecl)
+            BType = self._typeof(cdecl)
             return self._backend.sizeof(BType)
         else:
             return self._backend.sizeof(cdecl)
@@ -129,7 +133,7 @@
         given as a string.
         """
         if isinstance(cdecl, basestring):
-            cdecl = self.typeof(cdecl)
+            cdecl = self._typeof(cdecl)
         return self._backend.alignof(cdecl)
 
     def offsetof(self, cdecl, fieldname):
@@ -137,7 +141,7 @@
         structure, which must be given as a C type name.
         """
         if isinstance(cdecl, basestring):
-            cdecl = self.typeof(cdecl)
+            cdecl = self._typeof(cdecl)
         return self._backend.offsetof(cdecl, fieldname)
 
     def new(self, cdecl, init=None):
@@ -163,16 +167,18 @@
         about that when copying the pointer to the memory somewhere
         else, e.g. into another structure.
         """
-        BType = self.typeof(cdecl)
-        return self._backend.newp(BType, init)
+        if isinstance(cdecl, basestring):
+            cdecl = self._typeof(cdecl)
+        return self._backend.newp(cdecl, init)
 
     def cast(self, cdecl, source):
         """Similar to a C cast: returns an instance of the named C
         type initialized with the given 'source'.  The source is
         casted between integers or pointers of any type.
         """
-        BType = self.typeof(cdecl)
-        return self._backend.cast(BType, source)
+        if isinstance(cdecl, basestring):
+            cdecl = self._typeof(cdecl)
+        return self._backend.cast(cdecl, source)
 
     def buffer(self, cdata, size=-1):
         """Return a read-write buffer object that references the raw C data
@@ -190,8 +196,9 @@
         """
         if not callable(python_callable):
             raise TypeError("the 'python_callable' argument is not callable")
-        BFunc = self.typeof(cdecl, consider_function_as_funcptr=True)
-        return self._backend.callback(BFunc, python_callable, error)
+        if isinstance(cdecl, basestring):
+            cdecl = self._typeof(cdecl, consider_function_as_funcptr=True)
+        return self._backend.callback(cdecl, python_callable, error)
 
     def getctype(self, cdecl, replace_with=''):
         """Return a string giving the C type 'cdecl', which may be itself
@@ -200,7 +207,7 @@
         a variable name, or '*' to get actually the C type 'pointer-to-cdecl'.
         """
         if isinstance(cdecl, basestring):
-            cdecl = self.typeof(cdecl)
+            cdecl = self._typeof(cdecl)
         replace_with = replace_with.strip()
         if (replace_with.startswith('*')
                 and '&[' in self._backend.getcname(cdecl, '&')):
diff --git a/cffi/backend_ctypes.py b/cffi/backend_ctypes.py
--- a/cffi/backend_ctypes.py
+++ b/cffi/backend_ctypes.py
@@ -904,6 +904,8 @@
         return BType._offsetof(fieldname)
 
     def newp(self, BType, source):
+        if not issubclass(BType, CTypesData):
+            raise TypeError
         return BType._newp(source)
 
     def cast(self, BType, source):
diff --git a/demo/_curses.py b/demo/_curses.py
--- a/demo/_curses.py
+++ b/demo/_curses.py
@@ -210,7 +210,7 @@
     if fd < 0:
         import sys
         fd = sys.stdout.fileno()
-    err = ffi.new("int")
+    err = ffi.new("int *")
     if lib.setupterm(term, fd, err) == ERR:
         if err[0] == 0:
             s = "setupterm: could not find terminal"
diff --git a/demo/btrfs-snap.py b/demo/btrfs-snap.py
--- a/demo/btrfs-snap.py
+++ b/demo/btrfs-snap.py
@@ -36,7 +36,7 @@
 target = os.open(opts.target, os.O_DIRECTORY)
 
 
-args = ffi.new('struct btrfs_ioctl_vol_args_v2')
+args = ffi.new('struct btrfs_ioctl_vol_args_v2 *')
 args.name = opts.newname
 args.fd = source
 args_buffer = ffi.buffer(args)
diff --git a/demo/cffi-cocoa.py b/demo/cffi-cocoa.py
--- a/demo/cffi-cocoa.py
+++ b/demo/cffi-cocoa.py
@@ -60,8 +60,8 @@
 NSTitledWindowMask = ffi.cast('NSUInteger', 1)
 NSBackingStoreBuffered = ffi.cast('NSBackingStoreType', 2)
 
-NSMakePoint = lambda x, y: ffi.new('NSPoint', (x, y))[0]
-NSMakeRect = lambda x, y, w, h: ffi.new('NSRect', ((x, y), (w, h)))[0]
+NSMakePoint = lambda x, y: ffi.new('NSPoint *', (x, y))[0]
+NSMakeRect = lambda x, y, w, h: ffi.new('NSRect *', ((x, y), (w, h)))[0]
 
 get, send, sel = objc.objc_getClass, objc.objc_msgSend, objc.sel_registerName
 at = lambda s: send(
diff --git a/demo/readdir.py b/demo/readdir.py
--- a/demo/readdir.py
+++ b/demo/readdir.py
@@ -40,8 +40,8 @@
         # error in openat()
         return
     dir = ffi.C.fdopendir(dirfd)
-    dirent = ffi.new("struct dirent")
-    result = ffi.new("struct dirent *")
+    dirent = ffi.new("struct dirent *")
+    result = ffi.new("struct dirent **")
     while True:
         if ffi.C.readdir_r(dir, dirent, result):
             # error in readdir_r()
diff --git a/demo/readdir2.py b/demo/readdir2.py
--- a/demo/readdir2.py
+++ b/demo/readdir2.py
@@ -47,8 +47,8 @@
         # error in openat()
         return
     dir = ffi.C.fdopendir(dirfd)
-    dirent = ffi.new("struct dirent")
-    result = ffi.new("struct dirent *")
+    dirent = ffi.new("struct dirent *")
+    result = ffi.new("struct dirent **")
     while True:
         if ffi.C.readdir_r(dir, dirent, result):
             # error in readdir_r()
diff --git a/demo/xclient.py b/demo/xclient.py
--- a/demo/xclient.py
+++ b/demo/xclient.py
@@ -4,7 +4,7 @@
 ffi.cdef("""
 
 typedef ... Display;
-typedef ... Window;
+typedef struct { ...; } Window;
 
 typedef struct { int type; ...; } XEvent;
 
@@ -33,7 +33,7 @@
     w = XCreateSimpleWindow(display, DefaultRootWindow(display),
                             10, 10, 500, 350, 0, 0, 0)
     XMapRaised(display, w)
-    event = ffi.new("XEvent")
+    event = ffi.new("XEvent *")
     XNextEvent(display, event)
 
 if __name__ == '__main__':
diff --git a/testing/backend_tests.py b/testing/backend_tests.py
--- a/testing/backend_tests.py
+++ b/testing/backend_tests.py
@@ -1187,3 +1187,16 @@
         assert ffi.getctype("e*") == 'enum $1 *'
         assert ffi.getctype("pe") == 'enum $1 *'
         assert ffi.getctype("e1*") == 'enum $2 *'
+
+    def test_new_ctype(self):
+        ffi = FFI(backend=self.Backend())
+        p = ffi.new("int *")
+        py.test.raises(TypeError, ffi.new, p)
+        p = ffi.new(ffi.typeof("int *"), 42)
+        assert p[0] == 42
+
+    def test_enum_with_non_injective_mapping(self):
+        ffi = FFI(backend=self.Backend())
+        ffi.cdef("enum e { AA=0, BB=0, CC=0, DD=0 };")
+        e = ffi.cast("enum e", 'CC')
+        assert str(e) == "AA"     # pick the first one arbitrarily


More information about the pypy-commit mailing list