[pypy-svn] r50693 - pypy/dist/pypy/lib/app_test/ctypes
fijal at codespeak.net
fijal at codespeak.net
Wed Jan 16 23:54:58 CET 2008
Author: fijal
Date: Wed Jan 16 23:54:58 2008
New Revision: 50693
Added:
pypy/dist/pypy/lib/app_test/ctypes/test_structures.py (contents, props changed)
Log:
Add this test, we pass few of those.
Added: pypy/dist/pypy/lib/app_test/ctypes/test_structures.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/lib/app_test/ctypes/test_structures.py Wed Jan 16 23:54:58 2008
@@ -0,0 +1,429 @@
+from ctypes import *
+from struct import calcsize
+
+import py
+
+class TestSubclasses:
+ def test_subclass(self):
+ class X(Structure):
+ _fields_ = [("a", c_int)]
+
+ class Y(X):
+ _fields_ = [("b", c_int)]
+
+ class Z(X):
+ pass
+
+ assert sizeof(X) == sizeof(c_int)
+ assert sizeof(Y) == sizeof(c_int)*2
+ assert sizeof(Z) == sizeof(c_int)
+ assert X._fields_ == [("a", c_int)]
+ assert Y._fields_ == [("b", c_int)]
+ assert Z._fields_ == [("a", c_int)]
+
+ def test_subclass_delayed(self):
+ class X(Structure):
+ pass
+ assert sizeof(X) == 0
+ X._fields_ = [("a", c_int)]
+
+ class Y(X):
+ pass
+ assert sizeof(Y) == sizeof(X)
+ Y._fields_ = [("b", c_int)]
+
+ class Z(X):
+ pass
+
+ assert sizeof(X) == sizeof(c_int)
+ assert sizeof(Y) == sizeof(c_int)*2
+ assert sizeof(Z) == sizeof(c_int)
+ assert X._fields_ == [("a", c_int)]
+ assert Y._fields_ == [("b", c_int)]
+ assert Z._fields_ == [("a", c_int)]
+
+class TestStructure:
+ formats = {"c": c_char,
+ "b": c_byte,
+ "B": c_ubyte,
+ "h": c_short,
+ "H": c_ushort,
+ "i": c_int,
+ "I": c_uint,
+ "l": c_long,
+ "L": c_ulong,
+ "q": c_longlong,
+ "Q": c_ulonglong,
+ "f": c_float,
+ "d": c_double,
+ }
+
+ def test_simple_structs(self):
+ for code, tp in self.formats.items():
+ class X(Structure):
+ _fields_ = [("x", c_char),
+ ("y", tp)]
+ assert (sizeof(X), code) == (
+ (calcsize("c%c0%c" % (code, code)), code))
+
+ def test_unions(self):
+ py.test.skip("No unions support")
+ for code, tp in self.formats.items():
+ class X(Union):
+ _fields_ = [("x", c_char),
+ ("y", tp)]
+ assert (sizeof(X), code) == (
+ (calcsize("%c" % (code)), code))
+
+ def test_struct_alignment(self):
+ py.test.skip("???")
+ class X(Structure):
+ _fields_ = [("x", c_char * 3)]
+ assert alignment(X) == calcsize("s")
+ assert sizeof(X) == calcsize("3s")
+
+ class Y(Structure):
+ _fields_ = [("x", c_char * 3),
+ ("y", c_int)]
+ assert alignment(Y) == calcsize("i")
+ assert sizeof(Y) == calcsize("3si")
+
+ class SI(Structure):
+ _fields_ = [("a", X),
+ ("b", Y)]
+ assert alignment(SI) == max(alignment(Y), alignment(X))
+ assert sizeof(SI) == calcsize("3s0i 3si 0i")
+
+ class IS(Structure):
+ _fields_ = [("b", Y),
+ ("a", X)]
+
+ assert alignment(SI) == max(alignment(X), alignment(Y))
+ assert sizeof(IS) == calcsize("3si 3s 0i")
+
+ class XX(Structure):
+ _fields_ = [("a", X),
+ ("b", X)]
+ assert alignment(XX) == alignment(X)
+ assert sizeof(XX) == calcsize("3s 3s 0s")
+
+ def test_emtpy(self):
+ py.test.skip("No unions support")
+ # I had problems with these
+ #
+ # Although these are patological cases: Empty Structures!
+ class X(Structure):
+ _fields_ = []
+
+ class Y(Union):
+ _fields_ = []
+
+ # Is this really the correct alignment, or should it be 0?
+ assert alignment(X) == alignment(Y) == 1
+ assert sizeof(X) == sizeof(Y) == 0
+
+ class XX(Structure):
+ _fields_ = [("a", X),
+ ("b", X)]
+
+ assert alignment(XX) == 1
+ assert sizeof(XX) == 0
+
+ def test_fields(self):
+ py.test.skip("???")
+ # test the offset and size attributes of Structure/Unoin fields.
+ class X(Structure):
+ _fields_ = [("x", c_int),
+ ("y", c_char)]
+
+ assert X.x.offset == 0
+ assert X.x.size == sizeof(c_int)
+
+ assert X.y.offset == sizeof(c_int)
+ assert X.y.size == sizeof(c_char)
+
+ # readonly
+ raises((TypeError, AttributeError), setattr, X.x, "offset", 92)
+ raises((TypeError, AttributeError), setattr, X.x, "size", 92)
+
+ class X(Union):
+ _fields_ = [("x", c_int),
+ ("y", c_char)]
+
+ assert X.x.offset == 0
+ assert X.x.size == sizeof(c_int)
+
+ assert X.y.offset == 0
+ assert X.y.size == sizeof(c_char)
+
+ # readonly
+ raises((TypeError, AttributeError), setattr, X.x, "offset", 92)
+ raises((TypeError, AttributeError), setattr, X.x, "size", 92)
+
+ # XXX Should we check nested data types also?
+ # offset is always relative to the class...
+
+ def test_packed(self):
+ py.test.skip("???")
+ class X(Structure):
+ _fields_ = [("a", c_byte),
+ ("b", c_longlong)]
+ _pack_ = 1
+
+ assert sizeof(X) == 9
+ assert X.b.offset == 1
+
+ class X(Structure):
+ _fields_ = [("a", c_byte),
+ ("b", c_longlong)]
+ _pack_ = 2
+ assert sizeof(X) == 10
+ assert X.b.offset == 2
+
+ class X(Structure):
+ _fields_ = [("a", c_byte),
+ ("b", c_longlong)]
+ _pack_ = 4
+ assert sizeof(X) == 12
+ assert X.b.offset == 4
+
+ import struct
+ longlong_size = struct.calcsize("q")
+ longlong_align = struct.calcsize("bq") - longlong_size
+
+ class X(Structure):
+ _fields_ = [("a", c_byte),
+ ("b", c_longlong)]
+ _pack_ = 8
+
+ assert sizeof(X) == longlong_align + longlong_size
+ assert X.b.offset == min(8, longlong_align)
+
+
+ d = {"_fields_": [("a", "b"),
+ ("b", "q")],
+ "_pack_": -1}
+ raises(ValueError, type(Structure), "X", (Structure,), d)
+
+ def test_initializers(self):
+ py.test.skip("???")
+ class Person(Structure):
+ _fields_ = [("name", c_char*6),
+ ("age", c_int)]
+
+ raises(TypeError, Person, 42)
+ raises(ValueError, Person, "asldkjaslkdjaslkdj")
+ raises(TypeError, Person, "Name", "HI")
+
+ # short enough
+ assert Person("12345", 5).name == "12345"
+ # exact fit
+ assert Person("123456", 5).name == "123456"
+ # too long
+ raises(ValueError, Person, "1234567", 5)
+
+
+ def test_keyword_initializers(self):
+ class POINT(Structure):
+ _fields_ = [("x", c_int), ("y", c_int)]
+ pt = POINT(1, 2)
+ assert (pt.x, pt.y) == (1, 2)
+
+ pt = POINT(y=2, x=1)
+ assert (pt.x, pt.y) == (1, 2)
+
+ def test_invalid_field_types(self):
+ py.test.skip("???")
+ class POINT(Structure):
+ pass
+ raises(TypeError, setattr, POINT, "_fields_", [("x", 1), ("y", 2)])
+
+ def test_intarray_fields(self):
+ py.test.skip("???")
+ class SomeInts(Structure):
+ _fields_ = [("a", c_int * 4)]
+
+ # can use tuple to initialize array (but not list!)
+ assert SomeInts((1, 2)).a[:] == [1, 2, 0, 0]
+ assert SomeInts((1, 2, 3, 4)).a[:] == [1, 2, 3, 4]
+ # too long
+ # XXX Should raise ValueError?, not RuntimeError
+ raises(RuntimeError, SomeInts, (1, 2, 3, 4, 5))
+
+ def test_nested_initializers(self):
+ py.test.skip("???")
+ # test initializing nested structures
+ class Phone(Structure):
+ _fields_ = [("areacode", c_char*6),
+ ("number", c_char*12)]
+
+ class Person(Structure):
+ _fields_ = [("name", c_char * 12),
+ ("phone", Phone),
+ ("age", c_int)]
+
+ p = Person("Someone", ("1234", "5678"), 5)
+
+ assert p.name == "Someone"
+ assert p.phone.areacode == "1234"
+ assert p.phone.number == "5678"
+ assert p.age == 5
+
+ def test_structures_with_wchar(self):
+ try:
+ c_wchar
+ except NameError:
+ return # no unicode
+
+ class PersonW(Structure):
+ _fields_ = [("name", c_wchar * 12),
+ ("age", c_int)]
+
+ p = PersonW(u"Someone")
+ assert p.name == "Someone"
+
+ assert PersonW(u"1234567890").name == u"1234567890"
+ assert PersonW(u"12345678901").name == u"12345678901"
+ # exact fit
+ assert PersonW(u"123456789012").name == u"123456789012"
+ #too long
+ raises(ValueError, PersonW, u"1234567890123")
+
+ def test_init_errors(self):
+ py.test.skip("???")
+ class Phone(Structure):
+ _fields_ = [("areacode", c_char*6),
+ ("number", c_char*12)]
+
+ class Person(Structure):
+ _fields_ = [("name", c_char * 12),
+ ("phone", Phone),
+ ("age", c_int)]
+
+ cls, msg = self.get_except(Person, "Someone", (1, 2))
+ assert cls == RuntimeError
+ # In Python 2.5, Exception is a new-style class, and the repr changed
+ if issubclass(Exception, object):
+ assert msg == (
+ "(Phone) <type 'exceptions.TypeError'>: "
+ "expected string or Unicode object, int found")
+ else:
+ assert msg == (
+ "(Phone) exceptions.TypeError: "
+ "expected string or Unicode object, int found")
+
+ cls, msg = self.get_except(Person, "Someone", ("a", "b", "c"))
+ assert cls == RuntimeError
+ if issubclass(Exception, object):
+ assert msg == (
+ "(Phone) <type 'exceptions.ValueError'>: too many initializers")
+ else:
+ assert msg == "(Phone) exceptions.ValueError: too many initializers"
+
+
+ def get_except(self, func, *args):
+ try:
+ func(*args)
+ except Exception, detail:
+ return detail.__class__, str(detail)
+
+
+## def test_subclass_creation(self):
+## meta = type(Structure)
+## # same as 'class X(Structure): pass'
+## # fails, since we need either a _fields_ or a _abstract_ attribute
+## cls, msg = self.get_except(meta, "X", (Structure,), {})
+## self.failUnlessEqual((cls, msg),
+## (AttributeError, "class must define a '_fields_' attribute"))
+
+ def test_abstract_class(self):
+ py.test.skip("???")
+ class X(Structure):
+ _abstract_ = "something"
+ # try 'X()'
+ cls, msg = self.get_except(eval, "X()", locals())
+ assert (cls, msg) == (TypeError, "abstract class")
+
+ def test_methods(self):
+ py.test.skip("???")
+## class X(Structure):
+## _fields_ = []
+
+ assert "in_dll" in dir(type(Structure))
+ assert "from_address" in dir(type(Structure))
+ assert "in_dll" in dir(type(Structure))
+
+class TestPointerMember:
+
+ def test_1(self):
+ # a Structure with a POINTER field
+ class S(Structure):
+ _fields_ = [("array", POINTER(c_int))]
+
+ s = S()
+ # We can assign arrays of the correct type
+ s.array = (c_int * 3)(1, 2, 3)
+ items = [s.array[i] for i in range(3)]
+ assert items == [1, 2, 3]
+
+ # The following are bugs, but are included here because the unittests
+ # also describe the current behaviour.
+ #
+ # This fails with SystemError: bad arg to internal function
+ # or with IndexError (with a patch I have)
+
+ s.array[0] = 42
+
+ items = [s.array[i] for i in range(3)]
+ assert items == [42, 2, 3]
+
+ s.array[0] = 1
+
+## s.array[1] = 42
+
+ items = [s.array[i] for i in range(3)]
+ assert items == [1, 2, 3]
+
+ def test_none_to_pointer_fields(self):
+ py.test.skip("???")
+ class S(Structure):
+ _fields_ = [("x", c_int),
+ ("p", POINTER(c_int))]
+
+ s = S()
+ s.x = 12345678
+ s.p = None
+ assert s.x == 12345678
+
+class TestRecursiveStructure:
+ def test_contains_itself(self):
+ py.test.skip("???")
+ class Recursive(Structure):
+ pass
+
+ try:
+ Recursive._fields_ = [("next", Recursive)]
+ except AttributeError, details:
+ assert ("Structure or union cannot contain itself" in
+ str(details))
+ else:
+ raise AssertionError, "Structure or union cannot contain itself"
+
+
+ def test_vice_versa(self):
+ py.test.skip("???")
+ class First(Structure):
+ pass
+ class Second(Structure):
+ pass
+
+ First._fields_ = [("second", Second)]
+
+ try:
+ Second._fields_ = [("first", First)]
+ except AttributeError, details:
+ assert ("_fields_ is final" in
+ str(details))
+ else:
+ raise AssertionError, "AttributeError not raised"
+
More information about the Pypy-commit
mailing list