From c593577a4a90b9c806c744b151efcb4e6b052aa4 Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Sat, 3 Apr 2010 15:54:36 +0000 Subject: 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. ........ --- Modules/_struct.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'Modules') diff --git a/Modules/_struct.c b/Modules/_struct.c index 43321a4e3d..e21487d05c 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; } -- cgit v1.2.1