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.
........
This commit is contained in:
Mark Dickinson 2010-04-03 15:54:36 +00:00
parent 089b00cbc3
commit c593577a4a
4 changed files with 50 additions and 4 deletions

View file

@ -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;
}