Commit d8d5f2da authored by Armin Rigo's avatar Armin Rigo
Browse files

Allow "from _cffimodule.lib import a, b, c". (thanks DanielHolth on irc)

Doesn't work to import "*", so far.
parent 17be464b073d
......@@ -141,11 +141,11 @@ static PyObject *_my_Py_InitModule(char *module_name)
static PyObject *b_init_cffi_1_0_external_module(PyObject *self, PyObject *arg)
PyObject *m;
PyObject *m, *modules_dict;
FFIObject *ffi;
LibObject *lib;
Py_ssize_t version;
char *module_name, *exports;
char *module_name, *exports, *module_name_with_lib;
void **raw;
const struct _cffi_type_context_s *ctx;
......@@ -189,5 +189,17 @@ static PyObject *b_init_cffi_1_0_external_module(PyObject *self, PyObject *arg)
&lib->l_types_builder->included_libs) < 0)
return NULL;
/* add manually 'module_name.lib' in sys.modules:
see test_import_from_lib */
modules_dict = PySys_GetObject("modules");
if (!modules_dict)
return NULL;
module_name_with_lib = alloca(strlen(module_name) + 5);
strcpy(module_name_with_lib, module_name);
strcat(module_name_with_lib, ".lib");
if (PyDict_SetItemString(modules_dict, module_name_with_lib,
(PyObject *)lib) < 0)
return NULL;
return m;
......@@ -1699,3 +1699,15 @@ class TestNewFFI1:
c_file = str(udir.join('test_emit_c_code'))
assert os.path.isfile(c_file)
def test_import_from_lib(self):
ffi2 = cffi.FFI()
ffi2.cdef("int myfunc(int);\n#define MYFOO ...\n")
outputfilename = recompile(ffi2, "_test_import_from_lib",
"int myfunc(int x) { return x + 1; }\n"
"#define MYFOO 42", tmpdir=str(udir))
imp.load_dynamic("_test_import_from_lib", outputfilename)
from _test_import_from_lib.lib import myfunc, MYFOO
assert MYFOO == 42
assert myfunc(43) == 44
py.test.raises(ImportError, "from _test_import_from_lib.lib import bar")
......@@ -1040,3 +1040,12 @@ def test_alignment_of_longlong():
"struct foo_s { unsigned long long x; };")
assert ffi.alignof('unsigned long long') == x1
assert ffi.alignof('struct foo_s') == x1
def test_import_from_lib():
ffi = FFI()
ffi.cdef("#define MYFOO ...")
lib = verify(ffi, 'test_import_from_lib', "#define MYFOO 42")
assert sys.modules['_CFFI_test_import_from_lib'].lib is lib
assert sys.modules['_CFFI_test_import_from_lib.lib'] is lib
from _CFFI_test_import_from_lib.lib import MYFOO
assert MYFOO == 42
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment