mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
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.
This commit is contained in:
parent
5680d0c5e3
commit
e2ae4684a5
3 changed files with 21 additions and 6 deletions
|
@ -63,6 +63,10 @@ class Int:
|
||||||
def __int__(self):
|
def __int__(self):
|
||||||
return 99
|
return 99
|
||||||
|
|
||||||
|
class InvalidLongAsString:
|
||||||
|
def __int__(self):
|
||||||
|
return 'foobar'
|
||||||
|
|
||||||
class Unsigned_TestCase(unittest.TestCase):
|
class Unsigned_TestCase(unittest.TestCase):
|
||||||
def test_b(self):
|
def test_b(self):
|
||||||
from _testcapi import getargs_b
|
from _testcapi import getargs_b
|
||||||
|
@ -199,6 +203,7 @@ class Signed_TestCase(unittest.TestCase):
|
||||||
self.failUnlessEqual(42, getargs_n(42))
|
self.failUnlessEqual(42, getargs_n(42))
|
||||||
self.assertRaises(OverflowError, getargs_n, VERY_LARGE)
|
self.assertRaises(OverflowError, getargs_n, VERY_LARGE)
|
||||||
|
|
||||||
|
self.assertRaises(TypeError, getargs_n, InvalidLongAsString())
|
||||||
|
|
||||||
class LongLong_TestCase(unittest.TestCase):
|
class LongLong_TestCase(unittest.TestCase):
|
||||||
def test_L(self):
|
def test_L(self):
|
||||||
|
|
|
@ -1220,6 +1220,7 @@ PyNumber_Absolute(PyObject *o)
|
||||||
PyObject *
|
PyObject *
|
||||||
PyNumber_Index(PyObject *item)
|
PyNumber_Index(PyObject *item)
|
||||||
{
|
{
|
||||||
|
PyNumberMethods *m;
|
||||||
PyObject *result = NULL;
|
PyObject *result = NULL;
|
||||||
if (item == NULL)
|
if (item == NULL)
|
||||||
return null_error();
|
return null_error();
|
||||||
|
@ -1227,8 +1228,9 @@ PyNumber_Index(PyObject *item)
|
||||||
Py_INCREF(item);
|
Py_INCREF(item);
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
m = item->ob_type->tp_as_number;
|
||||||
if (PyIndex_Check(item)) {
|
if (PyIndex_Check(item)) {
|
||||||
result = item->ob_type->tp_as_number->nb_index(item);
|
result = m->nb_index(item);
|
||||||
if (result && !PyLong_Check(result)) {
|
if (result && !PyLong_Check(result)) {
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"__index__ returned non-int "
|
"__index__ returned non-int "
|
||||||
|
@ -1238,7 +1240,17 @@ PyNumber_Index(PyObject *item)
|
||||||
return NULL;
|
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,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"'%.200s' object cannot be interpreted "
|
"'%.200s' object cannot be interpreted "
|
||||||
"as an integer", item->ob_type->tp_name);
|
"as an integer", item->ob_type->tp_name);
|
||||||
|
|
|
@ -663,7 +663,6 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'n': /* Py_ssize_t */
|
case 'n': /* Py_ssize_t */
|
||||||
#if SIZEOF_SIZE_T != SIZEOF_LONG
|
|
||||||
{
|
{
|
||||||
PyObject *iobj;
|
PyObject *iobj;
|
||||||
Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
|
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,
|
||||||
return converterr("integer<n>", arg, msgbuf, bufsize);
|
return converterr("integer<n>", arg, msgbuf, bufsize);
|
||||||
iobj = PyNumber_Index(arg);
|
iobj = PyNumber_Index(arg);
|
||||||
if (iobj != NULL)
|
if (iobj != NULL)
|
||||||
ival = PyLong_AsSsize_t(arg);
|
ival = PyLong_AsSsize_t(iobj);
|
||||||
if (ival == -1 && PyErr_Occurred())
|
if (ival == -1 && PyErr_Occurred())
|
||||||
return converterr("integer<n>", arg, msgbuf, bufsize);
|
return converterr("integer<n>", arg, msgbuf, bufsize);
|
||||||
*p = ival;
|
*p = ival;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
/* Fall through from 'n' to 'l' if Py_ssize_t is int */
|
|
||||||
case 'l': {/* long int */
|
case 'l': {/* long int */
|
||||||
long *p = va_arg(*p_va, long *);
|
long *p = va_arg(*p_va, long *);
|
||||||
long ival;
|
long ival;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue