Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1 | import array |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 2 | import unittest |
| 3 | import struct |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 4 | import sys |
Mark Dickinson | 79d38ac | 2010-03-05 14:44:08 +0000 | [diff] [blame] | 5 | |
| 6 | from test.support import run_unittest |
| 7 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 8 | ISBIGENDIAN = sys.byteorder == "big" |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 9 | IS32BIT = sys.maxsize == 0x7fffffff |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 10 | |
Mark Dickinson | b9f751a | 2010-04-03 15:07:40 +0000 | [diff] [blame] | 11 | integer_codes = 'b', 'B', 'h', 'H', 'i', 'I', 'l', 'L', 'q', 'Q' |
| 12 | byteorders = '', '@', '=', '<', '>', '!' |
| 13 | |
| 14 | # Native 'q' packing isn't available on systems that don't have the C |
| 15 | # long long type. |
| 16 | try: |
| 17 | struct.pack('q', 5) |
| 18 | except struct.error: |
| 19 | HAVE_LONG_LONG = False |
| 20 | else: |
| 21 | HAVE_LONG_LONG = True |
| 22 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 23 | def string_reverse(s): |
Guido van Rossum | e625fd5 | 2007-05-27 09:19:04 +0000 | [diff] [blame] | 24 | return s[::-1] |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 25 | |
| 26 | def bigendian_to_native(value): |
| 27 | if ISBIGENDIAN: |
| 28 | return value |
| 29 | else: |
| 30 | return string_reverse(value) |
| 31 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 32 | class StructTest(unittest.TestCase): |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 33 | def test_isbigendian(self): |
Amaury Forgeot d'Arc | f9f2982 | 2008-06-17 21:56:01 +0000 | [diff] [blame] | 34 | self.assertEqual((struct.pack('=i', 1)[0] == 0), ISBIGENDIAN) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 35 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 36 | def test_consistence(self): |
| 37 | self.assertRaises(struct.error, struct.calcsize, 'Z') |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 38 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 39 | sz = struct.calcsize('i') |
| 40 | self.assertEqual(sz * 3, struct.calcsize('iii')) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 41 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 42 | fmt = 'cbxxxxxxhhhhiillffd?' |
| 43 | fmt3 = '3c3b18x12h6i6l6f3d3?' |
| 44 | sz = struct.calcsize(fmt) |
| 45 | sz3 = struct.calcsize(fmt3) |
| 46 | self.assertEqual(sz * 3, sz3) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 47 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 48 | self.assertRaises(struct.error, struct.pack, 'iii', 3) |
| 49 | self.assertRaises(struct.error, struct.pack, 'i', 3, 3, 3) |
| 50 | self.assertRaises(struct.error, struct.pack, 'i', 'foo') |
| 51 | self.assertRaises(struct.error, struct.pack, 'P', 'foo') |
| 52 | self.assertRaises(struct.error, struct.unpack, 'd', b'flap') |
| 53 | s = struct.pack('ii', 1, 2) |
| 54 | self.assertRaises(struct.error, struct.unpack, 'iii', s) |
| 55 | self.assertRaises(struct.error, struct.unpack, 'i', s) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 56 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 57 | def test_transitiveness(self): |
| 58 | c = b'a' |
| 59 | b = 1 |
| 60 | h = 255 |
| 61 | i = 65535 |
| 62 | l = 65536 |
| 63 | f = 3.1415 |
| 64 | d = 3.1415 |
| 65 | t = True |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 66 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 67 | for prefix in ('', '@', '<', '>', '=', '!'): |
| 68 | for format in ('xcbhilfd?', 'xcBHILfd?'): |
| 69 | format = prefix + format |
| 70 | s = struct.pack(format, c, b, h, i, l, f, d, t) |
| 71 | cp, bp, hp, ip, lp, fp, dp, tp = struct.unpack(format, s) |
| 72 | self.assertEqual(cp, c) |
| 73 | self.assertEqual(bp, b) |
| 74 | self.assertEqual(hp, h) |
| 75 | self.assertEqual(ip, i) |
| 76 | self.assertEqual(lp, l) |
| 77 | self.assertEqual(int(100 * fp), int(100 * f)) |
| 78 | self.assertEqual(int(100 * dp), int(100 * d)) |
| 79 | self.assertEqual(tp, t) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 80 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 81 | def test_new_features(self): |
| 82 | # Test some of the new features in detail |
| 83 | # (format, argument, big-endian result, little-endian result, asymmetric) |
| 84 | tests = [ |
| 85 | ('c', 'a', 'a', 'a', 0), |
| 86 | ('xc', 'a', '\0a', '\0a', 0), |
| 87 | ('cx', 'a', 'a\0', 'a\0', 0), |
| 88 | ('s', 'a', 'a', 'a', 0), |
| 89 | ('0s', 'helloworld', '', '', 1), |
| 90 | ('1s', 'helloworld', 'h', 'h', 1), |
| 91 | ('9s', 'helloworld', 'helloworl', 'helloworl', 1), |
| 92 | ('10s', 'helloworld', 'helloworld', 'helloworld', 0), |
| 93 | ('11s', 'helloworld', 'helloworld\0', 'helloworld\0', 1), |
| 94 | ('20s', 'helloworld', 'helloworld'+10*'\0', 'helloworld'+10*'\0', 1), |
| 95 | ('b', 7, '\7', '\7', 0), |
| 96 | ('b', -7, '\371', '\371', 0), |
| 97 | ('B', 7, '\7', '\7', 0), |
| 98 | ('B', 249, '\371', '\371', 0), |
| 99 | ('h', 700, '\002\274', '\274\002', 0), |
| 100 | ('h', -700, '\375D', 'D\375', 0), |
| 101 | ('H', 700, '\002\274', '\274\002', 0), |
| 102 | ('H', 0x10000-700, '\375D', 'D\375', 0), |
| 103 | ('i', 70000000, '\004,\035\200', '\200\035,\004', 0), |
| 104 | ('i', -70000000, '\373\323\342\200', '\200\342\323\373', 0), |
| 105 | ('I', 70000000, '\004,\035\200', '\200\035,\004', 0), |
| 106 | ('I', 0x100000000-70000000, '\373\323\342\200', '\200\342\323\373', 0), |
| 107 | ('l', 70000000, '\004,\035\200', '\200\035,\004', 0), |
| 108 | ('l', -70000000, '\373\323\342\200', '\200\342\323\373', 0), |
| 109 | ('L', 70000000, '\004,\035\200', '\200\035,\004', 0), |
| 110 | ('L', 0x100000000-70000000, '\373\323\342\200', '\200\342\323\373', 0), |
| 111 | ('f', 2.0, '@\000\000\000', '\000\000\000@', 0), |
| 112 | ('d', 2.0, '@\000\000\000\000\000\000\000', |
| 113 | '\000\000\000\000\000\000\000@', 0), |
| 114 | ('f', -2.0, '\300\000\000\000', '\000\000\000\300', 0), |
| 115 | ('d', -2.0, '\300\000\000\000\000\000\000\000', |
| 116 | '\000\000\000\000\000\000\000\300', 0), |
| 117 | ('?', 0, '\0', '\0', 0), |
| 118 | ('?', 3, '\1', '\1', 1), |
| 119 | ('?', True, '\1', '\1', 0), |
| 120 | ('?', [], '\0', '\0', 1), |
| 121 | ('?', (1,), '\1', '\1', 1), |
| 122 | ] |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 123 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 124 | for fmt, arg, big, lil, asy in tests: |
| 125 | big = bytes(big, "latin-1") |
| 126 | lil = bytes(lil, "latin-1") |
| 127 | for (xfmt, exp) in [('>'+fmt, big), ('!'+fmt, big), ('<'+fmt, lil), |
| 128 | ('='+fmt, ISBIGENDIAN and big or lil)]: |
| 129 | res = struct.pack(xfmt, arg) |
| 130 | self.assertEqual(res, exp) |
| 131 | self.assertEqual(struct.calcsize(xfmt), len(res)) |
| 132 | rev = struct.unpack(xfmt, res)[0] |
| 133 | if isinstance(arg, str): |
| 134 | # Strings are returned as bytes since you can't know the |
| 135 | # encoding of the string when packed. |
| 136 | arg = bytes(arg, 'latin1') |
| 137 | if rev != arg: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 138 | self.assertTrue(asy) |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 139 | |
Mark Dickinson | b9f751a | 2010-04-03 15:07:40 +0000 | [diff] [blame] | 140 | def test_calcsize(self): |
| 141 | expected_size = { |
| 142 | 'b': 1, 'B': 1, |
| 143 | 'h': 2, 'H': 2, |
| 144 | 'i': 4, 'I': 4, |
| 145 | 'l': 4, 'L': 4, |
| 146 | 'q': 8, 'Q': 8, |
| 147 | } |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 148 | |
Mark Dickinson | b9f751a | 2010-04-03 15:07:40 +0000 | [diff] [blame] | 149 | # standard integer sizes |
| 150 | for code in integer_codes: |
| 151 | for byteorder in '=', '<', '>', '!': |
| 152 | format = byteorder+code |
| 153 | size = struct.calcsize(format) |
| 154 | self.assertEqual(size, expected_size[code]) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 155 | |
Mark Dickinson | b9f751a | 2010-04-03 15:07:40 +0000 | [diff] [blame] | 156 | # native integer sizes |
| 157 | native_pairs = 'bB', 'hH', 'iI', 'lL' |
| 158 | if HAVE_LONG_LONG: |
| 159 | native_pairs += 'qQ', |
| 160 | for format_pair in native_pairs: |
| 161 | for byteorder in '', '@': |
| 162 | signed_size = struct.calcsize(byteorder + format_pair[0]) |
| 163 | unsigned_size = struct.calcsize(byteorder + format_pair[1]) |
| 164 | self.assertEqual(signed_size, unsigned_size) |
| 165 | |
| 166 | # bounds for native integer sizes |
Ezio Melotti | 4c28ddc | 2010-04-04 07:21:15 +0000 | [diff] [blame] | 167 | self.assertEqual(struct.calcsize('b'), 1) |
| 168 | self.assertLessEqual(2, struct.calcsize('h')) |
| 169 | self.assertLessEqual(4, struct.calcsize('l')) |
| 170 | self.assertLessEqual(struct.calcsize('h'), struct.calcsize('i')) |
| 171 | self.assertLessEqual(struct.calcsize('i'), struct.calcsize('l')) |
Mark Dickinson | b9f751a | 2010-04-03 15:07:40 +0000 | [diff] [blame] | 172 | if HAVE_LONG_LONG: |
Ezio Melotti | 4c28ddc | 2010-04-04 07:21:15 +0000 | [diff] [blame] | 173 | self.assertLessEqual(8, struct.calcsize('q')) |
| 174 | self.assertLessEqual(struct.calcsize('l'), struct.calcsize('q')) |
Mark Dickinson | b9f751a | 2010-04-03 15:07:40 +0000 | [diff] [blame] | 175 | |
| 176 | def test_integers(self): |
| 177 | # Integer tests (bBhHiIlLqQ). |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 178 | import binascii |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 179 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 180 | class IntTester(unittest.TestCase): |
Mark Dickinson | b9f751a | 2010-04-03 15:07:40 +0000 | [diff] [blame] | 181 | def __init__(self, format): |
Benjamin Peterson | 7fe73a1 | 2009-04-04 16:35:46 +0000 | [diff] [blame] | 182 | super(IntTester, self).__init__(methodName='test_one') |
Mark Dickinson | b9f751a | 2010-04-03 15:07:40 +0000 | [diff] [blame] | 183 | self.format = format |
| 184 | self.code = format[-1] |
| 185 | self.byteorder = format[:-1] |
| 186 | if not self.byteorder in byteorders: |
| 187 | raise ValueError("unrecognized packing byteorder: %s" % |
| 188 | self.byteorder) |
| 189 | self.bytesize = struct.calcsize(format) |
| 190 | self.bitsize = self.bytesize * 8 |
| 191 | if self.code in tuple('bhilq'): |
| 192 | self.signed = True |
| 193 | self.min_value = -(2**(self.bitsize-1)) |
| 194 | self.max_value = 2**(self.bitsize-1) - 1 |
| 195 | elif self.code in tuple('BHILQ'): |
| 196 | self.signed = False |
| 197 | self.min_value = 0 |
| 198 | self.max_value = 2**self.bitsize - 1 |
| 199 | else: |
| 200 | raise ValueError("unrecognized format code: %s" % |
| 201 | self.code) |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 202 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 203 | def test_one(self, x, pack=struct.pack, |
| 204 | unpack=struct.unpack, |
| 205 | unhexlify=binascii.unhexlify): |
Mark Dickinson | b9f751a | 2010-04-03 15:07:40 +0000 | [diff] [blame] | 206 | |
| 207 | format = self.format |
| 208 | if self.min_value <= x <= self.max_value: |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 209 | expected = x |
Mark Dickinson | b9f751a | 2010-04-03 15:07:40 +0000 | [diff] [blame] | 210 | if self.signed and x < 0: |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 211 | expected += 1 << self.bitsize |
Ezio Melotti | 4c28ddc | 2010-04-04 07:21:15 +0000 | [diff] [blame] | 212 | self.assertGreaterEqual(expected, 0) |
Mark Dickinson | b9f751a | 2010-04-03 15:07:40 +0000 | [diff] [blame] | 213 | expected = '%x' % expected |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 214 | if len(expected) & 1: |
| 215 | expected = "0" + expected |
| 216 | expected = unhexlify(expected) |
Mark Dickinson | b9f751a | 2010-04-03 15:07:40 +0000 | [diff] [blame] | 217 | expected = (b"\x00" * (self.bytesize - len(expected)) + |
| 218 | expected) |
| 219 | if (self.byteorder == '<' or |
| 220 | self.byteorder in ('', '@', '=') and not ISBIGENDIAN): |
| 221 | expected = string_reverse(expected) |
| 222 | self.assertEqual(len(expected), self.bytesize) |
Tim Peters | 0891ac0 | 2001-09-15 02:35:15 +0000 | [diff] [blame] | 223 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 224 | # Pack work? |
| 225 | got = pack(format, x) |
| 226 | self.assertEqual(got, expected) |
Tim Peters | d50ade6 | 2003-03-20 18:32:13 +0000 | [diff] [blame] | 227 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 228 | # Unpack work? |
| 229 | retrieved = unpack(format, got)[0] |
| 230 | self.assertEqual(x, retrieved) |
Tim Peters | d50ade6 | 2003-03-20 18:32:13 +0000 | [diff] [blame] | 231 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 232 | # Adding any byte should cause a "too big" error. |
Mark Dickinson | b9f751a | 2010-04-03 15:07:40 +0000 | [diff] [blame] | 233 | self.assertRaises((struct.error, TypeError), unpack, format, |
| 234 | b'\x01' + got) |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 235 | else: |
| 236 | # x is out of range -- verify pack realizes that. |
Mark Dickinson | b9f751a | 2010-04-03 15:07:40 +0000 | [diff] [blame] | 237 | self.assertRaises(struct.error, pack, format, x) |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 238 | |
| 239 | def run(self): |
| 240 | from random import randrange |
| 241 | |
| 242 | # Create all interesting powers of 2. |
| 243 | values = [] |
| 244 | for exp in range(self.bitsize + 3): |
| 245 | values.append(1 << exp) |
| 246 | |
| 247 | # Add some random values. |
| 248 | for i in range(self.bitsize): |
| 249 | val = 0 |
| 250 | for j in range(self.bytesize): |
| 251 | val = (val << 8) | randrange(256) |
| 252 | values.append(val) |
| 253 | |
Mark Dickinson | b9f751a | 2010-04-03 15:07:40 +0000 | [diff] [blame] | 254 | # Values absorbed from other tests |
| 255 | values.extend([300, 700000, sys.maxsize*4]) |
| 256 | |
| 257 | # Try all those, and their negations, and +-1 from |
| 258 | # them. Note that this tests all power-of-2 |
| 259 | # boundaries in range, and a few out of range, plus |
| 260 | # +-(2**n +- 1). |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 261 | for base in values: |
| 262 | for val in -base, base: |
| 263 | for incr in -1, 0, 1: |
| 264 | x = val + incr |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 265 | self.test_one(x) |
| 266 | |
| 267 | # Some error cases. |
Mark Dickinson | b9f751a | 2010-04-03 15:07:40 +0000 | [diff] [blame] | 268 | class NotAnInt: |
| 269 | def __int__(self): |
| 270 | return 42 |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 271 | |
Mark Dickinson | e9493a1 | 2010-04-04 08:52:51 +0000 | [diff] [blame] | 272 | # Objects with an '__index__' method should be allowed |
| 273 | # to pack as integers. That is assuming the implemented |
| 274 | # '__index__' method returns and 'int' or 'long'. |
| 275 | class Indexable(object): |
| 276 | def __init__(self, value): |
| 277 | self._value = value |
| 278 | |
| 279 | def __index__(self): |
| 280 | return self._value |
| 281 | |
| 282 | # If the '__index__' method raises a type error, then |
| 283 | # '__int__' should be used with a deprecation warning. |
| 284 | class BadIndex(object): |
| 285 | def __index__(self): |
| 286 | raise TypeError |
| 287 | |
| 288 | def __int__(self): |
| 289 | return 42 |
| 290 | |
Mark Dickinson | b9f751a | 2010-04-03 15:07:40 +0000 | [diff] [blame] | 291 | self.assertRaises((TypeError, struct.error), |
| 292 | struct.pack, self.format, |
| 293 | "a string") |
| 294 | self.assertRaises((TypeError, struct.error), |
| 295 | struct.pack, self.format, |
| 296 | randrange) |
| 297 | self.assertRaises((TypeError, struct.error), |
| 298 | struct.pack, self.format, |
| 299 | 3+42j) |
| 300 | self.assertRaises((TypeError, struct.error), |
| 301 | struct.pack, self.format, |
Mark Dickinson | e9493a1 | 2010-04-04 08:52:51 +0000 | [diff] [blame] | 302 | NotAnInt()) |
| 303 | self.assertRaises((TypeError, struct.error), |
| 304 | struct.pack, self.format, |
| 305 | BadIndex()) |
Mark Dickinson | b9f751a | 2010-04-03 15:07:40 +0000 | [diff] [blame] | 306 | |
Mark Dickinson | e9493a1 | 2010-04-04 08:52:51 +0000 | [diff] [blame] | 307 | # Check for legitimate values from '__index__'. |
Mark Dickinson | c593577 | 2010-04-03 15:54:36 +0000 | [diff] [blame] | 308 | for obj in (Indexable(0), Indexable(10), Indexable(17), |
| 309 | Indexable(42), Indexable(100), Indexable(127)): |
| 310 | try: |
| 311 | struct.pack(format, obj) |
| 312 | except: |
| 313 | self.fail("integer code pack failed on object " |
| 314 | "with '__index__' method") |
| 315 | |
Mark Dickinson | e9493a1 | 2010-04-04 08:52:51 +0000 | [diff] [blame] | 316 | # Check for bogus values from '__index__'. |
| 317 | for obj in (Indexable(b'a'), Indexable('b'), Indexable(None), |
| 318 | Indexable({'a': 1}), Indexable([1, 2, 3])): |
| 319 | self.assertRaises((TypeError, struct.error), |
| 320 | struct.pack, self.format, |
| 321 | obj) |
| 322 | |
Mark Dickinson | b9f751a | 2010-04-03 15:07:40 +0000 | [diff] [blame] | 323 | for code in integer_codes: |
| 324 | for byteorder in byteorders: |
| 325 | if (byteorder in ('', '@') and code in ('q', 'Q') and |
| 326 | not HAVE_LONG_LONG): |
| 327 | continue |
| 328 | format = byteorder+code |
| 329 | t = IntTester(format) |
| 330 | t.run() |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 331 | |
| 332 | def test_p_code(self): |
| 333 | # Test p ("Pascal string") code. |
| 334 | for code, input, expected, expectedback in [ |
| 335 | ('p','abc', '\x00', b''), |
| 336 | ('1p', 'abc', '\x00', b''), |
| 337 | ('2p', 'abc', '\x01a', b'a'), |
| 338 | ('3p', 'abc', '\x02ab', b'ab'), |
| 339 | ('4p', 'abc', '\x03abc', b'abc'), |
| 340 | ('5p', 'abc', '\x03abc\x00', b'abc'), |
| 341 | ('6p', 'abc', '\x03abc\x00\x00', b'abc'), |
| 342 | ('1000p', 'x'*1000, '\xff' + 'x'*999, b'x'*255)]: |
| 343 | expected = bytes(expected, "latin-1") |
| 344 | got = struct.pack(code, input) |
| 345 | self.assertEqual(got, expected) |
| 346 | (got,) = struct.unpack(code, got) |
| 347 | self.assertEqual(got, expectedback) |
| 348 | |
| 349 | def test_705836(self): |
| 350 | # SF bug 705836. "<f" and ">f" had a severe rounding bug, where a carry |
| 351 | # from the low-order discarded bits could propagate into the exponent |
| 352 | # field, causing the result to be wrong by a factor of 2. |
| 353 | import math |
| 354 | |
| 355 | for base in range(1, 33): |
| 356 | # smaller <- largest representable float less than base. |
| 357 | delta = 0.5 |
| 358 | while base - delta / 2.0 != base: |
| 359 | delta /= 2.0 |
| 360 | smaller = base - delta |
| 361 | # Packing this rounds away a solid string of trailing 1 bits. |
| 362 | packed = struct.pack("<f", smaller) |
| 363 | unpacked = struct.unpack("<f", packed)[0] |
| 364 | # This failed at base = 2, 4, and 32, with unpacked = 1, 2, and |
| 365 | # 16, respectively. |
| 366 | self.assertEqual(base, unpacked) |
| 367 | bigpacked = struct.pack(">f", smaller) |
| 368 | self.assertEqual(bigpacked, string_reverse(packed)) |
| 369 | unpacked = struct.unpack(">f", bigpacked)[0] |
| 370 | self.assertEqual(base, unpacked) |
| 371 | |
| 372 | # Largest finite IEEE single. |
| 373 | big = (1 << 24) - 1 |
| 374 | big = math.ldexp(big, 127 - 23) |
Tim Peters | d50ade6 | 2003-03-20 18:32:13 +0000 | [diff] [blame] | 375 | packed = struct.pack(">f", big) |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 376 | unpacked = struct.unpack(">f", packed)[0] |
| 377 | self.assertEqual(big, unpacked) |
Tim Peters | d50ade6 | 2003-03-20 18:32:13 +0000 | [diff] [blame] | 378 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 379 | # The same, but tack on a 1 bit so it rounds up to infinity. |
| 380 | big = (1 << 25) - 1 |
| 381 | big = math.ldexp(big, 127 - 24) |
| 382 | self.assertRaises(OverflowError, struct.pack, ">f", big) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 383 | |
Mark Dickinson | ea835e7 | 2009-04-19 20:40:33 +0000 | [diff] [blame] | 384 | def test_1530559(self): |
Mark Dickinson | b9f751a | 2010-04-03 15:07:40 +0000 | [diff] [blame] | 385 | for byteorder in '', '@', '=', '<', '>', '!': |
| 386 | for code in integer_codes: |
| 387 | if (byteorder in ('', '@') and code in ('q', 'Q') and |
| 388 | not HAVE_LONG_LONG): |
| 389 | continue |
| 390 | format = byteorder + code |
| 391 | self.assertRaises(struct.error, struct.pack, format, 1.0) |
| 392 | self.assertRaises(struct.error, struct.pack, format, 1.5) |
Mark Dickinson | ea835e7 | 2009-04-19 20:40:33 +0000 | [diff] [blame] | 393 | self.assertRaises(struct.error, struct.pack, 'P', 1.0) |
| 394 | self.assertRaises(struct.error, struct.pack, 'P', 1.5) |
| 395 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 396 | def test_unpack_from(self): |
| 397 | test_string = b'abcd01234' |
| 398 | fmt = '4s' |
| 399 | s = struct.Struct(fmt) |
| 400 | for cls in (bytes, bytearray): |
| 401 | data = cls(test_string) |
| 402 | if not isinstance(data, (bytes, bytearray)): |
| 403 | bytes_data = bytes(data, 'latin1') |
| 404 | else: |
| 405 | bytes_data = data |
| 406 | self.assertEqual(s.unpack_from(data), (b'abcd',)) |
| 407 | self.assertEqual(s.unpack_from(data, 2), (b'cd01',)) |
| 408 | self.assertEqual(s.unpack_from(data, 4), (b'0123',)) |
| 409 | for i in range(6): |
| 410 | self.assertEqual(s.unpack_from(data, i), (bytes_data[i:i+4],)) |
| 411 | for i in range(6, len(test_string) + 1): |
| 412 | self.assertRaises(struct.error, s.unpack_from, data, i) |
| 413 | for cls in (bytes, bytearray): |
| 414 | data = cls(test_string) |
| 415 | self.assertEqual(struct.unpack_from(fmt, data), (b'abcd',)) |
| 416 | self.assertEqual(struct.unpack_from(fmt, data, 2), (b'cd01',)) |
| 417 | self.assertEqual(struct.unpack_from(fmt, data, 4), (b'0123',)) |
| 418 | for i in range(6): |
| 419 | self.assertEqual(struct.unpack_from(fmt, data, i), (data[i:i+4],)) |
| 420 | for i in range(6, len(test_string) + 1): |
| 421 | self.assertRaises(struct.error, struct.unpack_from, fmt, data, i) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 422 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 423 | def test_pack_into(self): |
| 424 | test_string = b'Reykjavik rocks, eow!' |
| 425 | writable_buf = array.array('b', b' '*100) |
| 426 | fmt = '21s' |
| 427 | s = struct.Struct(fmt) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 428 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 429 | # Test without offset |
| 430 | s.pack_into(writable_buf, 0, test_string) |
| 431 | from_buf = writable_buf.tostring()[:len(test_string)] |
| 432 | self.assertEqual(from_buf, test_string) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 433 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 434 | # Test with offset. |
| 435 | s.pack_into(writable_buf, 10, test_string) |
| 436 | from_buf = writable_buf.tostring()[:len(test_string)+10] |
| 437 | self.assertEqual(from_buf, test_string[:10] + test_string) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 438 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 439 | # Go beyond boundaries. |
| 440 | small_buf = array.array('b', b' '*10) |
| 441 | self.assertRaises(struct.error, s.pack_into, small_buf, 0, test_string) |
| 442 | self.assertRaises(struct.error, s.pack_into, small_buf, 2, test_string) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 443 | |
Georg Brandl | 75c3d6f | 2009-02-13 11:01:07 +0000 | [diff] [blame] | 444 | # Test bogus offset (issue 3694) |
| 445 | sb = small_buf |
| 446 | self.assertRaises(TypeError, struct.pack_into, b'1', sb, None) |
| 447 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 448 | def test_pack_into_fn(self): |
| 449 | test_string = b'Reykjavik rocks, eow!' |
| 450 | writable_buf = array.array('b', b' '*100) |
| 451 | fmt = '21s' |
| 452 | pack_into = lambda *args: struct.pack_into(fmt, *args) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 453 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 454 | # Test without offset. |
| 455 | pack_into(writable_buf, 0, test_string) |
| 456 | from_buf = writable_buf.tostring()[:len(test_string)] |
| 457 | self.assertEqual(from_buf, test_string) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 458 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 459 | # Test with offset. |
| 460 | pack_into(writable_buf, 10, test_string) |
| 461 | from_buf = writable_buf.tostring()[:len(test_string)+10] |
| 462 | self.assertEqual(from_buf, test_string[:10] + test_string) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 463 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 464 | # Go beyond boundaries. |
| 465 | small_buf = array.array('b', b' '*10) |
| 466 | self.assertRaises(struct.error, pack_into, small_buf, 0, test_string) |
| 467 | self.assertRaises(struct.error, pack_into, small_buf, 2, test_string) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 468 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 469 | def test_unpack_with_buffer(self): |
| 470 | # SF bug 1563759: struct.unpack doens't support buffer protocol objects |
| 471 | data1 = array.array('B', b'\x12\x34\x56\x78') |
| 472 | data2 = memoryview(b'\x12\x34\x56\x78') # XXX b'......XXXX......', 6, 4 |
| 473 | for data in [data1, data2]: |
| 474 | value, = struct.unpack('>I', data) |
| 475 | self.assertEqual(value, 0x12345678) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 476 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 477 | def test_bool(self): |
| 478 | for prefix in tuple("<>!=")+('',): |
| 479 | false = (), [], [], '', 0 |
| 480 | true = [1], 'test', 5, -1, 0xffffffff+1, 0xffffffff/2 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 481 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 482 | falseFormat = prefix + '?' * len(false) |
| 483 | packedFalse = struct.pack(falseFormat, *false) |
| 484 | unpackedFalse = struct.unpack(falseFormat, packedFalse) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 485 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 486 | trueFormat = prefix + '?' * len(true) |
| 487 | packedTrue = struct.pack(trueFormat, *true) |
| 488 | unpackedTrue = struct.unpack(trueFormat, packedTrue) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 489 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 490 | self.assertEqual(len(true), len(unpackedTrue)) |
| 491 | self.assertEqual(len(false), len(unpackedFalse)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 492 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 493 | for t in unpackedFalse: |
| 494 | self.assertFalse(t) |
| 495 | for t in unpackedTrue: |
| 496 | self.assertTrue(t) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 497 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 498 | packed = struct.pack(prefix+'?', 1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 499 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 500 | self.assertEqual(len(packed), struct.calcsize(prefix+'?')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 501 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 502 | if len(packed) != 1: |
| 503 | self.assertFalse(prefix, msg='encoded bool is not one byte: %r' |
| 504 | %packed) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 505 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 506 | for c in [b'\x01', b'\x7f', b'\xff', b'\x0f', b'\xf0']: |
| 507 | self.assertTrue(struct.unpack('>?', c)[0]) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 508 | |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 509 | if IS32BIT: |
| 510 | def test_crasher(self): |
| 511 | self.assertRaises(MemoryError, struct.pack, "357913941b", "a") |
| 512 | |
| 513 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 514 | def test_main(): |
| 515 | run_unittest(StructTest) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 516 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 517 | if __name__ == '__main__': |
| 518 | test_main() |