diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2010-04-04 08:43:04 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2010-04-04 08:43:04 (GMT) |
commit | fdaaa9c9d8a768c324a49250c91295ebce6b201a (patch) | |
tree | 2a3cbe6e2caac85e72fd9f1622a531828cbb2401 /Lib/test/test_struct.py | |
parent | 0d57caa2678fcb5c08a247a9ed17c73b311c7c00 (diff) | |
download | cpython-fdaaa9c9d8a768c324a49250c91295ebce6b201a.zip cpython-fdaaa9c9d8a768c324a49250c91295ebce6b201a.tar.gz cpython-fdaaa9c9d8a768c324a49250c91295ebce6b201a.tar.bz2 |
Issue #8300 (__index__ handling in struct.pack): Remove redundant check
and improve test coverage. Thanks Meador Inge for the patch.
Diffstat (limited to 'Lib/test/test_struct.py')
-rw-r--r-- | Lib/test/test_struct.py | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index 3e7b259..aeb52f8 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -289,6 +289,25 @@ class StructTest(unittest.TestCase): def __long__(self): return -163L + # Objects with an '__index__' method should be allowed + # to pack as integers. That is assuming the implemented + # '__index__' method returns and 'int' or 'long'. + class Indexable(object): + def __init__(self, value): + self._value = value + + def __index__(self): + return self._value + + # If the '__index__' method raises a type error, then + # '__int__' should be used with a deprecation warning. + class BadIndex(object): + def __index__(self): + raise TypeError + + def __int__(self): + return 42 + self.assertRaises((TypeError, struct.error), struct.pack, self.format, "a string") @@ -304,7 +323,7 @@ class StructTest(unittest.TestCase): # an attempt to convert a non-integer (with an # implicit conversion via __int__) should succeed, # with a DeprecationWarning - for nonint in NotAnIntNS(), NotAnIntOS(): + for nonint in NotAnIntNS(), NotAnIntOS(), BadIndex(): with check_warnings((".*integer argument expected, got non" "-integer", DeprecationWarning)) as w: got = struct.pack(self.format, nonint) @@ -315,15 +334,7 @@ class StructTest(unittest.TestCase): expected = struct.pack(self.format, int(nonint)) self.assertEqual(got, expected) - # Objects with an '__index__' method should be allowed - # to pack as integers. - class Indexable(object): - def __init__(self, value): - self._value = value - - def __index__(self): - return self._value - + # Check for legitimate values from '__index__'. for obj in (Indexable(0), Indexable(10), Indexable(17), Indexable(42), Indexable(100), Indexable(127)): try: @@ -332,6 +343,12 @@ class StructTest(unittest.TestCase): self.fail("integer code pack failed on object " "with '__index__' method") + # Check for bogus values from '__index__'. + for obj in (Indexable('a'), Indexable(u'b'), Indexable(None), + Indexable({'a': 1}), Indexable([1, 2, 3])): + self.assertRaises((TypeError, struct.error), + struct.pack, self.format, + obj) byteorders = '', '@', '=', '<', '>', '!' for code in integer_codes: |