diff options
author | Martin Panter <vadmium+py@gmail.com> | 2015-11-13 22:12:12 +0000 |
---|---|---|
committer | Martin Panter <vadmium+py@gmail.com> | 2015-11-13 22:12:12 +0000 |
commit | 0987aabb36fb39866172fe7f8420cdc54d7d9871 (patch) | |
tree | 61223f7cde67774af7e6cd2030e0e2363a92afdc /Lib/ctypes | |
parent | 849adb29309f0b7b5a3877fa0b8e55279e6473b4 (diff) | |
parent | 78bb4fe76169350deb64c60dadff5dd7c0b315c5 (diff) | |
download | cpython-0987aabb36fb39866172fe7f8420cdc54d7d9871.tar.gz |
Issue #25498: Merge ctypes crash fix from 3.4 into 3.5
Diffstat (limited to 'Lib/ctypes')
-rw-r--r-- | Lib/ctypes/test/test_frombuffer.py | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/Lib/ctypes/test/test_frombuffer.py b/Lib/ctypes/test/test_frombuffer.py index 6aa2d1c591..86954fe61e 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) |