summaryrefslogtreecommitdiffstats
path: root/Lib/ctypes/test
diff options
context:
space:
mode:
authorThomas Heller <theller@ctypes.org>2008-04-25 15:44:16 (GMT)
committerThomas Heller <theller@ctypes.org>2008-04-25 15:44:16 (GMT)
commit6ad5fbb7ea90d475d77ee3b41a6ba198316b0d05 (patch)
treefef0c3cc8ea18ee7ea044abb324af6a6932dad0b /Lib/ctypes/test
parent5364e2e46f7566e1917d013f83c431a72de76009 (diff)
downloadcpython-6ad5fbb7ea90d475d77ee3b41a6ba198316b0d05.zip
cpython-6ad5fbb7ea90d475d77ee3b41a6ba198316b0d05.tar.gz
cpython-6ad5fbb7ea90d475d77ee3b41a6ba198316b0d05.tar.bz2
Add from_buffer and from_buffer_copy class methods to ctypes types.
Diffstat (limited to 'Lib/ctypes/test')
-rw-r--r--Lib/ctypes/test/test_frombuffer.py81
1 files changed, 81 insertions, 0 deletions
diff --git a/Lib/ctypes/test/test_frombuffer.py b/Lib/ctypes/test/test_frombuffer.py
new file mode 100644
index 0000000..4285e40
--- /dev/null
+++ b/Lib/ctypes/test/test_frombuffer.py
@@ -0,0 +1,81 @@
+from ctypes import *
+import array
+import gc
+import unittest
+
+class X(Structure):
+ _fields_ = [("c_int", c_int)]
+ init_called = False
+ def __init__(self):
+ self._init_called = True
+
+class Test(unittest.TestCase):
+ def test_fom_buffer(self):
+ a = array.array("i", range(16))
+ x = (c_int * 16).from_buffer(a)
+
+ y = X.from_buffer(a)
+ self.assertEqual(y.c_int, a[0])
+ self.failIf(y.init_called)
+
+ self.assertEqual(x[:], a.tolist())
+
+ a[0], a[-1] = 200, -200
+ self.assertEqual(x[:], a.tolist())
+
+ self.assert_(a in x._objects.values())
+
+ self.assertRaises(ValueError,
+ c_int.from_buffer, a, -1)
+
+ expected = x[:]
+ del a; gc.collect(); gc.collect(); gc.collect()
+ self.assertEqual(x[:], expected)
+
+ self.assertRaises(TypeError,
+ (c_char * 16).from_buffer, "a" * 16)
+
+ def test_fom_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)))
+
+ def test_from_buffer_copy(self):
+ a = array.array("i", range(16))
+ x = (c_int * 16).from_buffer_copy(a)
+
+ y = X.from_buffer_copy(a)
+ self.assertEqual(y.c_int, a[0])
+ self.failIf(y.init_called)
+
+ self.assertEqual(x[:], range(16))
+
+ a[0], a[-1] = 200, -200
+ self.assertEqual(x[:], range(16))
+
+ self.assertEqual(x._objects, None)
+
+ self.assertRaises(ValueError,
+ c_int.from_buffer, a, -1)
+
+ del a; gc.collect(); gc.collect(); gc.collect()
+ self.assertEqual(x[:], range(16))
+
+ x = (c_char * 16).from_buffer_copy("a" * 16)
+ self.assertEqual(x[:], "a" * 16)
+
+ def test_fom_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))
+
+if __name__ == '__main__':
+ unittest.main()