Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e2ae468

Browse files
committed
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.
1 parent 5680d0c commit e2ae468

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

Lib/test/test_getargs2.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ class Int:
6363
def __int__(self):
6464
return 99
6565

66+
class InvalidLongAsString:
67+
def __int__(self):
68+
return 'foobar'
69+
6670
class Unsigned_TestCase(unittest.TestCase):
6771
def test_b(self):
6872
from _testcapi import getargs_b
@@ -199,6 +203,7 @@ def test_n(self):
199203
self.failUnlessEqual(42, getargs_n(42))
200204
self.assertRaises(OverflowError, getargs_n, VERY_LARGE)
201205

206+
self.assertRaises(TypeError, getargs_n, InvalidLongAsString())
202207

203208
class LongLong_TestCase(unittest.TestCase):
204209
def test_L(self):

Objects/abstract.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,15 +1220,17 @@ PyNumber_Absolute(PyObject *o)
12201220
PyObject *
12211221
PyNumber_Index(PyObject *item)
12221222
{
1223+
PyNumberMethods *m;
12231224
PyObject *result = NULL;
12241225
if (item == NULL)
12251226
return null_error();
12261227
if (PyLong_Check(item)) {
12271228
Py_INCREF(item);
12281229
return item;
12291230
}
1231+
m = item->ob_type->tp_as_number;
12301232
if (PyIndex_Check(item)) {
1231-
result = item->ob_type->tp_as_number->nb_index(item);
1233+
result = m->nb_index(item);
12321234
if (result && !PyLong_Check(result)) {
12331235
PyErr_Format(PyExc_TypeError,
12341236
"__index__ returned non-int "
@@ -1238,7 +1240,17 @@ PyNumber_Index(PyObject *item)
12381240
return NULL;
12391241
}
12401242
}
1241-
else {
1243+
else if (m && m->nb_int != NULL) {
1244+
result = m->nb_int(item);
1245+
if (result && !PyLong_Check(result)) {
1246+
PyErr_Format(PyExc_TypeError,
1247+
"__int__ returned non-int "
1248+
"(type %.200s)",
1249+
result->ob_type->tp_name);
1250+
Py_DECREF(result);
1251+
return NULL;
1252+
}
1253+
} else {
12421254
PyErr_Format(PyExc_TypeError,
12431255
"'%.200s' object cannot be interpreted "
12441256
"as an integer", item->ob_type->tp_name);

Python/getargs.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,6 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
663663
}
664664

665665
case 'n': /* Py_ssize_t */
666-
#if SIZEOF_SIZE_T != SIZEOF_LONG
667666
{
668667
PyObject *iobj;
669668
Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
@@ -672,14 +671,13 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
672671
return converterr("integer<n>", arg, msgbuf, bufsize);
673672
iobj = PyNumber_Index(arg);
674673
if (iobj != NULL)
675-
ival = PyLong_AsSsize_t(arg);
674+
ival = PyLong_AsSsize_t(iobj);
676675
if (ival == -1 && PyErr_Occurred())
677676
return converterr("integer<n>", arg, msgbuf, bufsize);
678677
*p = ival;
679678
break;
680679
}
681-
#endif
682-
/* Fall through from 'n' to 'l' if Py_ssize_t is int */
680+
683681
case 'l': {/* long int */
684682
long *p = va_arg(*p_va, long *);
685683
long ival;

0 commit comments

Comments
 (0)