def test_import_from_lib(self):
ffi2 = cffi.FFI()
ffi2.cdef("int myfunc(int); int myvar;\n#define MYFOO ...\n")
outputfilename = recompile(ffi2, "_test_import_from_lib",
"int myfunc(int x) { return x + 1; }\n"
"int myvar = -5;\n"
"#define MYFOO 42", tmpdir=str(udir))
imp.load_dynamic("_test_import_from_lib", outputfilename)
from _test_import_from_lib.lib import myfunc, myvar, MYFOO
assert MYFOO == 42
assert myfunc(43) == 44
assert myvar == -5 # but can't be changed, so not very useful
py.test.raises(ImportError, "from _test_import_from_lib.lib import bar")
d = {}
exec("from _test_import_from_lib.lib import *", d)
assert (set(key for key in d if not key.startswith('_')) ==
set(['myfunc', 'MYFOO']))
#
# also test "import *" on the module itself, which should be
# equivalent to "import ffi, lib"
d = {}
exec("from _test_import_from_lib import *", d)
assert (sorted([x for x in d.keys() if not x.startswith('__')]) ==
['ffi', 'lib'])
评论列表
文章目录