Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Attributes
test.test_struct.StructTest Class Reference

List of all members.

Public Member Functions

def test_isbigendian
def test_consistence
def test_transitiveness
def test_new_features
def test_calcsize
def test_integers
def test_p_code
def test_705836
def test_1530559
def test_unpack_from
def test_pack_into
def test_pack_into_fn
def test_unpack_with_buffer
def test_bool
def test_count_overflow
def test_crasher
def test_trailing_counter
def test_Struct_reinitialization

Public Attributes

 format
 code
 byteorder
 bytesize
 bitsize
 signed
 min_value
 max_value

Private Attributes

 _value

Detailed Description

Definition at line 32 of file test_struct.py.


Member Function Documentation

Definition at line 379 of file test_struct.py.

00379 
00380     def test_1530559(self):
00381         for byteorder in '', '@', '=', '<', '>', '!':
00382             for code in integer_codes:
00383                 if (byteorder in ('', '@') and code in ('q', 'Q') and
00384                     not HAVE_LONG_LONG):
00385                     continue
00386                 format = byteorder + code
00387                 self.assertRaises(struct.error, struct.pack, format, 1.0)
00388                 self.assertRaises(struct.error, struct.pack, format, 1.5)
00389         self.assertRaises(struct.error, struct.pack, 'P', 1.0)
00390         self.assertRaises(struct.error, struct.pack, 'P', 1.5)

Here is the call graph for this function:

Definition at line 344 of file test_struct.py.

00344 
00345     def test_705836(self):
00346         # SF bug 705836.  "<f" and ">f" had a severe rounding bug, where a carry
00347         # from the low-order discarded bits could propagate into the exponent
00348         # field, causing the result to be wrong by a factor of 2.
00349         import math
00350 
00351         for base in range(1, 33):
00352             # smaller <- largest representable float less than base.
00353             delta = 0.5
00354             while base - delta / 2.0 != base:
00355                 delta /= 2.0
00356             smaller = base - delta
00357             # Packing this rounds away a solid string of trailing 1 bits.
00358             packed = struct.pack("<f", smaller)
00359             unpacked = struct.unpack("<f", packed)[0]
00360             # This failed at base = 2, 4, and 32, with unpacked = 1, 2, and
00361             # 16, respectively.
00362             self.assertEqual(base, unpacked)
00363             bigpacked = struct.pack(">f", smaller)
00364             self.assertEqual(bigpacked, string_reverse(packed))
00365             unpacked = struct.unpack(">f", bigpacked)[0]
00366             self.assertEqual(base, unpacked)
00367 
00368         # Largest finite IEEE single.
00369         big = (1 << 24) - 1
00370         big = math.ldexp(big, 127 - 23)
00371         packed = struct.pack(">f", big)
00372         unpacked = struct.unpack(">f", packed)[0]
00373         self.assertEqual(big, unpacked)
00374 
00375         # The same, but tack on a 1 bit so it rounds up to infinity.
00376         big = (1 << 25) - 1
00377         big = math.ldexp(big, 127 - 24)
00378         self.assertRaises(OverflowError, struct.pack, ">f", big)

Here is the call graph for this function:

Definition at line 473 of file test_struct.py.

