Author: Armin Rigo <ar...@tunes.org> Branch: Changeset: r84290:df4850cd8713 Date: 2016-05-08 18:39 +0200 http://bitbucket.org/pypy/pypy/changeset/df4850cd8713/
Log: import cffi/72a157dc2bde diff --git a/lib_pypy/cffi/api.py b/lib_pypy/cffi/api.py --- a/lib_pypy/cffi/api.py +++ b/lib_pypy/cffi/api.py @@ -397,20 +397,7 @@ data. Later, when this new cdata object is garbage-collected, 'destructor(old_cdata_object)' will be called. """ - try: - gcp = self._backend.gcp - except AttributeError: - pass - else: - return gcp(cdata, destructor) - # - with self._lock: - try: - gc_weakrefs = self.gc_weakrefs - except AttributeError: - from .gc_weakref import GcWeakrefs - gc_weakrefs = self.gc_weakrefs = GcWeakrefs(self) - return gc_weakrefs.build(cdata, destructor) + return self._backend.gcp(cdata, destructor) def _get_cached_btype(self, type): assert self._lock.acquire(False) is False diff --git a/lib_pypy/cffi/backend_ctypes.py b/lib_pypy/cffi/backend_ctypes.py --- a/lib_pypy/cffi/backend_ctypes.py +++ b/lib_pypy/cffi/backend_ctypes.py @@ -460,6 +460,11 @@ return x._value raise TypeError("character expected, got %s" % type(x).__name__) + def __nonzero__(self): + return ord(self._value) != 0 + else: + def __nonzero__(self): + return self._value != 0 if kind == 'float': @staticmethod @@ -993,6 +998,31 @@ assert onerror is None # XXX not implemented return BType(source, error) + def gcp(self, cdata, destructor): + BType = self.typeof(cdata) + + if destructor is None: + if not (hasattr(BType, '_gcp_type') and + BType._gcp_type is BType): + raise TypeError("Can remove destructor only on a object " + "previously returned by ffi.gc()") + cdata._destructor = None + return None + + try: + gcp_type = BType._gcp_type + except AttributeError: + class CTypesDataGcp(BType): + __slots__ = ['_orig', '_destructor'] + def __del__(self): + if self._destructor is not None: + self._destructor(self._orig) + gcp_type = BType._gcp_type = CTypesDataGcp + new_cdata = self.cast(gcp_type, cdata) + new_cdata._orig = cdata + new_cdata._destructor = destructor + return new_cdata + typeof = type def getcname(self, BType, replace_with): diff --git a/pypy/module/test_lib_pypy/cffi_tests/cffi0/backend_tests.py b/pypy/module/test_lib_pypy/cffi_tests/cffi0/backend_tests.py --- a/pypy/module/test_lib_pypy/cffi_tests/cffi0/backend_tests.py +++ b/pypy/module/test_lib_pypy/cffi_tests/cffi0/backend_tests.py @@ -56,7 +56,7 @@ max = int(max) p = ffi.cast(c_decl, min) assert p != min # no __eq__(int) - assert bool(p) is True + assert bool(p) is bool(min) assert int(p) == min p = ffi.cast(c_decl, max) assert int(p) == max @@ -285,7 +285,9 @@ assert ffi.new("char*", b"\xff")[0] == b'\xff' assert ffi.new("char*")[0] == b'\x00' assert int(ffi.cast("char", 300)) == 300 - 256 - assert bool(ffi.cast("char", 0)) + assert not bool(ffi.cast("char", 0)) + assert bool(ffi.cast("char", 1)) + assert bool(ffi.cast("char", 255)) py.test.raises(TypeError, ffi.new, "char*", 32) py.test.raises(TypeError, ffi.new, "char*", u+"x") py.test.raises(TypeError, ffi.new, "char*", b"foo") @@ -326,7 +328,11 @@ py.test.raises(TypeError, ffi.new, "wchar_t*", u+'\U00012345') assert ffi.new("wchar_t*")[0] == u+'\x00' assert int(ffi.cast("wchar_t", 300)) == 300 - assert bool(ffi.cast("wchar_t", 0)) + assert not bool(ffi.cast("wchar_t", 0)) + assert bool(ffi.cast("wchar_t", 1)) + assert bool(ffi.cast("wchar_t", 65535)) + if SIZE_OF_WCHAR > 2: + assert bool(ffi.cast("wchar_t", 65536)) py.test.raises(TypeError, ffi.new, "wchar_t*", 32) py.test.raises(TypeError, ffi.new, "wchar_t*", "foo") # @@ -1523,21 +1529,30 @@ import gc; gc.collect(); gc.collect(); gc.collect() assert seen == [3] + def test_gc_disable(self): + ffi = FFI(backend=self.Backend()) + p = ffi.new("int *", 123) + py.test.raises(TypeError, ffi.gc, p, None) + seen = [] + q1 = ffi.gc(p, lambda p: seen.append(1)) + q2 = ffi.gc(q1, lambda p: seen.append(2)) + import gc; gc.collect() + assert seen == [] + assert ffi.gc(q1, None) is None + del q1, q2 + import gc; gc.collect(); gc.collect(); gc.collect() + assert seen == [2] + def test_gc_finite_list(self): ffi = FFI(backend=self.Backend()) - public = not hasattr(ffi._backend, 'gcp') p = ffi.new("int *", 123) keepalive = [] for i in range(10): keepalive.append(ffi.gc(p, lambda p: None)) - if public: - assert len(ffi.gc_weakrefs.data) == i + 1 del keepalive[:] import gc; gc.collect(); gc.collect() for i in range(10): keepalive.append(ffi.gc(p, lambda p: None)) - if public: - assert len(ffi.gc_weakrefs.data) == 10 def test_CData_CType(self): ffi = FFI(backend=self.Backend()) diff --git a/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_ffi_backend.py b/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_ffi_backend.py --- a/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_ffi_backend.py +++ b/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_ffi_backend.py @@ -467,12 +467,12 @@ def test_introspect_order(self): ffi = FFI() - ffi.cdef("union aaa { int a; }; typedef struct ccc { int a; } b;") - ffi.cdef("union g { int a; }; typedef struct cc { int a; } bbb;") - ffi.cdef("union aa { int a; }; typedef struct a { int a; } bb;") - assert ffi.list_types() == (['b', 'bb', 'bbb'], - ['a', 'cc', 'ccc'], - ['aa', 'aaa', 'g']) + ffi.cdef("union CFFIaaa { int a; }; typedef struct CFFIccc { int a; } CFFIb;") + ffi.cdef("union CFFIg { int a; }; typedef struct CFFIcc { int a; } CFFIbbb;") + ffi.cdef("union CFFIaa { int a; }; typedef struct CFFIa { int a; } CFFIbb;") + assert ffi.list_types() == (['CFFIb', 'CFFIbb', 'CFFIbbb'], + ['CFFIa', 'CFFIcc', 'CFFIccc'], + ['CFFIaa', 'CFFIaaa', 'CFFIg']) def test_unpack(self): ffi = FFI() diff --git a/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_new_ffi_1.py b/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_new_ffi_1.py --- a/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_new_ffi_1.py +++ b/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_new_ffi_1.py @@ -139,7 +139,7 @@ max = int(max) p = ffi.cast(c_decl, min) assert p != min # no __eq__(int) - assert bool(p) is True + assert bool(p) is bool(min) assert int(p) == min p = ffi.cast(c_decl, max) assert int(p) == max @@ -351,7 +351,9 @@ assert ffi.new("char*", b"\xff")[0] == b'\xff' assert ffi.new("char*")[0] == b'\x00' assert int(ffi.cast("char", 300)) == 300 - 256 - assert bool(ffi.cast("char", 0)) + assert not bool(ffi.cast("char", 0)) + assert bool(ffi.cast("char", 1)) + assert bool(ffi.cast("char", 255)) py.test.raises(TypeError, ffi.new, "char*", 32) py.test.raises(TypeError, ffi.new, "char*", u+"x") py.test.raises(TypeError, ffi.new, "char*", b"foo") @@ -391,7 +393,11 @@ py.test.raises(TypeError, ffi.new, "wchar_t*", u+'\U00012345') assert ffi.new("wchar_t*")[0] == u+'\x00' assert int(ffi.cast("wchar_t", 300)) == 300 - assert bool(ffi.cast("wchar_t", 0)) + assert not bool(ffi.cast("wchar_t", 0)) + assert bool(ffi.cast("wchar_t", 1)) + assert bool(ffi.cast("wchar_t", 65535)) + if SIZE_OF_WCHAR > 2: + assert bool(ffi.cast("wchar_t", 65536)) py.test.raises(TypeError, ffi.new, "wchar_t*", 32) py.test.raises(TypeError, ffi.new, "wchar_t*", "foo") # diff --git a/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_recompiler.py b/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_recompiler.py --- a/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_recompiler.py +++ b/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_recompiler.py @@ -1898,14 +1898,14 @@ def test_introspect_order(): ffi = FFI() - ffi.cdef("union aaa { int a; }; typedef struct ccc { int a; } b;") - ffi.cdef("union g { int a; }; typedef struct cc { int a; } bbb;") - ffi.cdef("union aa { int a; }; typedef struct a { int a; } bb;") + ffi.cdef("union CFFIaaa { int a; }; typedef struct CFFIccc { int a; } CFFIb;") + ffi.cdef("union CFFIg { int a; }; typedef struct CFFIcc { int a; } CFFIbbb;") + ffi.cdef("union CFFIaa { int a; }; typedef struct CFFIa { int a; } CFFIbb;") verify(ffi, "test_introspect_order", """ - union aaa { int a; }; typedef struct ccc { int a; } b; - union g { int a; }; typedef struct cc { int a; } bbb; - union aa { int a; }; typedef struct a { int a; } bb; + union CFFIaaa { int a; }; typedef struct CFFIccc { int a; } CFFIb; + union CFFIg { int a; }; typedef struct CFFIcc { int a; } CFFIbbb; + union CFFIaa { int a; }; typedef struct CFFIa { int a; } CFFIbb; """) - assert ffi.list_types() == (['b', 'bb', 'bbb'], - ['a', 'cc', 'ccc'], - ['aa', 'aaa', 'g']) + assert ffi.list_types() == (['CFFIb', 'CFFIbb', 'CFFIbbb'], + ['CFFIa', 'CFFIcc', 'CFFIccc'], + ['CFFIaa', 'CFFIaaa', 'CFFIg']) diff --git a/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_zdist.py b/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_zdist.py --- a/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_zdist.py +++ b/pypy/module/test_lib_pypy/cffi_tests/cffi1/test_zdist.py @@ -280,6 +280,14 @@ pass with open("setup.py", "w") as f: f.write("""if 1: + # https://bugs.python.org/issue23246 + import sys + if sys.platform == 'win32': + try: + import setuptools + except ImportError: + pass + import cffi ffi = cffi.FFI() ffi.set_source("pack1.mymod", "/*code would be here*/") diff --git a/pypy/module/test_lib_pypy/cffi_tests/embedding/test_basic.py b/pypy/module/test_lib_pypy/cffi_tests/embedding/test_basic.py --- a/pypy/module/test_lib_pypy/cffi_tests/embedding/test_basic.py +++ b/pypy/module/test_lib_pypy/cffi_tests/embedding/test_basic.py @@ -80,8 +80,21 @@ # find a solution to that: we could hack sys.path inside the # script run here, but we can't hack it in the same way in # execute(). - output = self._run([sys.executable, - os.path.join(local_dir, filename)]) + pathname = os.path.join(path, filename) + with open(pathname, 'w') as g: + g.write(''' +# https://bugs.python.org/issue23246 +import sys +if sys.platform == 'win32': + try: + import setuptools + except ImportError: + pass +''') + with open(os.path.join(local_dir, filename), 'r') as f: + g.write(f.read()) + + output = self._run([sys.executable, pathname]) match = re.compile(r"\bFILENAME: (.+)").search(output) assert match dynamic_lib_name = match.group(1) diff --git a/pypy/module/test_lib_pypy/cffi_tests/udir.py b/pypy/module/test_lib_pypy/cffi_tests/udir.py --- a/pypy/module/test_lib_pypy/cffi_tests/udir.py +++ b/pypy/module/test_lib_pypy/cffi_tests/udir.py @@ -1,4 +1,14 @@ # Generated by pypy/tool/import_cffi.py import py +import sys udir = py.path.local.make_numbered_dir(prefix = 'ffi-') + + +# Windows-only workaround for some configurations: see +# https://bugs.python.org/issue23246 (Python 2.7.9) +if sys.platform == 'win32': + try: + import setuptools + except ImportError: + pass _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit