Issue #5463: Remove deprecated float coercion from struct module, along

with the _PY_STRUCT_FLOAT_COERCE constant.  Simplify tests accordingly,
and reenable (now-fixed) broken tests.
This commit is contained in:
Mark Dickinson 2009-04-19 20:40:33 +00:00
parent 769ba47bb2
commit ea835e7a87
3 changed files with 71 additions and 135 deletions

View file

@ -11,13 +11,6 @@ ISBIGENDIAN = sys.byteorder == "big"
IS32BIT = sys.maxsize == 0x7fffffff
del sys
try:
import _struct
except ImportError:
PY_STRUCT_FLOAT_COERCE = 2
else:
PY_STRUCT_FLOAT_COERCE = getattr(_struct, '_PY_STRUCT_FLOAT_COERCE', 0)
def string_reverse(s):
return s[::-1]
@ -27,40 +20,7 @@ def bigendian_to_native(value):
else:
return string_reverse(value)
def with_warning_restore(func):
@wraps(func)
def decorator(*args, **kw):
with warnings.catch_warnings():
# We need this function to warn every time, so stick an
# unqualifed 'always' at the head of the filter list
warnings.simplefilter("always")
warnings.filterwarnings("error", category=DeprecationWarning)
return func(*args, **kw)
return decorator
class StructTest(unittest.TestCase):
@with_warning_restore
def check_float_coerce(self, format, number):
# SF bug 1530559. struct.pack raises TypeError where it used to convert.
if PY_STRUCT_FLOAT_COERCE == 2:
# Test for pre-2.5 struct module
packed = struct.pack(format, number)
floored = struct.unpack(format, packed)[0]
self.assertEqual(floored, int(number),
"did not correcly coerce float to int")
return
try:
struct.pack(format, number)
except (struct.error, TypeError):
if PY_STRUCT_FLOAT_COERCE:
self.fail("expected DeprecationWarning for float coerce")
except DeprecationWarning:
if not PY_STRUCT_FLOAT_COERCE:
self.fail("expected to raise struct.error for float coerce")
else:
self.fail("did not raise error for float coerce")
def test_isbigendian(self):
self.assertEqual((struct.pack('=i', 1)[0] == 0), ISBIGENDIAN)
@ -270,10 +230,8 @@ class StructTest(unittest.TestCase):
else:
# x is out of range -- verify pack realizes that.
self.assertRaises((struct.error, OverflowError),
pack, ">" + code, x)
self.assertRaises((struct.error, OverflowError),
pack, "<" + code, x)
self.assertRaises(struct.error, pack, ">" + code, x)
self.assertRaises(struct.error, pack, "<" + code, x)
# Much the same for unsigned.
code = self.unsigned_code
@ -317,10 +275,8 @@ class StructTest(unittest.TestCase):
else:
# x is out of range -- verify pack realizes that.
self.assertRaises((struct.error, OverflowError),
pack, ">" + code, x)
self.assertRaises((struct.error, OverflowError),
pack, "<" + code, x)
self.assertRaises(struct.error, pack, ">" + code, x)
self.assertRaises(struct.error, pack, "<" + code, x)
def run(self):
from random import randrange
@ -353,10 +309,10 @@ class StructTest(unittest.TestCase):
# Some error cases.
for direction in "<>":
for code in self.formatpair:
for badobject in "a string", 3+42j, randrange:
self.assertRaises((struct.error, TypeError),
struct.pack, direction + code,
badobject)
for badobject in "a string", 3+42j, randrange, -1729.0:
self.assertRaises(struct.error,
struct.pack, direction + code,
badobject)
for args in [("bB", 1),
("hH", 2),
@ -437,13 +393,14 @@ class StructTest(unittest.TestCase):
self.assertRaises((struct.error, OverflowError), struct.pack,
endian + 'L', sys.maxsize * 4)
def XXXtest_1530559(self):
# XXX This is broken: see the bug report
# SF bug 1530559. struct.pack raises TypeError where it used to convert.
def test_1530559(self):
for endian in ('', '>', '<'):
for fmt in ('B', 'H', 'I', 'L', 'b', 'h', 'i', 'l'):
self.check_float_coerce(endian + fmt, 1.0)
self.check_float_coerce(endian + fmt, 1.5)
for fmt in ('B', 'H', 'I', 'L', 'Q', 'b', 'h', 'i', 'l', 'q'):
self.assertRaises(struct.error, struct.pack, endian + fmt, 1.0)
self.assertRaises(struct.error, struct.pack, endian + fmt, 1.5)
self.assertRaises(struct.error, struct.pack, 'P', 1.0)
self.assertRaises(struct.error, struct.pack, 'P', 1.5)
def test_unpack_from(self):
test_string = b'abcd01234'