00473 
00474     def test_bool(self):
00475         class ExplodingBool(object):
00476             def __bool__(self):
00477                 raise IOError
00478         for prefix in tuple("<>!=")+('',):
00479             false = (), [], [], '', 0
00480             true = [1], 'test', 5, -1, 0xffffffff+1, 0xffffffff/2
00481 
00482             falseFormat = prefix + '?' * len(false)
00483             packedFalse = struct.pack(falseFormat, *false)
00484             unpackedFalse = struct.unpack(falseFormat, packedFalse)
00485 
00486             trueFormat = prefix + '?' * len(true)
00487             packedTrue = struct.pack(trueFormat, *true)
00488             unpackedTrue = struct.unpack(trueFormat, packedTrue)
00489 
00490             self.assertEqual(len(true), len(unpackedTrue))
00491             self.assertEqual(len(false), len(unpackedFalse))
00492 
00493             for t in unpackedFalse:
00494                 self.assertFalse(t)
00495             for t in unpackedTrue:
00496                 self.assertTrue(t)
00497 
00498             packed = struct.pack(prefix+'?', 1)
00499 
00500             self.assertEqual(len(packed), struct.calcsize(prefix+'?'))
00501 
00502             if len(packed) != 1:
00503                 self.assertFalse(prefix, msg='encoded bool is not one byte: %r'
00504                                              %packed)
00505 
00506             try:
00507                 struct.pack(prefix + '?', ExplodingBool())
00508             except IOError:
00509                 pass
00510             else:
00511                 self.fail("Expected IOError: struct.pack(%r, "
00512                           "ExplodingBool())" % (prefix + '?'))
00513 
00514         for c in [b'\x01', b'\x7f', b'\xff', b'\x0f', b'\xf0']:
00515             self.assertTrue(struct.unpack('>?', c)[0])

Here is the call graph for this function:

Definition at line 134 of file test_struct.py.

00134 
00135     def test_calcsize(self):
00136         expected_size = {
00137             'b': 1, 'B': 1,
00138             'h': 2, 'H': 2,
00139             'i': 4, 'I': 4,
00140             'l': 4, 'L': 4,
00141             'q': 8, 'Q': 8,
00142             }
00143 
00144         # standard integer sizes
00145         for code in integer_codes:
00146             for byteorder in '=', '<', '>', '!':
00147                 format = byteorder+code
00148                 size = struct.calcsize(format)
00149                 self.assertEqual(size, expected_size[code])
00150 
00151         # native integer sizes
00152         native_pairs = 'bB', 'hH', 'iI', 'lL'
00153         if HAVE_LONG_LONG:
00154             native_pairs += 'qQ',
00155         for format_pair in native_pairs:
00156             for byteorder in '', '@':
00157                 signed_size = struct.calcsize(byteorder + format_pair[0])
00158                 unsigned_size = struct.calcsize(byteorder + format_pair[1])
00159                 self.assertEqual(signed_size, unsigned_size)
00160 
00161         # bounds for native integer sizes
00162         self.assertEqual(struct.calcsize('b'), 1)
00163         self.assertLessEqual(2, struct.calcsize('h'))
00164         self.assertLessEqual(4, struct.calcsize('l'))
00165         self.assertLessEqual(struct.calcsize('h'), struct.calcsize('i'))
00166         self.assertLessEqual(struct.calcsize('i'), struct.calcsize('l'))
00167         if HAVE_LONG_LONG:
00168             self.assertLessEqual(8, struct.calcsize('q'))
00169             self.assertLessEqual(struct.calcsize('l'), struct.calcsize('q'))

Here is the call graph for this function:

Definition at line 36 of file test_struct.py.

00036 
00037     def test_consistence(self):
00038         self.assertRaises(struct.error, struct.calcsize, 'Z')
00039 
00040         sz = struct.calcsize('i')
00041         self.assertEqual(sz * 3, struct.calcsize('iii'))
00042 
00043         fmt = 'cbxxxxxxhhhhiillffd?'
00044         fmt3 = '3c3b18x12h6i6l6f3d3?'
00045         sz = struct.calcsize(fmt)
00046         sz3 = struct.calcsize(fmt3)
00047         self.assertEqual(sz * 3, sz3)
00048 
00049         self.assertRaises(struct.error, struct.pack, 'iii', 3)
00050         self.assertRaises(struct.error, struct.pack, 'i', 3, 3, 3)
00051         self.assertRaises((TypeError, struct.error), struct.pack, 'i', 'foo')
00052         self.assertRaises((TypeError, struct.error), struct.pack, 'P', 'foo')
00053         self.assertRaises(struct.error, struct.unpack, 'd', b'flap')
00054         s = struct.pack('ii', 1, 2)
00055         self.assertRaises(struct.error, struct.unpack, 'iii', s)
00056         self.assertRaises(struct.error, struct.unpack, 'i', s)

