Back to index

python3.2  3.2.2
Public Member Functions
ctypes.test.test_bitfields.BitFieldTest Class Reference

List of all members.

Public Member Functions

def test_longlong
def test_ulonglong
def test_signed
def test_unsigned
def fail_fields
def test_nonint_types
def test_single_bitfield_size
def test_multi_bitfields_size
def get_except
def test_mixed_1
def test_mixed_2
def test_mixed_3
def test_mixed_4
def test_anon_bitfields

Detailed Description

Definition at line 53 of file test_bitfields.py.


Member Function Documentation

Definition at line 111 of file test_bitfields.py.

00111 
00112     def fail_fields(self, *fields):
00113         return self.get_except(type(Structure), "X", (),
00114                                {"_fields_": fields})

Here is the call graph for this function:

Here is the caller graph for this function:

def ctypes.test.test_bitfields.BitFieldTest.get_except (   self,
  func,
  args,
  kw 
)

Definition at line 191 of file test_bitfields.py.

00191 
00192     def get_except(self, func, *args, **kw):
00193         try:
00194             func(*args, **kw)
00195         except Exception as detail:
00196             return detail.__class__, str(detail)

Here is the caller graph for this function:

Definition at line 234 of file test_bitfields.py.

00234 
00235     def test_anon_bitfields(self):
00236         # anonymous bit-fields gave a strange error message
00237         class X(Structure):
00238             _fields_ = [("a", c_byte, 4),
00239                         ("b", c_ubyte, 4)]
00240         class Y(Structure):
00241             _anonymous_ = ["_"]
00242             _fields_ = [("_", X)]

Definition at line 55 of file test_bitfields.py.

00055 
00056     def test_longlong(self):
00057         class X(Structure):
00058             _fields_ = [("a", c_longlong, 1),
00059                         ("b", c_longlong, 62),
00060                         ("c", c_longlong, 1)]
00061 
00062         self.assertEqual(sizeof(X), sizeof(c_longlong))
00063         x = X()
00064         x.a, x.b, x.c = -1, 7, -1
00065         self.assertEqual((x.a, x.b, x.c), (-1, 7, -1))

Here is the call graph for this function:

Definition at line 197 of file test_bitfields.py.

00197 
00198     def test_mixed_1(self):
00199         class X(Structure):
00200             _fields_ = [("a", c_byte, 4),
00201                         ("b", c_int, 4)]
00202         if os.name in ("nt", "ce"):
00203             self.assertEqual(sizeof(X), sizeof(c_int)*2)
00204         else:
00205             self.assertEqual(sizeof(X), sizeof(c_int))

Here is the call graph for this function:

Definition at line 206 of file test_bitfields.py.

00206 
00207     def test_mixed_2(self):
00208         class X(Structure):
00209             _fields_ = [("a", c_byte, 4),
00210                         ("b", c_int, 32)]
00211         self.assertEqual(sizeof(X), sizeof(c_int)*2)

Here is the call graph for this function:

Definition at line 212 of file test_bitfields.py.

00212 
00213     def test_mixed_3(self):
00214         class X(Structure):
00215             _fields_ = [("a", c_byte, 4),
00216                         ("b", c_ubyte, 4)]
00217         self.assertEqual(sizeof(X), sizeof(c_byte))

Here is the call graph for this function:

Definition at line 218 of file test_bitfields.py.

00218 
00219     def test_mixed_4(self):
00220         class X(Structure):
00221             _fields_ = [("a", c_short, 4),
00222                         ("b", c_short, 4),
00223                         ("c", c_int, 24),
00224                         ("d", c_short, 4),
00225                         ("e", c_short, 4),
00226                         ("f", c_int, 24)]
00227         # MSVC does NOT combine c_short and c_int into one field, GCC
00228         # does (unless GCC is run with '-mms-bitfields' which
00229         # produces code compatible with MSVC).
00230         if os.name in ("nt", "ce"):
00231             self.assertEqual(sizeof(X), sizeof(c_int) * 4)
00232         else:
00233             self.assertEqual(sizeof(X), sizeof(c_int) * 2)

Here is the call graph for this function:

Definition at line 163 of file test_bitfields.py.

00163 
00164     def test_multi_bitfields_size(self):
00165         class X(Structure):
00166             _fields_ = [("a", c_short, 1),
00167                         ("b", c_short, 14),
00168                         ("c", c_short, 1)]
00169         self.assertEqual(sizeof(X), sizeof(c_short))
00170 
00171         class X(Structure):
00172             _fields_ = [("a", c_short, 1),
00173                         ("a1", c_short),
00174                         ("b", c_short, 14),
00175                         ("c", c_short, 1)]
00176         self.assertEqual(sizeof(X), sizeof(c_short)*3)
00177         self.assertEqual(X.a.offset, 0)
00178         self.assertEqual(X.a1.offset, sizeof(c_short))
00179         self.assertEqual(X.b.offset, sizeof(c_short)*2)
00180         self.assertEqual(X.c.offset, sizeof(c_short)*2)
00181 
00182         class X(Structure):
00183             _fields_ = [("a", c_short, 3),
00184                         ("b", c_short, 14),
00185                         ("c", c_short, 14)]
00186         self.assertEqual(sizeof(X), sizeof(c_short)*3)
00187         self.assertEqual(X.a.offset, sizeof(c_short)*0)
00188         self.assertEqual(X.b.offset, sizeof(c_short)*1)
00189         self.assertEqual(X.c.offset, sizeof(c_short)*2)
00190 

