summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMartin Panter <vadmium+py@gmail.com>2015-11-13 22:14:53 (GMT)
committerMartin Panter <vadmium+py@gmail.com>2015-11-13 22:14:53 (GMT)
commitceaabc100c6d2dc05a559520b5be27f9c021dc13 (patch)
treecc77faafc5ad3c108464a984834cbbcc99bd45ce /Lib
parent84fdc4958f7a89976bccaf42e1d876777fa00831 (diff)
parent75559affad77d929ab8a87da226f080ec7b59dbd (diff)
downloadcpython-ceaabc100c6d2dc05a559520b5be27f9c021dc13.zip
cpython-ceaabc100c6d2dc05a559520b5be27f9c021dc13.tar.gz
cpython-ceaabc100c6d2dc05a559520b5be27f9c021dc13.tar.bz2
Issue #25498: Merge ctypes crash fix from 3.5
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..29c5a19 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 = "bytes-like object is required"
+ 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)