summaryrefslogtreecommitdiffstats
path: root/Modules/_struct.c
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2010-04-03 15:54:36 (GMT)
committerMark Dickinson <dickinsm@gmail.com>2010-04-03 15:54:36 (GMT)
commitc593577a4a90b9c806c744b151efcb4e6b052aa4 (patch)
treec88828d3c2cb1fd0925f2058cfaf3063edd289c2 /Modules/_struct.c
parent089b00cbc3006fa60fe71f64341e38b83cc4498d (diff)
downloadcpython-c593577a4a90b9c806c744b151efcb4e6b052aa4.zip
cpython-c593577a4a90b9c806c744b151efcb4e6b052aa4.tar.gz
cpython-c593577a4a90b9c806c744b151efcb4e6b052aa4.tar.bz2
Merged revisions 79674 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r79674 | mark.dickinson | 2010-04-03 15:05:10 +0100 (Sat, 03 Apr 2010) | 3 lines Issue #8300: Let struct.pack use __index__ to convert and pack non-integers. Based on a patch by Meador Inge. ........
Diffstat (limited to 'Modules/_struct.c')
-rw-r--r--Modules/_struct.c23
1 files changed, 19 insertions, 4 deletions
diff --git a/Modules/_struct.c b/Modules/_struct.c
index 43321a4..e21487d 100644
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -97,12 +97,27 @@ get_pylong(PyObject *v)
{
assert(v != NULL);
if (!PyLong_Check(v)) {
- PyErr_SetString(StructError,
- "required argument is not an integer");
- return NULL;
+ /* Not an integer; try to use __index__ to convert. */
+ if (PyIndex_Check(v)) {
+ 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,
+ "required argument is not an integer");
+ return NULL;
+ }
}
+ else
+ Py_INCREF(v);
- Py_INCREF(v);
return v;
}