diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2020-05-28 07:33:45 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-28 07:33:45 (GMT) |
commit | 5f4b229df7812f1788287095eb6b138bb21876a4 (patch) | |
tree | f00cef402d0467010a34e9e617d89ccebebe6d1a /Objects | |
parent | eaca2aa117d663acf8160a0b4543ee2c7006fcc7 (diff) | |
download | cpython-5f4b229df7812f1788287095eb6b138bb21876a4.zip cpython-5f4b229df7812f1788287095eb6b138bb21876a4.tar.gz cpython-5f4b229df7812f1788287095eb6b138bb21876a4.tar.bz2 |
bpo-40792: Make the result of PyNumber_Index() always having exact type int. (GH-20443)
Previously, the result could have been an instance of a subclass of int.
Also revert bpo-26202 and make attributes start, stop and step of the range
object having exact type int.
Add private function _PyNumber_Index() which preserves the old behavior
of PyNumber_Index() for performance to use it in the conversion functions
like PyLong_AsLong().
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/abstract.c | 32 | ||||
-rw-r--r-- | Objects/bytesobject.c | 2 | ||||
-rw-r--r-- | Objects/clinic/bytearrayobject.c.h | 12 | ||||
-rw-r--r-- | Objects/clinic/bytesobject.c.h | 8 | ||||
-rw-r--r-- | Objects/clinic/listobject.c.h | 6 | ||||
-rw-r--r-- | Objects/clinic/longobject.c.h | 4 | ||||
-rw-r--r-- | Objects/clinic/unicodeobject.c.h | 16 | ||||
-rw-r--r-- | Objects/floatobject.c | 2 | ||||
-rw-r--r-- | Objects/longobject.c | 12 | ||||
-rw-r--r-- | Objects/memoryobject.c | 12 | ||||
-rw-r--r-- | Objects/stringlib/clinic/transmogrify.h.h | 10 | ||||
-rw-r--r-- | Objects/typeobject.c | 2 | ||||
-rw-r--r-- | Objects/unicodeobject.c | 2 |
13 files changed, 64 insertions, 56 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c index e819849..973c43f 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -1317,11 +1317,12 @@ PyIndex_Check(PyObject *obj) /* Return a Python int from the object item. + Can return an instance of int subclass. Raise TypeError if the result is not an int or if the object cannot be interpreted as an index. */ PyObject * -PyNumber_Index(PyObject *item) +_PyNumber_Index(PyObject *item) { PyObject *result = NULL; if (item == NULL) { @@ -1360,6 +1361,20 @@ PyNumber_Index(PyObject *item) return result; } +/* Return an exact Python int from the object item. + Raise TypeError if the result is not an int + or if the object cannot be interpreted as an index. +*/ +PyObject * +PyNumber_Index(PyObject *item) +{ + PyObject *result = _PyNumber_Index(item); + if (result != NULL && !PyLong_CheckExact(result)) { + Py_SETREF(result, _PyLong_Copy((PyLongObject *)result)); + } + return result; +} + /* Return an error on Overflow only if err is not NULL*/ Py_ssize_t @@ -1367,7 +1382,7 @@ PyNumber_AsSsize_t(PyObject *item, PyObject *err) { Py_ssize_t result; PyObject *runerr; - PyObject *value = PyNumber_Index(item); + PyObject *value = _PyNumber_Index(item); if (value == NULL) return -1; @@ -1451,11 +1466,7 @@ PyNumber_Long(PyObject *o) return result; } if (m && m->nb_index) { - result = PyNumber_Index(o); - if (result != NULL && !PyLong_CheckExact(result)) { - Py_SETREF(result, _PyLong_Copy((PyLongObject *)result)); - } - return result; + return PyNumber_Index(o); } trunc_func = _PyObject_LookupSpecial(o, &PyId___trunc__); if (trunc_func) { @@ -1479,9 +1490,6 @@ PyNumber_Long(PyObject *o) return NULL; } Py_SETREF(result, PyNumber_Index(result)); - if (result != NULL && !PyLong_CheckExact(result)) { - Py_SETREF(result, _PyLong_Copy((PyLongObject *)result)); - } return result; } if (PyErr_Occurred()) @@ -1564,7 +1572,7 @@ PyNumber_Float(PyObject *o) return PyFloat_FromDouble(val); } if (m && m->nb_index) { - PyObject *res = PyNumber_Index(o); + PyObject *res = _PyNumber_Index(o); if (!res) { return NULL; } @@ -1590,7 +1598,7 @@ PyNumber_ToBase(PyObject *n, int base) "PyNumber_ToBase: base must be 2, 8, 10 or 16"); return NULL; } - PyObject *index = PyNumber_Index(n); + PyObject *index = _PyNumber_Index(n); if (!index) return NULL; PyObject *res = _PyLong_Format(index, base); diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 25d9814..8d64540 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -455,7 +455,7 @@ formatlong(PyObject *v, int flags, int prec, int type) if (PyNumber_Check(v)) { /* make sure number is a type of integer for o, x, and X */ if (type == 'o' || type == 'x' || type == 'X') - iobj = PyNumber_Index(v); + iobj = _PyNumber_Index(v); else iobj = PyNumber_Long(v); if (iobj == NULL) { diff --git a/Objects/clinic/bytearrayobject.c.h b/Objects/clinic/bytearrayobject.c.h index 83b0d03..cbe6f20 100644 --- a/Objects/clinic/bytearrayobject.c.h +++ b/Objects/clinic/bytearrayobject.c.h @@ -270,7 +270,7 @@ bytearray_replace(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nar } { Py_ssize_t ival = -1; - PyObject *iobj = PyNumber_Index(args[2]); + PyObject *iobj = _PyNumber_Index(args[2]); if (iobj != NULL) { ival = PyLong_AsSsize_t(iobj); Py_DECREF(iobj); @@ -343,7 +343,7 @@ bytearray_split(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs } { Py_ssize_t ival = -1; - PyObject *iobj = PyNumber_Index(args[1]); + PyObject *iobj = _PyNumber_Index(args[1]); if (iobj != NULL) { ival = PyLong_AsSsize_t(iobj); Py_DECREF(iobj); @@ -442,7 +442,7 @@ bytearray_rsplit(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t narg } { Py_ssize_t ival = -1; - PyObject *iobj = PyNumber_Index(args[1]); + PyObject *iobj = _PyNumber_Index(args[1]); if (iobj != NULL) { ival = PyLong_AsSsize_t(iobj); Py_DECREF(iobj); @@ -506,7 +506,7 @@ bytearray_insert(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t narg } { Py_ssize_t ival = -1; - PyObject *iobj = PyNumber_Index(args[0]); + PyObject *iobj = _PyNumber_Index(args[0]); if (iobj != NULL) { ival = PyLong_AsSsize_t(iobj); Py_DECREF(iobj); @@ -599,7 +599,7 @@ bytearray_pop(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs) } { Py_ssize_t ival = -1; - PyObject *iobj = PyNumber_Index(args[0]); + PyObject *iobj = _PyNumber_Index(args[0]); if (iobj != NULL) { ival = PyLong_AsSsize_t(iobj); Py_DECREF(iobj); @@ -1051,4 +1051,4 @@ bytearray_sizeof(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored)) { return bytearray_sizeof_impl(self); } -/*[clinic end generated code: output=920748990279fb9d input=a9049054013a1b77]*/ +/*[clinic end generated code: output=0cd59180c7d5dce5 input=a9049054013a1b77]*/ diff --git a/Objects/clinic/bytesobject.c.h b/Objects/clinic/bytesobject.c.h index c4a2d0c..201627e 100644 --- a/Objects/clinic/bytesobject.c.h +++ b/Objects/clinic/bytesobject.c.h @@ -48,7 +48,7 @@ bytes_split(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObje } { Py_ssize_t ival = -1; - PyObject *iobj = PyNumber_Index(args[1]); + PyObject *iobj = _PyNumber_Index(args[1]); if (iobj != NULL) { ival = PyLong_AsSsize_t(iobj); Py_DECREF(iobj); @@ -199,7 +199,7 @@ bytes_rsplit(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObj } { Py_ssize_t ival = -1; - PyObject *iobj = PyNumber_Index(args[1]); + PyObject *iobj = _PyNumber_Index(args[1]); if (iobj != NULL) { ival = PyLong_AsSsize_t(iobj); Py_DECREF(iobj); @@ -485,7 +485,7 @@ bytes_replace(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs) } { Py_ssize_t ival = -1; - PyObject *iobj = PyNumber_Index(args[2]); + PyObject *iobj = _PyNumber_Index(args[2]); if (iobj != NULL) { ival = PyLong_AsSsize_t(iobj); Py_DECREF(iobj); @@ -809,4 +809,4 @@ skip_optional_pos: exit: return return_value; } -/*[clinic end generated code: output=a0c31faea2671a8c input=a9049054013a1b77]*/ +/*[clinic end generated code: output=dc1bc13e6990e452 input=a9049054013a1b77]*/ diff --git a/Objects/clinic/listobject.c.h b/Objects/clinic/listobject.c.h index 82884a4..01e31d7 100644 --- a/Objects/clinic/listobject.c.h +++ b/Objects/clinic/listobject.c.h @@ -26,7 +26,7 @@ list_insert(PyListObject *self, PyObject *const *args, Py_ssize_t nargs) } { Py_ssize_t ival = -1; - PyObject *iobj = PyNumber_Index(args[0]); + PyObject *iobj = _PyNumber_Index(args[0]); if (iobj != NULL) { ival = PyLong_AsSsize_t(iobj); Py_DECREF(iobj); @@ -125,7 +125,7 @@ list_pop(PyListObject *self, PyObject *const *args, Py_ssize_t nargs) } { Py_ssize_t ival = -1; - PyObject *iobj = PyNumber_Index(args[0]); + PyObject *iobj = _PyNumber_Index(args[0]); if (iobj != NULL) { ival = PyLong_AsSsize_t(iobj); Py_DECREF(iobj); @@ -352,4 +352,4 @@ list___reversed__(PyListObject *self, PyObject *Py_UNUSED(ignored)) { return list___reversed___impl(self); } -/*[clinic end generated code: output=137db7b11196b581 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=0063aad535edf62d input=a9049054013a1b77]*/ diff --git a/Objects/clinic/longobject.c.h b/Objects/clinic/longobject.c.h index d3d5c19..7db8965 100644 --- a/Objects/clinic/longobject.c.h +++ b/Objects/clinic/longobject.c.h @@ -211,7 +211,7 @@ int_to_bytes(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject * } { Py_ssize_t ival = -1; - PyObject *iobj = PyNumber_Index(args[0]); + PyObject *iobj = _PyNumber_Index(args[0]); if (iobj != NULL) { ival = PyLong_AsSsize_t(iobj); Py_DECREF(iobj); @@ -308,4 +308,4 @@ skip_optional_kwonly: exit: return return_value; } -/*[clinic end generated code: output=46d40c8aa6d420b7 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=63b8274fc784d617 input=a9049054013a1b77]*/ diff --git a/Objects/clinic/unicodeobject.c.h b/Objects/clinic/unicodeobject.c.h index 2d81730..ecd409e 100644 --- a/Objects/clinic/unicodeobject.c.h +++ b/Objects/clinic/unicodeobject.c.h @@ -88,7 +88,7 @@ unicode_center(PyObject *self, PyObject *const *args, Py_ssize_t nargs) } { Py_ssize_t ival = -1; - PyObject *iobj = PyNumber_Index(args[0]); + PyObject *iobj = _PyNumber_Index(args[0]); if (iobj != NULL) { ival = PyLong_AsSsize_t(iobj); Py_DECREF(iobj); @@ -522,7 +522,7 @@ unicode_ljust(PyObject *self, PyObject *const *args, Py_ssize_t nargs) } { Py_ssize_t ival = -1; - PyObject *iobj = PyNumber_Index(args[0]); + PyObject *iobj = _PyNumber_Index(args[0]); if (iobj != NULL) { ival = PyLong_AsSsize_t(iobj); Py_DECREF(iobj); @@ -717,7 +717,7 @@ unicode_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs) } { Py_ssize_t ival = -1; - PyObject *iobj = PyNumber_Index(args[2]); + PyObject *iobj = _PyNumber_Index(args[2]); if (iobj != NULL) { ival = PyLong_AsSsize_t(iobj); Py_DECREF(iobj); @@ -831,7 +831,7 @@ unicode_rjust(PyObject *self, PyObject *const *args, Py_ssize_t nargs) } { Py_ssize_t ival = -1; - PyObject *iobj = PyNumber_Index(args[0]); + PyObject *iobj = _PyNumber_Index(args[0]); if (iobj != NULL) { ival = PyLong_AsSsize_t(iobj); Py_DECREF(iobj); @@ -900,7 +900,7 @@ unicode_split(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject } { Py_ssize_t ival = -1; - PyObject *iobj = PyNumber_Index(args[1]); + PyObject *iobj = _PyNumber_Index(args[1]); if (iobj != NULL) { ival = PyLong_AsSsize_t(iobj); Py_DECREF(iobj); @@ -997,7 +997,7 @@ unicode_rsplit(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject } { Py_ssize_t ival = -1; - PyObject *iobj = PyNumber_Index(args[1]); + PyObject *iobj = _PyNumber_Index(args[1]); if (iobj != NULL) { ival = PyLong_AsSsize_t(iobj); Py_DECREF(iobj); @@ -1193,7 +1193,7 @@ unicode_zfill(PyObject *self, PyObject *arg) { Py_ssize_t ival = -1; - PyObject *iobj = PyNumber_Index(arg); + PyObject *iobj = _PyNumber_Index(arg); if (iobj != NULL) { ival = PyLong_AsSsize_t(iobj); Py_DECREF(iobj); @@ -1258,4 +1258,4 @@ unicode_sizeof(PyObject *self, PyObject *Py_UNUSED(ignored)) { return unicode_sizeof_impl(self); } -/*[clinic end generated code: output=ea1aff10c743be14 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=c5eb21e314da78b8 input=a9049054013a1b77]*/ diff --git a/Objects/floatobject.c b/Objects/floatobject.c index cc0ae8c..868b729 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -250,7 +250,7 @@ PyFloat_AsDouble(PyObject *op) nb = Py_TYPE(op)->tp_as_number; if (nb == NULL || nb->nb_float == NULL) { if (nb && nb->nb_index) { - PyObject *res = PyNumber_Index(op); + PyObject *res = _PyNumber_Index(op); if (!res) { return -1; } diff --git a/Objects/longobject.c b/Objects/longobject.c index e040d6c..4ae17c9 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -394,7 +394,7 @@ PyLong_AsLongAndOverflow(PyObject *vv, int *overflow) v = (PyLongObject *)vv; } else { - v = (PyLongObject *)PyNumber_Index(vv); + v = (PyLongObject *)_PyNumber_Index(vv); if (v == NULL) return -1; do_decref = 1; @@ -674,7 +674,7 @@ PyLong_AsUnsignedLongMask(PyObject *op) return _PyLong_AsUnsignedLongMask(op); } - lo = (PyLongObject *)PyNumber_Index(op); + lo = (PyLongObject *)_PyNumber_Index(op); if (lo == NULL) return (unsigned long)-1; @@ -1132,7 +1132,7 @@ PyLong_AsLongLong(PyObject *vv) v = (PyLongObject *)vv; } else { - v = (PyLongObject *)PyNumber_Index(vv); + v = (PyLongObject *)_PyNumber_Index(vv); if (v == NULL) return -1; do_decref = 1; @@ -1247,7 +1247,7 @@ PyLong_AsUnsignedLongLongMask(PyObject *op) return _PyLong_AsUnsignedLongLongMask(op); } - lo = (PyLongObject *)PyNumber_Index(op); + lo = (PyLongObject *)_PyNumber_Index(op); if (lo == NULL) return (unsigned long long)-1; @@ -1287,7 +1287,7 @@ PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow) v = (PyLongObject *)vv; } else { - v = (PyLongObject *)PyNumber_Index(vv); + v = (PyLongObject *)_PyNumber_Index(vv); if (v == NULL) return -1; do_decref = 1; @@ -5180,7 +5180,7 @@ long_round(PyObject *self, PyObject *args) if (o_ndigits == NULL) return long_long(self); - ndigits = PyNumber_Index(o_ndigits); + ndigits = _PyNumber_Index(o_ndigits); if (ndigits == NULL) return NULL; diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c index 682bbe8..e3d3bd6a 100644 --- a/Objects/memoryobject.c +++ b/Objects/memoryobject.c @@ -1578,7 +1578,7 @@ pylong_as_ld(PyObject *item) PyObject *tmp; long ld; - tmp = PyNumber_Index(item); + tmp = _PyNumber_Index(item); if (tmp == NULL) return -1; @@ -1593,7 +1593,7 @@ pylong_as_lu(PyObject *item) PyObject *tmp; unsigned long lu; - tmp = PyNumber_Index(item); + tmp = _PyNumber_Index(item); if (tmp == NULL) return (unsigned long)-1; @@ -1608,7 +1608,7 @@ pylong_as_lld(PyObject *item) PyObject *tmp; long long lld; - tmp = PyNumber_Index(item); + tmp = _PyNumber_Index(item); if (tmp == NULL) return -1; @@ -1623,7 +1623,7 @@ pylong_as_llu(PyObject *item) PyObject *tmp; unsigned long long llu; - tmp = PyNumber_Index(item); + tmp = _PyNumber_Index(item); if (tmp == NULL) return (unsigned long long)-1; @@ -1638,7 +1638,7 @@ pylong_as_zd(PyObject *item) PyObject *tmp; Py_ssize_t zd; - tmp = PyNumber_Index(item); + tmp = _PyNumber_Index(item); if (tmp == NULL) return -1; @@ -1653,7 +1653,7 @@ pylong_as_zu(PyObject *item) PyObject *tmp; size_t zu; - tmp = PyNumber_Index(item); + tmp = _PyNumber_Index(item); if (tmp == NULL) return (size_t)-1; diff --git a/Objects/stringlib/clinic/transmogrify.h.h b/Objects/stringlib/clinic/transmogrify.h.h index 8dd7e6b..a5135a0 100644 --- a/Objects/stringlib/clinic/transmogrify.h.h +++ b/Objects/stringlib/clinic/transmogrify.h.h @@ -70,7 +70,7 @@ stringlib_ljust(PyObject *self, PyObject *const *args, Py_ssize_t nargs) } { Py_ssize_t ival = -1; - PyObject *iobj = PyNumber_Index(args[0]); + PyObject *iobj = _PyNumber_Index(args[0]); if (iobj != NULL) { ival = PyLong_AsSsize_t(iobj); Py_DECREF(iobj); @@ -126,7 +126,7 @@ stringlib_rjust(PyObject *self, PyObject *const *args, Py_ssize_t nargs) } { Py_ssize_t ival = -1; - PyObject *iobj = PyNumber_Index(args[0]); + PyObject *iobj = _PyNumber_Index(args[0]); if (iobj != NULL) { ival = PyLong_AsSsize_t(iobj); Py_DECREF(iobj); @@ -182,7 +182,7 @@ stringlib_center(PyObject *self, PyObject *const *args, Py_ssize_t nargs) } { Py_ssize_t ival = -1; - PyObject *iobj = PyNumber_Index(args[0]); + PyObject *iobj = _PyNumber_Index(args[0]); if (iobj != NULL) { ival = PyLong_AsSsize_t(iobj); Py_DECREF(iobj); @@ -234,7 +234,7 @@ stringlib_zfill(PyObject *self, PyObject *arg) { Py_ssize_t ival = -1; - PyObject *iobj = PyNumber_Index(arg); + PyObject *iobj = _PyNumber_Index(arg); if (iobj != NULL) { ival = PyLong_AsSsize_t(iobj); Py_DECREF(iobj); @@ -249,4 +249,4 @@ stringlib_zfill(PyObject *self, PyObject *arg) exit: return return_value; } -/*[clinic end generated code: output=cd5ecdbf1d9e849a input=a9049054013a1b77]*/ +/*[clinic end generated code: output=2d9abc7b1cffeca6 input=a9049054013a1b77]*/ diff --git a/Objects/typeobject.c b/Objects/typeobject.c index ba2a852..1d556e9 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -6334,7 +6334,7 @@ slot_sq_length(PyObject *self) if (res == NULL) return -1; - Py_SETREF(res, PyNumber_Index(res)); + Py_SETREF(res, _PyNumber_Index(res)); if (res == NULL) return -1; diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index ea46a44..5116404 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -14617,7 +14617,7 @@ mainformatlong(PyObject *v, /* make sure number is a type of integer for o, x, and X */ if (!PyLong_Check(v)) { if (type == 'o' || type == 'x' || type == 'X') { - iobj = PyNumber_Index(v); + iobj = _PyNumber_Index(v); if (iobj == NULL) { if (PyErr_ExceptionMatches(PyExc_TypeError)) goto wrongtype; |