From d59b41641e7f800a65f4241d05710b774cafa04f Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Sat, 13 Mar 2010 11:34:40 +0000 Subject: Issue #8014: Fix PyLong_As methods not to produce an internal error on non-integer input: they now raise TypeError instead. This is needed for attributes declared via PyMemberDefs. --- Objects/longobject.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'Objects/longobject.c') diff --git a/Objects/longobject.c b/Objects/longobject.c index ce87084e28..d182e7ce30 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -435,10 +435,15 @@ PyLong_AsSsize_t(PyObject *vv) { Py_ssize_t i; int sign; - if (vv == NULL || !PyLong_Check(vv)) { + if (vv == NULL) { PyErr_BadInternalCall(); return -1; } + if (!PyLong_Check(vv)) { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return -1; + } + v = (PyLongObject *)vv; i = Py_SIZE(v); switch (i) { @@ -485,10 +490,15 @@ PyLong_AsUnsignedLong(PyObject *vv) unsigned long x, prev; Py_ssize_t i; - if (vv == NULL || !PyLong_Check(vv)) { + if (vv == NULL) { PyErr_BadInternalCall(); - return (unsigned long) -1; + return (unsigned long)-1; + } + if (!PyLong_Check(vv)) { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return (unsigned long)-1; } + v = (PyLongObject *)vv; i = Py_SIZE(v); x = 0; @@ -523,10 +533,15 @@ PyLong_AsSize_t(PyObject *vv) size_t x, prev; Py_ssize_t i; - if (vv == NULL || !PyLong_Check(vv)) { + if (vv == NULL) { PyErr_BadInternalCall(); - return (unsigned long) -1; + return (size_t) -1; + } + if (!PyLong_Check(vv)) { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return (size_t)-1; } + v = (PyLongObject *)vv; i = Py_SIZE(v); x = 0; -- cgit v1.2.1