Author: Armin Rigo <ar...@tunes.org>
Branch: cffi-1.0
Changeset: r77187:aee219ca72d9
Date: 2015-05-08 08:54 +0200
http://bitbucket.org/pypy/pypy/changeset/aee219ca72d9/

Log:    Tweaks

diff --git a/pypy/module/_cffi_backend/cffi1_module.py 
b/pypy/module/_cffi_backend/cffi1_module.py
--- a/pypy/module/_cffi_backend/cffi1_module.py
+++ b/pypy/module/_cffi_backend/cffi1_module.py
@@ -8,7 +8,8 @@
 from pypy.module._cffi_backend.lib_obj import W_LibObject
 
 
-EXPECTED_VERSION = 0x10000f0
+VERSION_MIN = 0x010000f0
+VERSION_MAX = 0x0100ffff
 
 initfunctype = lltype.Ptr(lltype.FuncType([rffi.VOIDPP], lltype.Void))
 
@@ -19,9 +20,9 @@
         with lltype.scoped_alloc(rffi.VOIDPP.TO, 2, zero=True) as p:
             initfunc(p)
             version = rffi.cast(lltype.Signed, p[0])
-            if version != EXPECTED_VERSION:
+            if not (VERSION_MIN <= version <= VERSION_MAX):
                 raise oefmt(space.w_ImportError,
-                    "the cffi extension module '%s' has unknown version %s",
+                    "cffi extension module '%s' has unknown version %s",
                     name, hex(version))
             src_ctx = rffi.cast(parse_c_type.PCTX, p[1])
     except:
diff --git a/pypy/module/_cffi_backend/lib_obj.py 
b/pypy/module/_cffi_backend/lib_obj.py
--- a/pypy/module/_cffi_backend/lib_obj.py
+++ b/pypy/module/_cffi_backend/lib_obj.py
@@ -48,8 +48,9 @@
                 w_result = W_CData(self.space, ptr, w_ct)
                 #
             else:
-                raise NotImplementedError("in lib_build_attr: op=%d" % op)
-            
+                raise oefmt(ffi.space.w_NotImplementedError,
+                            "in lib_build_attr: op=%d", op)
+
             self.dict_w[attr] = w_result
         return w_result
 
diff --git a/pypy/module/_cffi_backend/parse_c_type.py 
b/pypy/module/_cffi_backend/parse_c_type.py
--- a/pypy/module/_cffi_backend/parse_c_type.py
+++ b/pypy/module/_cffi_backend/parse_c_type.py
@@ -59,7 +59,8 @@
                        ('num_enums', rffi.INT),
                        ('num_typenames', rffi.INT),
                        ('includes', rffi.CCHARPP),
