Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1 | from test.test_support import TestFailed, verbose, verify, vereq |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2 | import test.test_support |
Barry Warsaw | 07a0eec | 1996-12-12 23:34:06 +0000 | [diff] [blame] | 3 | import struct |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4 | import array |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 5 | import warnings |
Barry Warsaw | 07a0eec | 1996-12-12 23:34:06 +0000 | [diff] [blame] | 6 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 7 | import sys |
| 8 | ISBIGENDIAN = sys.byteorder == "big" |
| 9 | del sys |
Guido van Rossum | e625fd5 | 2007-05-27 09:19:04 +0000 | [diff] [blame] | 10 | verify((struct.pack('=i', 1)[0] == 0) == ISBIGENDIAN, |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 11 | "bigendian determination appears wrong") |
| 12 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 13 | try: |
| 14 | import _struct |
| 15 | except ImportError: |
| 16 | PY_STRUCT_RANGE_CHECKING = 0 |
| 17 | PY_STRUCT_OVERFLOW_MASKING = 1 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 18 | PY_STRUCT_FLOAT_COERCE = 2 |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 19 | else: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 20 | PY_STRUCT_RANGE_CHECKING = getattr(_struct, '_PY_STRUCT_RANGE_CHECKING', 0) |
| 21 | PY_STRUCT_OVERFLOW_MASKING = getattr(_struct, '_PY_STRUCT_OVERFLOW_MASKING', 0) |
| 22 | PY_STRUCT_FLOAT_COERCE = getattr(_struct, '_PY_STRUCT_FLOAT_COERCE', 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 23 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 24 | def string_reverse(s): |
Guido van Rossum | e625fd5 | 2007-05-27 09:19:04 +0000 | [diff] [blame] | 25 | return s[::-1] |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 26 | |
| 27 | def bigendian_to_native(value): |
| 28 | if ISBIGENDIAN: |
| 29 | return value |
| 30 | else: |
| 31 | return string_reverse(value) |
| 32 | |
Barry Warsaw | 07a0eec | 1996-12-12 23:34:06 +0000 | [diff] [blame] | 33 | def simple_err(func, *args): |
| 34 | try: |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 35 | func(*args) |
Barry Warsaw | 07a0eec | 1996-12-12 23:34:06 +0000 | [diff] [blame] | 36 | except struct.error: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 37 | pass |
Barry Warsaw | 07a0eec | 1996-12-12 23:34:06 +0000 | [diff] [blame] | 38 | else: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 39 | raise TestFailed("%s%s did not raise struct.error" % ( |
| 40 | func.__name__, args)) |
Barry Warsaw | 07a0eec | 1996-12-12 23:34:06 +0000 | [diff] [blame] | 41 | |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 42 | def any_err(func, *args): |
| 43 | try: |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 44 | func(*args) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 45 | except (struct.error, TypeError): |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 46 | pass |
| 47 | else: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 48 | raise TestFailed("%s%s did not raise error" % ( |
| 49 | func.__name__, args)) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 50 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 51 | def with_warning_restore(func): |
| 52 | def _with_warning_restore(*args, **kw): |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 53 | with test.test_support.catch_warning(): |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 54 | # Grrr, we need this function to warn every time. Without removing |
| 55 | # the warningregistry, running test_tarfile then test_struct would fail |
| 56 | # on 64-bit platforms. |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 57 | globals = func.__globals__ |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 58 | if '__warningregistry__' in globals: |
| 59 | del globals['__warningregistry__'] |
| 60 | warnings.filterwarnings("error", r"""^struct.*""", DeprecationWarning) |
| 61 | warnings.filterwarnings("error", r""".*format requires.*""", |
| 62 | DeprecationWarning) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 63 | return func(*args, **kw) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 64 | return _with_warning_restore |
| 65 | |
| 66 | def deprecated_err(func, *args): |
| 67 | try: |
| 68 | func(*args) |
| 69 | except (struct.error, TypeError): |
| 70 | pass |
| 71 | except DeprecationWarning: |
| 72 | if not PY_STRUCT_OVERFLOW_MASKING: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 73 | raise TestFailed("%s%s expected to raise struct.error" % ( |
| 74 | func.__name__, args)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 75 | else: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 76 | raise TestFailed("%s%s did not raise error" % ( |
| 77 | func.__name__, args)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 78 | deprecated_err = with_warning_restore(deprecated_err) |
| 79 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 80 | |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 81 | simple_err(struct.calcsize, 'Z') |
Barry Warsaw | 07a0eec | 1996-12-12 23:34:06 +0000 | [diff] [blame] | 82 | |
| 83 | sz = struct.calcsize('i') |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 84 | if sz * 3 != struct.calcsize('iii'): |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 85 | raise TestFailed('inconsistent sizes') |
Barry Warsaw | 07a0eec | 1996-12-12 23:34:06 +0000 | [diff] [blame] | 86 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 87 | fmt = 'cbxxxxxxhhhhiillffdt' |
| 88 | fmt3 = '3c3b18x12h6i6l6f3d3t' |
Guido van Rossum | 04ebf5c | 1997-01-03 19:00:37 +0000 | [diff] [blame] | 89 | sz = struct.calcsize(fmt) |
| 90 | sz3 = struct.calcsize(fmt3) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 91 | if sz * 3 != sz3: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 92 | raise TestFailed('inconsistent sizes (3*%r -> 3*%d = %d, %r -> %d)' % ( |
| 93 | fmt, sz, 3*sz, fmt3, sz3)) |
Barry Warsaw | 07a0eec | 1996-12-12 23:34:06 +0000 | [diff] [blame] | 94 | |
| 95 | simple_err(struct.pack, 'iii', 3) |
| 96 | simple_err(struct.pack, 'i', 3, 3, 3) |
| 97 | simple_err(struct.pack, 'i', 'foo') |
Armin Rigo | 9f90439 | 2004-09-27 19:27:51 +0000 | [diff] [blame] | 98 | simple_err(struct.pack, 'P', 'foo') |
Barry Warsaw | 07a0eec | 1996-12-12 23:34:06 +0000 | [diff] [blame] | 99 | simple_err(struct.unpack, 'd', 'flap') |
| 100 | s = struct.pack('ii', 1, 2) |
| 101 | simple_err(struct.unpack, 'iii', s) |
| 102 | simple_err(struct.unpack, 'i', s) |
| 103 | |
| 104 | c = 'a' |
Guido van Rossum | 2a37850 | 1996-12-31 17:25:47 +0000 | [diff] [blame] | 105 | b = 1 |
Barry Warsaw | 07a0eec | 1996-12-12 23:34:06 +0000 | [diff] [blame] | 106 | h = 255 |
| 107 | i = 65535 |
| 108 | l = 65536 |
| 109 | f = 3.1415 |
| 110 | d = 3.1415 |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 111 | t = True |
Barry Warsaw | 07a0eec | 1996-12-12 23:34:06 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | 420c11c | 1997-01-03 00:09:46 +0000 | [diff] [blame] | 113 | for prefix in ('', '@', '<', '>', '=', '!'): |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 114 | for format in ('xcbhilfdt', 'xcBHILfdt'): |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 115 | format = prefix + format |
| 116 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 117 | print("trying:", format) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 118 | s = struct.pack(format, c, b, h, i, l, f, d, t) |
| 119 | cp, bp, hp, ip, lp, fp, dp, tp = struct.unpack(format, s) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 120 | if (cp != c or bp != b or hp != h or ip != i or lp != l or |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 121 | int(100 * fp) != int(100 * f) or int(100 * dp) != int(100 * d) or |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 122 | tp != t): |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 123 | # ^^^ calculate only to two decimal places |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 124 | raise TestFailed("unpack/pack not transitive (%s, %s)" % ( |
| 125 | str(format), str((cp, bp, hp, ip, lp, fp, dp, tp)))) |
Guido van Rossum | 2a37850 | 1996-12-31 17:25:47 +0000 | [diff] [blame] | 126 | |
Guido van Rossum | 420c11c | 1997-01-03 00:09:46 +0000 | [diff] [blame] | 127 | # Test some of the new features in detail |
Guido van Rossum | 2a37850 | 1996-12-31 17:25:47 +0000 | [diff] [blame] | 128 | |
| 129 | # (format, argument, big-endian result, little-endian result, asymmetric) |
| 130 | tests = [ |
| 131 | ('c', 'a', 'a', 'a', 0), |
| 132 | ('xc', 'a', '\0a', '\0a', 0), |
| 133 | ('cx', 'a', 'a\0', 'a\0', 0), |
| 134 | ('s', 'a', 'a', 'a', 0), |
| 135 | ('0s', 'helloworld', '', '', 1), |
| 136 | ('1s', 'helloworld', 'h', 'h', 1), |
| 137 | ('9s', 'helloworld', 'helloworl', 'helloworl', 1), |
| 138 | ('10s', 'helloworld', 'helloworld', 'helloworld', 0), |
| 139 | ('11s', 'helloworld', 'helloworld\0', 'helloworld\0', 1), |
| 140 | ('20s', 'helloworld', 'helloworld'+10*'\0', 'helloworld'+10*'\0', 1), |
| 141 | ('b', 7, '\7', '\7', 0), |
| 142 | ('b', -7, '\371', '\371', 0), |
| 143 | ('B', 7, '\7', '\7', 0), |
| 144 | ('B', 249, '\371', '\371', 0), |
| 145 | ('h', 700, '\002\274', '\274\002', 0), |
| 146 | ('h', -700, '\375D', 'D\375', 0), |
| 147 | ('H', 700, '\002\274', '\274\002', 0), |
| 148 | ('H', 0x10000-700, '\375D', 'D\375', 0), |
| 149 | ('i', 70000000, '\004,\035\200', '\200\035,\004', 0), |
| 150 | ('i', -70000000, '\373\323\342\200', '\200\342\323\373', 0), |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 151 | ('I', 70000000, '\004,\035\200', '\200\035,\004', 0), |
| 152 | ('I', 0x100000000-70000000, '\373\323\342\200', '\200\342\323\373', 0), |
Guido van Rossum | 2a37850 | 1996-12-31 17:25:47 +0000 | [diff] [blame] | 153 | ('l', 70000000, '\004,\035\200', '\200\035,\004', 0), |
| 154 | ('l', -70000000, '\373\323\342\200', '\200\342\323\373', 0), |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 155 | ('L', 70000000, '\004,\035\200', '\200\035,\004', 0), |
| 156 | ('L', 0x100000000-70000000, '\373\323\342\200', '\200\342\323\373', 0), |
Guido van Rossum | 420c11c | 1997-01-03 00:09:46 +0000 | [diff] [blame] | 157 | ('f', 2.0, '@\000\000\000', '\000\000\000@', 0), |
| 158 | ('d', 2.0, '@\000\000\000\000\000\000\000', |
| 159 | '\000\000\000\000\000\000\000@', 0), |
| 160 | ('f', -2.0, '\300\000\000\000', '\000\000\000\300', 0), |
| 161 | ('d', -2.0, '\300\000\000\000\000\000\000\000', |
| 162 | '\000\000\000\000\000\000\000\300', 0), |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 163 | ('t', 0, '\0', '\0', 0), |
| 164 | ('t', 3, '\1', '\1', 1), |
| 165 | ('t', True, '\1', '\1', 0), |
| 166 | ('t', [], '\0', '\0', 1), |
| 167 | ('t', (1,), '\1', '\1', 1), |
Guido van Rossum | 2a37850 | 1996-12-31 17:25:47 +0000 | [diff] [blame] | 168 | ] |
| 169 | |
Guido van Rossum | 2a37850 | 1996-12-31 17:25:47 +0000 | [diff] [blame] | 170 | for fmt, arg, big, lil, asy in tests: |
Guido van Rossum | e625fd5 | 2007-05-27 09:19:04 +0000 | [diff] [blame] | 171 | big = bytes(big, "latin-1") |
| 172 | lil = bytes(lil, "latin-1") |
Guido van Rossum | 2a37850 | 1996-12-31 17:25:47 +0000 | [diff] [blame] | 173 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 174 | print("%r %r %r %r" % (fmt, arg, big, lil)) |
Guido van Rossum | 2a37850 | 1996-12-31 17:25:47 +0000 | [diff] [blame] | 175 | for (xfmt, exp) in [('>'+fmt, big), ('!'+fmt, big), ('<'+fmt, lil), |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 176 | ('='+fmt, ISBIGENDIAN and big or lil)]: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 177 | res = struct.pack(xfmt, arg) |
| 178 | if res != exp: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 179 | raise TestFailed("pack(%r, %r) -> %r # expected %r" % ( |
| 180 | fmt, arg, res, exp)) |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 181 | n = struct.calcsize(xfmt) |
| 182 | if n != len(res): |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 183 | raise TestFailed("calcsize(%r) -> %d # expected %d" % ( |
| 184 | xfmt, n, len(res))) |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 185 | rev = struct.unpack(xfmt, res)[0] |
| 186 | if rev != arg and not asy: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 187 | raise TestFailed("unpack(%r, %r) -> (%r,) # expected (%r,)" % ( |
| 188 | fmt, res, rev, arg)) |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 189 | |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 190 | ########################################################################### |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 191 | # Simple native q/Q tests. |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 192 | |
| 193 | has_native_qQ = 1 |
| 194 | try: |
| 195 | struct.pack("q", 5) |
| 196 | except struct.error: |
| 197 | has_native_qQ = 0 |
| 198 | |
| 199 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 200 | print("Platform has native q/Q?", has_native_qQ and "Yes." or "No.") |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 201 | |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 202 | any_err(struct.pack, "Q", -1) # can't pack -1 as unsigned regardless |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 203 | simple_err(struct.pack, "q", "a") # can't pack string as 'q' regardless |
| 204 | simple_err(struct.pack, "Q", "a") # ditto, but 'Q' |
| 205 | |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 206 | def test_native_qQ(): |
Guido van Rossum | e625fd5 | 2007-05-27 09:19:04 +0000 | [diff] [blame] | 207 | nbytes = struct.calcsize('q') |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 208 | # The expected values here are in big-endian format, primarily because |
| 209 | # I'm on a little-endian machine and so this is the clearest way (for |
| 210 | # me) to force the code to get exercised. |
| 211 | for format, input, expected in ( |
Guido van Rossum | e625fd5 | 2007-05-27 09:19:04 +0000 | [diff] [blame] | 212 | ('q', -1, '\xff' * nbytes), |
| 213 | ('q', 0, '\x00' * nbytes), |
| 214 | ('Q', 0, '\x00' * nbytes), |
| 215 | ('q', 1, '\x00' * (nbytes-1) + '\x01'), |
| 216 | ('Q', (1 << (8*nbytes))-1, '\xff' * nbytes), |
| 217 | ('q', (1 << (8*nbytes-1))-1, '\x7f' + '\xff' * (nbytes - 1))): |
| 218 | expected = bytes(expected, "latin-1") |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 219 | got = struct.pack(format, input) |
Tim Peters | c533edc | 2001-06-10 23:52:59 +0000 | [diff] [blame] | 220 | native_expected = bigendian_to_native(expected) |
| 221 | verify(got == native_expected, |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 222 | "%r-pack of %r gave %r, not %r" % |
Tim Peters | c533edc | 2001-06-10 23:52:59 +0000 | [diff] [blame] | 223 | (format, input, got, native_expected)) |
Tim Peters | 7b9542a | 2001-06-10 23:40:19 +0000 | [diff] [blame] | 224 | retrieved = struct.unpack(format, got)[0] |
| 225 | verify(retrieved == input, |
| 226 | "%r-unpack of %r gave %r, not %r" % |
| 227 | (format, got, retrieved, input)) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 228 | |
| 229 | if has_native_qQ: |
| 230 | test_native_qQ() |
| 231 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 232 | ########################################################################### |
| 233 | # Standard integer tests (bBhHiIlLqQ). |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 234 | |
| 235 | import binascii |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 236 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 237 | class IntTester: |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 238 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 239 | # XXX Most std integer modes fail to test for out-of-range. |
Tim Peters | 3eec38a | 2001-06-18 22:27:39 +0000 | [diff] [blame] | 240 | # The "i" and "l" codes appear to range-check OK on 32-bit boxes, but |
| 241 | # fail to check correctly on some 64-bit ones (Tru64 Unix + Compaq C |
| 242 | # reported by Mark Favas). |
| 243 | BUGGY_RANGE_CHECK = "bBhHiIlL" |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 244 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 245 | def __init__(self, formatpair, bytesize): |
| 246 | assert len(formatpair) == 2 |
| 247 | self.formatpair = formatpair |
| 248 | for direction in "<>!=": |
| 249 | for code in formatpair: |
| 250 | format = direction + code |
| 251 | verify(struct.calcsize(format) == bytesize) |
| 252 | self.bytesize = bytesize |
| 253 | self.bitsize = bytesize * 8 |
| 254 | self.signed_code, self.unsigned_code = formatpair |
| 255 | self.unsigned_min = 0 |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 256 | self.unsigned_max = 2**self.bitsize - 1 |
| 257 | self.signed_min = -(2**(self.bitsize-1)) |
| 258 | self.signed_max = 2**(self.bitsize-1) - 1 |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 259 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 260 | def test_one(self, x, pack=struct.pack, |
| 261 | unpack=struct.unpack, |
| 262 | unhexlify=binascii.unhexlify): |
| 263 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 264 | print("trying std", self.formatpair, "on", x, "==", hex(x)) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 265 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 266 | # Try signed. |
| 267 | code = self.signed_code |
| 268 | if self.signed_min <= x <= self.signed_max: |
| 269 | # Try big-endian. |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 270 | expected = int(x) |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 271 | if x < 0: |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 272 | expected += 1 << self.bitsize |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 273 | assert expected > 0 |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 274 | expected = hex(expected)[2:] # chop "0x" |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 275 | if len(expected) & 1: |
| 276 | expected = "0" + expected |
| 277 | expected = unhexlify(expected) |
Guido van Rossum | e625fd5 | 2007-05-27 09:19:04 +0000 | [diff] [blame] | 278 | expected = b"\x00" * (self.bytesize - len(expected)) + expected |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 279 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 280 | # Pack work? |
| 281 | format = ">" + code |
| 282 | got = pack(format, x) |
| 283 | verify(got == expected, |
| 284 | "'%s'-pack of %r gave %r, not %r" % |
| 285 | (format, x, got, expected)) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 286 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 287 | # Unpack work? |
| 288 | retrieved = unpack(format, got)[0] |
| 289 | verify(x == retrieved, |
| 290 | "'%s'-unpack of %r gave %r, not %r" % |
| 291 | (format, got, retrieved, x)) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 292 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 293 | # Adding any byte should cause a "too big" error. |
Guido van Rossum | e625fd5 | 2007-05-27 09:19:04 +0000 | [diff] [blame] | 294 | any_err(unpack, format, b'\x01' + got) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 295 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 296 | # Try little-endian. |
| 297 | format = "<" + code |
| 298 | expected = string_reverse(expected) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 299 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 300 | # Pack work? |
| 301 | got = pack(format, x) |
| 302 | verify(got == expected, |
| 303 | "'%s'-pack of %r gave %r, not %r" % |
| 304 | (format, x, got, expected)) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 305 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 306 | # Unpack work? |
| 307 | retrieved = unpack(format, got)[0] |
| 308 | verify(x == retrieved, |
| 309 | "'%s'-unpack of %r gave %r, not %r" % |
| 310 | (format, got, retrieved, x)) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 311 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 312 | # Adding any byte should cause a "too big" error. |
Guido van Rossum | e625fd5 | 2007-05-27 09:19:04 +0000 | [diff] [blame] | 313 | any_err(unpack, format, b'\x01' + got) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 314 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 315 | else: |
| 316 | # x is out of range -- verify pack realizes that. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 317 | if not PY_STRUCT_RANGE_CHECKING and code in self.BUGGY_RANGE_CHECK: |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 318 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 319 | print("Skipping buggy range check for code", code) |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 320 | else: |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 321 | deprecated_err(pack, ">" + code, x) |
| 322 | deprecated_err(pack, "<" + code, x) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 323 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 324 | # Much the same for unsigned. |
| 325 | code = self.unsigned_code |
| 326 | if self.unsigned_min <= x <= self.unsigned_max: |
| 327 | # Try big-endian. |
| 328 | format = ">" + code |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 329 | expected = int(x) |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 330 | expected = hex(expected)[2:] # chop "0x" |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 331 | if len(expected) & 1: |
| 332 | expected = "0" + expected |
| 333 | expected = unhexlify(expected) |
Guido van Rossum | e625fd5 | 2007-05-27 09:19:04 +0000 | [diff] [blame] | 334 | expected = b"\x00" * (self.bytesize - len(expected)) + expected |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 335 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 336 | # Pack work? |
| 337 | got = pack(format, x) |
| 338 | verify(got == expected, |
| 339 | "'%s'-pack of %r gave %r, not %r" % |
| 340 | (format, x, got, expected)) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 341 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 342 | # Unpack work? |
| 343 | retrieved = unpack(format, got)[0] |
| 344 | verify(x == retrieved, |
| 345 | "'%s'-unpack of %r gave %r, not %r" % |
| 346 | (format, got, retrieved, x)) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 347 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 348 | # Adding any byte should cause a "too big" error. |
Guido van Rossum | e625fd5 | 2007-05-27 09:19:04 +0000 | [diff] [blame] | 349 | any_err(unpack, format, b'\x01' + got) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 350 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 351 | # Try little-endian. |
| 352 | format = "<" + code |
| 353 | expected = string_reverse(expected) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 354 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 355 | # Pack work? |
| 356 | got = pack(format, x) |
| 357 | verify(got == expected, |
| 358 | "'%s'-pack of %r gave %r, not %r" % |
| 359 | (format, x, got, expected)) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 360 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 361 | # Unpack work? |
| 362 | retrieved = unpack(format, got)[0] |
| 363 | verify(x == retrieved, |
| 364 | "'%s'-unpack of %r gave %r, not %r" % |
| 365 | (format, got, retrieved, x)) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 366 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 367 | # Adding any byte should cause a "too big" error. |
Guido van Rossum | e625fd5 | 2007-05-27 09:19:04 +0000 | [diff] [blame] | 368 | any_err(unpack, format, b'\x01' + got) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 369 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 370 | else: |
| 371 | # x is out of range -- verify pack realizes that. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 372 | if not PY_STRUCT_RANGE_CHECKING and code in self.BUGGY_RANGE_CHECK: |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 373 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 374 | print("Skipping buggy range check for code", code) |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 375 | else: |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 376 | deprecated_err(pack, ">" + code, x) |
| 377 | deprecated_err(pack, "<" + code, x) |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 378 | |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 379 | def run(self): |
| 380 | from random import randrange |
| 381 | |
| 382 | # Create all interesting powers of 2. |
| 383 | values = [] |
| 384 | for exp in range(self.bitsize + 3): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 385 | values.append(1 << exp) |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 386 | |
| 387 | # Add some random values. |
| 388 | for i in range(self.bitsize): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 389 | val = 0 |
Tim Peters | 17e17d4 | 2001-06-13 22:45:27 +0000 | [diff] [blame] | 390 | for j in range(self.bytesize): |
| 391 | val = (val << 8) | randrange(256) |
| 392 | values.append(val) |
| 393 | |
| 394 | # Try all those, and their negations, and +-1 from them. Note |
| 395 | # that this tests all power-of-2 boundaries in range, and a few out |
| 396 | # of range, plus +-(2**n +- 1). |
| 397 | for base in values: |
| 398 | for val in -base, base: |
| 399 | for incr in -1, 0, 1: |
| 400 | x = val + incr |
| 401 | try: |
| 402 | x = int(x) |
| 403 | except OverflowError: |
| 404 | pass |
| 405 | self.test_one(x) |
| 406 | |
| 407 | # Some error cases. |
| 408 | for direction in "<>": |
| 409 | for code in self.formatpair: |
| 410 | for badobject in "a string", 3+42j, randrange: |
| 411 | any_err(struct.pack, direction + code, badobject) |
| 412 | |
| 413 | for args in [("bB", 1), |
| 414 | ("hH", 2), |
| 415 | ("iI", 4), |
| 416 | ("lL", 4), |
| 417 | ("qQ", 8)]: |
| 418 | t = IntTester(*args) |
| 419 | t.run() |
Tim Peters | 0891ac0 | 2001-09-15 02:35:15 +0000 | [diff] [blame] | 420 | |
| 421 | |
| 422 | ########################################################################### |
| 423 | # The p ("Pascal string") code. |
| 424 | |
| 425 | def test_p_code(): |
| 426 | for code, input, expected, expectedback in [ |
| 427 | ('p','abc', '\x00', ''), |
| 428 | ('1p', 'abc', '\x00', ''), |
| 429 | ('2p', 'abc', '\x01a', 'a'), |
| 430 | ('3p', 'abc', '\x02ab', 'ab'), |
| 431 | ('4p', 'abc', '\x03abc', 'abc'), |
| 432 | ('5p', 'abc', '\x03abc\x00', 'abc'), |
| 433 | ('6p', 'abc', '\x03abc\x00\x00', 'abc'), |
| 434 | ('1000p', 'x'*1000, '\xff' + 'x'*999, 'x'*255)]: |
Guido van Rossum | e625fd5 | 2007-05-27 09:19:04 +0000 | [diff] [blame] | 435 | expected = bytes(expected, "latin-1") |
Tim Peters | 0891ac0 | 2001-09-15 02:35:15 +0000 | [diff] [blame] | 436 | got = struct.pack(code, input) |
| 437 | if got != expected: |
| 438 | raise TestFailed("pack(%r, %r) == %r but expected %r" % |
| 439 | (code, input, got, expected)) |
| 440 | (got,) = struct.unpack(code, got) |
| 441 | if got != expectedback: |
| 442 | raise TestFailed("unpack(%r, %r) == %r but expected %r" % |
| 443 | (code, input, got, expectedback)) |
| 444 | |
| 445 | test_p_code() |
Tim Peters | d50ade6 | 2003-03-20 18:32:13 +0000 | [diff] [blame] | 446 | |
| 447 | |
| 448 | ########################################################################### |
| 449 | # SF bug 705836. "<f" and ">f" had a severe rounding bug, where a carry |
| 450 | # from the low-order discarded bits could propagate into the exponent |
| 451 | # field, causing the result to be wrong by a factor of 2. |
| 452 | |
| 453 | def test_705836(): |
| 454 | import math |
| 455 | |
| 456 | for base in range(1, 33): |
| 457 | # smaller <- largest representable float less than base. |
| 458 | delta = 0.5 |
| 459 | while base - delta / 2.0 != base: |
| 460 | delta /= 2.0 |
| 461 | smaller = base - delta |
| 462 | # Packing this rounds away a solid string of trailing 1 bits. |
| 463 | packed = struct.pack("<f", smaller) |
| 464 | unpacked = struct.unpack("<f", packed)[0] |
| 465 | # This failed at base = 2, 4, and 32, with unpacked = 1, 2, and |
| 466 | # 16, respectively. |
| 467 | verify(base == unpacked) |
| 468 | bigpacked = struct.pack(">f", smaller) |
| 469 | verify(bigpacked == string_reverse(packed), |
| 470 | ">f pack should be byte-reversal of <f pack") |
| 471 | unpacked = struct.unpack(">f", bigpacked)[0] |
| 472 | verify(base == unpacked) |
| 473 | |
| 474 | # Largest finite IEEE single. |
| 475 | big = (1 << 24) - 1 |
| 476 | big = math.ldexp(big, 127 - 23) |
| 477 | packed = struct.pack(">f", big) |
| 478 | unpacked = struct.unpack(">f", packed)[0] |
| 479 | verify(big == unpacked) |
| 480 | |
| 481 | # The same, but tack on a 1 bit so it rounds up to infinity. |
| 482 | big = (1 << 25) - 1 |
| 483 | big = math.ldexp(big, 127 - 24) |
| 484 | try: |
| 485 | packed = struct.pack(">f", big) |
| 486 | except OverflowError: |
| 487 | pass |
| 488 | else: |
| 489 | TestFailed("expected OverflowError") |
| 490 | |
| 491 | test_705836() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 492 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 493 | ########################################################################### |
| 494 | # SF bug 1229380. No struct.pack exception for some out of range integers |
| 495 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 496 | def test_1229380(): |
| 497 | import sys |
| 498 | for endian in ('', '>', '<'): |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 499 | for fmt in ('B', 'H', 'I', 'L'): |
| 500 | deprecated_err(struct.pack, endian + fmt, -1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 501 | |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 502 | deprecated_err(struct.pack, endian + 'B', 300) |
| 503 | deprecated_err(struct.pack, endian + 'H', 70000) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 504 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 505 | deprecated_err(struct.pack, endian + 'I', sys.maxint * 4) |
| 506 | deprecated_err(struct.pack, endian + 'L', sys.maxint * 4) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 507 | |
| 508 | if PY_STRUCT_RANGE_CHECKING: |
| 509 | test_1229380() |
| 510 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 511 | ########################################################################### |
| 512 | # SF bug 1530559. struct.pack raises TypeError where it used to convert. |
| 513 | |
| 514 | def check_float_coerce(format, number): |
| 515 | if PY_STRUCT_FLOAT_COERCE == 2: |
| 516 | # Test for pre-2.5 struct module |
| 517 | packed = struct.pack(format, number) |
| 518 | floored = struct.unpack(format, packed)[0] |
| 519 | if floored != int(number): |
| 520 | raise TestFailed("did not correcly coerce float to int") |
| 521 | return |
| 522 | try: |
| 523 | func(*args) |
| 524 | except (struct.error, TypeError): |
| 525 | if PY_STRUCT_FLOAT_COERCE: |
| 526 | raise TestFailed("expected DeprecationWarning for float coerce") |
| 527 | except DeprecationWarning: |
| 528 | if not PY_STRUCT_FLOAT_COERCE: |
| 529 | raise TestFailed("expected to raise struct.error for float coerce") |
| 530 | else: |
| 531 | raise TestFailed("did not raise error for float coerce") |
| 532 | |
| 533 | check_float_coerce = with_warning_restore(deprecated_err) |
| 534 | |
| 535 | def test_1530559(): |
| 536 | for endian in ('', '>', '<'): |
| 537 | for fmt in ('B', 'H', 'I', 'L', 'b', 'h', 'i', 'l'): |
| 538 | check_float_coerce(endian + fmt, 1.0) |
| 539 | check_float_coerce(endian + fmt, 1.5) |
| 540 | |
| 541 | test_1530559() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 542 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 543 | ########################################################################### |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame^] | 544 | # Packing and unpacking to/from memory views. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 545 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 546 | # Copied and modified from unittest. |
| 547 | def assertRaises(excClass, callableObj, *args, **kwargs): |
| 548 | try: |
| 549 | callableObj(*args, **kwargs) |
| 550 | except excClass: |
| 551 | return |
| 552 | else: |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 553 | raise TestFailed("%s not raised." % excClass) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 554 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 555 | def test_unpack_from(): |
Guido van Rossum | e625fd5 | 2007-05-27 09:19:04 +0000 | [diff] [blame] | 556 | test_string = b'abcd01234' |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 557 | fmt = '4s' |
| 558 | s = struct.Struct(fmt) |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame^] | 559 | for cls in (str, str8, bytes): # XXX + memoryview |
Guido van Rossum | e625fd5 | 2007-05-27 09:19:04 +0000 | [diff] [blame] | 560 | if verbose: |
| 561 | print("test_unpack_from using", cls.__name__) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 562 | data = cls(test_string) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 563 | vereq(s.unpack_from(data), ('abcd',)) |
| 564 | vereq(s.unpack_from(data, 2), ('cd01',)) |
| 565 | vereq(s.unpack_from(data, 4), ('0123',)) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 566 | for i in range(6): |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 567 | vereq(s.unpack_from(data, i), (data[i:i+4],)) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 568 | for i in range(6, len(test_string) + 1): |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 569 | simple_err(s.unpack_from, data, i) |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame^] | 570 | for cls in (str, str8, bytes): # XXX + memoryview |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 571 | data = cls(test_string) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 572 | vereq(struct.unpack_from(fmt, data), ('abcd',)) |
| 573 | vereq(struct.unpack_from(fmt, data, 2), ('cd01',)) |
| 574 | vereq(struct.unpack_from(fmt, data, 4), ('0123',)) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 575 | for i in range(6): |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 576 | vereq(struct.unpack_from(fmt, data, i), (data[i:i+4],)) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 577 | for i in range(6, len(test_string) + 1): |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 578 | simple_err(struct.unpack_from, fmt, data, i) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 579 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 580 | def test_pack_into(): |
Guido van Rossum | 99c0c22 | 2007-07-03 16:28:47 +0000 | [diff] [blame] | 581 | test_string = b'Reykjavik rocks, eow!' |
| 582 | writable_buf = array.array('b', b' '*100) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 583 | fmt = '21s' |
| 584 | s = struct.Struct(fmt) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 585 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 586 | # Test without offset |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 587 | s.pack_into(writable_buf, 0, test_string) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 588 | from_buf = writable_buf.tostring()[:len(test_string)] |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 589 | vereq(from_buf, test_string) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 590 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 591 | # Test with offset. |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 592 | s.pack_into(writable_buf, 10, test_string) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 593 | from_buf = writable_buf.tostring()[:len(test_string)+10] |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 594 | vereq(from_buf, test_string[:10] + test_string) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 595 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 596 | # Go beyond boundaries. |
Guido van Rossum | 99c0c22 | 2007-07-03 16:28:47 +0000 | [diff] [blame] | 597 | small_buf = array.array('b', b' '*10) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 598 | assertRaises(struct.error, s.pack_into, small_buf, 0, test_string) |
| 599 | assertRaises(struct.error, s.pack_into, small_buf, 2, test_string) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 600 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 601 | def test_pack_into_fn(): |
Guido van Rossum | 99c0c22 | 2007-07-03 16:28:47 +0000 | [diff] [blame] | 602 | test_string = b'Reykjavik rocks, eow!' |
| 603 | writable_buf = array.array('b', b' '*100) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 604 | fmt = '21s' |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 605 | pack_into = lambda *args: struct.pack_into(fmt, *args) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 606 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 607 | # Test without offset. |
| 608 | pack_into(writable_buf, 0, test_string) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 609 | from_buf = writable_buf.tostring()[:len(test_string)] |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 610 | vereq(from_buf, test_string) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 611 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 612 | # Test with offset. |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 613 | pack_into(writable_buf, 10, test_string) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 614 | from_buf = writable_buf.tostring()[:len(test_string)+10] |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 615 | vereq(from_buf, test_string[:10] + test_string) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 616 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 617 | # Go beyond boundaries. |
Guido van Rossum | 99c0c22 | 2007-07-03 16:28:47 +0000 | [diff] [blame] | 618 | small_buf = array.array('b', b' '*10) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 619 | assertRaises(struct.error, pack_into, small_buf, 0, test_string) |
| 620 | assertRaises(struct.error, pack_into, small_buf, 2, test_string) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 621 | |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame^] | 622 | def test_unpack_with_memoryview(): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 623 | # SF bug 1563759: struct.unpack doens't support buffer protocol objects |
Guido van Rossum | e625fd5 | 2007-05-27 09:19:04 +0000 | [diff] [blame] | 624 | data1 = array.array('B', b'\x12\x34\x56\x78') |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame^] | 625 | data2 = memoryview(b'\x12\x34\x56\x78') # XXX b'......XXXX......', 6, 4 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 626 | for data in [data1, data2]: |
| 627 | value, = struct.unpack('>I', data) |
| 628 | vereq(value, 0x12345678) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 629 | |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame^] | 630 | # Test methods to pack and unpack from memoryviews rather than strings. |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 631 | test_unpack_from() |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 632 | test_pack_into() |
| 633 | test_pack_into_fn() |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame^] | 634 | test_unpack_with_memoryview() |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 635 | |
| 636 | def test_bool(): |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 637 | for prefix in tuple("<>!=")+('',): |
| 638 | false = (), [], [], '', 0 |
| 639 | true = [1], 'test', 5, -1, 0xffffffff+1, 0xffffffff/2 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 640 | |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 641 | falseFormat = prefix + 't' * len(false) |
| 642 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 643 | print('trying bool pack/unpack on', false, 'using format', falseFormat) |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 644 | packedFalse = struct.pack(falseFormat, *false) |
| 645 | unpackedFalse = struct.unpack(falseFormat, packedFalse) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 646 | |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 647 | trueFormat = prefix + 't' * len(true) |
| 648 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 649 | print('trying bool pack/unpack on', true, 'using format', trueFormat) |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 650 | packedTrue = struct.pack(trueFormat, *true) |
| 651 | unpackedTrue = struct.unpack(trueFormat, packedTrue) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 652 | |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 653 | if len(true) != len(unpackedTrue): |
| 654 | raise TestFailed('unpacked true array is not of same size as input') |
| 655 | if len(false) != len(unpackedFalse): |
| 656 | raise TestFailed('unpacked false array is not of same size as input') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 657 | |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 658 | for t in unpackedFalse: |
| 659 | if t is not False: |
| 660 | raise TestFailed('%r did not unpack as False' % t) |
| 661 | for t in unpackedTrue: |
| 662 | if t is not True: |
| 663 | raise TestFailed('%r did not unpack as false' % t) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 664 | |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 665 | if prefix and verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 666 | print('trying size of bool with format %r' % (prefix+'t')) |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 667 | packed = struct.pack(prefix+'t', 1) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 668 | |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 669 | if len(packed) != struct.calcsize(prefix+'t'): |
| 670 | raise TestFailed('packed length is not equal to calculated size') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 671 | |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 672 | if len(packed) != 1 and prefix: |
| 673 | raise TestFailed('encoded bool is not one byte: %r' % packed) |
| 674 | elif not prefix and verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 675 | print('size of bool in native format is %i' % (len(packed))) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 676 | |
Guido van Rossum | e625fd5 | 2007-05-27 09:19:04 +0000 | [diff] [blame] | 677 | for c in str8('\x01\x7f\xff\x0f\xf0'): |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 678 | if struct.unpack('>t', c)[0] is not True: |
| 679 | raise TestFailed('%c did not unpack as True' % c) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 680 | |
| 681 | test_bool() |