bpo-40170: Add _PyIndex_Check() internal function (GH-19426)

Add _PyIndex_Check() function to the internal C API: fast inlined
verson of PyIndex_Check().

Add Include/internal/pycore_abstract.h header file.

Replace PyIndex_Check() with _PyIndex_Check() in C files of Objects
and Python subdirectories.
This commit is contained in:
Victor Stinner 2020-04-08 02:01:56 +02:00 committed by GitHub
parent 45ec5b99ae
commit a15e260b70
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 72 additions and 30 deletions

View file

@ -1,7 +1,8 @@
/* Abstract Object Interface (many thanks to Jim Fulton) */
#include "Python.h"
#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
#include "pycore_pyerrors.h"
#include "pycore_pystate.h"
#include <ctype.h>
@ -160,7 +161,7 @@ PyObject_GetItem(PyObject *o, PyObject *key)
ms = Py_TYPE(o)->tp_as_sequence;
if (ms && ms->sq_item) {
if (PyIndex_Check(key)) {
if (_PyIndex_Check(key)) {
Py_ssize_t key_value;
key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
if (key_value == -1 && PyErr_Occurred())
@ -176,7 +177,7 @@ PyObject_GetItem(PyObject *o, PyObject *key)
if (PyType_Check(o)) {
PyObject *meth, *result;
_Py_IDENTIFIER(__class_getitem__);
// Special case type[int], but disallow other types so str[int] fails
if ((PyTypeObject*)o == &PyType_Type) {
return Py_GenericAlias(o, key);
@ -209,7 +210,7 @@ PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)
return m->mp_ass_subscript(o, key, value);
if (Py_TYPE(o)->tp_as_sequence) {
if (PyIndex_Check(key)) {
if (_PyIndex_Check(key)) {
Py_ssize_t key_value;
key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
if (key_value == -1 && PyErr_Occurred())
@ -241,7 +242,7 @@ PyObject_DelItem(PyObject *o, PyObject *key)
return m->mp_ass_subscript(o, key, (PyObject*)NULL);
if (Py_TYPE(o)->tp_as_sequence) {
if (PyIndex_Check(key)) {
if (_PyIndex_Check(key)) {
Py_ssize_t key_value;
key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
if (key_value == -1 && PyErr_Occurred())
@ -1030,7 +1031,7 @@ static PyObject *
sequence_repeat(ssizeargfunc repeatfunc, PyObject *seq, PyObject *n)
{
Py_ssize_t count;
if (PyIndex_Check(n)) {
if (_PyIndex_Check(n)) {
count = PyNumber_AsSsize_t(n, PyExc_OverflowError);
if (count == -1 && PyErr_Occurred())
return NULL;
@ -1307,15 +1308,16 @@ PyNumber_Absolute(PyObject *o)
return type_error("bad operand type for abs(): '%.200s'", o);
}
#undef PyIndex_Check
int
PyIndex_Check(PyObject *obj)
{
return Py_TYPE(obj)->tp_as_number != NULL &&
Py_TYPE(obj)->tp_as_number->nb_index != NULL;
return _PyIndex_Check(obj);
}
/* Return a 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.
@ -1332,7 +1334,7 @@ PyNumber_Index(PyObject *item)
Py_INCREF(item);
return item;
}
if (!PyIndex_Check(item)) {
if (!_PyIndex_Check(item)) {
PyErr_Format(PyExc_TypeError,
"'%.200s' object cannot be interpreted "
"as an integer", Py_TYPE(item)->tp_name);