diff options
| author | Armin Rigo <arigo@tunes.org> | 2015-05-16 11:22:15 +0200 |
|---|---|---|
| committer | Armin Rigo <arigo@tunes.org> | 2015-05-16 11:22:15 +0200 |
| commit | ddd2436428ecbb817c56c3e3425bc187b5f2def6 (patch) | |
| tree | d06830b1e4f5bc5ac1bf4c7a30346fcc8f9bf9e5 | |
| parent | bf54ee40591350b7c4639f33e4a4788527e48b33 (diff) | |
| download | cffi-ddd2436428ecbb817c56c3e3425bc187b5f2def6.tar.gz | |
Clean up
| -rw-r--r-- | cffi/api.py | 19 | ||||
| -rw-r--r-- | cffi/recompiler.py | 35 | ||||
| -rw-r--r-- | testing/cffi1/test_dlopen.py | 1 | ||||
| -rw-r--r-- | testing/cffi1/test_new_ffi_1.py | 2 | ||||
| -rw-r--r-- | testing/cffi1/test_recompiler.py | 6 | ||||
| -rw-r--r-- | testing/cffi1/test_verify1.py | 10 |
6 files changed, 47 insertions, 26 deletions
diff --git a/cffi/api.py b/cffi/api.py index 4efaf2b..2b7e4ad 100644 --- a/cffi/api.py +++ b/cffi/api.py @@ -481,8 +481,7 @@ class FFI(object): "per ffi object") if not isinstance(module_name, basestring): raise TypeError("'module_name' must be a string") - self._recompiler_module_name = str(module_name) - self._assigned_source = (source, kwds) + self._assigned_source = (source, kwds, str(module_name)) def distutils_extension(self, tmpdir='build', verbose=True): from distutils.dir_util import mkpath @@ -493,13 +492,13 @@ class FFI(object): return self.verifier.get_extension() raise ValueError("set_source() must be called before" " distutils_extension()") - source, kwds = self._assigned_source + source, kwds, module_name = self._assigned_source if source is None: raise TypeError("distutils_extension() is only for C extension " "modules, not for dlopen()-style pure Python " "modules") mkpath(tmpdir) - ext, updated = recompile(self, self._recompiler_module_name, + ext, updated = recompile(self, module_name, source, tmpdir=tmpdir, call_c_compiler=False, **kwds) if verbose: @@ -514,11 +513,11 @@ class FFI(object): # if not hasattr(self, '_assigned_source'): raise ValueError("set_source() must be called before emit_c_code()") - source, kwds = self._assigned_source + source, kwds, module_name = self._assigned_source if source is None: raise TypeError("emit_c_code() is only for C extension modules, " "not for dlopen()-style pure Python modules") - recompile(self, self._recompiler_module_name, source, + recompile(self, module_name, source, c_file=filename, call_c_compiler=False, **kwds) def emit_python_code(self, filename): @@ -526,11 +525,11 @@ class FFI(object): # if not hasattr(self, '_assigned_source'): raise ValueError("set_source() must be called before emit_c_code()") - source, kwds = self._assigned_source + source, kwds, module_name = self._assigned_source if source is not None: raise TypeError("emit_python_code() is only for dlopen()-style " "pure Python modules, not for C extension modules") - recompile(self, self._recompiler_module_name, source, + recompile(self, module_name, source, c_file=filename, call_c_compiler=False, **kwds) def compile(self, tmpdir='.'): @@ -538,8 +537,8 @@ class FFI(object): # if not hasattr(self, '_assigned_source'): raise ValueError("set_source() must be called before compile()") - source, kwds = self._assigned_source - return recompile(self, self._recompiler_module_name, + source, kwds, module_name = self._assigned_source + return recompile(self, module_name, source, tmpdir=tmpdir, **kwds) diff --git a/cffi/recompiler.py b/cffi/recompiler.py index acd7510..d13df61 100644 --- a/cffi/recompiler.py +++ b/cffi/recompiler.py @@ -332,11 +332,19 @@ class Recompiler: if self.ffi._included_ffis: prnt('static const char * const _cffi_includes[] = {') for ffi_to_include in self.ffi._included_ffis: - if not hasattr(ffi_to_include, '_recompiler_module_name'): + try: + included_source, _, included_module_name = ( + ffi_to_include._assigned_source) + except AttributeError: + raise ffiplatform.VerificationError( + "ffi object %r includes %r, but the latter has not " + "been prepared with set_source()" % ( + self.ffi, ffi_to_include,)) + if included_source is None: raise ffiplatform.VerificationError( - "this ffi includes %r, but the latter has not been " - "compiled yet" % (ffi_to_include,)) - prnt(' "%s",' % (ffi_to_include._recompiler_module_name,)) + "not implemented yet: ffi.include() of a Python-based " + "ffi inside a C-based ffi") + prnt(' "%s",' % (included_module_name,)) prnt(' NULL') prnt('};') prnt() @@ -396,7 +404,6 @@ class Recompiler: self.module_name,)) prnt('}') prnt('#endif') - self.ffi._recompiler_module_name = self.module_name def _to_py(self, x): if isinstance(x, str): @@ -422,12 +429,19 @@ class Recompiler: num_includes = len(self.ffi._included_ffis or ()) for i in range(num_includes): ffi_to_include = self.ffi._included_ffis[i] - if not hasattr(ffi_to_include, '_recompiler_module_name'): + try: + included_source, _, included_module_name = ( + ffi_to_include._assigned_source) + except AttributeError: + raise ffiplatform.VerificationError( + "ffi object %r includes %r, but the latter has not " + "been prepared with set_source()" % ( + self.ffi, ffi_to_include,)) + if included_source is not None: raise ffiplatform.VerificationError( - "this ffi includes %r, but the latter has not been " - "compiled yet" % (ffi_to_include,)) - prnt('from %s import ffi as _ffi%d' % ( - ffi_to_include._recompiler_module_name, i)) + "not implemented yet: ffi.include() of a C-based " + "ffi inside a Python-based ffi") + prnt('from %s import ffi as _ffi%d' % (included_module_name, i)) prnt() prnt("ffi = _cffi_backend.FFI(%s," % (self._to_py(self.module_name),)) # @@ -450,7 +464,6 @@ class Recompiler: # # the footer prnt(')') - self.ffi._recompiler_module_name = self.module_name # ---------- diff --git a/testing/cffi1/test_dlopen.py b/testing/cffi1/test_dlopen.py index bc80900..1078029 100644 --- a/testing/cffi1/test_dlopen.py +++ b/testing/cffi1/test_dlopen.py @@ -86,6 +86,7 @@ ffi = _cffi_backend.FFI(b'test_struct', def test_include(): ffi = FFI() ffi.cdef("#define ABC 123") + ffi.set_source('test_include', None) target = udir.join('test_include.py') assert make_py_source(ffi, 'test_include', str(target)) assert target.read() == r"""# auto-generated file diff --git a/testing/cffi1/test_new_ffi_1.py b/testing/cffi1/test_new_ffi_1.py index d517f99..45382dd 100644 --- a/testing/cffi1/test_new_ffi_1.py +++ b/testing/cffi1/test_new_ffi_1.py @@ -85,6 +85,7 @@ def setup_module(): ffi1.cdef(DEFS) ffi1.cdef(DEFS_PACKED, packed=True) + ffi1.set_source("test_new_ffi_1", CCODE) outputfilename = recompile(ffi1, "test_new_ffi_1", CCODE, tmpdir=str(udir)) @@ -1514,7 +1515,6 @@ class TestNewFFI1: assert foo2.b == 30 def test_include_struct_union_enum_typedef(self): - #py.test.xfail("ffi.include") ffi1, CCODE = construction_params ffi2 = cffi.FFI() ffi2.include(ffi1) diff --git a/testing/cffi1/test_recompiler.py b/testing/cffi1/test_recompiler.py index fa2357d..023dd5b 100644 --- a/testing/cffi1/test_recompiler.py +++ b/testing/cffi1/test_recompiler.py @@ -16,9 +16,11 @@ def check_type_table(input, expected_output, included=None): recomp.collect_type_table() assert ''.join(map(str, recomp.cffi_types)) == expected_output -def verify(ffi, module_name, *args, **kwds): +def verify(ffi, module_name, source, *args, **kwds): kwds.setdefault('undef_macros', ['NDEBUG']) - return recompiler._verify(ffi, '_CFFI_' + module_name, *args, **kwds) + module_name = '_CFFI_' + module_name + ffi.set_source(module_name, source) + return recompiler._verify(ffi, module_name, source, *args, **kwds) def test_type_table_func(): diff --git a/testing/cffi1/test_verify1.py b/testing/cffi1/test_verify1.py index bf40b49..343cc2d 100644 --- a/testing/cffi1/test_verify1.py +++ b/testing/cffi1/test_verify1.py @@ -28,9 +28,15 @@ class FFI(FFI): _verify_counter = 0 def verify(self, preamble='', *args, **kwds): + # HACK to reuse the tests from ../cffi0/test_verify.py FFI._verify_counter += 1 - return recompiler._verify(self, 'verify%d' % FFI._verify_counter, - preamble, *args, + module_name = 'verify%d' % FFI._verify_counter + try: + del self._assigned_source + except AttributeError: + pass + self.set_source(module_name, preamble) + return recompiler._verify(self, module_name, preamble, *args, extra_compile_args=self._extra_compile_args, **kwds) |
