summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-02-02 23:25:42 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-02-02 23:25:42 (GMT)
commit3dd3e26680110b3eeadedc9c8457dfe1db2b913c (patch)
tree584a98eda6e93033ddaf313b95826e6e3cd887cb /Lib
parent0b2a6dc42b9bf02408764133a8dfcd843bbd0e66 (diff)
parent4fdb68491e8b2d044c9173babf625bbb815c39d1 (diff)
downloadcpython-3dd3e26680110b3eeadedc9c8457dfe1db2b913c.zip
cpython-3dd3e26680110b3eeadedc9c8457dfe1db2b913c.tar.gz
cpython-3dd3e26680110b3eeadedc9c8457dfe1db2b913c.tar.bz2
Issue #22896: Avoid to use PyObject_AsCharBuffer(), PyObject_AsReadBuffer()
and PyObject_AsWriteBuffer().
Diffstat (limited to 'Lib')
-rw-r--r--Lib/ctypes/test/test_frombuffer.py50
1 files changed, 33 insertions, 17 deletions
diff --git a/Lib/ctypes/test/test_frombuffer.py b/Lib/ctypes/test/test_frombuffer.py
index ffb27a6..6aa2d1c 100644
--- a/Lib/ctypes/test/test_frombuffer.py
+++ b/Lib/ctypes/test/test_frombuffer.py
@@ -10,7 +10,7 @@ class X(Structure):
self._init_called = True
class Test(unittest.TestCase):
- def test_fom_buffer(self):
+ def test_from_buffer(self):
a = array.array("i", range(16))
x = (c_int * 16).from_buffer(a)
@@ -23,25 +23,37 @@ class Test(unittest.TestCase):
a[0], a[-1] = 200, -200
self.assertEqual(x[:], a.tolist())
- self.assertIn(a, x._objects.values())
+ self.assertRaises(BufferError, a.append, 100)
+ self.assertRaises(BufferError, a.pop)
- self.assertRaises(ValueError,
- c_int.from_buffer, a, -1)
+ del x; del y; gc.collect(); gc.collect(); gc.collect()
+ a.append(100)
+ a.pop()
+ x = (c_int * 16).from_buffer(a)
+
+ self.assertIn(a, [obj.obj if isinstance(obj, memoryview) else obj
+ for obj in x._objects.values()])
expected = x[:]
del a; gc.collect(); gc.collect(); gc.collect()
self.assertEqual(x[:], expected)
- self.assertRaises(TypeError,
- (c_char * 16).from_buffer, "a" * 16)
+ with self.assertRaises(TypeError):
+ (c_char * 16).from_buffer(b"a" * 16)
+ with self.assertRaises(TypeError):
+ (c_char * 16).from_buffer("a" * 16)
- def test_fom_buffer_with_offset(self):
+ def test_from_buffer_with_offset(self):
a = array.array("i", range(16))
x = (c_int * 15).from_buffer(a, sizeof(c_int))
self.assertEqual(x[:], a.tolist()[1:])
- self.assertRaises(ValueError, lambda: (c_int * 16).from_buffer(a, sizeof(c_int)))
- self.assertRaises(ValueError, lambda: (c_int * 1).from_buffer(a, 16 * sizeof(c_int)))
+ with self.assertRaises(ValueError):
+ c_int.from_buffer(a, -1)
+ with self.assertRaises(ValueError):
+ (c_int * 16).from_buffer(a, sizeof(c_int))
+ with self.assertRaises(ValueError):
+ (c_int * 1).from_buffer(a, 16 * sizeof(c_int))
def test_from_buffer_copy(self):
a = array.array("i", range(16))
@@ -56,26 +68,30 @@ class Test(unittest.TestCase):
a[0], a[-1] = 200, -200
self.assertEqual(x[:], list(range(16)))
- self.assertEqual(x._objects, None)
+ a.append(100)
+ self.assertEqual(x[:], list(range(16)))
- self.assertRaises(ValueError,
- c_int.from_buffer, a, -1)
+ self.assertEqual(x._objects, None)
del a; gc.collect(); gc.collect(); gc.collect()
self.assertEqual(x[:], list(range(16)))
x = (c_char * 16).from_buffer_copy(b"a" * 16)
self.assertEqual(x[:], b"a" * 16)
+ with self.assertRaises(TypeError):
+ (c_char * 16).from_buffer_copy("a" * 16)
- def test_fom_buffer_copy_with_offset(self):
+ def test_from_buffer_copy_with_offset(self):
a = array.array("i", range(16))
x = (c_int * 15).from_buffer_copy(a, sizeof(c_int))
self.assertEqual(x[:], a.tolist()[1:])
- self.assertRaises(ValueError,
- (c_int * 16).from_buffer_copy, a, sizeof(c_int))
- self.assertRaises(ValueError,
- (c_int * 1).from_buffer_copy, a, 16 * sizeof(c_int))
+ with self.assertRaises(ValueError):
+ c_int.from_buffer_copy(a, -1)
+ with self.assertRaises(ValueError):
+ (c_int * 16).from_buffer_copy(a, sizeof(c_int))
+ with self.assertRaises(ValueError):
+ (c_int * 1).from_buffer_copy(a, 16 * sizeof(c_int))
if __name__ == '__main__':
unittest.main()