[pypy-commit] pypy default: Issue #2878

arigo pypy.commits at gmail.com
Sat Sep 1 08:59:25 EDT 2018


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r95055:28c9dafa5a46
Date: 2018-09-01 14:58 +0200
http://bitbucket.org/pypy/pypy/changeset/28c9dafa5a46/

Log:	Issue #2878

	Handle a case of keepalive that I imagined would never occur

diff --git a/lib_pypy/_ctypes/primitive.py b/lib_pypy/_ctypes/primitive.py
--- a/lib_pypy/_ctypes/primitive.py
+++ b/lib_pypy/_ctypes/primitive.py
@@ -393,9 +393,11 @@
     _init_no_arg_ = __init__
 
     def _ensure_objects(self):
-        if self._type_ not in 'zZP':
-            assert self._objects is None
-        return self._objects
+        # No '_objects' is the common case for primitives.  Examples
+        # where there is an _objects is if _type in 'zZP', or if
+        # self comes from 'from_buffer(buf)'.  See module/test_lib_pypy/
+        # ctypes_test/test_buffers.py: test_from_buffer_keepalive.
+        return getattr(self, '_objects', None)
 
     def _getvalue(self):
         return self._buffer[0]
diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_buffers.py b/pypy/module/test_lib_pypy/ctypes_tests/test_buffers.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_buffers.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_buffers.py
@@ -39,6 +39,15 @@
         assert b.value in (1684234849,   # little endian
                            1633837924)   # big endian
 
+    def test_from_buffer_keepalive(self):
+        # Issue #2878
+        b1 = bytearray("ab")
+        array = (c_uint16 * 32)()
+        array[6] = c_uint16.from_buffer(b1)
+        # this is also what we get on CPython.  I don't think it makes
+        # sense because the array contains just a copy of the number.
+        assert array._objects == {'6': b1}
+
     try:
         c_wchar
     except NameError:


More information about the pypy-commit mailing list