summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorTrent Nelson <trent.nelson@snakebite.org>2008-04-10 16:25:37 (GMT)
committerTrent Nelson <trent.nelson@snakebite.org>2008-04-10 16:25:37 (GMT)
commite2ae4684a5617ec5bc8d48e09af2dd7a24711f23 (patch)
tree4b065c8f03e00df65181af57df668136d3d8f0ed /Objects
parent5680d0c5e3a9d42b6c21f4a31a7dbfe72c9e5170 (diff)
downloadcpython-e2ae4684a5617ec5bc8d48e09af2dd7a24711f23.zip
cpython-e2ae4684a5617ec5bc8d48e09af2dd7a24711f23.tar.gz
cpython-e2ae4684a5617ec5bc8d48e09af2dd7a24711f23.tar.bz2
Issue 2440: fix the handling of %n in Python/getargs.c's convertsimple(), extend Objects/abstract.c's PyNumber_Index() to accept PyObjects that have nb_int slots, and update test_getargs2 to test that an exception is thrown when __int__() returns a non-int object.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/abstract.c16
1 files changed, 14 insertions, 2 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c
index dac80d9..6335aa2 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -1220,6 +1220,7 @@ PyNumber_Absolute(PyObject *o)
PyObject *
PyNumber_Index(PyObject *item)
{
+ PyNumberMethods *m;
PyObject *result = NULL;
if (item == NULL)
return null_error();
@@ -1227,8 +1228,9 @@ PyNumber_Index(PyObject *item)
Py_INCREF(item);
return item;
}
+ m = item->ob_type->tp_as_number;
if (PyIndex_Check(item)) {
- result = item->ob_type->tp_as_number->nb_index(item);
+ result = m->nb_index(item);
if (result && !PyLong_Check(result)) {
PyErr_Format(PyExc_TypeError,
"__index__ returned non-int "
@@ -1238,7 +1240,17 @@ PyNumber_Index(PyObject *item)
return NULL;
}
}
- else {
+ else if (m && m->nb_int != NULL) {
+ result = m->nb_int(item);
+ if (result && !PyLong_Check(result)) {
+ PyErr_Format(PyExc_TypeError,
+ "__int__ returned non-int "
+ "(type %.200s)",
+ result->ob_type->tp_name);
+ Py_DECREF(result);
+ return NULL;
+ }
+ } else {
PyErr_Format(PyExc_TypeError,
"'%.200s' object cannot be interpreted "
"as an integer", item->ob_type->tp_name);