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().
This commit is contained in:
Serhiy Storchaka 2020-05-28 10:33:45 +03:00 committed by GitHub
parent eaca2aa117
commit 5f4b229df7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
51 changed files with 187 additions and 187 deletions

View file

@ -844,7 +844,7 @@ math_gcd(PyObject *module, PyObject * const *args, Py_ssize_t nargs)
return res;
}
for (i = 1; i < nargs; i++) {
x = PyNumber_Index(args[i]);
x = _PyNumber_Index(args[i]);
if (x == NULL) {
Py_DECREF(res);
return NULL;
@ -1723,7 +1723,7 @@ math_isqrt(PyObject *module, PyObject *n)
uint64_t m, u;
PyObject *a = NULL, *b;
n = PyNumber_Index(n);
n = _PyNumber_Index(n);
if (n == NULL) {
return NULL;
}
@ -3103,24 +3103,11 @@ math_perm_impl(PyObject *module, PyObject *n, PyObject *k)
if (n == NULL) {
return NULL;
}
if (!PyLong_CheckExact(n)) {
Py_SETREF(n, _PyLong_Copy((PyLongObject *)n));
if (n == NULL) {
return NULL;
}
}
k = PyNumber_Index(k);
if (k == NULL) {
Py_DECREF(n);
return NULL;
}
if (!PyLong_CheckExact(k)) {
Py_SETREF(k, _PyLong_Copy((PyLongObject *)k));
if (k == NULL) {
Py_DECREF(n);
return NULL;
}
}
if (Py_SIZE(n) < 0) {
PyErr_SetString(PyExc_ValueError,
@ -3226,24 +3213,11 @@ math_comb_impl(PyObject *module, PyObject *n, PyObject *k)
if (n == NULL) {
return NULL;
}
if (!PyLong_CheckExact(n)) {
Py_SETREF(n, _PyLong_Copy((PyLongObject *)n));
if (n == NULL) {
return NULL;
}
}
k = PyNumber_Index(k);
if (k == NULL) {
Py_DECREF(n);
return NULL;
}
if (!PyLong_CheckExact(k)) {
Py_SETREF(k, _PyLong_Copy((PyLongObject *)k));
if (k == NULL) {
Py_DECREF(n);
return NULL;
}
}
if (Py_SIZE(n) < 0) {
PyErr_SetString(PyExc_ValueError,