diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2009-07-05 10:01:24 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2009-07-05 10:01:24 (GMT) |
commit | 463dc4bf26c37c5f90cfe9328087976a0bdc5757 (patch) | |
tree | 5d8a0bcbd0a219f46f60b8125d9203c4dc4e22fa /Lib/test/test_struct.py | |
parent | 5b1abb7bb0553c0f9ed41bf75d1003eb47a86013 (diff) | |
download | cpython-463dc4bf26c37c5f90cfe9328087976a0bdc5757.zip cpython-463dc4bf26c37c5f90cfe9328087976a0bdc5757.tar.gz cpython-463dc4bf26c37c5f90cfe9328087976a0bdc5757.tar.bz2 |
Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
integer packing, and reenable some previously broken tests.
Diffstat (limited to 'Lib/test/test_struct.py')
-rw-r--r-- | Lib/test/test_struct.py | 51 |
1 files changed, 26 insertions, 25 deletions
diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index ef05e3c..61f48d6 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -73,7 +73,7 @@ class StructTest(unittest.TestCase): return try: struct.pack(format, number) - except (struct.error, TypeError): + except struct.error: if PY_STRUCT_FLOAT_COERCE: self.fail("expected DeprecationWarning for float coerce") except DeprecationWarning: @@ -220,12 +220,6 @@ class StructTest(unittest.TestCase): class IntTester(unittest.TestCase): - # XXX Most std integer modes fail to test for out-of-range. - # The "i" and "l" codes appear to range-check OK on 32-bit boxes, but - # fail to check correctly on some 64-bit ones (Tru64 Unix + Compaq C - # reported by Mark Favas). - BUGGY_RANGE_CHECK = "bBhHiIlL" - def __init__(self, formatpair, bytesize): super(IntTester, self).__init__(methodName='test_one') self.assertEqual(len(formatpair), 2) @@ -289,12 +283,8 @@ class StructTest(unittest.TestCase): else: # x is out of range -- verify pack realizes that. - if not PY_STRUCT_RANGE_CHECKING and code in self.BUGGY_RANGE_CHECK: - if verbose: - print "Skipping buggy range check for code", code - else: - deprecated_err(pack, ">" + code, x) - deprecated_err(pack, "<" + code, x) + deprecated_err(pack, ">" + code, x) + deprecated_err(pack, "<" + code, x) # Much the same for unsigned. code = self.unsigned_code @@ -338,12 +328,8 @@ class StructTest(unittest.TestCase): else: # x is out of range -- verify pack realizes that. - if not PY_STRUCT_RANGE_CHECKING and code in self.BUGGY_RANGE_CHECK: - if verbose: - print "Skipping buggy range check for code", code - else: - deprecated_err(pack, ">" + code, x) - deprecated_err(pack, "<" + code, x) + deprecated_err(pack, ">" + code, x) + deprecated_err(pack, "<" + code, x) def run(self): from random import randrange @@ -374,10 +360,25 @@ class StructTest(unittest.TestCase): self.test_one(x) # Some error cases. + class NotAnIntNS(object): + def __int__(self): + return 42 + + def __long__(self): + return 1729L + + class NotAnIntOS: + def __int__(self): + return 10585 + + def __long__(self): + return -163L + for direction in "<>": for code in self.formatpair: - for badobject in "a string", 3+42j, randrange: - self.assertRaises((struct.error, TypeError), + for badobject in ("a string", 3+42j, randrange, + NotAnIntNS(), NotAnIntOS()): + self.assertRaises(struct.error, struct.pack, direction + code, badobject) @@ -447,7 +448,7 @@ class StructTest(unittest.TestCase): import sys for endian in ('', '>', '<'): for cls in (int, long): - for fmt in ('B', 'H', 'I', 'L'): + for fmt in ('B', 'H', 'I', 'L', 'Q'): deprecated_err(struct.pack, endian + fmt, cls(-1)) deprecated_err(struct.pack, endian + 'B', cls(300)) @@ -455,12 +456,12 @@ class StructTest(unittest.TestCase): deprecated_err(struct.pack, endian + 'I', sys.maxint * 4L) deprecated_err(struct.pack, endian + 'L', sys.maxint * 4L) + deprecated_err(struct.pack, endian + 'Q', 2**64) - def XXXtest_1530559(self): - # XXX This is broken: see the bug report + def test_1530559(self): # SF bug 1530559. struct.pack raises TypeError where it used to convert. for endian in ('', '>', '<'): - for fmt in ('B', 'H', 'I', 'L', 'b', 'h', 'i', 'l'): + for fmt in ('B', 'H', 'I', 'L', 'Q', 'b', 'h', 'i', 'l', 'q'): self.check_float_coerce(endian + fmt, 1.0) self.check_float_coerce(endian + fmt, 1.5) |