mirror of
https://github.com/python/cpython.git
synced 2025-07-29 06:05:00 +00:00
Issue #8300: Let struct.pack use __index__ to convert and pack non-integers.
Based on a patch by Meador Inge.
This commit is contained in:
parent
ae509520de
commit
4846a8e828
4 changed files with 70 additions and 14 deletions
|
@ -315,6 +315,24 @@ 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
|
||||
|
||||
for obj in (Indexable(0), Indexable(10), Indexable(17),
|
||||
Indexable(42), Indexable(100), Indexable(127)):
|
||||
try:
|
||||
struct.pack(format, obj)
|
||||
except:
|
||||
self.fail("integer code pack failed on object "
|
||||
"with '__index__' method")
|
||||
|
||||
|
||||
byteorders = '', '@', '=', '<', '>', '!'
|
||||
for code in integer_codes:
|
||||
for byteorder in byteorders:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue