summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArmin Rigo <arigo@tunes.org>2015-05-18 17:25:29 +0200
committerArmin Rigo <arigo@tunes.org>2015-05-18 17:25:29 +0200
commit7170cc6aefba2b37506e120719197c8e1e0f7eba (patch)
treec3d7e61f7c021b77a828b57ea6031b8858659942
parent1e575afa8553f3f792e23801c4da85b3b589fe11 (diff)
downloadcffi-7170cc6aefba2b37506e120719197c8e1e0f7eba.tar.gz
Forgot about the version checking and specifying in the ABI-mode modules
-rw-r--r--c/cdlopen.c11
-rw-r--r--c/cffi1_module.c6
-rw-r--r--cffi/recompiler.py13
-rw-r--r--testing/cffi1/test_dlopen.py10
-rw-r--r--testing/cffi1/test_re_python.py7
5 files changed, 38 insertions, 9 deletions
diff --git a/c/cdlopen.c b/c/cdlopen.c
index 43c2e66..032bd3f 100644
--- a/c/cdlopen.c
+++ b/c/cdlopen.c
@@ -139,7 +139,7 @@ static int ffiobj_init(PyObject *self, PyObject *args, PyObject *kwds)
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 @@ static int ffiobj_init(PyObject *self, PyObject *args, PyObject *kwds)
}
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
index 09ff624..e84dc2f 100644
--- 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 @@ static PyObject *_my_Py_InitModule(char *module_name)
#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
index e72a17c..3513a47 100644
--- a/cffi/recompiler.py
+++ b/cffi/recompiler.py
@@ -2,6 +2,8 @@ import os, sys, io
from . import ffiplatform, model
from .cffi_opcode import *
+VERSION = "0x2601"
+
try:
int_type = (int, long)
except NameError: # Python 3
@@ -375,7 +377,7 @@ class Recompiler:
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 @@ class Recompiler:
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 @@ class Recompiler:
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
index e5d9d07..23a7ed6 100644
--- a/testing/cffi1/test_dlopen.py
+++ b/testing/cffi1/test_dlopen.py
@@ -13,6 +13,7 @@ def test_simple():
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 @@ def test_typename():
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 @@ def test_enum():
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 @@ def test_struct():
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 @@ def test_include():
import _cffi_backend
ffi = _cffi_backend.FFI('test_include',
+ _version = 0x2601,
_types = b'',
_globals = (b'\xFF\xFF\xFF\x1FABC',123,),
)
@@ -124,6 +129,7 @@ import _cffi_backend
from test_include import ffi as _ffi0
ffi = _cffi_backend.FFI('test2_include',
+ _version = 0x2601,
_types = b'',
_includes = (_ffi0,),
)
@@ -138,6 +144,7 @@ def test_negative_constant():
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 @@ import _cffi_backend
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 @@ def test_array():
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 @@ def test_global_var():
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
index 0d25c54..2ca6c31 100644
--- a/testing/cffi1/test_re_python.py
+++ b/testing/cffi1/test_re_python.py
@@ -151,3 +151,10 @@ def test_no_such_function_or_global_var():
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")