Back to index

python3.2  3.2.2
Public Member Functions | Static Public Attributes
ctypes.test.test_structures.StructureTestCase Class Reference

List of all members.

Public Member Functions

def test_simple_structs
def test_unions
def test_struct_alignment
def test_emtpy
def test_fields
def test_packed
def test_initializers
def test_conflicting_initializers
def test_keyword_initializers
def test_invalid_field_types
def test_intarray_fields
def test_nested_initializers
def test_structures_with_wchar
def test_init_errors
def get_except

Static Public Attributes

dictionary formats

Detailed Description

Definition at line 44 of file test_structures.py.


Member Function Documentation

def ctypes.test.test_structures.StructureTestCase.get_except (   self,
  func,
  args 
)

Definition at line 322 of file test_structures.py.

00322 
00323     def get_except(self, func, *args):
00324         try:
00325             func(*args)
00326         except Exception as detail:
00327             return detail.__class__, str(detail)
00328 

Here is the caller graph for this function:

Definition at line 218 of file test_structures.py.

00218 
00219     def test_conflicting_initializers(self):
00220         class POINT(Structure):
00221             _fields_ = [("x", c_int), ("y", c_int)]
00222         # conflicting positional and keyword args
00223         self.assertRaises(TypeError, POINT, 2, 3, x=4)
00224         self.assertRaises(TypeError, POINT, 2, 3, y=4)
00225 
00226         # too many initializers
00227         self.assertRaises(TypeError, POINT, 2, 3, 4)

Here is the call graph for this function:

Definition at line 107 of file test_structures.py.

00107 
00108     def test_emtpy(self):
00109         # I had problems with these
00110         #
00111         # Although these are patological cases: Empty Structures!
00112         class X(Structure):
00113             _fields_ = []
00114 
00115         class Y(Union):
00116             _fields_ = []
00117 
00118         # Is this really the correct alignment, or should it be 0?
00119         self.assertTrue(alignment(X) == alignment(Y) == 1)
00120         self.assertTrue(sizeof(X) == sizeof(Y) == 0)
00121 
00122         class XX(Structure):
00123             _fields_ = [("a", X),
00124                         ("b", X)]
00125 
00126         self.assertEqual(alignment(XX), 1)
00127         self.assertEqual(sizeof(XX), 0)

Here is the call graph for this function:

Definition at line 128 of file test_structures.py.

00128 
00129     def test_fields(self):
00130         # test the offset and size attributes of Structure/Unoin fields.
00131         class X(Structure):
00132             _fields_ = [("x", c_int),
00133                         ("y", c_char)]
00134 
00135         self.assertEqual(X.x.offset, 0)
00136         self.assertEqual(X.x.size, sizeof(c_int))
00137 
00138         self.assertEqual(X.y.offset, sizeof(c_int))
00139         self.assertEqual(X.y.size, sizeof(c_char))
00140 
00141         # readonly
00142         self.assertRaises((TypeError, AttributeError), setattr, X.x, "offset", 92)
00143         self.assertRaises((TypeError, AttributeError), setattr, X.x, "size", 92)
00144 
00145         class X(Union):
00146             _fields_ = [("x", c_int),
00147                         ("y", c_char)]
00148 
00149         self.assertEqual(X.x.offset, 0)
00150         self.assertEqual(X.x.size, sizeof(c_int))
00151 
00152         self.assertEqual(X.y.offset, 0)
00153         self.assertEqual(X.y.size, sizeof(c_char))
00154 
00155         # readonly
00156         self.assertRaises((TypeError, AttributeError), setattr, X.x, "offset", 92)
00157         self.assertRaises((TypeError, AttributeError), setattr, X.x, "size", 92)
00158 
00159         # XXX Should we check nested data types also?
00160         # offset is always relative to the class...

Here is the call graph for this function:

Definition at line 297 of file test_structures.py.

00297 
00298     def test_init_errors(self):
00299         class Phone(Structure):
00300             _fields_ = [("areacode", c_char*6),
00301                         ("number", c_char*12)]
00302 
00303         class Person(Structure):
00304             _fields_ = [("name", c_char * 12),
00305                         ("phone", Phone),
00306                         ("age", c_int)]
00307 
00308         cls, msg = self.get_except(Person, b"Someone", (1, 2))
00309         self.assertEqual(cls, RuntimeError)
00310         self.assertEqual(msg,
00311                              "(Phone) <class 'TypeError'>: "
00312                              "expected string, int found")
00313 
00314         cls, msg = self.get_except(Person, b"Someone", (b"a", b"b", b"c"))
00315         self.assertEqual(cls, RuntimeError)
00316         if issubclass(Exception, object):
00317             self.assertEqual(msg,
00318                                  "(Phone) <class 'TypeError'>: too many initializers")
00319         else:
00320             self.assertEqual(msg, "(Phone) TypeError: too many initializers")
00321 

