summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_struct.py
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2010-04-03 14:05:10 (GMT)
committerMark Dickinson <dickinsm@gmail.com>2010-04-03 14:05:10 (GMT)
commit4846a8e828e345772de53aa410fcabe6a4aae772 (patch)
tree03d81d42998c42f59482f15001446c36ba70211e /Lib/test/test_struct.py
parentae509520de5a0321f58c79afffad10ae59dae8b9 (diff)
downloadcpython-4846a8e828e345772de53aa410fcabe6a4aae772.zip
cpython-4846a8e828e345772de53aa410fcabe6a4aae772.tar.gz
cpython-4846a8e828e345772de53aa410fcabe6a4aae772.tar.bz2
Issue #8300: Let struct.pack use __index__ to convert and pack non-integers.
Based on a patch by Meador Inge.
Diffstat (limited to 'Lib/test/test_struct.py')
-rw-r--r--Lib/test/test_struct.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py
index 6d2a95a..4329e95 100644
--- a/Lib/test/test_struct.py
+++ b/Lib/test/test_struct.py
@@ -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: