[pypy-commit] cffi cffi-1.0: Forgot about the version checking and specifying in the ABI-mode modules

arigo noreply at buildbot.pypy.org
Mon May 18 17:24:52 CEST 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-1.0
Changeset: r2047:2d4469c2fec5
Date: 2015-05-18 17:25 +0200
http://bitbucket.org/cffi/cffi/changeset/2d4469c2fec5/

Log:	Forgot about the version checking and specifying in the ABI-mode
	modules

diff --git a/c/cdlopen.c b/c/cdlopen.c
--- a/c/cdlopen.c
+++ b/c/cdlopen.c
@@ -139,7 +139,7 @@
     static char *keywords[] = {"module_name", "_version", "_types",
                                "_globals", "_struct_unions", "_enums",
                                "_typenames", "_includes", NULL};
-    char *ffiname = NULL, *types = NULL, *building = NULL;
+    char *ffiname = "?", *types = NULL, *building = NULL;
     Py_ssize_t version = -1;
     Py_ssize_t types_len = 0;
     PyObject *globals = NULL, *struct_unions = NULL, *enums = NULL;
@@ -163,6 +163,15 @@
     }
     ffi->ctx_is_nonempty = 1;
 
+    if (version == -1 && types_len == 0)
+        return 0;
+    if (version < CFFI_VERSION_MIN || version > CFFI_VERSION_MAX) {
+        PyErr_Format(PyExc_ImportError,
+                     "cffi out-of-line Python module '%s' has unknown "
+                     "version %p", ffiname, (void *)version);
+        return -1;
+    }
+
     if (types_len > 0) {
         /* unpack a string of 4-byte entries into an array of _cffi_opcode_t */
         _cffi_opcode_t *ntypes;
diff --git a/c/cffi1_module.c b/c/cffi1_module.c
--- a/c/cffi1_module.c
+++ b/c/cffi1_module.c
@@ -2,6 +2,9 @@
 #include "parse_c_type.c"
 #include "realize_c_type.c"
 
+#define CFFI_VERSION_MIN    0x2601
+#define CFFI_VERSION_MAX    0x26FF
+
 typedef struct FFIObject_s FFIObject;
 typedef struct LibObject_s LibObject;
 
@@ -136,9 +139,6 @@
 #endif
 }
 