Here is the call graph for this function:

Definition at line 202 of file test_structures.py.

00202 
00203     def test_initializers(self):
00204         class Person(Structure):
00205             _fields_ = [("name", c_char*6),
00206                         ("age", c_int)]
00207 
00208         self.assertRaises(TypeError, Person, 42)
00209         self.assertRaises(ValueError, Person, b"asldkjaslkdjaslkdj")
00210         self.assertRaises(TypeError, Person, "Name", "HI")
00211 
00212         # short enough
00213         self.assertEqual(Person(b"12345", 5).name, b"12345")
00214         # exact fit
00215         self.assertEqual(Person(b"123456", 5).name, b"123456")
00216         # too long
00217         self.assertRaises(ValueError, Person, b"1234567", 5)

Here is the call graph for this function:

Definition at line 242 of file test_structures.py.

00242 
00243     def test_intarray_fields(self):
00244         class SomeInts(Structure):
00245             _fields_ = [("a", c_int * 4)]
00246 
00247         # can use tuple to initialize array (but not list!)
00248         self.assertEqual(SomeInts((1, 2)).a[:], [1, 2, 0, 0])
00249         self.assertEqual(SomeInts((1, 2)).a[::], [1, 2, 0, 0])
00250         self.assertEqual(SomeInts((1, 2)).a[::-1], [0, 0, 2, 1])
00251         self.assertEqual(SomeInts((1, 2)).a[::2], [1, 0])
00252         self.assertEqual(SomeInts((1, 2)).a[1:5:6], [2])
00253         self.assertEqual(SomeInts((1, 2)).a[6:4:-1], [])
00254         self.assertEqual(SomeInts((1, 2, 3, 4)).a[:], [1, 2, 3, 4])
00255         self.assertEqual(SomeInts((1, 2, 3, 4)).a[::], [1, 2, 3, 4])
00256         # too long
00257         # XXX Should raise ValueError?, not RuntimeError
00258         self.assertRaises(RuntimeError, SomeInts, (1, 2, 3, 4, 5))

Here is the call graph for this function:

Definition at line 237 of file test_structures.py.

00237 
00238     def test_invalid_field_types(self):
00239         class POINT(Structure):
00240             pass
00241         self.assertRaises(TypeError, setattr, POINT, "_fields_", [("x", 1), ("y", 2)])

Definition at line 228 of file test_structures.py.

00228 
00229     def test_keyword_initializers(self):
00230         class POINT(Structure):
00231             _fields_ = [("x", c_int), ("y", c_int)]
00232         pt = POINT(1, 2)
00233         self.assertEqual((pt.x, pt.y), (1, 2))
00234 
00235         pt = POINT(y=2, x=1)
00236         self.assertEqual((pt.x, pt.y), (1, 2))

Here is the call graph for this function:

Definition at line 259 of file test_structures.py.

00259 
00260     def test_nested_initializers(self):
00261         # test initializing nested structures
00262         class Phone(Structure):
00263             _fields_ = [("areacode", c_char*6),
00264                         ("number", c_char*12)]
00265 
00266         class Person(Structure):
00267             _fields_ = [("name", c_char * 12),
00268                         ("phone", Phone),
00269                         ("age", c_int)]
00270 
00271         p = Person(b"Someone", (b"1234", b"5678"), 5)
00272 
00273         self.assertEqual(p.name, b"Someone")
00274         self.assertEqual(p.phone.areacode, b"1234")
00275         self.assertEqual(p.phone.number, b"5678")
00276         self.assertEqual(p.age, 5)

Here is the call graph for this function:

Definition at line 161 of file test_structures.py.

