summaryrefslogtreecommitdiffstats
path: root/Modules/_struct.c
diff options
context:
space:
mode:
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;
}