-#define CFFI_VERSION_MIN    0x2601
-#define CFFI_VERSION_MAX    0x26FF
-
 static PyObject *b_init_cffi_1_0_external_module(PyObject *self, PyObject *arg)
 {
     PyObject *m;
diff --git a/cffi/recompiler.py b/cffi/recompiler.py
--- a/cffi/recompiler.py
+++ b/cffi/recompiler.py
@@ -2,6 +2,8 @@
 from . import ffiplatform, model
 from .cffi_opcode import *
 
+VERSION = "0x2601"
+
 try:
     int_type = (int, long)
 except NameError:    # Python 3
@@ -375,7 +377,7 @@
         prnt('PyMODINIT_FUNC')
         prnt('_cffi_pypyinit_%s(const void *p[])' % (base_module_name,))
         prnt('{')
-        prnt('    p[0] = (const void *)0x2601;')
+        prnt('    p[0] = (const void *)%s;' % VERSION)
         prnt('    p[1] = &_cffi_type_context;')
         prnt('}')
         # on Windows, distutils insists on putting init_cffi_xyz in
@@ -393,15 +395,15 @@
         prnt('PyMODINIT_FUNC')
         prnt('PyInit_%s(void)' % (base_module_name,))
         prnt('{')
-        prnt('  return _cffi_init("%s", 0x2601, &_cffi_type_context);' % (
-            self.module_name,))
+        prnt('  return _cffi_init("%s", %s, &_cffi_type_context);' % (
+            self.module_name, VERSION))
         prnt('}')
         prnt('#else')
         prnt('PyMODINIT_FUNC')
         prnt('init%s(void)' % (base_module_name,))
         prnt('{')
-        prnt('  _cffi_init("%s", 0x2601, &_cffi_type_context);' % (
-            self.module_name,))
+        prnt('  _cffi_init("%s", %s, &_cffi_type_context);' % (
+            self.module_name, VERSION))
         prnt('}')
         prnt('#endif')
 
@@ -442,6 +444,7 @@
             prnt('from %s import ffi as _ffi%d' % (included_module_name, i))
         prnt()
         prnt("ffi = _cffi_backend.FFI('%s'," % (self.module_name,))
+        prnt("    _version = %s," % (VERSION,))
         #
         # the '_types' keyword argument
         self.cffi_types = tuple(self.cffi_types)    # don't change any more
diff --git a/testing/cffi1/test_dlopen.py b/testing/cffi1/test_dlopen.py
--- a/testing/cffi1/test_dlopen.py
+++ b/testing/cffi1/test_dlopen.py
@@ -13,6 +13,7 @@
 import _cffi_backend
 
 ffi = _cffi_backend.FFI('test_simple',
+    _version = 0x2601,
     _types = b'\x00\x00\x01\x0D\x00\x00\x07\x01\x00\x00\x00\x0F',
     _globals = (b'\xFF\xFF\xFF\x1FBB',42,b'\x00\x00\x00\x23close',0,b'\x00\x00\x01\x21somevar',0),
 )
@@ -66,6 +67,7 @@
 import _cffi_backend
 
 ffi = _cffi_backend.FFI('test_typename',
+    _version = 0x2601,
     _types = b'\x00\x00\x07\x01',
     _typenames = (b'\x00\x00\x00\x00foobar_t',),
 )
@@ -80,6 +82,7 @@
 import _cffi_backend
 
 ffi = _cffi_backend.FFI('test_enum',
+    _version = 0x2601,
     _types = b'\x00\x00\x00\x0B',
     _globals = (b'\xFF\xFF\xFF\x0BAA',0,b'\xFF\xFF\xFF\x0BBB',1,b'\xFF\xFF\xFF\x0BCC',-42),
     _enums = (b'\x00\x00\x00\x00\x00\x00\x00\x15myenum_e\x00AA,BB,CC',),
@@ -95,6 +98,7 @@
 import _cffi_backend
 
 ffi = _cffi_backend.FFI('test_struct',
+    _version = 0x2601,
     _types = b'\x00\x00\x07\x01\x00\x00\x03\x01\x00\x00\x01\x07\x00\x00\x00\x09\x00\x00\x01\x09',
     _struct_unions = ((b'\x00\x00\x00\x03\x00\x00\x00\x10bar_s',),(b'\x00\x00\x00\x04\x00\x00\x00\x02foo_s',b'\x00\x00\x00\x11a',b'\x00\x00\x02\x11b')),
 )
@@ -110,6 +114,7 @@
 import _cffi_backend
 
 ffi = _cffi_backend.FFI('test_include',
+    _version = 0x2601,
     _types = b'',
     _globals = (b'\xFF\xFF\xFF\x1FABC',123,),
 )
@@ -124,6 +129,7 @@
 from test_include import ffi as _ffi0
 
 ffi = _cffi_backend.FFI('test2_include',
+    _version = 0x2601,
     _types = b'',
     _includes = (_ffi0,),
 )
@@ -138,6 +144,7 @@
 import _cffi_backend
 
 ffi = _cffi_backend.FFI('test_negative_constant',
+    _version = 0x2601,
     _types = b'',
     _globals = (b'\xFF\xFF\xFF\x1FBB',-42,),
 )
@@ -157,6 +164,7 @@
 from test_struct_included_base import ffi as _ffi0
 
 ffi = _cffi_backend.FFI('test_struct_included',
+    _version = 0x2601,
     _types = b'\x00\x00\x00\x09',
     _struct_unions = ((b'\x00\x00\x00\x00\x00\x00\x00\x08foo_s',),),
     _includes = (_ffi0,),
@@ -182,6 +190,7 @@
 import _cffi_backend
 
 ffi = _cffi_backend.FFI('test_array',
+    _version = 0x2601,
     _types = b'\x00\x00\x15\x01\x00\x00\x00\x05\x00\x00\x00\x2A',
     _typenames = (b'\x00\x00\x00\x01my_array_t',),
 )
@@ -203,6 +212,7 @@
 import _cffi_backend
 
 ffi = _cffi_backend.FFI('test_global_var',
+    _version = 0x2601,
     _types = b'\x00\x00\x07\x01',
     _globals = (b'\x00\x00\x00\x21myglob',0,),
 )
diff --git a/testing/cffi1/test_re_python.py b/testing/cffi1/test_re_python.py
--- a/testing/cffi1/test_re_python.py
+++ b/testing/cffi1/test_re_python.py
@@ -151,3 +151,10 @@
     e = py.test.raises(ffi.error, getattr, lib, 'no_such_globalvar')
     assert str(e.value).startswith(
         "symbol 'no_such_globalvar' not found in library '")
+
+def test_check_version():
+    import _cffi_backend
+    e = py.test.raises(ImportError, _cffi_backend.FFI,
+                       "foobar", _version=0x2594)
+    assert str(e.value).startswith(
+        "cffi out-of-line Python module 'foobar' has unknown version")


More information about the pypy-commit mailing list