00161 
00162     def test_packed(self):
00163         class X(Structure):
00164             _fields_ = [("a", c_byte),
00165                         ("b", c_longlong)]
00166             _pack_ = 1
00167 
00168         self.assertEqual(sizeof(X), 9)
00169         self.assertEqual(X.b.offset, 1)
00170 
00171         class X(Structure):
00172             _fields_ = [("a", c_byte),
00173                         ("b", c_longlong)]
00174             _pack_ = 2
00175         self.assertEqual(sizeof(X), 10)
00176         self.assertEqual(X.b.offset, 2)
00177 
00178         class X(Structure):
00179             _fields_ = [("a", c_byte),
00180                         ("b", c_longlong)]
00181             _pack_ = 4
00182         self.assertEqual(sizeof(X), 12)
00183         self.assertEqual(X.b.offset, 4)
00184 
00185         import struct
00186         longlong_size = struct.calcsize("q")
00187         longlong_align = struct.calcsize("bq") - longlong_size
00188 
00189         class X(Structure):
00190             _fields_ = [("a", c_byte),
00191                         ("b", c_longlong)]
00192             _pack_ = 8
00193 
00194         self.assertEqual(sizeof(X), longlong_align + longlong_size)
00195         self.assertEqual(X.b.offset, min(8, longlong_align))
00196 
00197 
00198         d = {"_fields_": [("a", "b"),
00199                           ("b", "q")],
00200              "_pack_": -1}
00201         self.assertRaises(ValueError, type(Structure), "X", (Structure,), d)

Here is the call graph for this function:

Definition at line 60 of file test_structures.py.

00060 
00061     def test_simple_structs(self):
00062         for code, tp in self.formats.items():
00063             class X(Structure):
00064                 _fields_ = [("x", c_char),
00065                             ("y", tp)]
00066             self.assertEqual((sizeof(X), code),
00067                                  (calcsize("c%c0%c" % (code, code)), code))

Here is the call graph for this function:

Definition at line 76 of file test_structures.py.

00076 
00077     def test_struct_alignment(self):
00078         class X(Structure):
00079             _fields_ = [("x", c_char * 3)]
00080         self.assertEqual(alignment(X), calcsize("s"))
00081         self.assertEqual(sizeof(X), calcsize("3s"))
00082 
00083         class Y(Structure):
00084             _fields_ = [("x", c_char * 3),
00085                         ("y", c_int)]
00086         self.assertEqual(alignment(Y), calcsize("i"))
00087         self.assertEqual(sizeof(Y), calcsize("3si"))
00088 
00089         class SI(Structure):
00090             _fields_ = [("a", X),
00091                         ("b", Y)]
00092         self.assertEqual(alignment(SI), max(alignment(Y), alignment(X)))
00093         self.assertEqual(sizeof(SI), calcsize("3s0i 3si 0i"))
00094 
00095         class IS(Structure):
00096             _fields_ = [("b", Y),
00097                         ("a", X)]
00098 
00099         self.assertEqual(alignment(SI), max(alignment(X), alignment(Y)))
00100         self.assertEqual(sizeof(IS), calcsize("3si 3s 0i"))
00101 
00102         class XX(Structure):
00103             _fields_ = [("a", X),
00104                         ("b", X)]
00105         self.assertEqual(alignment(XX), alignment(X))
00106         self.assertEqual(sizeof(XX), calcsize("3s 3s 0s"))

Here is the call graph for this function:

Definition at line 277 of file test_structures.py.

00277 
00278     def test_structures_with_wchar(self):
00279         try:
00280             c_wchar
00281         except NameError:
00282             return # no unicode
00283 
00284         class PersonW(Structure):
00285             _fields_ = [("name", c_wchar * 12),
00286                         ("age", c_int)]
00287 
00288         p = PersonW("Someone \xe9")
00289         self.assertEqual(p.name, "Someone \xe9")
00290 
00291         self.assertEqual(PersonW("1234567890").name, "1234567890")
00292         self.assertEqual(PersonW("12345678901").name, "12345678901")
00293         # exact fit
00294         self.assertEqual(PersonW("123456789012").name, "123456789012")
00295         #too long
00296         self.assertRaises(ValueError, PersonW, "1234567890123")

Here is the call graph for this function:

Definition at line 68 of file test_structures.py.

00068 
00069     def test_unions(self):
00070         for code, tp in self.formats.items():
00071             class X(Union):
00072                 _fields_ = [("x", c_char),
00073                             ("y", tp)]
00074             self.assertEqual((sizeof(X), code),
00075                                  (calcsize("%c" % (code)), code))

Here is the call graph for this function:


Member Data Documentation

Initial value:
{"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,
               }

Definition at line 45 of file test_structures.py.


The documentation for this class was generated from the following file: