diff options
author | Armin Rigo <arigo@tunes.org> | 2016-09-03 19:29:47 +0200 |
---|---|---|
committer | Armin Rigo <arigo@tunes.org> | 2016-09-03 19:29:47 +0200 |
commit | 3b4972b89c6f5efc62d90e2d5969c280e2bdd613 (patch) | |
tree | ea6d513a2baf87bd812ef9678cb831add13e7a60 /cffi/api.py | |
parent | bc99d488c73e2cfc4907088ddf4bf055821864e5 (diff) | |
download | cffi-3b4972b89c6f5efc62d90e2d5969c280e2bdd613.tar.gz |
Backed out changeset 0087e2aec9ef
Un-kill the ctypes backend. Issue #282 for a justification.
Diffstat (limited to 'cffi/api.py')
-rw-r--r-- | cffi/api.py | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/cffi/api.py b/cffi/api.py index 6d1eb48..eda7209 100644 --- a/cffi/api.py +++ b/cffi/api.py @@ -46,21 +46,20 @@ class FFI(object): ''' def __init__(self, backend=None): - """Create an FFI instance. - - The 'backend' argument is not used any more and must be set to None. - It is still present only so that 'FFI(None)' still works, and - for a few tests. + """Create an FFI instance. The 'backend' argument is used to + select a non-default backend, mostly for tests. """ from . import cparser, model - if backend is None: - # You need the corresponding version of PyPy, or CPython - # with the '_cffi_backend' C extension module compiled. + # You need PyPy (>= 2.0 beta), or a CPython (>= 2.6) with + # _cffi_backend.so compiled. import _cffi_backend as backend from . import __version__ assert backend.__version__ == __version__, \ "version mismatch, %s != %s" % (backend.__version__, __version__) + # (If you insist you can also try to pass the option + # 'backend=backend_ctypes.CTypesBackend()', but don't + # rely on it! It's probably not going to work well.) self._backend = backend self._lock = allocate_lock() @@ -76,6 +75,8 @@ class FFI(object): self._init_once_cache = {} self._cdef_version = None self._embedding = None + if hasattr(backend, 'set_ffi'): + backend.set_ffi(self) for name in backend.__dict__: if name.startswith('RTLD_'): setattr(self, name, getattr(backend, name)) @@ -83,10 +84,15 @@ class FFI(object): with self._lock: self.BVoidP = self._get_cached_btype(model.voidp_type) self.BCharA = self._get_cached_btype(model.char_array_type) - # attach these constants to the class - if not hasattr(FFI, 'NULL'): - FFI.NULL = self.cast(self.BVoidP, 0) - FFI.CData, FFI.CType = backend._get_types() + if isinstance(backend, types.ModuleType): + # _cffi_backend: attach these constants to the class + if not hasattr(FFI, 'NULL'): + FFI.NULL = self.cast(self.BVoidP, 0) + FFI.CData, FFI.CType = backend._get_types() + else: + # ctypes backend: attach these constants to the instance + self.NULL = self.cast(self.BVoidP, 0) + self.CData, self.CType = backend._get_types() def cdef(self, csource, override=False, packed=False): """Parse the given C source. This registers all declared functions, |