summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorThomas Heller <theller@ctypes.org>2008-04-30 17:11:46 (GMT)
committerThomas Heller <theller@ctypes.org>2008-04-30 17:11:46 (GMT)
commitb041fdaf941e84513c643ac02febfb64a4c77435 (patch)
tree4ad2dfb0b140ac234e54c0e8634f097e588ca399 /Lib
parent6471f9633bef73e6727bde467bbcd59d0fea4bf1 (diff)
downloadcpython-b041fdaf941e84513c643ac02febfb64a4c77435.zip
cpython-b041fdaf941e84513c643ac02febfb64a4c77435.tar.gz
cpython-b041fdaf941e84513c643ac02febfb64a4c77435.tar.bz2
Merged revisions 60056-60071,60073-60127,60129-60261,60263-60284,60286-62589,62591-62594 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k-ctypes-pep3118 ........ r60059 | thomas.heller | 2008-01-18 22:17:05 +0100 (Fri, 18 Jan 2008) | 1 line Implement pep3118 format strings for SimpleCData types. ........ r60108 | thomas.heller | 2008-01-19 22:56:12 +0100 (Sat, 19 Jan 2008) | 3 lines Always use explicit endian specifiers for simple types, and a bug fix too. Add unittest. ........ r60112 | thomas.heller | 2008-01-19 23:25:14 +0100 (Sat, 19 Jan 2008) | 2 lines Fully implement tp_asbuffer for pointer types. ........ r60261 | thomas.heller | 2008-01-24 22:01:29 +0100 (Thu, 24 Jan 2008) | 4 lines Added shape and ndim field to StgDictObject. Implemented pep3118 format string, ndim, and shape for array types. Added a buffer_info(type_or_object) for testing. ........ r60278 | thomas.heller | 2008-01-25 11:53:33 +0100 (Fri, 25 Jan 2008) | 2 lines Implement pep3118 format strings for ctypes.Structure and ctypes.Union. ........ r60288 | thomas.heller | 2008-01-25 17:58:30 +0100 (Fri, 25 Jan 2008) | 2 lines All ctypes types now use the same CData_GetBuffer function. ........ r60289 | thomas.heller | 2008-01-25 19:59:45 +0100 (Fri, 25 Jan 2008) | 2 lines Fix format string for structures, and itemsize for arrays. ........ r60290 | thomas.heller | 2008-01-25 20:09:03 +0100 (Fri, 25 Jan 2008) | 2 lines Implement to format string for function pointers. ........ r60292 | thomas.heller | 2008-01-25 20:32:20 +0100 (Fri, 25 Jan 2008) | 3 lines Only structures with native packing implement the pep. Unions, or packed structures do not. ........ r60293 | thomas.heller | 2008-01-25 20:34:31 +0100 (Fri, 25 Jan 2008) | 2 lines Update the test. ........ r60295 | thomas.heller | 2008-01-25 20:44:41 +0100 (Fri, 25 Jan 2008) | 2 lines Fixed a few XXX markers. ........ r60298 | thomas.heller | 2008-01-25 21:11:08 +0100 (Fri, 25 Jan 2008) | 1 line Fix test for 64-bt platform. ........ r60299 | thomas.heller | 2008-01-25 21:34:11 +0100 (Fri, 25 Jan 2008) | 2 lines Add test for the readonly bit. ........ r60384 | thomas.heller | 2008-01-28 08:45:04 +0100 (Mon, 28 Jan 2008) | 4 lines Restructure the test so that it contains little endian format strings. On big endian machines, the format strings are converted by replacing '<' with '>'. ........ r60385 | thomas.heller | 2008-01-28 08:58:46 +0100 (Mon, 28 Jan 2008) | 1 line Bugfix and test for explicit big and little endian types. ........ r60428 | thomas.heller | 2008-01-29 22:00:37 +0100 (Tue, 29 Jan 2008) | 1 line Add comments to clarify the tests. ........ r62589 | thomas.heller | 2008-04-30 13:49:46 +0200 (Wed, 30 Apr 2008) | 1 line Fix compiler warnings. ........
Diffstat (limited to 'Lib')
-rw-r--r--Lib/ctypes/test/test_pep3118.py170
1 files changed, 170 insertions, 0 deletions
diff --git a/Lib/ctypes/test/test_pep3118.py b/Lib/ctypes/test/test_pep3118.py
new file mode 100644
index 0000000..42a124e
--- /dev/null
+++ b/Lib/ctypes/test/test_pep3118.py
@@ -0,0 +1,170 @@
+import unittest
+from ctypes import *
+import re, struct, sys
+
+if sys.byteorder == "little":
+ THIS_ENDIAN = "<"
+ OTHER_ENDIAN = ">"
+else:
+ THIS_ENDIAN = ">"
+ OTHER_ENDIAN = "<"
+
+def normalize(format):
+ # Remove current endian specifier and white space from a format
+ # string
+ format = format.replace(OTHER_ENDIAN, THIS_ENDIAN)
+ return re.sub(r"\s", "", format)
+
+class Test(unittest.TestCase):
+
+ def test_native_types(self):
+ for tp, fmt, shape, itemtp in native_types:
+ ob = tp()
+ v = memoryview(ob)
+ try:
+ self.failUnlessEqual(normalize(v.format), normalize(fmt))
+ self.failUnlessEqual(v.size, sizeof(ob))
+ self.failUnlessEqual(v.itemsize, sizeof(itemtp))
+ self.failUnlessEqual(v.shape, shape)
+ # ctypes object always have a non-strided memory block
+ self.failUnlessEqual(v.strides, None)
+ # they are always read/write
+ self.failIf(v.readonly)
+
+ if v.shape:
+ n = 1
+ for dim in v.shape:
+ n = n * dim
+ self.failUnlessEqual(v.itemsize * n, v.size)
+ except:
+ # so that we can see the failing type
+ print(tp)
+ raise
+
+ def test_endian_types(self):
+ for tp, fmt, shape, itemtp in endian_types:
+ ob = tp()
+ v = memoryview(ob)
+ try:
+ self.failUnlessEqual(v.format, fmt)
+ self.failUnlessEqual(v.size, sizeof(ob))
+ self.failUnlessEqual(v.itemsize, sizeof(itemtp))
+ self.failUnlessEqual(v.shape, shape)
+ # ctypes object always have a non-strided memory block
+ self.failUnlessEqual(v.strides, None)
+ # they are always read/write
+ self.failIf(v.readonly)
+
+ if v.shape:
+ n = 1
+ for dim in v.shape:
+ n = n * dim
+ self.failUnlessEqual(v.itemsize * n, v.size)
+ except:
+ # so that we can see the failing type
+ print(tp)
+ raise
+
+# define some structure classes
+
+class Point(Structure):
+ _fields_ = [("x", c_long), ("y", c_long)]
+
+class PackedPoint(Structure):
+ _pack_ = 2
+ _fields_ = [("x", c_long), ("y", c_long)]
+
+class Point2(Structure):
+ pass
+Point2._fields_ = [("x", c_long), ("y", c_long)]
+
+class EmptyStruct(Structure):
+ _fields_ = []
+
+class aUnion(Union):
+ _fields_ = [("a", c_int)]
+
+################################################################
+#
+# This table contains format strings as they look on little endian
+# machines. The test replaces '<' with '>' on big endian machines.
+#
+native_types = [
+ # type format shape calc itemsize
+
+ ## simple types
+
+ (c_char, "<c", None, c_char),
+ (c_byte, "<b", None, c_byte),
+ (c_ubyte, "<B", None, c_ubyte),
+ (c_short, "<h", None, c_short),
+ (c_ushort, "<H", None, c_ushort),
+
+ # c_int and c_uint may be aliases to c_long
+ #(c_int, "<i", None, c_int),
+ #(c_uint, "<I", None, c_uint),
+
+ (c_long, "<l", None, c_long),
+ (c_ulong, "<L", None, c_ulong),
+
+ # c_longlong and c_ulonglong are aliases on 64-bit platforms
+ #(c_longlong, "<q", None, c_longlong),
+ #(c_ulonglong, "<Q", None, c_ulonglong),
+
+ (c_float, "<f", None, c_float),
+ (c_double, "<d", None, c_double),
+ # c_longdouble may be an alias to c_double
+
+ (c_bool, "<?", None, c_bool),
+ (py_object, "<O", None, py_object),
+
+ ## pointers
+
+ (POINTER(c_byte), "&<b", None, POINTER(c_byte)),
+ (POINTER(POINTER(c_long)), "&&<l", None, POINTER(POINTER(c_long))),
+
+ ## arrays and pointers
+
+ (c_double * 4, "(4)<d", (4,), c_double),
+ (c_float * 4 * 3 * 2, "(2,3,4)<f", (2,3,4), c_float),
+ (POINTER(c_short) * 2, "(2)&<h", (2,), POINTER(c_short)),
+ (POINTER(c_short) * 2 * 3, "(3,2)&<h", (3,2,), POINTER(c_short)),
+ (POINTER(c_short * 2), "&(2)<h", None, POINTER(c_short)),
+
+ ## structures and unions
+
+ (Point, "T{<l:x:<l:y:}", None, Point),
+ # packed structures do not implement the pep
+ (PackedPoint, "B", None, PackedPoint),
+ (Point2, "T{<l:x:<l:y:}", None, Point2),
+ (EmptyStruct, "T{}", None, EmptyStruct),
+ # the pep does't support unions
+ (aUnion, "B", None, aUnion),
+
+ ## other
+
+ # function signatures are not implemented
+ (CFUNCTYPE(None), "X{}", None, CFUNCTYPE(None)),
+
+ ]
+
+class BEPoint(BigEndianStructure):
+ _fields_ = [("x", c_long), ("y", c_long)]
+
+class LEPoint(LittleEndianStructure):
+ _fields_ = [("x", c_long), ("y", c_long)]
+
+################################################################
+#
+# This table contains format strings as they really look, on both big
+# and little endian machines.
+#
+endian_types = [
+ (BEPoint, "T{>l:x:>l:y:}", None, BEPoint),
+ (LEPoint, "T{<l:x:<l:y:}", None, LEPoint),
+ (POINTER(BEPoint), "&T{>l:x:>l:y:}", None, POINTER(BEPoint)),
+ (POINTER(LEPoint), "&T{<l:x:<l:y:}", None, POINTER(LEPoint)),
+ ]
+
+if __name__ == "__main__":
+ unittest.main()