diff options
Diffstat (limited to 'Lib/ctypes/test')
-rw-r--r-- | Lib/ctypes/test/test_anon.py | 60 | ||||
-rw-r--r-- | Lib/ctypes/test/test_cast.py | 37 | ||||
-rw-r--r-- | Lib/ctypes/test/test_keeprefs.py | 9 | ||||
-rw-r--r-- | Lib/ctypes/test/test_loading.py | 10 | ||||
-rw-r--r-- | Lib/ctypes/test/test_objects.py | 70 | ||||
-rw-r--r-- | Lib/ctypes/test/test_parameters.py | 35 | ||||
-rw-r--r-- | Lib/ctypes/test/test_pointers.py | 17 | ||||
-rw-r--r-- | Lib/ctypes/test/test_slicing.py | 26 | ||||
-rw-r--r-- | Lib/ctypes/test/test_structures.py | 22 | ||||
-rw-r--r-- | Lib/ctypes/test/test_varsize_struct.py | 50 | ||||
-rw-r--r-- | Lib/ctypes/test/test_win32.py | 14 |
11 files changed, 310 insertions, 40 deletions
diff --git a/Lib/ctypes/test/test_anon.py b/Lib/ctypes/test/test_anon.py new file mode 100644 index 0000000..99e02cb --- /dev/null +++ b/Lib/ctypes/test/test_anon.py @@ -0,0 +1,60 @@ +import unittest +from ctypes import * + +class AnonTest(unittest.TestCase): + + def test_anon(self): + class ANON(Union): + _fields_ = [("a", c_int), + ("b", c_int)] + + class Y(Structure): + _fields_ = [("x", c_int), + ("_", ANON), + ("y", c_int)] + _anonymous_ = ["_"] + + self.failUnlessEqual(Y.a.offset, sizeof(c_int)) + self.failUnlessEqual(Y.b.offset, sizeof(c_int)) + + self.failUnlessEqual(ANON.a.offset, 0) + self.failUnlessEqual(ANON.b.offset, 0) + + def test_anon_nonseq(self): + # TypeError: _anonymous_ must be a sequence + self.failUnlessRaises(TypeError, + lambda: type(Structure)("Name", + (Structure,), + {"_fields_": [], "_anonymous_": 42})) + + def test_anon_nonmember(self): + # AttributeError: type object 'Name' has no attribute 'x' + self.failUnlessRaises(AttributeError, + lambda: type(Structure)("Name", + (Structure,), + {"_fields_": [], + "_anonymous_": ["x"]})) + + def test_nested(self): + class ANON_S(Structure): + _fields_ = [("a", c_int)] + + class ANON_U(Union): + _fields_ = [("_", ANON_S), + ("b", c_int)] + _anonymous_ = ["_"] + + class Y(Structure): + _fields_ = [("x", c_int), + ("_", ANON_U), + ("y", c_int)] + _anonymous_ = ["_"] + + self.failUnlessEqual(Y.x.offset, 0) + self.failUnlessEqual(Y.a.offset, sizeof(c_int)) + self.failUnlessEqual(Y.b.offset, sizeof(c_int)) + self.failUnlessEqual(Y._.offset, sizeof(c_int)) + self.failUnlessEqual(Y.y.offset, sizeof(c_int) * 2) + +if __name__ == "__main__": + unittest.main() diff --git a/Lib/ctypes/test/test_cast.py b/Lib/ctypes/test/test_cast.py index 821ce3f..09e928f 100644 --- a/Lib/ctypes/test/test_cast.py +++ b/Lib/ctypes/test/test_cast.py @@ -30,17 +30,32 @@ class Test(unittest.TestCase): ptr = cast(address, POINTER(c_int)) self.failUnlessEqual([ptr[i] for i in range(3)], [42, 17, 2]) - - def test_ptr2array(self): - array = (c_int * 3)(42, 17, 2) - - from sys import getrefcount - - before = getrefcount(array) - ptr = cast(array, POINTER(c_int)) - self.failUnlessEqual(getrefcount(array), before + 1) - del ptr - self.failUnlessEqual(getrefcount(array), before) + def test_p2a_objects(self): + array = (c_char_p * 5)() + self.failUnlessEqual(array._objects, None) + array[0] = "foo bar" + self.failUnlessEqual(array._objects, {'0': "foo bar"}) + + p = cast(array, POINTER(c_char_p)) + # array and p share a common _objects attribute + self.failUnless(p._objects is array._objects) + self.failUnlessEqual(array._objects, {'0': "foo bar", id(array): array}) + p[0] = "spam spam" + self.failUnlessEqual(p._objects, {'0': "spam spam", id(array): array}) + self.failUnless(array._objects is p._objects) + p[1] = "foo bar" + self.failUnlessEqual(p._objects, {'1': 'foo bar', '0': "spam spam", id(array): array}) + self.failUnless(array._objects is p._objects) + + def test_other(self): + p = cast((c_int * 4)(1, 2, 3, 4), POINTER(c_int)) + self.failUnlessEqual(p[:4], [1,2, 3, 4]) + c_int() + self.failUnlessEqual(p[:4], [1, 2, 3, 4]) + p[2] = 96 + self.failUnlessEqual(p[:4], [1, 2, 96, 4]) + c_int() + self.failUnlessEqual(p[:4], [1, 2, 96, 4]) if __name__ == "__main__": unittest.main() diff --git a/Lib/ctypes/test/test_keeprefs.py b/Lib/ctypes/test/test_keeprefs.py index 7318f29..80b6ca2 100644 --- a/Lib/ctypes/test/test_keeprefs.py +++ b/Lib/ctypes/test/test_keeprefs.py @@ -61,6 +61,8 @@ class StructureTestCase(unittest.TestCase): r.ul.x = 22 r.ul.y = 44 self.assertEquals(r._objects, {'0': {}}) + r.lr = POINT() + self.assertEquals(r._objects, {'0': {}, '1': {}}) class ArrayTestCase(unittest.TestCase): def test_cint_array(self): @@ -86,9 +88,10 @@ class ArrayTestCase(unittest.TestCase): self.assertEquals(x._objects, {'1': {}}) class PointerTestCase(unittest.TestCase): - def X_test_p_cint(self): - x = pointer(c_int(42)) - print x._objects + def test_p_cint(self): + i = c_int(42) + x = pointer(i) + self.failUnlessEqual(x._objects, {'1': i}) class DeletePointerTestCase(unittest.TestCase): def X_test(self): diff --git a/Lib/ctypes/test/test_loading.py b/Lib/ctypes/test/test_loading.py index 45585ae..28c83fd4 100644 --- a/Lib/ctypes/test/test_loading.py +++ b/Lib/ctypes/test/test_loading.py @@ -9,18 +9,10 @@ if os.name == "nt": libc_name = "msvcrt" elif os.name == "ce": libc_name = "coredll" -elif sys.platform == "darwin": - libc_name = "libc.dylib" elif sys.platform == "cygwin": libc_name = "cygwin1.dll" else: - for line in os.popen("ldd %s" % sys.executable): - if "libc.so" in line: - if sys.platform == "openbsd3": - libc_name = line.split()[4] - else: - libc_name = line.split()[2] - break + libc_name = find_library("c") if is_resource_enabled("printing"): print "libc_name is", libc_name diff --git a/Lib/ctypes/test/test_objects.py b/Lib/ctypes/test/test_objects.py new file mode 100644 index 0000000..4d921d2 --- /dev/null +++ b/Lib/ctypes/test/test_objects.py @@ -0,0 +1,70 @@ +r''' +This tests the '_objects' attribute of ctypes instances. '_objects' +holds references to objects that must be kept alive as long as the +ctypes instance, to make sure that the memory buffer is valid. + +WARNING: The '_objects' attribute is exposed ONLY for debugging ctypes itself, +it MUST NEVER BE MODIFIED! + +'_objects' is initialized to a dictionary on first use, before that it +is None. + +Here is an array of string pointers: + +>>> from ctypes import * +>>> array = (c_char_p * 5)() +>>> print array._objects +None +>>> + +The memory block stores pointers to strings, and the strings itself +assigned from Python must be kept. + +>>> array[4] = 'foo bar' +>>> array._objects +{'4': 'foo bar'} +>>> array[4] +'foo bar' +>>> + +It gets more complicated when the ctypes instance itself is contained +in a 'base' object. + +>>> class X(Structure): +... _fields_ = [("x", c_int), ("y", c_int), ("array", c_char_p * 5)] +... +>>> x = X() +>>> print x._objects +None +>>> + +The'array' attribute of the 'x' object shares part of the memory buffer +of 'x' ('_b_base_' is either None, or the root object owning the memory block): + +>>> print x.array._b_base_ # doctest: +ELLIPSIS +<ctypes.test.test_objects.X object at 0x...> +>>> + +>>> x.array[0] = 'spam spam spam' +>>> x._objects +{'0:2': 'spam spam spam'} +>>> x.array._b_base_._objects +{'0:2': 'spam spam spam'} +>>> + +''' + +import unittest, doctest, sys + +import ctypes.test.test_objects + +class TestCase(unittest.TestCase): + if sys.hexversion > 0x02040000: + # Python 2.3 has no ELLIPSIS flag, so we don't test with this + # version: + def test(self): + doctest.testmod(ctypes.test.test_objects) + +if __name__ == '__main__': + if sys.hexversion > 0x02040000: + doctest.testmod(ctypes.test.test_objects) diff --git a/Lib/ctypes/test/test_parameters.py b/Lib/ctypes/test/test_parameters.py index 9537400..1b7f0dc 100644 --- a/Lib/ctypes/test/test_parameters.py +++ b/Lib/ctypes/test/test_parameters.py @@ -147,6 +147,41 @@ class SimpleTypesTestCase(unittest.TestCase): ## def test_performance(self): ## check_perf() + def test_noctypes_argtype(self): + import _ctypes_test + from ctypes import CDLL, c_void_p, ArgumentError + + func = CDLL(_ctypes_test.__file__)._testfunc_p_p + func.restype = c_void_p + # TypeError: has no from_param method + self.assertRaises(TypeError, setattr, func, "argtypes", (object,)) + + class Adapter(object): + def from_param(cls, obj): + return None + + func.argtypes = (Adapter(),) + self.failUnlessEqual(func(None), None) + self.failUnlessEqual(func(object()), None) + + class Adapter(object): + def from_param(cls, obj): + return obj + + func.argtypes = (Adapter(),) + # don't know how to convert parameter 1 + self.assertRaises(ArgumentError, func, object()) + self.failUnlessEqual(func(c_void_p(42)), 42) + + class Adapter(object): + def from_param(cls, obj): + raise ValueError(obj) + + func.argtypes = (Adapter(),) + # ArgumentError: argument 1: ValueError: 99 + self.assertRaises(ArgumentError, func, 99) + + ################################################################ if __name__ == '__main__': diff --git a/Lib/ctypes/test/test_pointers.py b/Lib/ctypes/test/test_pointers.py index a7a2802..586655a 100644 --- a/Lib/ctypes/test/test_pointers.py +++ b/Lib/ctypes/test/test_pointers.py @@ -157,6 +157,23 @@ class PointersTestCase(unittest.TestCase): q = pointer(y) pp[0] = q # <== self.failUnlessEqual(p[0], 6) + def test_c_void_p(self): + # http://sourceforge.net/tracker/?func=detail&aid=1518190&group_id=5470&atid=105470 + if sizeof(c_void_p) == 4: + self.failUnlessEqual(c_void_p(0xFFFFFFFFL).value, + c_void_p(-1).value) + self.failUnlessEqual(c_void_p(0xFFFFFFFFFFFFFFFFL).value, + c_void_p(-1).value) + elif sizeof(c_void_p) == 8: + self.failUnlessEqual(c_void_p(0xFFFFFFFFL).value, + 0xFFFFFFFFL) + self.failUnlessEqual(c_void_p(0xFFFFFFFFFFFFFFFFL).value, + c_void_p(-1).value) + self.failUnlessEqual(c_void_p(0xFFFFFFFFFFFFFFFFFFFFFFFFL).value, + c_void_p(-1).value) + + self.assertRaises(TypeError, c_void_p, 3.14) # make sure floats are NOT accepted + self.assertRaises(TypeError, c_void_p, object()) # nor other objects if __name__ == '__main__': unittest.main() diff --git a/Lib/ctypes/test/test_slicing.py b/Lib/ctypes/test/test_slicing.py index 08c811e..511c3d3 100644 --- a/Lib/ctypes/test/test_slicing.py +++ b/Lib/ctypes/test/test_slicing.py @@ -35,7 +35,7 @@ class SlicesTestCase(unittest.TestCase): self.assertRaises(ValueError, setslice, a, 0, 5, range(32)) def test_char_ptr(self): - s = "abcdefghijklmnopqrstuvwxyz\0" + s = "abcdefghijklmnopqrstuvwxyz" dll = CDLL(_ctypes_test.__file__) dll.my_strdup.restype = POINTER(c_char) @@ -50,9 +50,31 @@ class SlicesTestCase(unittest.TestCase): dll.my_strdup.restype = POINTER(c_byte) res = dll.my_strdup(s) - self.failUnlessEqual(res[:len(s)-1], range(ord("a"), ord("z")+1)) + self.failUnlessEqual(res[:len(s)], range(ord("a"), ord("z")+1)) dll.my_free(res) + def test_char_ptr_with_free(self): + dll = CDLL(_ctypes_test.__file__) + s = "abcdefghijklmnopqrstuvwxyz" + + class allocated_c_char_p(c_char_p): + pass + + dll.my_free.restype = None + def errcheck(result, func, args): + retval = result.value + dll.my_free(result) + return retval + + dll.my_strdup.restype = allocated_c_char_p + dll.my_strdup.errcheck = errcheck + try: + res = dll.my_strdup(s) + self.failUnlessEqual(res, s) + finally: + del dll.my_strdup.errcheck + + def test_char_array(self): s = "abcdefghijklmnopqrstuvwxyz\0" diff --git a/Lib/ctypes/test/test_structures.py b/Lib/ctypes/test/test_structures.py index 49f064b..8a4531d 100644 --- a/Lib/ctypes/test/test_structures.py +++ b/Lib/ctypes/test/test_structures.py @@ -138,8 +138,8 @@ class StructureTestCase(unittest.TestCase): self.failUnlessEqual(X.y.size, sizeof(c_char)) # readonly - self.assertRaises(AttributeError, setattr, X.x, "offset", 92) - self.assertRaises(AttributeError, setattr, X.x, "size", 92) + self.assertRaises((TypeError, AttributeError), setattr, X.x, "offset", 92) + self.assertRaises((TypeError, AttributeError), setattr, X.x, "size", 92) class X(Union): _fields_ = [("x", c_int), @@ -152,8 +152,8 @@ class StructureTestCase(unittest.TestCase): self.failUnlessEqual(X.y.size, sizeof(c_char)) # readonly - self.assertRaises(AttributeError, setattr, X.x, "offset", 92) - self.assertRaises(AttributeError, setattr, X.x, "size", 92) + self.assertRaises((TypeError, AttributeError), setattr, X.x, "offset", 92) + self.assertRaises((TypeError, AttributeError), setattr, X.x, "size", 92) # XXX Should we check nested data types also? # offset is always relative to the class... @@ -298,7 +298,7 @@ class StructureTestCase(unittest.TestCase): "expected string or Unicode object, int found") else: self.failUnlessEqual(msg, - "(Phone) TypeError: " + "(Phone) exceptions.TypeError: " "expected string or Unicode object, int found") cls, msg = self.get_except(Person, "Someone", ("a", "b", "c")) @@ -307,7 +307,7 @@ class StructureTestCase(unittest.TestCase): self.failUnlessEqual(msg, "(Phone) <type 'exceptions.ValueError'>: too many initializers") else: - self.failUnlessEqual(msg, "(Phone) ValueError: too many initializers") + self.failUnlessEqual(msg, "(Phone) exceptions.ValueError: too many initializers") def get_except(self, func, *args): @@ -371,5 +371,15 @@ class PointerMemberTestCase(unittest.TestCase): items = [s.array[i] for i in range(3)] self.failUnlessEqual(items, [1, 2, 3]) + def test_none_to_pointer_fields(self): + class S(Structure): + _fields_ = [("x", c_int), + ("p", POINTER(c_int))] + + s = S() + s.x = 12345678 + s.p = None + self.failUnlessEqual(s.x, 12345678) + if __name__ == '__main__': unittest.main() diff --git a/Lib/ctypes/test/test_varsize_struct.py b/Lib/ctypes/test/test_varsize_struct.py new file mode 100644 index 0000000..06d2323 --- /dev/null +++ b/Lib/ctypes/test/test_varsize_struct.py @@ -0,0 +1,50 @@ +from ctypes import * +import unittest + +class VarSizeTest(unittest.TestCase): + def test_resize(self): + class X(Structure): + _fields_ = [("item", c_int), + ("array", c_int * 1)] + + self.failUnlessEqual(sizeof(X), sizeof(c_int) * 2) + x = X() + x.item = 42 + x.array[0] = 100 + self.failUnlessEqual(sizeof(x), sizeof(c_int) * 2) + + # make room for one additional item + new_size = sizeof(X) + sizeof(c_int) * 1 + resize(x, new_size) + self.failUnlessEqual(sizeof(x), new_size) + self.failUnlessEqual((x.item, x.array[0]), (42, 100)) + + # make room for 10 additional items + new_size = sizeof(X) + sizeof(c_int) * 9 + resize(x, new_size) + self.failUnlessEqual(sizeof(x), new_size) + self.failUnlessEqual((x.item, x.array[0]), (42, 100)) + + # make room for one additional item + new_size = sizeof(X) + sizeof(c_int) * 1 + resize(x, new_size) + self.failUnlessEqual(sizeof(x), new_size) + self.failUnlessEqual((x.item, x.array[0]), (42, 100)) + + def test_array_invalid_length(self): + # cannot create arrays with non-positive size + self.failUnlessRaises(ValueError, lambda: c_int * -1) + self.failUnlessRaises(ValueError, lambda: c_int * -3) + + def test_zerosized_array(self): + array = (c_int * 0)() + # accessing elements of zero-sized arrays raise IndexError + self.failUnlessRaises(IndexError, array.__setitem__, 0, None) + self.failUnlessRaises(IndexError, array.__getitem__, 0) + self.failUnlessRaises(IndexError, array.__setitem__, 1, None) + self.failUnlessRaises(IndexError, array.__getitem__, 1) + self.failUnlessRaises(IndexError, array.__setitem__, -1, None) + self.failUnlessRaises(IndexError, array.__getitem__, -1) + +if __name__ == "__main__": + unittest.main() diff --git a/Lib/ctypes/test/test_win32.py b/Lib/ctypes/test/test_win32.py index 8247d37..db530d3 100644 --- a/Lib/ctypes/test/test_win32.py +++ b/Lib/ctypes/test/test_win32.py @@ -1,6 +1,7 @@ # Windows specific tests from ctypes import * +from ctypes.test import is_resource_enabled import unittest, sys import _ctypes_test @@ -30,15 +31,10 @@ if sys.platform == "win32": # or wrong calling convention self.assertRaises(ValueError, IsWindow, None) - def test_SEH(self): - # Call functions with invalid arguments, and make sure that access violations - # are trapped and raise an exception. - # - # Normally, in a debug build of the _ctypes extension - # module, exceptions are not trapped, so we can only run - # this test in a release build. - import sys - if not hasattr(sys, "getobjects"): + if is_resource_enabled("SEH"): + def test_SEH(self): + # Call functions with invalid arguments, and make sure that access violations + # are trapped and raise an exception. self.assertRaises(WindowsError, windll.kernel32.GetModuleHandleA, 32) class Structures(unittest.TestCase): |