Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b40b947

Browse files
committed
Issue #5463: Remove _PY_STRUCT_RANGE_CHECKING constant from struct
module, and remove associated code from test_struct. This was a mechanism for skipping some of the tests for overflow behaviour when packing integers; it's no longer necessary.
1 parent 4feda2a commit b40b947

4 files changed

Lines changed: 39 additions & 50 deletions

File tree

Doc/library/struct.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ make it fit. For unpacking, the resulting bytes object always has exactly the
134134
specified number of bytes. As a special case, ``'0s'`` means a single, empty
135135
string (while ``'0c'`` means 0 characters).
136136

137+
When packing a value ``x`` using one of the integer formats (``'b'``,
138+
``'B'``, ``'h'``, ``'H'``, ``'i'``, ``'I'``, ``'l'``, ``'L'``,
139+
``'q'``, ``'Q'``), if ``x`` is outside the valid range for that format
140+
then :exc:`struct.error` is raised.
141+
142+
.. versionchanged:: 3.1
143+
In 3.0, some of the integer formats wrapped out-of-range values and
144+
raised :exc:`DeprecationWarning` instead of :exc:`struct.error`.
145+
146+
137147
The ``'p'`` format character encodes a "Pascal string", meaning a short
138148
variable-length string stored in a fixed number of bytes. The count is the total
139149
number of bytes stored. The first byte stored is the length of the string, or

Lib/test/test_struct.py

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
try:
1515
import _struct
1616
except ImportError:
17-
PY_STRUCT_RANGE_CHECKING = 0
1817
PY_STRUCT_FLOAT_COERCE = 2
1918
else:
20-
PY_STRUCT_RANGE_CHECKING = getattr(_struct, '_PY_STRUCT_RANGE_CHECKING', 0)
2119
PY_STRUCT_FLOAT_COERCE = getattr(_struct, '_PY_STRUCT_FLOAT_COERCE', 0)
2220

2321
def string_reverse(s):
@@ -40,20 +38,6 @@ def decorator(*args, **kw):
4038
return func(*args, **kw)
4139
return decorator
4240

43-
@with_warning_restore
44-
def deprecated_err(func, *args):
45-
try:
46-
func(*args)
47-
except (struct.error, OverflowError):
48-
pass
49-
except DeprecationWarning:
50-
raise TestFailed("%s%s expected to raise DeprecationWarning" % (
51-
func.__name__, args))
52-
else:
53-
raise TestFailed("%s%s did not raise error" % (
54-
func.__name__, args))
55-
56-
5741
class StructTest(unittest.TestCase):
5842

5943
@with_warning_restore
@@ -222,12 +206,6 @@ def test_standard_integers(self):
222206

223207
class IntTester(unittest.TestCase):
224208

225-
# XXX Most std integer modes fail to test for out-of-range.
226-
# The "i" and "l" codes appear to range-check OK on 32-bit boxes, but
227-
# fail to check correctly on some 64-bit ones (Tru64 Unix + Compaq C
228-
# reported by Mark Favas).
229-
BUGGY_RANGE_CHECK = "bBhHiIlL"
230-
231209
def __init__(self, formatpair, bytesize):
232210
self.assertEqual(len(formatpair), 2)
233211
self.formatpair = formatpair
@@ -291,12 +269,10 @@ def test_one(self, x, pack=struct.pack,
291269

292270
else:
293271
# x is out of range -- verify pack realizes that.
294-
if not PY_STRUCT_RANGE_CHECKING and code in self.BUGGY_RANGE_CHECK:
295-
if verbose:
296-
print("Skipping buggy range check for code", code)
297-
else:
298-
deprecated_err(pack, ">" + code, x)
299-
deprecated_err(pack, "<" + code, x)
272+
self.assertRaises((struct.error, OverflowError),
273+
pack, ">" + code, x)
274+
self.assertRaises((struct.error, OverflowError),
275+
pack, "<" + code, x)
300276

301277
# Much the same for unsigned.
302278
code = self.unsigned_code
@@ -340,12 +316,10 @@ def test_one(self, x, pack=struct.pack,
340316

341317
else:
342318
# x is out of range -- verify pack realizes that.
343-
if not PY_STRUCT_RANGE_CHECKING and code in self.BUGGY_RANGE_CHECK:
344-
if verbose:
345-
print("Skipping buggy range check for code", code)
346-
else:
347-
deprecated_err(pack, ">" + code, x)
348-
deprecated_err(pack, "<" + code, x)
319+
self.assertRaises((struct.error, OverflowError),
320+
pack, ">" + code, x)
321+
self.assertRaises((struct.error, OverflowError),
322+
pack, "<" + code, x)
349323

350324
def run(self):
351325
from random import randrange
@@ -443,20 +417,24 @@ def test_705836(self):
443417
big = math.ldexp(big, 127 - 24)
444418
self.assertRaises(OverflowError, struct.pack, ">f", big)
445419

446-
if PY_STRUCT_RANGE_CHECKING:
447-
def test_1229380(self):
448-
# SF bug 1229380. No struct.pack exception for some out of
449-
# range integers
450-
import sys
451-
for endian in ('', '>', '<'):
452-
for fmt in ('B', 'H', 'I', 'L'):
453-
deprecated_err(struct.pack, endian + fmt, -1)
454-
455-
deprecated_err(struct.pack, endian + 'B', 300)
456-
deprecated_err(struct.pack, endian + 'H', 70000)
457-
458-
deprecated_err(struct.pack, endian + 'I', sys.maxsize * 4)
459-
deprecated_err(struct.pack, endian + 'L', sys.maxsize * 4)
420+
def test_1229380(self):
421+
# SF bug 1229380. No struct.pack exception for some out of
422+
# range integers
423+
import sys
424+
for endian in ('', '>', '<'):
425+
for fmt in ('B', 'H', 'I', 'L'):
426+
self.assertRaises((struct.error, OverflowError), struct.pack,
427+
endian + fmt, -1)
428+
429+
self.assertRaises((struct.error, OverflowError), struct.pack,
430+
endian + 'B', 300)
431+
self.assertRaises((struct.error, OverflowError), struct.pack,
432+
endian + 'H', 70000)
433+
434+
self.assertRaises((struct.error, OverflowError), struct.pack,
435+
endian + 'I', sys.maxsize * 4)
436+
self.assertRaises((struct.error, OverflowError), struct.pack,
437+
endian + 'L', sys.maxsize * 4)
460438

461439
def XXXtest_1530559(self):
462440
# XXX This is broken: see the bug report

Misc/NEWS

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ Extension Modules
7373

7474
- Issue #5463: In struct module, remove deprecated overflow wrapping
7575
when packing an integer: struct.pack('=L', -1) now raises
76-
struct.error instead of returning b'\xff\xff\xff\xff'.
76+
struct.error instead of returning b'\xff\xff\xff\xff'. The
77+
_PY_STRUCT_RANGE_CHECKING and _PY_STRUCT_OVERFLOW_MASKING constants
78+
have been removed from the struct module.
7779

7880

7981
What's New in Python 3.1 alpha 1

Modules/_struct.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2028,7 +2028,6 @@ PyInit__struct(void)
20282028

20292029
PyModule_AddObject(m, "__version__", ver);
20302030

2031-
PyModule_AddIntConstant(m, "_PY_STRUCT_RANGE_CHECKING", 1);
20322031
#ifdef PY_STRUCT_FLOAT_COERCE
20332032
PyModule_AddIntConstant(m, "_PY_STRUCT_FLOAT_COERCE", 1);
20342033
#endif

0 commit comments

Comments
 (0)