summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMartin Panter <vadmium+py@gmail.com>2015-11-13 21:43:39 (GMT)
committerMartin Panter <vadmium+py@gmail.com>2015-11-13 21:43:39 (GMT)
commit1bb651540e0743c4e58d875d1de240597862fa34 (patch)
treea0be0c700ee103423c37bcb2377d7fe345bc7656 /Lib
parentabe9625eeb71e40f042ccfccfe6a4489a6dcdf35 (diff)
downloadcpython-1bb651540e0743c4e58d875d1de240597862fa34.zip
cpython-1bb651540e0743c4e58d875d1de240597862fa34.tar.gz
cpython-1bb651540e0743c4e58d875d1de240597862fa34.tar.bz2
Issue #25498: Fix GC crash due to ctypes objects wrapping a memoryview
This was a regression caused by revision 1da9630e9b7f. Based on patch by Eryksun.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/ctypes/test/test_frombuffer.py31
1 files changed, 29 insertions, 2 deletions
diff --git a/Lib/ctypes/test/test_frombuffer.py b/Lib/ctypes/test/test_frombuffer.py
index 6aa2d1c..86954fe 100644
--- a/Lib/ctypes/test/test_frombuffer.py
+++ b/Lib/ctypes/test/test_frombuffer.py
@@ -38,11 +38,32 @@ class Test(unittest.TestCase):
del a; gc.collect(); gc.collect(); gc.collect()
self.assertEqual(x[:], expected)
- with self.assertRaises(TypeError):
+ with self.assertRaisesRegex(TypeError, "not writable"):
(c_char * 16).from_buffer(b"a" * 16)
- with self.assertRaises(TypeError):
+ with self.assertRaisesRegex(TypeError, "not writable"):
+ (c_char * 16).from_buffer(memoryview(b"a" * 16))
+ with self.assertRaisesRegex(TypeError, "not C contiguous"):
+ (c_char * 16).from_buffer(memoryview(bytearray(b"a" * 16))[::-1])
+ msg = "does not have the buffer interface"
+ with self.assertRaisesRegex(TypeError, msg):
(c_char * 16).from_buffer("a" * 16)
+ def test_fortran_contiguous(self):
+ try:
+ import _testbuffer
+ except ImportError as err:
+ self.skipTest(str(err))
+ flags = _testbuffer.ND_WRITABLE | _testbuffer.ND_FORTRAN
+ array = _testbuffer.ndarray(
+ [97] * 16, format="B", shape=[4, 4], flags=flags)
+ with self.assertRaisesRegex(TypeError, "not C contiguous"):
+ (c_char * 16).from_buffer(array)
+ array = memoryview(array)
+ self.assertTrue(array.f_contiguous)
+ self.assertFalse(array.c_contiguous)
+ with self.assertRaisesRegex(TypeError, "not C contiguous"):
+ (c_char * 16).from_buffer(array)
+
def test_from_buffer_with_offset(self):
a = array.array("i", range(16))
x = (c_int * 15).from_buffer(a, sizeof(c_int))
@@ -55,6 +76,12 @@ class Test(unittest.TestCase):
with self.assertRaises(ValueError):
(c_int * 1).from_buffer(a, 16 * sizeof(c_int))
+ def test_from_buffer_memoryview(self):
+ a = [c_char.from_buffer(memoryview(bytearray(b'a')))]
+ a.append(a)
+ del a
+ gc.collect() # Should not crash
+
def test_from_buffer_copy(self):
a = array.array("i", range(16))
x = (c_int * 16).from_buffer_copy(a)