1111IS32BIT = sys .maxsize == 0x7fffffff
1212del sys
1313
14- try :
15- import _struct
16- except ImportError :
17- PY_STRUCT_FLOAT_COERCE = 2
18- else :
19- PY_STRUCT_FLOAT_COERCE = getattr (_struct , '_PY_STRUCT_FLOAT_COERCE' , 0 )
20-
2114def string_reverse (s ):
2215 return s [::- 1 ]
2316
@@ -27,40 +20,7 @@ def bigendian_to_native(value):
2720 else :
2821 return string_reverse (value )
2922
30- def with_warning_restore (func ):
31- @wraps (func )
32- def decorator (* args , ** kw ):
33- with warnings .catch_warnings ():
34- # We need this function to warn every time, so stick an
35- # unqualifed 'always' at the head of the filter list
36- warnings .simplefilter ("always" )
37- warnings .filterwarnings ("error" , category = DeprecationWarning )
38- return func (* args , ** kw )
39- return decorator
40-
4123class StructTest (unittest .TestCase ):
42-
43- @with_warning_restore
44- def check_float_coerce (self , format , number ):
45- # SF bug 1530559. struct.pack raises TypeError where it used to convert.
46- if PY_STRUCT_FLOAT_COERCE == 2 :
47- # Test for pre-2.5 struct module
48- packed = struct .pack (format , number )
49- floored = struct .unpack (format , packed )[0 ]
50- self .assertEqual (floored , int (number ),
51- "did not correcly coerce float to int" )
52- return
53- try :
54- struct .pack (format , number )
55- except (struct .error , TypeError ):
56- if PY_STRUCT_FLOAT_COERCE :
57- self .fail ("expected DeprecationWarning for float coerce" )
58- except DeprecationWarning :
59- if not PY_STRUCT_FLOAT_COERCE :
60- self .fail ("expected to raise struct.error for float coerce" )
61- else :
62- self .fail ("did not raise error for float coerce" )
63-
6424 def test_isbigendian (self ):
6525 self .assertEqual ((struct .pack ('=i' , 1 )[0 ] == 0 ), ISBIGENDIAN )
6626
@@ -270,10 +230,8 @@ def test_one(self, x, pack=struct.pack,
270230
271231 else :
272232 # x is out of range -- verify pack realizes that.
273- self .assertRaises ((struct .error , OverflowError ),
274- pack , ">" + code , x )
275- self .assertRaises ((struct .error , OverflowError ),
276- pack , "<" + code , x )
233+ self .assertRaises (struct .error , pack , ">" + code , x )
234+ self .assertRaises (struct .error , pack , "<" + code , x )
277235
278236 # Much the same for unsigned.
279237 code = self .unsigned_code
@@ -317,10 +275,8 @@ def test_one(self, x, pack=struct.pack,
317275
318276 else :
319277 # x is out of range -- verify pack realizes that.
320- self .assertRaises ((struct .error , OverflowError ),
321- pack , ">" + code , x )
322- self .assertRaises ((struct .error , OverflowError ),
323- pack , "<" + code , x )
278+ self .assertRaises (struct .error , pack , ">" + code , x )
279+ self .assertRaises (struct .error , pack , "<" + code , x )
324280
325281 def run (self ):
326282 from random import randrange
@@ -353,10 +309,10 @@ def run(self):
353309 # Some error cases.
354310 for direction in "<>" :
355311 for code in self .formatpair :
356- for badobject in "a string" , 3 + 42j , randrange :
357- self .assertRaises (( struct .error , TypeError ) ,
358- struct .pack , direction + code ,
359- badobject )
312+ for badobject in "a string" , 3 + 42j , randrange , - 1729.0 :
313+ self .assertRaises (struct .error ,
314+ struct .pack , direction + code ,
315+ badobject )
360316
361317 for args in [("bB" , 1 ),
362318 ("hH" , 2 ),
@@ -437,13 +393,14 @@ def test_1229380(self):
437393 self .assertRaises ((struct .error , OverflowError ), struct .pack ,
438394 endian + 'L' , sys .maxsize * 4 )
439395
440- def XXXtest_1530559 (self ):
441- # XXX This is broken: see the bug report
442- # SF bug 1530559. struct.pack raises TypeError where it used to convert.
396+ def test_1530559 (self ):
443397 for endian in ('' , '>' , '<' ):
444- for fmt in ('B' , 'H' , 'I' , 'L' , 'b' , 'h' , 'i' , 'l' ):
445- self .check_float_coerce (endian + fmt , 1.0 )
446- self .check_float_coerce (endian + fmt , 1.5 )
398+ for fmt in ('B' , 'H' , 'I' , 'L' , 'Q' , 'b' , 'h' , 'i' , 'l' , 'q' ):
399+ self .assertRaises (struct .error , struct .pack , endian + fmt , 1.0 )
400+ self .assertRaises (struct .error , struct .pack , endian + fmt , 1.5 )
401+ self .assertRaises (struct .error , struct .pack , 'P' , 1.0 )
402+ self .assertRaises (struct .error , struct .pack , 'P' , 1.5 )
403+
447404
448405 def test_unpack_from (self ):
449406 test_string = b'abcd01234'
0 commit comments