-                       ('num_types', rffi.INT))
+                       ('num_types', rffi.INT),
+                       ('flags', rffi.INT))
 
 PINFO = rffi.CStructPtr('struct _cffi_parse_info_s',
                         ('ctx', PCTX),
diff --git a/pypy/module/_cffi_backend/src/parse_c_type.h 
b/pypy/module/_cffi_backend/src/parse_c_type.h
--- a/pypy/module/_cffi_backend/src/parse_c_type.h
+++ b/pypy/module/_cffi_backend/src/parse_c_type.h
@@ -131,6 +131,7 @@
     int num_typenames;
     const char *const *includes;
     int num_types;
+    int flags;      /* future extension */
 };
 
 struct _cffi_parse_info_s {
diff --git a/pypy/module/_cffi_backend/test/test_recompiler.py 
b/pypy/module/_cffi_backend/test/test_recompiler.py
--- a/pypy/module/_cffi_backend/test/test_recompiler.py
+++ b/pypy/module/_cffi_backend/test/test_recompiler.py
@@ -1,6 +1,4 @@
-import sys, os, py
-from cffi import FFI              # <== the system one, which
-from _cffi1 import recompiler     # needs to be at least cffi 1.0.0b3
+import os, py
 
 from rpython.tool.udir import udir
 from pypy.interpreter.gateway import unwrap_spec, interp2app
@@ -8,6 +6,11 @@
 
 @unwrap_spec(cdef=str, module_name=str, source=str)
 def prepare(space, cdef, module_name, source):
+    try:
+        from cffi import FFI              # <== the system one, which
+        from _cffi1 import recompiler     # needs to be at least cffi 1.0.0b3
+    except ImportError:
+        py.test.skip("system cffi module not found or older than 1.0.0")
     module_name = '_CFFI_' + module_name
     rdir = udir.ensure('recompiler', dir=1)
     rdir.join('Python.h').write(
@@ -43,52 +46,61 @@
 
     def test_math_sin(self):
         import math
-        ffi, lib = self.prepare("float sin(double); double cos(double);",
-                                'test_math_sin',
-                                '#include <math.h>')
+        ffi, lib = self.prepare(
+            "float sin(double); double cos(double);",
+            'test_math_sin',
+            '#include <math.h>')
         assert lib.cos(1.43) == math.cos(1.43)
 
-    def test_funcarg_ptr():
-        ffi = FFI()
-        ffi.cdef("int foo(int *);")
-        lib = verify(ffi, 'test_funcarg_ptr', 'int foo(int *p) { return *p; }')
+    def test_funcarg_ptr(self):
+        ffi, lib = self.prepare(
+            "int foo(int *);",
+            'test_funcarg_ptr',
+            'int foo(int *p) { return *p; }')
         assert lib.foo([-12345]) == -12345
 
-    def test_funcres_ptr():
-        ffi = FFI()
-        ffi.cdef("int *foo(void);")
-        lib = verify(ffi, 'test_funcres_ptr',
-                     'int *foo(void) { static int x=-12345; return &x; }')
+    def test_funcres_ptr(self):
+        ffi, lib = self.prepare(
+            "int *foo(void);",
+            'test_funcres_ptr',
+            'int *foo(void) { static int x=-12345; return &x; }')
         assert lib.foo()[0] == -12345
 
-    def test_global_var_array():
-        ffi = FFI()
-        ffi.cdef("int a[100];")
-        lib = verify(ffi, 'test_global_var_array', 'int a[100] = { 9999 };')
+    def test_global_var_array(self):
+        ffi, lib = self.prepare(
+            "int a[100];",
+            'test_global_var_array',
+            'int a[100] = { 9999 };')
         lib.a[42] = 123456
         assert lib.a[42] == 123456
         assert lib.a[0] == 9999
 
-    def test_verify_typedef():
-        ffi = FFI()
-        ffi.cdef("typedef int **foo_t;")
-        lib = verify(ffi, 'test_verify_typedef', 'typedef int **foo_t;')
+    def test_verify_typedef(self):
+        ffi, lib = self.prepare(
+            "typedef int **foo_t;",
+            'test_verify_typedef',
+            'typedef int **foo_t;')
         assert ffi.sizeof("foo_t") == ffi.sizeof("void *")
 
-    def test_verify_typedef_dotdotdot():
-        ffi = FFI()
-        ffi.cdef("typedef ... foo_t;")
-        verify(ffi, 'test_verify_typedef_dotdotdot', 'typedef int **foo_t;')
+    def test_verify_typedef_dotdotdot(self):
+        ffi, lib = self.prepare(
+            "typedef ... foo_t;",
+            'test_verify_typedef_dotdotdot',
+            'typedef int **foo_t;')
+        # did not crash
 
-    def test_verify_typedef_star_dotdotdot():
-        ffi = FFI()
-        ffi.cdef("typedef ... *foo_t;")
-        verify(ffi, 'test_verify_typedef_star_dotdotdot', 'typedef int 
**foo_t;')
+    def test_verify_typedef_star_dotdotdot(self):
+        ffi, lib = self.prepare(
+            "typedef ... *foo_t;",
+            'test_verify_typedef_star_dotdotdot',
+            'typedef int **foo_t;')
+        # did not crash
 
-    def test_global_var_int():
-        ffi = FFI()
-        ffi.cdef("int a, b, c;")
-        lib = verify(ffi, 'test_global_var_int', 'int a = 999, b, c;')
+    def test_global_var_int(self):
+        ffi, lib = self.prepare(
+            "int a, b, c;",
+            'test_global_var_int',
+            'int a = 999, b, c;')
         assert lib.a == 999
         lib.a -= 1001
         assert lib.a == -2
@@ -102,35 +114,39 @@
         py.test.raises(AttributeError, "del lib.c")
         py.test.raises(AttributeError, "del lib.foobarbaz")
 
-    def test_macro():
-        ffi = FFI()
-        ffi.cdef("#define FOOBAR ...")
-        lib = verify(ffi, 'test_macro', "#define FOOBAR (-6912)")
+    def test_macro(self):
+        ffi, lib = self.prepare(
+            "#define FOOBAR ...",
+            'test_macro',
+            "#define FOOBAR (-6912)")
         assert lib.FOOBAR == -6912
         py.test.raises(AttributeError, "lib.FOOBAR = 2")
 
-    def test_macro_check_value():
+    def test_macro_check_value(self):
         # the value '-0x80000000' in C sources does not have a clear meaning
         # to me; it appears to have a different effect than '-2147483648'...
         # Moreover, on 32-bits, -2147483648 is actually equal to
         # -2147483648U, which in turn is equal to 2147483648U and so positive.
+        import sys
         vals = ['42', '-42', '0x80000000', '-2147483648',
                 '0', '9223372036854775809ULL',
                 '-9223372036854775807LL']
         if sys.maxsize <= 2**32:
             vals.remove('-2147483648')
-        ffi = FFI()
+
         cdef_lines = ['#define FOO_%d_%d %s' % (i, j, vals[i])
                       for i in range(len(vals))
                       for j in range(len(vals))]
-        ffi.cdef('\n'.join(cdef_lines))
 
         verify_lines = ['#define FOO_%d_%d %s' % (i, j, vals[j])  # [j], not 
[i]
                         for i in range(len(vals))
                         for j in range(len(vals))]
-        lib = verify(ffi, 'test_macro_check_value_ok',
-                     '\n'.join(verify_lines))
-        #
+
+        ffi, lib = self.prepare(
+            '\n'.join(cdef_lines),
+            'test_macro_check_value_ok',
+            '\n'.join(verify_lines))
+
         for j in range(len(vals)):
             c_got = int(vals[j].replace('U', '').replace('L', ''), 0)
             c_compiler_msg = str(c_got)
@@ -148,24 +164,27 @@
                         "the C compiler says '%s' is equal to "
                         "%s, but the cdef disagrees" % (attrname, 
c_compiler_msg))
 
-    def test_constant():
-        ffi = FFI()
-        ffi.cdef("static const int FOOBAR;")
-        lib = verify(ffi, 'test_constant', "#define FOOBAR (-6912)")
+    def test_constant(self):
+        ffi, lib = self.prepare(
+            "static const int FOOBAR;",
+            'test_constant',
+            "#define FOOBAR (-6912)")
         assert lib.FOOBAR == -6912
         py.test.raises(AttributeError, "lib.FOOBAR = 2")
 
-    def test_constant_nonint():
-        ffi = FFI()
-        ffi.cdef("static const double FOOBAR;")
-        lib = verify(ffi, 'test_constant_nonint', "#define FOOBAR (-6912.5)")
+    def test_constant_nonint(self):
+        ffi, lib = self.prepare(
+            "static const double FOOBAR;",
+            'test_constant_nonint',
+            "#define FOOBAR (-6912.5)")
         assert lib.FOOBAR == -6912.5
         py.test.raises(AttributeError, "lib.FOOBAR = 2")
 
-    def test_constant_ptr():
-        ffi = FFI()
-        ffi.cdef("static double *const FOOBAR;")
-        lib = verify(ffi, 'test_constant_ptr', "#define FOOBAR NULL")
+    def test_constant_ptr(self):
+        ffi, lib = self.prepare(
+            "static double *const FOOBAR;",
+            'test_constant_ptr',
+            "#define FOOBAR NULL")
         assert lib.FOOBAR == ffi.NULL
         assert ffi.typeof(lib.FOOBAR) == ffi.typeof("double *")
 
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to