annotate Lib/test/test_struct.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents fa95b04e67fd
children be4bec689de3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
63649
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
1 import os
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 38277
diff changeset
2 import array
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
3 import unittest
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
4 import struct
63649
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
5 import inspect
78317
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 78311
diff changeset
6 from test import test_support as support
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 78311
diff changeset
7 from test.test_support import (check_warnings, check_py3k_warnings)
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
8
18111
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
9 import sys
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
10 ISBIGENDIAN = sys.byteorder == "big"
48834
75f64fd1f113 Fix bug 3625: test issues on 64bit windows. r=pitrou
Mark Hammond <mhammond@skippinet.com.au>
parents: 47942
diff changeset
11 IS32BIT = sys.maxsize == 0x7fffffff
18111
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
12
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
13 integer_codes = 'b', 'B', 'h', 'H', 'i', 'I', 'l', 'L', 'q', 'Q'
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
14
63649
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
15 testmod_filename = os.path.splitext(__file__)[0] + '.py'
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
16 # Native 'q' packing isn't available on systems that don't have the C
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
17 # long long type.
38493
7d510e4f3c98 struct: modulo math plus warning on all endian-explicit formats for compatibility with older struct usage (ugly)
Bob Ippolito <bob@redivi.com>
parents: 38430
diff changeset
18 try:
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
19 struct.pack('q', 5)
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
20 except struct.error:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
21 HAVE_LONG_LONG = False
38493
7d510e4f3c98 struct: modulo math plus warning on all endian-explicit formats for compatibility with older struct usage (ugly)
Bob Ippolito <bob@redivi.com>
parents: 38430
diff changeset
22 else:
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
23 HAVE_LONG_LONG = True
38363
5213b6065197 fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents: 38355
diff changeset
24
18111
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
25 def string_reverse(s):
38615
c4b44222a599 Access _struct attributes directly instead of mucking with getattr.
Tim Peters <tim.peters@gmail.com>
parents: 38580
diff changeset
26 return "".join(reversed(s))
18111
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
27
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
28 def bigendian_to_native(value):
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
29 if ISBIGENDIAN:
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
30 return value
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
31 else:
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
32 return string_reverse(value)
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
33
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
34 class StructTest(unittest.TestCase):
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
35
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
36 def check_float_coerce(self, format, number):
59436
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
37 # SF bug 1530559. struct.pack raises TypeError where it used
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
38 # to convert.
59774
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
39 with check_warnings((".*integer argument expected, got float",
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
40 DeprecationWarning)) as w:
59436
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
41 got = struct.pack(format, number)
59774
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
42 lineno = inspect.currentframe().f_lineno - 1
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
43 self.assertEqual(w.filename, testmod_filename)
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
44 self.assertEqual(w.lineno, lineno)
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
45 self.assertEqual(len(w.warnings), 1)
59436
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
46 expected = struct.pack(format, int(number))
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
47 self.assertEqual(got, expected)
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
48
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
49 def test_isbigendian(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
50 self.assertEqual((struct.pack('=i', 1)[0] == chr(0)), ISBIGENDIAN)
4706
9785114326ab A test of the struct module
Barry Warsaw <barry@python.org>
parents:
diff changeset
51
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
52 def test_consistence(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
53 self.assertRaises(struct.error, struct.calcsize, 'Z')
4847
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
54
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
55 sz = struct.calcsize('i')
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
56 self.assertEqual(sz * 3, struct.calcsize('iii'))
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
57
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
58 fmt = 'cbxxxxxxhhhhiillffd?'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
59 fmt3 = '3c3b18x12h6i6l6f3d3?'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
60 sz = struct.calcsize(fmt)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
61 sz3 = struct.calcsize(fmt3)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
62 self.assertEqual(sz * 3, sz3)
4847
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
63
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
64 self.assertRaises(struct.error, struct.pack, 'iii', 3)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
65 self.assertRaises(struct.error, struct.pack, 'i', 3, 3, 3)
62897
815ae7f3732b Merged revisions 82739,82741 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62891
diff changeset
66 self.assertRaises((TypeError, struct.error), struct.pack, 'i', 'foo')
815ae7f3732b Merged revisions 82739,82741 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62891
diff changeset
67 self.assertRaises((TypeError, struct.error), struct.pack, 'P', 'foo')
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
68 self.assertRaises(struct.error, struct.unpack, 'd', 'flap')
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
69 s = struct.pack('ii', 1, 2)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
70 self.assertRaises(struct.error, struct.unpack, 'iii', s)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
71 self.assertRaises(struct.error, struct.unpack, 'i', s)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
72
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
73 def test_transitiveness(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
74 c = 'a'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
75 b = 1
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
76 h = 255
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
77 i = 65535
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
78 l = 65536
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
79 f = 3.1415
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
80 d = 3.1415
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
81 t = True
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 15540
diff changeset
82
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
83 for prefix in ('', '@', '<', '>', '=', '!'):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
84 for format in ('xcbhilfd?', 'xcBHILfd?'):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
85 format = prefix + format
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
86 s = struct.pack(format, c, b, h, i, l, f, d, t)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
87 cp, bp, hp, ip, lp, fp, dp, tp = struct.unpack(format, s)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
88 self.assertEqual(cp, c)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
89 self.assertEqual(bp, b)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
90 self.assertEqual(hp, h)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
91 self.assertEqual(ip, i)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
92 self.assertEqual(lp, l)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
93 self.assertEqual(int(100 * fp), int(100 * f))
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
94 self.assertEqual(int(100 * dp), int(100 * d))
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
95 self.assertEqual(tp, t)
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18070
diff changeset
96
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
97 def test_new_features(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
98 # Test some of the new features in detail
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
99 # (format, argument, big-endian result, little-endian result, asymmetric)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
100 tests = [
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
101 ('c', 'a', 'a', 'a', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
102 ('xc', 'a', '\0a', '\0a', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
103 ('cx', 'a', 'a\0', 'a\0', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
104 ('s', 'a', 'a', 'a', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
105 ('0s', 'helloworld', '', '', 1),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
106 ('1s', 'helloworld', 'h', 'h', 1),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
107 ('9s', 'helloworld', 'helloworl', 'helloworl', 1),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
108 ('10s', 'helloworld', 'helloworld', 'helloworld', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
109 ('11s', 'helloworld', 'helloworld\0', 'helloworld\0', 1),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
110 ('20s', 'helloworld', 'helloworld'+10*'\0', 'helloworld'+10*'\0', 1),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
111 ('b', 7, '\7', '\7', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
112 ('b', -7, '\371', '\371', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
113 ('B', 7, '\7', '\7', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
114 ('B', 249, '\371', '\371', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
115 ('h', 700, '\002\274', '\274\002', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
116 ('h', -700, '\375D', 'D\375', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
117 ('H', 700, '\002\274', '\274\002', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
118 ('H', 0x10000-700, '\375D', 'D\375', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
119 ('i', 70000000, '\004,\035\200', '\200\035,\004', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
120 ('i', -70000000, '\373\323\342\200', '\200\342\323\373', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
121 ('I', 70000000L, '\004,\035\200', '\200\035,\004', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
122 ('I', 0x100000000L-70000000, '\373\323\342\200', '\200\342\323\373', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
123 ('l', 70000000, '\004,\035\200', '\200\035,\004', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
124 ('l', -70000000, '\373\323\342\200', '\200\342\323\373', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
125 ('L', 70000000L, '\004,\035\200', '\200\035,\004', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
126 ('L', 0x100000000L-70000000, '\373\323\342\200', '\200\342\323\373', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
127 ('f', 2.0, '@\000\000\000', '\000\000\000@', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
128 ('d', 2.0, '@\000\000\000\000\000\000\000',
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
129 '\000\000\000\000\000\000\000@', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
130 ('f', -2.0, '\300\000\000\000', '\000\000\000\300', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
131 ('d', -2.0, '\300\000\000\000\000\000\000\000',
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
132 '\000\000\000\000\000\000\000\300', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
133 ('?', 0, '\0', '\0', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
134 ('?', 3, '\1', '\1', 1),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
135 ('?', True, '\1', '\1', 0),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
136 ('?', [], '\0', '\0', 1),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
137 ('?', (1,), '\1', '\1', 1),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
138 ]
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18070
diff changeset
139
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
140 for fmt, arg, big, lil, asy in tests:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
141 for (xfmt, exp) in [('>'+fmt, big), ('!'+fmt, big), ('<'+fmt, lil),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
142 ('='+fmt, ISBIGENDIAN and big or lil)]:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
143 res = struct.pack(xfmt, arg)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
144 self.assertEqual(res, exp)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
145 self.assertEqual(struct.calcsize(xfmt), len(res))
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
146 rev = struct.unpack(xfmt, res)[0]
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
147 if rev != arg:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52540
diff changeset
148 self.assertTrue(asy)
4847
d53812d68408 Exercise the new feature set somewhat.
Guido van Rossum <guido@python.org>
parents: 4706
diff changeset
149
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
150 def test_calcsize(self):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
151 expected_size = {
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
152 'b': 1, 'B': 1,
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
153 'h': 2, 'H': 2,
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
154 'i': 4, 'I': 4,
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
155 'l': 4, 'L': 4,
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
156 'q': 8, 'Q': 8,
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
157 }
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
158
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
159 # standard integer sizes
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
160 for code in integer_codes:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
161 for byteorder in ('=', '<', '>', '!'):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
162 format = byteorder+code
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
163 size = struct.calcsize(format)
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
164 self.assertEqual(size, expected_size[code])
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18070
diff changeset
165
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
166 # native integer sizes, except 'q' and 'Q'
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
167 for format_pair in ('bB', 'hH', 'iI', 'lL'):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
168 for byteorder in ['', '@']:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
169 signed_size = struct.calcsize(byteorder + format_pair[0])
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
170 unsigned_size = struct.calcsize(byteorder + format_pair[1])
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
171 self.assertEqual(signed_size, unsigned_size)
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18070
diff changeset
172
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
173 # bounds for native integer sizes
60303
460f41bd0e34 Use more specific assert* methods in test_struct.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60238
diff changeset
174 self.assertEqual(struct.calcsize('b'), 1)
460f41bd0e34 Use more specific assert* methods in test_struct.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60238
diff changeset
175 self.assertLessEqual(2, struct.calcsize('h'))
460f41bd0e34 Use more specific assert* methods in test_struct.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60238
diff changeset
176 self.assertLessEqual(4, struct.calcsize('l'))
460f41bd0e34 Use more specific assert* methods in test_struct.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60238
diff changeset
177 self.assertLessEqual(struct.calcsize('h'), struct.calcsize('i'))
460f41bd0e34 Use more specific assert* methods in test_struct.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60238
diff changeset
178 self.assertLessEqual(struct.calcsize('i'), struct.calcsize('l'))
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
179
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
180 # tests for native 'q' and 'Q' when applicable
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
181 if HAVE_LONG_LONG:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
182 self.assertEqual(struct.calcsize('q'), struct.calcsize('Q'))
60303
460f41bd0e34 Use more specific assert* methods in test_struct.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60238
diff changeset
183 self.assertLessEqual(8, struct.calcsize('q'))
460f41bd0e34 Use more specific assert* methods in test_struct.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60238
diff changeset
184 self.assertLessEqual(struct.calcsize('l'), struct.calcsize('q'))
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
185
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
186 def test_integers(self):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
187 # Integer tests (bBhHiIlLqQ).
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
188 import binascii
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18070
diff changeset
189
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
190 class IntTester(unittest.TestCase):
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
191 def __init__(self, format):
52540
11d6e538a279 The unittest.TestCase.assertEqual() now displays the differences in lists,
Gregory P. Smith <greg@mad-scientist.com>
parents: 51597
diff changeset
192 super(IntTester, self).__init__(methodName='test_one')
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
193 self.format = format
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
194 self.code = format[-1]
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
195 self.direction = format[:-1]
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
196 if not self.direction in ('', '@', '=', '<', '>', '!'):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
197 raise ValueError("unrecognized packing direction: %s" %
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
198 self.direction)
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
199 self.bytesize = struct.calcsize(format)
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
200 self.bitsize = self.bytesize * 8
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
201 if self.code in tuple('bhilq'):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
202 self.signed = True
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
203 self.min_value = -(2L**(self.bitsize-1))
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
204 self.max_value = 2L**(self.bitsize-1) - 1
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
205 elif self.code in tuple('BHILQ'):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
206 self.signed = False
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
207 self.min_value = 0
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
208 self.max_value = 2L**self.bitsize - 1
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
209 else:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
210 raise ValueError("unrecognized format code: %s" %
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
211 self.code)
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18070
diff changeset
212
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
213 def test_one(self, x, pack=struct.pack,
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
214 unpack=struct.unpack,
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
215 unhexlify=binascii.unhexlify):
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
216
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
217 format = self.format
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
218 if self.min_value <= x <= self.max_value:
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
219 expected = long(x)
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
220 if self.signed and x < 0:
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
221 expected += 1L << self.bitsize
60303
460f41bd0e34 Use more specific assert* methods in test_struct.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60238
diff changeset
222 self.assertGreaterEqual(expected, 0)
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
223 expected = '%x' % expected
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
224 if len(expected) & 1:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
225 expected = "0" + expected
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
226 expected = unhexlify(expected)
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
227 expected = ("\x00" * (self.bytesize - len(expected)) +
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
228 expected)
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
229 if (self.direction == '<' or
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
230 self.direction in ('', '@', '=') and not ISBIGENDIAN):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
231 expected = string_reverse(expected)
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
232 self.assertEqual(len(expected), self.bytesize)
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18070
diff changeset
233
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
234 # Pack work?
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
235 got = pack(format, x)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
236 self.assertEqual(got, expected)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
237
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
238 # Unpack work?
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
239 retrieved = unpack(format, got)[0]
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
240 self.assertEqual(x, retrieved)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
241
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
242 # Adding any byte should cause a "too big" error.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
243 self.assertRaises((struct.error, TypeError), unpack, format,
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
244 '\x01' + got)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
245 else:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
246 # x is out of range -- verify pack realizes that.
62933
28755eb53f38 Merged revisions 82777 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62902
diff changeset
247 self.assertRaises((OverflowError, ValueError, struct.error),
28755eb53f38 Merged revisions 82777 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62902
diff changeset
248 pack, format, x)
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18070
diff changeset
249
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
250 def run(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
251 from random import randrange
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
252
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
253 # Create all interesting powers of 2.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
254 values = []
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
255 for exp in range(self.bitsize + 3):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
256 values.append(1L << exp)
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18070
diff changeset
257
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
258 # Add some random values.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
259 for i in range(self.bitsize):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
260 val = 0L
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
261 for j in range(self.bytesize):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
262 val = (val << 8) | randrange(256)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
263 values.append(val)
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18070
diff changeset
264
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
265 # Values absorbed from other tests
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
266 values.extend([300, 700000, sys.maxint*4])
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
267
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
268 # Try all those, and their negations, and +-1 from
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
269 # them. Note that this tests all power-of-2
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
270 # boundaries in range, and a few out of range, plus
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
271 # +-(2**n +- 1).
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
272 for base in values:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
273 for val in -base, base:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
274 for incr in -1, 0, 1:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
275 x = val + incr
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
276 self.test_one(int(x))
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
277 self.test_one(long(x))
18111
099ae4918069 Generalize the new qQ std-mode tests to all int codes (bBhHiIlLqQ).
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
278
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
279 # Some error cases.
55121
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
280 class NotAnIntNS(object):
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
281 def __int__(self):
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
282 return 42
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
283
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
284 def __long__(self):
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
285 return 1729L
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
286
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
287 class NotAnIntOS:
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
288 def __int__(self):
59436
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
289 return 85
55121
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
290
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
291 def __long__(self):
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
292 return -163L
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
293
60308
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
294 # Objects with an '__index__' method should be allowed
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
295 # to pack as integers. That is assuming the implemented
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
296 # '__index__' method returns and 'int' or 'long'.
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
297 class Indexable(object):
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
298 def __init__(self, value):
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
299 self._value = value
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
300
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
301 def __index__(self):
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
302 return self._value
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
303
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
304 # If the '__index__' method raises a type error, then
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
305 # '__int__' should be used with a deprecation warning.
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
306 class BadIndex(object):
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
307 def __index__(self):
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
308 raise TypeError
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
309
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
310 def __int__(self):
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
311 return 42
19924
01f66f5db326 The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents: 18149
diff changeset
312
59774
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
313 self.assertRaises((TypeError, struct.error),
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
314 struct.pack, self.format,
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
315 "a string")
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
316 self.assertRaises((TypeError, struct.error),
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
317 struct.pack, self.format,
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
318 randrange)
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
319 with check_warnings(("integer argument expected, "
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
320 "got non-integer", DeprecationWarning)):
62902
d41a31f5bf82 this makes checking for warnings less error prone
Benjamin Peterson <benjamin@python.org>
parents: 62897
diff changeset
321 with self.assertRaises((TypeError, struct.error)):
d41a31f5bf82 this makes checking for warnings less error prone
Benjamin Peterson <benjamin@python.org>
parents: 62897
diff changeset
322 struct.pack(self.format, 3+42j)
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
323
59436
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
324 # an attempt to convert a non-integer (with an
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
325 # implicit conversion via __int__) should succeed,
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
326 # with a DeprecationWarning
60308
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
327 for nonint in NotAnIntNS(), NotAnIntOS(), BadIndex():
59774
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
328 with check_warnings((".*integer argument expected, got non"
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
329 "-integer", DeprecationWarning)) as w:
59436
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
330 got = struct.pack(self.format, nonint)
59774
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
331 lineno = inspect.currentframe().f_lineno - 1
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
332 self.assertEqual(w.filename, testmod_filename)
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
333 self.assertEqual(w.lineno, lineno)
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
334 self.assertEqual(len(w.warnings), 1)
59436
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
335 expected = struct.pack(self.format, int(nonint))
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
336 self.assertEqual(got, expected)
cfb442d702cd Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents: 59364
diff changeset
337
60308
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
338 # Check for legitimate values from '__index__'.
60238
0540a9628698 Issue #8300: Let struct.pack use __index__ to convert and pack non-integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 59774
diff changeset
339 for obj in (Indexable(0), Indexable(10), Indexable(17),
0540a9628698 Issue #8300: Let struct.pack use __index__ to convert and pack non-integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 59774
diff changeset
340 Indexable(42), Indexable(100), Indexable(127)):
0540a9628698 Issue #8300: Let struct.pack use __index__ to convert and pack non-integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 59774
diff changeset
341 try:
0540a9628698 Issue #8300: Let struct.pack use __index__ to convert and pack non-integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 59774
diff changeset
342 struct.pack(format, obj)
0540a9628698 Issue #8300: Let struct.pack use __index__ to convert and pack non-integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 59774
diff changeset
343 except:
0540a9628698 Issue #8300: Let struct.pack use __index__ to convert and pack non-integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 59774
diff changeset
344 self.fail("integer code pack failed on object "
0540a9628698 Issue #8300: Let struct.pack use __index__ to convert and pack non-integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 59774
diff changeset
345 "with '__index__' method")
0540a9628698 Issue #8300: Let struct.pack use __index__ to convert and pack non-integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 59774
diff changeset
346
60308
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
347 # Check for bogus values from '__index__'.
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
348 for obj in (Indexable('a'), Indexable(u'b'), Indexable(None),
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
349 Indexable({'a': 1}), Indexable([1, 2, 3])):
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
350 self.assertRaises((TypeError, struct.error),
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
351 struct.pack, self.format,
c399e93bf0e4 Issue #8300 (__index__ handling in struct.pack): Remove redundant check
Mark Dickinson <dickinsm@gmail.com>
parents: 60303
diff changeset
352 obj)
60238
0540a9628698 Issue #8300: Let struct.pack use __index__ to convert and pack non-integers.
Mark Dickinson <dickinsm@gmail.com>
parents: 59774
diff changeset
353
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
354 byteorders = '', '@', '=', '<', '>', '!'
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
355 for code in integer_codes:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
356 for byteorder in byteorders:
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
357 if (byteorder in ('', '@') and code in ('q', 'Q') and
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
358 not HAVE_LONG_LONG):
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
359 continue
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
360 format = byteorder+code
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
361 t = IntTester(format)
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
362 t.run()
19924
01f66f5db326 The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents: 18149
diff changeset
363
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
364 def test_p_code(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
365 # Test p ("Pascal string") code.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
366 for code, input, expected, expectedback in [
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
367 ('p','abc', '\x00', ''),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
368 ('1p', 'abc', '\x00', ''),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
369 ('2p', 'abc', '\x01a', 'a'),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
370 ('3p', 'abc', '\x02ab', 'ab'),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
371 ('4p', 'abc', '\x03abc', 'abc'),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
372 ('5p', 'abc', '\x03abc\x00', 'abc'),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
373 ('6p', 'abc', '\x03abc\x00\x00', 'abc'),
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
374 ('1000p', 'x'*1000, '\xff' + 'x'*999, 'x'*255)]:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
375 got = struct.pack(code, input)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
376 self.assertEqual(got, expected)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
377 (got,) = struct.unpack(code, got)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
378 self.assertEqual(got, expectedback)
28334
9f287e5eb037 SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
379
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
380 def test_705836(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
381 # SF bug 705836. "<f" and ">f" had a severe rounding bug, where a carry
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
382 # from the low-order discarded bits could propagate into the exponent
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
383 # field, causing the result to be wrong by a factor of 2.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
384 import math
28334
9f287e5eb037 SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
385
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
386 for base in range(1, 33):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
387 # smaller <- largest representable float less than base.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
388 delta = 0.5
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
389 while base - delta / 2.0 != base:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
390 delta /= 2.0
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
391 smaller = base - delta
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
392 # Packing this rounds away a solid string of trailing 1 bits.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
393 packed = struct.pack("<f", smaller)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
394 unpacked = struct.unpack("<f", packed)[0]
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
395 # This failed at base = 2, 4, and 32, with unpacked = 1, 2, and
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
396 # 16, respectively.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
397 self.assertEqual(base, unpacked)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
398 bigpacked = struct.pack(">f", smaller)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
399 self.assertEqual(bigpacked, string_reverse(packed))
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
400 unpacked = struct.unpack(">f", bigpacked)[0]
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
401 self.assertEqual(base, unpacked)
28334
9f287e5eb037 SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
402
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
403 # Largest finite IEEE single.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
404 big = (1 << 24) - 1
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
405 big = math.ldexp(big, 127 - 23)
28334
9f287e5eb037 SF bug 705836: struct.pack of floats in non-native endian order
Tim Peters <tim.peters@gmail.com>
parents: 28089
diff changeset
406 packed = struct.pack(">f", big)
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
407 unpacked = struct.unpack(">f", packed)[0]
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
408 self.assertEqual(big, unpacked)
38277
b825e6227366 refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents: 33599
diff changeset
409
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
410 # The same, but tack on a 1 bit so it rounds up to infinity.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
411 big = (1 << 25) - 1
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
412 big = math.ldexp(big, 127 - 24)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
413 self.assertRaises(OverflowError, struct.pack, ">f", big)
39380
9cb4565e60ca Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents: 38615
diff changeset
414
55121
1fc37fecaf8f Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents: 55002
diff changeset
415 def test_1530559(self):
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
416 # SF bug 1530559. struct.pack raises TypeError where it used to convert.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
417 for endian in ('', '>', '<'):
55150
897bded80020 Expand test coverage for struct.pack with native integer packing;
Mark Dickinson <dickinsm@gmail.com>
parents: 55146
diff changeset
418 for fmt in integer_codes:
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
419 self.check_float_coerce(endian + fmt, 1.0)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
420 self.check_float_coerce(endian + fmt, 1.5)
39380
9cb4565e60ca Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents: 38615
diff changeset
421
63649
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
422 def test_unpack_from(self, cls=str):
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
423 data = cls('abcd01234')
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
424 fmt = '4s'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
425 s = struct.Struct(fmt)
63649
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
426
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
427 self.assertEqual(s.unpack_from(data), ('abcd',))
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
428 self.assertEqual(struct.unpack_from(fmt, data), ('abcd',))
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
429 for i in xrange(6):
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
430 self.assertEqual(s.unpack_from(data, i), (data[i:i+4],))
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
431 self.assertEqual(struct.unpack_from(fmt, data, i), (data[i:i+4],))
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
432 for i in xrange(6, len(data) + 1):
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
433 self.assertRaises(struct.error, s.unpack_from, data, i)
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
434 self.assertRaises(struct.error, struct.unpack_from, fmt, data, i)
39380
9cb4565e60ca Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents: 38615
diff changeset
435
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
436 def test_pack_into(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
437 test_string = 'Reykjavik rocks, eow!'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
438 writable_buf = array.array('c', ' '*100)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
439 fmt = '21s'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
440 s = struct.Struct(fmt)
39380
9cb4565e60ca Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents: 38615
diff changeset
441
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
442 # Test without offset
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
443 s.pack_into(writable_buf, 0, test_string)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
444 from_buf = writable_buf.tostring()[:len(test_string)]
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
445 self.assertEqual(from_buf, test_string)
38545
21e8dd3e3945 Fixed struct test to not use unittest.
Martin Blais <blais@furius.ca>
parents: 38522
diff changeset
446
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
447 # Test with offset.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
448 s.pack_into(writable_buf, 10, test_string)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
449 from_buf = writable_buf.tostring()[:len(test_string)+10]
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
450 self.assertEqual(from_buf, test_string[:10] + test_string)
50143
4d2422989981 Issue #4228: Pack negative values the same way as 2.4
Martin v. Löwis <martin@v.loewis.de>
parents: 45390
diff changeset
451
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
452 # Go beyond boundaries.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
453 small_buf = array.array('c', ' '*10)
62857
a0082a74d125 Merged revisions 82637 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62848
diff changeset
454 self.assertRaises((ValueError, struct.error), s.pack_into, small_buf, 0,
a0082a74d125 Merged revisions 82637 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62848
diff changeset
455 test_string)
a0082a74d125 Merged revisions 82637 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62848
diff changeset
456 self.assertRaises((ValueError, struct.error), s.pack_into, small_buf, 2,
a0082a74d125 Merged revisions 82637 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62848
diff changeset
457 test_string)
50143
4d2422989981 Issue #4228: Pack negative values the same way as 2.4
Martin v. Löwis <martin@v.loewis.de>
parents: 45390
diff changeset
458
52909
f3b26a729e82 Merged revisions 69578-69580,69901,69907,69994,70022-70023,70025-70026,70166,70273,70275,70342,70386-70387,70389-70390,70392-70393,70395,70397,70400,70418 via svnmerge
Georg Brandl <georg@python.org>
parents: 50424
diff changeset
459 # Test bogus offset (issue 3694)
f3b26a729e82 Merged revisions 69578-69580,69901,69907,69994,70022-70023,70025-70026,70166,70273,70275,70342,70386-70387,70389-70390,70392-70393,70395,70397,70400,70418 via svnmerge
Georg Brandl <georg@python.org>
parents: 50424
diff changeset
460 sb = small_buf
62897
815ae7f3732b Merged revisions 82739,82741 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62891
diff changeset
461 self.assertRaises((TypeError, struct.error), struct.pack_into, b'', sb,
815ae7f3732b Merged revisions 82739,82741 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62891
diff changeset
462 None)
50143
4d2422989981 Issue #4228: Pack negative values the same way as 2.4
Martin v. Löwis <martin@v.loewis.de>
parents: 45390
diff changeset
463
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
464 def test_pack_into_fn(self):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
465 test_string = 'Reykjavik rocks, eow!'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
466 writable_buf = array.array('c', ' '*100)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
467 fmt = '21s'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
468 pack_into = lambda *args: struct.pack_into(fmt, *args)
38545
21e8dd3e3945 Fixed struct test to not use unittest.
Martin Blais <blais@furius.ca>
parents: 38522
diff changeset
469
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
470 # Test without offset.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
471 pack_into(writable_buf, 0, test_string)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
472 from_buf = writable_buf.tostring()[:len(test_string)]
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
473 self.assertEqual(from_buf, test_string)
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 38277
diff changeset
474
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
475 # Test with offset.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
476 pack_into(writable_buf, 10, test_string)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
477 from_buf = writable_buf.tostring()[:len(test_string)+10]
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
478 self.assertEqual(from_buf, test_string[:10] + test_string)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
479
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
480 # Go beyond boundaries.
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
481 small_buf = array.array('c', ' '*10)
62857
a0082a74d125 Merged revisions 82637 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62848
diff changeset
482 self.assertRaises((ValueError, struct.error), pack_into, small_buf, 0,
a0082a74d125 Merged revisions 82637 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62848
diff changeset
483 test_string)
a0082a74d125 Merged revisions 82637 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62848
diff changeset
484 self.assertRaises((ValueError, struct.error), pack_into, small_buf, 2,
a0082a74d125 Merged revisions 82637 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62848
diff changeset
485 test_string)
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 38277
diff changeset
486
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
487 def test_unpack_with_buffer(self):
59774
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
488 with check_py3k_warnings(("buffer.. not supported in 3.x",
63649
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
489 DeprecationWarning)):
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
490 # SF bug 1563759: struct.unpack doesn't support buffer protocol objects
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
491 data1 = array.array('B', '\x12\x34\x56\x78')
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
492 data2 = buffer('......\x12\x34\x56\x78......', 6, 4)
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
493 for data in [data1, data2]:
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
494 value, = struct.unpack('>I', data)
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
495 self.assertEqual(value, 0x12345678)
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
496
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
497 self.test_unpack_from(cls=buffer)
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 38277
diff changeset
498
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
499 def test_bool(self):
62848
e7da968e3e5a Merged revisions 82628,82630 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62252
diff changeset
500 class ExplodingBool(object):
e7da968e3e5a Merged revisions 82628,82630 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62252
diff changeset
501 def __nonzero__(self):
e7da968e3e5a Merged revisions 82628,82630 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62252
diff changeset
502 raise IOError
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
503 for prefix in tuple("<>!=")+('',):
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
504 false = (), [], [], '', 0
63649
2aca34460fdb Merged revisions 79156 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62284
diff changeset
505 true = [1], 'test', 5, -1, 0xffffffffL+1, 0xffffffff//2
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
506
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
507 falseFormat = prefix + '?' * len(false)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
508 packedFalse = struct.pack(falseFormat, *false)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
509 unpackedFalse = struct.unpack(falseFormat, packedFalse)
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18070
diff changeset
510
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
511 trueFormat = prefix + '?' * len(true)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
512 packedTrue = struct.pack(trueFormat, *true)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
513 unpackedTrue = struct.unpack(trueFormat, packedTrue)
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 38277
diff changeset
514
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
515 self.assertEqual(len(true), len(unpackedTrue))
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
516 self.assertEqual(len(false), len(unpackedFalse))
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 38277
diff changeset
517
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
518 for t in unpackedFalse:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
519 self.assertFalse(t)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
520 for t in unpackedTrue:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
521 self.assertTrue(t)
38353
36cd4a4ab8f7 Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents: 38277
diff changeset
522
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
523 packed = struct.pack(prefix+'?', 1)
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
524
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
525 self.assertEqual(len(packed), struct.calcsize(prefix+'?'))
38430
795d1cb1df01 Fix up struct docstrings, add struct.pack_to function for symmetry
Bob Ippolito <bob@redivi.com>
parents: 38411
diff changeset
526
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
527 if len(packed) != 1:
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
528 self.assertFalse(prefix, msg='encoded bool is not one byte: %r'
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
529 %packed)
38430
795d1cb1df01 Fix up struct docstrings, add struct.pack_to function for symmetry
Bob Ippolito <bob@redivi.com>
parents: 38411
diff changeset
530
62848
e7da968e3e5a Merged revisions 82628,82630 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62252
diff changeset
531 self.assertRaises(IOError, struct.pack, prefix + '?',
e7da968e3e5a Merged revisions 82628,82630 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62252
diff changeset
532 ExplodingBool())
38430
795d1cb1df01 Fix up struct docstrings, add struct.pack_to function for symmetry
Bob Ippolito <bob@redivi.com>
parents: 38411
diff changeset
533
62848
e7da968e3e5a Merged revisions 82628,82630 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62252
diff changeset
534 for c in [b'\x01', b'\x7f', b'\xff', b'\x0f', b'\xf0']:
e7da968e3e5a Merged revisions 82628,82630 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 62252
diff changeset
535 self.assertTrue(struct.unpack('>?', c)[0])
19924
01f66f5db326 The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents: 18149
diff changeset
536
59774
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
537 @unittest.skipUnless(IS32BIT, "Specific to 32bit machines")
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
538 def test_crasher(self):
4f719be7c875 Cleanup test_struct using check_warnings.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59436
diff changeset
539 self.assertRaises(MemoryError, struct.pack, "357913941c", "a")
38430
795d1cb1df01 Fix up struct docstrings, add struct.pack_to function for symmetry
Bob Ippolito <bob@redivi.com>
parents: 38411
diff changeset
540
62284
80110fd43191 Merged revisions 81904 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 60392
diff changeset
541 def test_count_overflow(self):
62252
7db7f8442414 Fix possible undefined behaviour from signed overflow in struct module.
Mark Dickinson <dickinsm@gmail.com>
parents: 60308
diff changeset
542 hugecount = '{}b'.format(sys.maxsize+1)
62284
80110fd43191 Merged revisions 81904 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 60392
diff changeset
543 self.assertRaises(struct.error, struct.calcsize, hugecount)
38574
6f7404f3fc57 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38545
diff changeset
544
62252
7db7f8442414 Fix possible undefined behaviour from signed overflow in struct module.
Mark Dickinson <dickinsm@gmail.com>
parents: 60308
diff changeset
545 hugecount2 = '{}b{}H'.format(sys.maxsize//2, sys.maxsize//2)
62284
80110fd43191 Merged revisions 81904 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 60392
diff changeset
546 self.assertRaises(struct.error, struct.calcsize, hugecount2)
19924
01f66f5db326 The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents: 18149
diff changeset
547
78317
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 78311
diff changeset
548 def check_sizeof(self, format_str, number_of_codes):
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 78311
diff changeset
549 # The size of 'PyStructObject'
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 78311
diff changeset
550 totalsize = support.calcobjsize('5P')
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 78311
diff changeset
551 # The size taken up by the 'formatcode' dynamic array
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 78311
diff changeset
552 totalsize += struct.calcsize('3P') * (number_of_codes + 1)
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 78311
diff changeset
553 support.check_sizeof(self, struct.Struct(format_str), totalsize)
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 78311
diff changeset
554
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 78311
diff changeset
555 @support.cpython_only
78311
37554bda2014 Issue #15402: Simplify Struct.__sizeof__ and make tests more precise.
Meador Inge <meadori@gmail.com>
parents: 78258
diff changeset
556 def test__sizeof__(self):
37554bda2014 Issue #15402: Simplify Struct.__sizeof__ and make tests more precise.
Meador Inge <meadori@gmail.com>
parents: 78258
diff changeset
557 for code in integer_codes:
37554bda2014 Issue #15402: Simplify Struct.__sizeof__ and make tests more precise.
Meador Inge <meadori@gmail.com>
parents: 78258
diff changeset
558 self.check_sizeof(code, 1)
37554bda2014 Issue #15402: Simplify Struct.__sizeof__ and make tests more precise.
Meador Inge <meadori@gmail.com>
parents: 78258
diff changeset
559 self.check_sizeof('BHILfdspP', 9)
37554bda2014 Issue #15402: Simplify Struct.__sizeof__ and make tests more precise.
Meador Inge <meadori@gmail.com>
parents: 78258
diff changeset
560 self.check_sizeof('B' * 1234, 1234)
37554bda2014 Issue #15402: Simplify Struct.__sizeof__ and make tests more precise.
Meador Inge <meadori@gmail.com>
parents: 78258
diff changeset
561 self.check_sizeof('fd', 2)
37554bda2014 Issue #15402: Simplify Struct.__sizeof__ and make tests more precise.
Meador Inge <meadori@gmail.com>
parents: 78258
diff changeset
562 self.check_sizeof('xxxxxxxxxxxxxx', 0)
37554bda2014 Issue #15402: Simplify Struct.__sizeof__ and make tests more precise.
Meador Inge <meadori@gmail.com>
parents: 78258
diff changeset
563 self.check_sizeof('100H', 100)
37554bda2014 Issue #15402: Simplify Struct.__sizeof__ and make tests more precise.
Meador Inge <meadori@gmail.com>
parents: 78258
diff changeset
564 self.check_sizeof('187s', 1)
37554bda2014 Issue #15402: Simplify Struct.__sizeof__ and make tests more precise.
Meador Inge <meadori@gmail.com>
parents: 78258
diff changeset
565 self.check_sizeof('20p', 1)
37554bda2014 Issue #15402: Simplify Struct.__sizeof__ and make tests more precise.
Meador Inge <meadori@gmail.com>
parents: 78258
diff changeset
566 self.check_sizeof('0s', 1)
37554bda2014 Issue #15402: Simplify Struct.__sizeof__ and make tests more precise.
Meador Inge <meadori@gmail.com>
parents: 78258
diff changeset
567 self.check_sizeof('0c', 0)
78255
dbe7f39ff341 Issue #15402: Add a __sizeof__ method to struct.Struct.
Meador Inge <meadori@gmail.com>
parents: 68256
diff changeset
568
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
569 def test_main():
78317
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 78311
diff changeset
570 support.run_unittest(StructTest)
38545
21e8dd3e3945 Fixed struct test to not use unittest.
Martin Blais <blais@furius.ca>
parents: 38522
diff changeset
571
47575
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
572 if __name__ == '__main__':
da307beeebb9 convert test_struct to a unittest thanks to Giampaolo Rodola
Benjamin Peterson <benjamin@python.org>
parents: 45759
diff changeset
573 test_main()