@@ -38,11 +38,32 @@ def test_from_buffer(self):
3838 del a ; gc .collect (); gc .collect (); gc .collect ()
3939 self .assertEqual (x [:], expected )
4040
41- with self .assertRaises (TypeError ):
41+ with self .assertRaisesRegex (TypeError , "not writable" ):
4242 (c_char * 16 ).from_buffer (b"a" * 16 )
43- with self .assertRaises (TypeError ):
43+ with self .assertRaisesRegex (TypeError , "not writable" ):
44+ (c_char * 16 ).from_buffer (memoryview (b"a" * 16 ))
45+ with self .assertRaisesRegex (TypeError , "not C contiguous" ):
46+ (c_char * 16 ).from_buffer (memoryview (bytearray (b"a" * 16 ))[::- 1 ])
47+ msg = "does not have the buffer interface"
48+ with self .assertRaisesRegex (TypeError , msg ):
4449 (c_char * 16 ).from_buffer ("a" * 16 )
4550
51+ def test_fortran_contiguous (self ):
52+ try :
53+ import _testbuffer
54+ except ImportError as err :
55+ self .skipTest (str (err ))
56+ flags = _testbuffer .ND_WRITABLE | _testbuffer .ND_FORTRAN
57+ array = _testbuffer .ndarray (
58+ [97 ] * 16 , format = "B" , shape = [4 , 4 ], flags = flags )
59+ with self .assertRaisesRegex (TypeError , "not C contiguous" ):
60+ (c_char * 16 ).from_buffer (array )
61+ array = memoryview (array )
62+ self .assertTrue (array .f_contiguous )
63+ self .assertFalse (array .c_contiguous )
64+ with self .assertRaisesRegex (TypeError , "not C contiguous" ):
65+ (c_char * 16 ).from_buffer (array )
66+
4667 def test_from_buffer_with_offset (self ):
4768 a = array .array ("i" , range (16 ))
4869 x = (c_int * 15 ).from_buffer (a , sizeof (c_int ))
@@ -55,6 +76,12 @@ def test_from_buffer_with_offset(self):
5576 with self .assertRaises (ValueError ):
5677 (c_int * 1 ).from_buffer (a , 16 * sizeof (c_int ))
5778
79+ def test_from_buffer_memoryview (self ):
80+ a = [c_char .from_buffer (memoryview (bytearray (b'a' )))]
81+ a .append (a )
82+ del a
83+ gc .collect () # Should not crash
84+
5885 def test_from_buffer_copy (self ):
5986 a = array .array ("i" , range (16 ))
6087 x = (c_int * 16 ).from_buffer_copy (a )
0 commit comments