[Python-checkins] r79746 - in python/branches/py3k: Lib/test/test_struct.py Modules/_struct.c
mark.dickinson
python-checkins at python.org
Sun Apr 4 10:52:51 CEST 2010
Author: mark.dickinson
Date: Sun Apr 4 10:52:51 2010
New Revision: 79746
Log:
Merged revisions 79745 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r79745 | mark.dickinson | 2010-04-04 09:43:04 +0100 (Sun, 04 Apr 2010) | 3 lines
Issue #8300 (__index__ handling in struct.pack): Remove redundant check
and improve test coverage. Thanks Meador Inge for the patch.
........
Modified:
python/branches/py3k/ (props changed)
python/branches/py3k/Lib/test/test_struct.py
python/branches/py3k/Modules/_struct.c
Modified: python/branches/py3k/Lib/test/test_struct.py
==============================================================================
--- python/branches/py3k/Lib/test/test_struct.py (original)
+++ python/branches/py3k/Lib/test/test_struct.py Sun Apr 4 10:52:51 2010
@@ -269,6 +269,25 @@
def __int__(self):
return 42
+ # 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")
@@ -280,17 +299,12 @@
3+42j)
self.assertRaises((TypeError, struct.error),
struct.pack, self.format,
- NotAnInt)
-
- # 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
+ NotAnInt())
+ self.assertRaises((TypeError, struct.error),
+ struct.pack, self.format,
+ BadIndex())
+ # Check for legitimate values from '__index__'.
for obj in (Indexable(0), Indexable(10), Indexable(17),
Indexable(42), Indexable(100), Indexable(127)):
try:
@@ -299,6 +313,13 @@
self.fail("integer code pack failed on object "
"with '__index__' method")
+ # Check for bogus values from '__index__'.
+ for obj in (Indexable(b'a'), Indexable('b'), Indexable(None),
+ Indexable({'a': 1}), Indexable([1, 2, 3])):
+ self.assertRaises((TypeError, struct.error),
+ struct.pack, self.format,
+ obj)
+
for code in integer_codes:
for byteorder in byteorders:
if (byteorder in ('', '@') and code in ('q', 'Q') and
Modified: python/branches/py3k/Modules/_struct.c
==============================================================================
--- python/branches/py3k/Modules/_struct.c (original)
+++ python/branches/py3k/Modules/_struct.c Sun Apr 4 10:52:51 2010
@@ -102,12 +102,6 @@
v = PyNumber_Index(v);
if (v == NULL)
return NULL;
- if (!PyLong_Check(v)) {
- PyErr_SetString(PyExc_TypeError,
- "__index__ method "
- "returned non-integer");
- return NULL;
- }
}
else {
PyErr_SetString(StructError,
@@ -118,6 +112,7 @@
else
Py_INCREF(v);
+ assert(PyLong_Check(v));
return v;
}
More information about the Python-checkins
mailing list