Here is the call graph for this function:

Definition at line 115 of file test_bitfields.py.

00115 
00116     def test_nonint_types(self):
00117         # bit fields are not allowed on non-integer types.
00118         result = self.fail_fields(("a", c_char_p, 1))
00119         self.assertEqual(result, (TypeError, 'bit fields not allowed for type c_char_p'))
00120 
00121         result = self.fail_fields(("a", c_void_p, 1))
00122         self.assertEqual(result, (TypeError, 'bit fields not allowed for type c_void_p'))
00123 
00124         if c_int != c_long:
00125             result = self.fail_fields(("a", POINTER(c_int), 1))
00126             self.assertEqual(result, (TypeError, 'bit fields not allowed for type LP_c_int'))
00127 
00128         result = self.fail_fields(("a", c_char, 1))
00129         self.assertEqual(result, (TypeError, 'bit fields not allowed for type c_char'))
00130 
00131         try:
00132             c_wchar
00133         except NameError:
00134             pass
00135         else:
00136             result = self.fail_fields(("a", c_wchar, 1))
00137             self.assertEqual(result, (TypeError, 'bit fields not allowed for type c_wchar'))
00138 
00139         class Dummy(Structure):
00140             _fields_ = []
00141 
00142         result = self.fail_fields(("a", Dummy, 1))
00143         self.assertEqual(result, (TypeError, 'bit fields not allowed for type Dummy'))

Here is the call graph for this function:

Definition at line 78 of file test_bitfields.py.

00078 
00079     def test_signed(self):
00080         for c_typ in signed_int_types:
00081             class X(Structure):
00082                 _fields_ = [("dummy", c_typ),
00083                             ("a", c_typ, 3),
00084                             ("b", c_typ, 3),
00085                             ("c", c_typ, 1)]
00086             self.assertEqual(sizeof(X), sizeof(c_typ)*2)
00087 
00088             x = X()
00089             self.assertEqual((c_typ, x.a, x.b, x.c), (c_typ, 0, 0, 0))
00090             x.a = -1
00091             self.assertEqual((c_typ, x.a, x.b, x.c), (c_typ, -1, 0, 0))
00092             x.a, x.b = 0, -1
00093             self.assertEqual((c_typ, x.a, x.b, x.c), (c_typ, 0, -1, 0))
00094 

Here is the call graph for this function:

Definition at line 144 of file test_bitfields.py.

00144 
00145     def test_single_bitfield_size(self):
00146         for c_typ in int_types:
00147             result = self.fail_fields(("a", c_typ, -1))
00148             self.assertEqual(result, (ValueError, 'number of bits invalid for bit field'))
00149 
00150             result = self.fail_fields(("a", c_typ, 0))
00151             self.assertEqual(result, (ValueError, 'number of bits invalid for bit field'))
00152 
00153             class X(Structure):
00154                 _fields_ = [("a", c_typ, 1)]
00155             self.assertEqual(sizeof(X), sizeof(c_typ))
00156 
00157             class X(Structure):
00158                 _fields_ = [("a", c_typ, sizeof(c_typ)*8)]
00159             self.assertEqual(sizeof(X), sizeof(c_typ))
00160 
00161             result = self.fail_fields(("a", c_typ, sizeof(c_typ)*8 + 1))
00162             self.assertEqual(result, (ValueError, 'number of bits invalid for bit field'))

Here is the call graph for this function:

Definition at line 66 of file test_bitfields.py.

00066 
00067     def test_ulonglong(self):
00068         class X(Structure):
00069             _fields_ = [("a", c_ulonglong, 1),
00070                         ("b", c_ulonglong, 62),
00071                         ("c", c_ulonglong, 1)]
00072 
00073         self.assertEqual(sizeof(X), sizeof(c_longlong))
00074         x = X()
00075         self.assertEqual((x.a, x.b, x.c), (0, 0, 0))
00076         x.a, x.b, x.c = 7, 7, 7
00077         self.assertEqual((x.a, x.b, x.c), (1, 7, 1))

Here is the call graph for this function:

Definition at line 95 of file test_bitfields.py.

00095 
00096     def test_unsigned(self):
00097         for c_typ in unsigned_int_types:
00098             class X(Structure):
00099                 _fields_ = [("a", c_typ, 3),
00100                             ("b", c_typ, 3),
00101                             ("c", c_typ, 1)]
00102             self.assertEqual(sizeof(X), sizeof(c_typ))
00103 
00104             x = X()
00105             self.assertEqual((c_typ, x.a, x.b, x.c), (c_typ, 0, 0, 0))
00106             x.a = -1
00107             self.assertEqual((c_typ, x.a, x.b, x.c), (c_typ, 7, 0, 0))
00108             x.a, x.b = 0, -1
00109             self.assertEqual((c_typ, x.a, x.b, x.c), (c_typ, 0, 7, 0))
00110 

Here is the call graph for this function:


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