Here is the call graph for this function:

Definition at line 516 of file test_struct.py.

00516 
00517     def test_count_overflow(self):
00518         hugecount = '{}b'.format(sys.maxsize+1)
00519         self.assertRaises(struct.error, struct.calcsize, hugecount)
00520 
00521         hugecount2 = '{}b{}H'.format(sys.maxsize//2, sys.maxsize//2)
00522         self.assertRaises(struct.error, struct.calcsize, hugecount2)

Here is the call graph for this function:

Definition at line 524 of file test_struct.py.

00524 
00525         def test_crasher(self):
00526             self.assertRaises(MemoryError, struct.pack, "357913941b", "a")

Here is the call graph for this function:

Definition at line 170 of file test_struct.py.

00170 
00171     def test_integers(self):
00172         # Integer tests (bBhHiIlLqQ).
00173         import binascii
00174 
00175         class IntTester(unittest.TestCase):
00176             def __init__(self, format):
00177                 super(IntTester, self).__init__(methodName='test_one')
00178                 self.format = format
00179                 self.code = format[-1]
00180                 self.byteorder = format[:-1]
00181                 if not self.byteorder in byteorders:
00182                     raise ValueError("unrecognized packing byteorder: %s" %
00183                                      self.byteorder)
00184                 self.bytesize = struct.calcsize(format)
00185                 self.bitsize = self.bytesize * 8
00186                 if self.code in tuple('bhilq'):
00187                     self.signed = True
00188                     self.min_value = -(2**(self.bitsize-1))
00189                     self.max_value = 2**(self.bitsize-1) - 1
00190                 elif self.code in tuple('BHILQ'):
00191                     self.signed = False
00192                     self.min_value = 0
00193                     self.max_value = 2**self.bitsize - 1
00194                 else:
00195                     raise ValueError("unrecognized format code: %s" %
00196                                      self.code)
00197 
00198             def test_one(self, x, pack=struct.pack,
00199                                   unpack=struct.unpack,
00200                                   unhexlify=binascii.unhexlify):
00201 
00202                 format = self.format
00203                 if self.min_value <= x <= self.max_value:
00204                     expected = x
00205                     if self.signed and x < 0:
00206                         expected += 1 << self.bitsize
00207                     self.assertGreaterEqual(expected, 0)
00208                     expected = '%x' % expected
00209                     if len(expected) & 1:
00210                         expected = "0" + expected
00211                     expected = expected.encode('ascii')
00212                     expected = unhexlify(expected)
00213                     expected = (b"\x00" * (self.bytesize - len(expected)) +
00214                                 expected)
00215                     if (self.byteorder == '<' or
00216                         self.byteorder in ('', '@', '=') and not ISBIGENDIAN):
00217                         expected = string_reverse(expected)
00218                     self.assertEqual(len(expected), self.bytesize)
00219 
00220                     # Pack work?
00221                     got = pack(format, x)
00222                     self.assertEqual(got, expected)
00223 
00224                     # Unpack work?
00225                     retrieved = unpack(format, got)[0]
00226                     self.assertEqual(x, retrieved)
00227 
00228                     # Adding any byte should cause a "too big" error.
00229                     self.assertRaises((struct.error, TypeError), unpack, format,
00230                                                                  b'\x01' + got)
00231                 else:
00232                     # x is out of range -- verify pack realizes that.
00233                     self.assertRaises((OverflowError, ValueError, struct.error),
00234                                       pack, format, x)
00235 
00236             def run(self):
00237                 from random import randrange
00238 
00239                 # Create all interesting powers of 2.
00240                 values = []
00241                 for exp in range(self.bitsize + 3):
00242                     values.append(1 << exp)
00243 
00244                 # Add some random values.
00245                 for i in range(self.bitsize):
00246                     val = 0
00247                     for j in range(self.bytesize):
00248                         val = (val << 8) | randrange(256)
00249                     values.append(val)
00250 
00251                 # Values absorbed from other tests
00252                 values.extend([300, 700000, sys.maxsize*4])
00253 
00254                 # Try all those, and their negations, and +-1 from
00255                 # them.  Note that this tests all power-of-2
00256                 # boundaries in range, and a few out of range, plus
00257                 # +-(2**n +- 1).
00258                 for base in values:
00259                     for val in -base, base:
00260                         for incr in -1, 0, 1:
00261                             x = val + incr
00262                             self.test_one(x)
00263 
00264                 # Some error cases.
00265                 class NotAnInt:
00266                     def __int__(self):
00267                         return 42
00268 
00269                 # Objects with an '__index__' method should be allowed
00270                 # to pack as integers.  That is assuming the implemented
00271                 # '__index__' method returns and 'int' or 'long'.
00272                 class Indexable(object):
00273                     def __init__(self, value):
00274                         self._value = value
00275 
00276                     def __index__(self):
00277                         return self._value
00278 
00279                 # If the '__index__' method raises a type error, then
00280                 # '__int__' should be used with a deprecation warning.
00281                 class BadIndex(object):
00282                     def __index__(self):
00283                         raise TypeError
00284 
00285                     def __int__(self):
00286                         return 42
00287 
00288                 self.assertRaises((TypeError, struct.error),
00289                                   struct.pack, self.format,
00290                                   "a string")
00291                 self.assertRaises((TypeError, struct.error),
00292                                   struct.pack, self.format,
00293                                   randrange)
00294                 self.assertRaises((TypeError, struct.error),
00295                                   struct.pack, self.format,
00296                                   3+42j)
00297                 self.assertRaises((TypeError, struct.error),
00298                                   struct.pack, self.format,
00299                                   NotAnInt())
00300                 self.assertRaises((TypeError, struct.error),
00301                                   struct.pack, self.format,
00302                                   BadIndex())
00303 
00304                 # Check for legitimate values from '__index__'.
00305                 for obj in (Indexable(0), Indexable(10), Indexable(17),
00306                             Indexable(42), Indexable(100), Indexable(127)):
00307                     try:
00308                         struct.pack(format, obj)
00309                     except:
00310                         self.fail("integer code pack failed on object "
00311                                   "with '__index__' method")
00312 
00313                 # Check for bogus values from '__index__'.
00314                 for obj in (Indexable(b'a'), Indexable('b'), Indexable(None),
00315                             Indexable({'a': 1}), Indexable([1, 2, 3])):
00316                     self.assertRaises((TypeError, struct.error),
00317                                       struct.pack, self.format,
00318                                       obj)
00319 
00320         for code in integer_codes:
00321             for byteorder in byteorders:
00322                 if (byteorder in ('', '@') and code in ('q', 'Q') and
00323                     not HAVE_LONG_LONG):
00324                     continue
00325                 format = byteorder+code
00326                 t = IntTester(format)
00327                 t.run()

Here is the call graph for this function:

Definition at line 33 of file test_struct.py.

00033 
00034     def test_isbigendian(self):
00035         self.assertEqual((struct.pack('=i', 1)[0] == 0), ISBIGENDIAN)

Here is the call graph for this function:

Definition at line 81 of file test_struct.py.

00081 
00082     def test_new_features(self):
00083         # Test some of the new features in detail
00084         # (format, argument, big-endian result, little-endian result, asymmetric)
00085         tests = [
00086             ('c', b'a', b'a', b'a', 0),
00087             ('xc', b'a', b'\0a', b'\0a', 0),
00088             ('cx', b'a', b'a\0', b'a\0', 0),
00089             ('s', b'a', b'a', b'a', 0),
00090             ('0s', b'helloworld', b'', b'', 1),
00091             ('1s', b'helloworld', b'h', b'h', 1),
00092             ('9s', b'helloworld', b'helloworl', b'helloworl', 1),
00093             ('10s', b'helloworld', b'helloworld', b'helloworld', 0),
00094             ('11s', b'helloworld', b'helloworld\0', b'helloworld\0', 1),
00095             ('20s', b'helloworld', b'helloworld'+10*b'\0', b'helloworld'+10*b'\0', 1),
00096             ('b', 7, b'\7', b'\7', 0),
00097             ('b', -7, b'\371', b'\371', 0),
00098             ('B', 7, b'\7', b'\7', 0),
00099             ('B', 249, b'\371', b'\371', 0),
00100             ('h', 700, b'\002\274', b'\274\002', 0),
00101             ('h', -700, b'\375D', b'D\375', 0),
00102             ('H', 700, b'\002\274', b'\274\002', 0),
00103             ('H', 0x10000-700, b'\375D', b'D\375', 0),
00104             ('i', 70000000, b'\004,\035\200', b'\200\035,\004', 0),
00105             ('i', -70000000, b'\373\323\342\200', b'\200\342\323\373', 0),
00106             ('I', 70000000, b'\004,\035\200', b'\200\035,\004', 0),
00107             ('I', 0x100000000-70000000, b'\373\323\342\200', b'\200\342\323\373', 0),
00108             ('l', 70000000, b'\004,\035\200', b'\200\035,\004', 0),
00109             ('l', -70000000, b'\373\323\342\200', b'\200\342\323\373', 0),
00110             ('L', 70000000, b'\004,\035\200', b'\200\035,\004', 0),
00111             ('L', 0x100000000-70000000, b'\373\323\342\200', b'\200\342\323\373', 0),
00112             ('f', 2.0, b'@\000\000\000', b'\000\000\000@', 0),
00113             ('d', 2.0, b'@\000\000\000\000\000\000\000',
00114                        b'\000\000\000\000\000\000\000@', 0),
00115             ('f', -2.0, b'\300\000\000\000', b'\000\000\000\300', 0),
00116             ('d', -2.0, b'\300\000\000\000\000\000\000\000',
00117                         b'\000\000\000\000\000\000\000\300', 0),
00118             ('?', 0, b'\0', b'\0', 0),
00119             ('?', 3, b'\1', b'\1', 1),
00120             ('?', True, b'\1', b'\1', 0),
00121             ('?', [], b'\0', b'\0', 1),
00122             ('?', (1,), b'\1', b'\1', 1),
00123         ]
00124 
00125         for fmt, arg, big, lil, asy in tests:
00126             for (xfmt, exp) in [('>'+fmt, big), ('!'+fmt, big), ('<'+fmt, lil),
00127                                 ('='+fmt, ISBIGENDIAN and big or lil)]:
00128                 res = struct.pack(xfmt, arg)
00129                 self.assertEqual(res, exp)
00130                 self.assertEqual(struct.calcsize(xfmt), len(res))
00131                 rev = struct.unpack(xfmt, res)[0]
00132                 if rev != arg:
00133                     self.assertTrue(asy)

Here is the call graph for this function:

Definition at line 328 of file test_struct.py.

00328 
00329     def test_p_code(self):
00330         # Test p ("Pascal string") code.
00331         for code, input, expected, expectedback in [
00332                 ('p',  b'abc', b'\x00',            b''),
00333                 ('1p', b'abc', b'\x00',            b''),
00334                 ('2p', b'abc', b'\x01a',           b'a'),
00335                 ('3p', b'abc', b'\x02ab',          b'ab'),
00336                 ('4p', b'abc', b'\x03abc',         b'abc'),
00337                 ('5p', b'abc', b'\x03abc\x00',     b'abc'),
00338                 ('6p', b'abc', b'\x03abc\x00\x00', b'abc'),
00339                 ('1000p', b'x'*1000, b'\xff' + b'x'*999, b'x'*255)]:
00340             got = struct.pack(code, input)
00341             self.assertEqual(got, expected)
00342             (got,) = struct.unpack(code, got)
00343             self.assertEqual(got, expectedback)

Here is the call graph for this function:

Definition at line 414 of file test_struct.py.

00414 
00415     def test_pack_into(self):
00416         test_string = b'Reykjavik rocks, eow!'
00417         writable_buf = array.array('b', b' '*100)
00418         fmt = '21s'
00419         s = struct.Struct(fmt)
00420 
00421         # Test without offset
00422         s.pack_into(writable_buf, 0, test_string)
00423         from_buf = writable_buf.tobytes()[:len(test_string)]
00424         self.assertEqual(from_buf, test_string)
00425 
00426         # Test with offset.
00427         s.pack_into(writable_buf, 10, test_string)
00428         from_buf = writable_buf.tobytes()[:len(test_string)+10]
00429         self.assertEqual(from_buf, test_string[:10] + test_string)
00430 
00431         # Go beyond boundaries.
00432         small_buf = array.array('b', b' '*10)
00433         self.assertRaises((ValueError, struct.error), s.pack_into, small_buf, 0,
00434                           test_string)
00435         self.assertRaises((ValueError, struct.error), s.pack_into, small_buf, 2,
00436                           test_string)
00437 
00438         # Test bogus offset (issue 3694)
00439         sb = small_buf
00440         self.assertRaises((TypeError, struct.error), struct.pack_into, b'', sb,
00441                           None)

Here is the call graph for this function:

Definition at line 442 of file test_struct.py.

00442 
00443     def test_pack_into_fn(self):
00444         test_string = b'Reykjavik rocks, eow!'
00445         writable_buf = array.array('b', b' '*100)
00446         fmt = '21s'
00447         pack_into = lambda *args: struct.pack_into(fmt, *args)
00448 
00449         # Test without offset.
00450         pack_into(writable_buf, 0, test_string)
00451         from_buf = writable_buf.tobytes()[:len(test_string)]
00452         self.assertEqual(from_buf, test_string)
00453 
00454         # Test with offset.
00455         pack_into(writable_buf, 10, test_string)
00456         from_buf = writable_buf.tobytes()[:len(test_string)+10]
00457         self.assertEqual(from_buf, test_string[:10] + test_string)
00458 
00459         # Go beyond boundaries.
00460         small_buf = array.array('b', b' '*10)
00461         self.assertRaises((ValueError, struct.error), pack_into, small_buf, 0,
00462                           test_string)
00463         self.assertRaises((ValueError, struct.error), pack_into, small_buf, 2,
00464                           test_string)

Here is the call graph for this function:

Definition at line 552 of file test_struct.py.

00552 
00553     def test_Struct_reinitialization(self):
00554         # Issue 9422: there was a memory leak when reinitializing a
00555         # Struct instance.  This test can be used to detect the leak
00556         # when running with regrtest -L.
00557         s = struct.Struct('i')
00558         s.__init__('ii')

Definition at line 527 of file test_struct.py.

00527 
00528     def test_trailing_counter(self):
00529         store = array.array('b', b' '*100)
00530 
00531         # format lists containing only count spec should result in an error
00532         self.assertRaises(struct.error, struct.pack, '12345')
00533         self.assertRaises(struct.error, struct.unpack, '12345', '')
00534         self.assertRaises(struct.error, struct.pack_into, '12345', store, 0)
00535         self.assertRaises(struct.error, struct.unpack_from, '12345', store, 0)
00536 
00537         # Format lists with trailing count spec should result in an error
00538         self.assertRaises(struct.error, struct.pack, 'c12345', 'x')
00539         self.assertRaises(struct.error, struct.unpack, 'c12345', 'x')
00540         self.assertRaises(struct.error, struct.pack_into, 'c12345', store, 0,
00541                            'x')
00542         self.assertRaises(struct.error, struct.unpack_from, 'c12345', store,
00543                            0)
00544 
00545         # Mixed format tests
00546         self.assertRaises(struct.error, struct.pack, '14s42', 'spam and eggs')
00547         self.assertRaises(struct.error, struct.unpack, '14s42',
00548                           'spam and eggs')
00549         self.assertRaises(struct.error, struct.pack_into, '14s42', store, 0,
00550                           'spam and eggs')
00551         self.assertRaises(struct.error, struct.unpack_from, '14s42', store, 0)

Here is the call graph for this function:

Definition at line 57 of file test_struct.py.

00057 
00058     def test_transitiveness(self):
00059         c = b'a'
00060         b = 1
00061         h = 255
00062         i = 65535
00063         l = 65536
00064         f = 3.1415
00065         d = 3.1415
00066         t = True
00067 
00068         for prefix in ('', '@', '<', '>', '=', '!'):
00069             for format in ('xcbhilfd?', 'xcBHILfd?'):
00070                 format = prefix + format
00071                 s = struct.pack(format, c, b, h, i, l, f, d, t)
00072                 cp, bp, hp, ip, lp, fp, dp, tp = struct.unpack(format, s)
00073                 self.assertEqual(cp, c)
00074                 self.assertEqual(bp, b)
00075                 self.assertEqual(hp, h)
00076                 self.assertEqual(ip, i)
00077                 self.assertEqual(lp, l)
00078                 self.assertEqual(int(100 * fp), int(100 * f))
00079                 self.assertEqual(int(100 * dp), int(100 * d))
00080                 self.assertEqual(tp, t)

Here is the call graph for this function:

Definition at line 391 of file test_struct.py.

00391 
00392     def test_unpack_from(self):
00393         test_string = b'abcd01234'
00394         fmt = '4s'
00395         s = struct.Struct(fmt)
00396         for cls in (bytes, bytearray):
00397             data = cls(test_string)
00398             self.assertEqual(s.unpack_from(data), (b'abcd',))
00399             self.assertEqual(s.unpack_from(data, 2), (b'cd01',))
00400             self.assertEqual(s.unpack_from(data, 4), (b'0123',))
00401             for i in range(6):
00402                 self.assertEqual(s.unpack_from(data, i), (data[i:i+4],))
00403             for i in range(6, len(test_string) + 1):
00404                 self.assertRaises(struct.error, s.unpack_from, data, i)
00405         for cls in (bytes, bytearray):
00406             data = cls(test_string)
00407             self.assertEqual(struct.unpack_from(fmt, data), (b'abcd',))
00408             self.assertEqual(struct.unpack_from(fmt, data, 2), (b'cd01',))
00409             self.assertEqual(struct.unpack_from(fmt, data, 4), (b'0123',))
00410             for i in range(6):
00411                 self.assertEqual(struct.unpack_from(fmt, data, i), (data[i:i+4],))
00412             for i in range(6, len(test_string) + 1):
00413                 self.assertRaises(struct.error, struct.unpack_from, fmt, data, i)

Here is the call graph for this function:

Definition at line 465 of file test_struct.py.

00465 
00466     def test_unpack_with_buffer(self):
00467         # SF bug 1563759: struct.unpack doesn't support buffer protocol objects
00468         data1 = array.array('B', b'\x12\x34\x56\x78')
00469         data2 = memoryview(b'\x12\x34\x56\x78') # XXX b'......XXXX......', 6, 4
00470         for data in [data1, data2]:
00471             value, = struct.unpack('>I', data)
00472             self.assertEqual(value, 0x12345678)

Here is the call graph for this function:


Member Data Documentation

Definition at line 273 of file test_struct.py.

Definition at line 184 of file test_struct.py.

Definition at line 179 of file test_struct.py.

Definition at line 183 of file test_struct.py.

Definition at line 178 of file test_struct.py.

Definition at line 177 of file test_struct.py.

Definition at line 188 of file test_struct.py.

Definition at line 187 of file test_struct.py.

Definition at line 186 of file test_struct.py.


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