diff options
Diffstat (limited to 'Objects/intobject.c')
-rw-r--r-- | Objects/intobject.c | 71 |
1 files changed, 70 insertions, 1 deletions
diff --git a/Objects/intobject.c b/Objects/intobject.c index 5a0b259..d1b9599 100644 --- a/Objects/intobject.c +++ b/Objects/intobject.c @@ -108,6 +108,22 @@ PyInt_FromLong(long ival) return (PyObject *) v; } +PyObject * +PyInt_FromSize_t(size_t ival) +{ + if (ival <= LONG_MAX) + return PyInt_FromLong((long)ival); + return _PyLong_FromSize_t(ival); +} + +PyObject * +PyInt_FromSsize_t(Py_ssize_t ival) +{ + if (ival >= LONG_MIN && ival <= LONG_MAX) + return PyInt_FromLong((long)ival); + return _PyLong_FromSsize_t(ival); +} + static void int_dealloc(PyIntObject *v) { @@ -169,6 +185,59 @@ PyInt_AsLong(register PyObject *op) return val; } +Py_ssize_t +PyInt_AsSsize_t(register PyObject *op) +{ + PyNumberMethods *nb; + PyIntObject *io; + Py_ssize_t val; + if (op && !PyInt_CheckExact(op) && PyLong_Check(op)) + return _PyLong_AsSsize_t(op); +#if SIZEOF_SIZE_T==SIZEOF_LONG + return PyInt_AsLong(op); +#else + + if (op && PyInt_Check(op)) + return PyInt_AS_LONG((PyIntObject*) op); + + if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || + (nb->nb_int == NULL && nb->nb_long == 0)) { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return -1; + } + + if (nb->nb_long != 0) { + io = (PyIntObject*) (*nb->nb_long) (op); + } else { + io = (PyIntObject*) (*nb->nb_int) (op); + } + if (io == NULL) + return -1; + if (!PyInt_Check(io)) { + if (PyLong_Check(io)) { + /* got a long? => retry int conversion */ + val = _PyLong_AsSsize_t((PyObject *)io); + Py_DECREF(io); + if ((val == -1) && PyErr_Occurred()) + return -1; + return val; + } + else + { + Py_DECREF(io); + PyErr_SetString(PyExc_TypeError, + "nb_int should return int object"); + return -1; + } + } + + val = PyInt_AS_LONG(io); + Py_DECREF(io); + + return val; +#endif +} + unsigned long PyInt_AsUnsignedLongMask(register PyObject *op) { @@ -302,7 +371,7 @@ PyInt_FromString(char *s, char **pend, int base) #ifdef Py_USING_UNICODE PyObject * -PyInt_FromUnicode(Py_UNICODE *s, int length, int base) +PyInt_FromUnicode(Py_UNICODE *s, Py_ssize_t length, int base) { PyObject *result; char *buffer = PyMem_MALLOC(length+1); |