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