[pypy-commit] pypy default: don't crash on c_int.from_buffer()

arigo pypy.commits at gmail.com
Wed Feb 1 09:24:52 EST 2017


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r89884:b0a257824f6a
Date: 2017-02-01 15:24 +0100
http://bitbucket.org/pypy/pypy/changeset/b0a257824f6a/

Log:	don't crash on c_int.from_buffer()

diff --git a/lib_pypy/_ctypes/basics.py b/lib_pypy/_ctypes/basics.py
--- a/lib_pypy/_ctypes/basics.py
+++ b/lib_pypy/_ctypes/basics.py
@@ -102,7 +102,11 @@
                 % (len(buf) + offset, size + offset))
         raw_addr = buf._pypy_raw_address()
         result = self.from_address(raw_addr)
-        result._ensure_objects()['ffffffff'] = obj
+        objects = result._ensure_objects()
+        if objects is not None:
+            objects['ffffffff'] = obj
+        else:   # case e.g. of a primitive type like c_int
+            result._objects = obj
         return result
 
     def from_buffer_copy(self, obj, offset=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
@@ -24,6 +24,16 @@
         assert b[0] == "a"
         assert b[:] == "abc\0"
 
+    def test_from_buffer(self):
+        b1 = bytearray("abcde")
+        b = (c_char * 5).from_buffer(b1)
+        assert b[2] == "c"
+        #
+        b1 = bytearray("abcd")
+        b = c_int.from_buffer(b1)
+        assert b.value in (1684234849,   # little endian
+                           1633837924)   # big endian
+
     try:
         c_wchar
     except NameError:


More information about the pypy-commit mailing list