bpo-40170: Add _PyIndex_Check() internal function (GH-19426) · python/cpython@a15e260 (original) (raw)

1

1

`/* Abstract Object Interface (many thanks to Jim Fulton) */

`

2

2

``

3

3

`#include "Python.h"

`

4

``

`-

#include "pycore_ceval.h" // _Py_EnterRecursiveCall()

`

``

4

`+

#include "pycore_abstract.h" // _PyIndex_Check()

`

``

5

`+

#include "pycore_ceval.h" // _Py_EnterRecursiveCall()

`

5

6

`#include "pycore_pyerrors.h"

`

6

7

`#include "pycore_pystate.h"

`

7

8

`#include <ctype.h>

`

`@@ -160,7 +161,7 @@ PyObject_GetItem(PyObject *o, PyObject *key)

`

160

161

``

161

162

`ms = Py_TYPE(o)->tp_as_sequence;

`

162

163

`if (ms && ms->sq_item) {

`

163

``

`-

if (PyIndex_Check(key)) {

`

``

164

`+

if (_PyIndex_Check(key)) {

`

164

165

`Py_ssize_t key_value;

`

165

166

`key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);

`

166

167

`if (key_value == -1 && PyErr_Occurred())

`

`@@ -176,7 +177,7 @@ PyObject_GetItem(PyObject *o, PyObject *key)

`

176

177

`if (PyType_Check(o)) {

`

177

178

`PyObject *meth, *result;

`

178

179

`_Py_IDENTIFIER(class_getitem);

`

179

``

-

``

180

+

180

181

`// Special case type[int], but disallow other types so str[int] fails

`

181

182

`if ((PyTypeObject*)o == &PyType_Type) {

`

182

183

`return Py_GenericAlias(o, key);

`

`@@ -209,7 +210,7 @@ PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)

`

209

210

`return m->mp_ass_subscript(o, key, value);

`

210

211

``

211

212

`if (Py_TYPE(o)->tp_as_sequence) {

`

212

``

`-

if (PyIndex_Check(key)) {

`

``

213

`+

if (_PyIndex_Check(key)) {

`

213

214

`Py_ssize_t key_value;

`

214

215

`key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);

`

215

216

`if (key_value == -1 && PyErr_Occurred())

`

`@@ -241,7 +242,7 @@ PyObject_DelItem(PyObject *o, PyObject *key)

`

241

242

`return m->mp_ass_subscript(o, key, (PyObject*)NULL);

`

242

243

``

243

244

`if (Py_TYPE(o)->tp_as_sequence) {

`

244

``

`-

if (PyIndex_Check(key)) {

`

``

245

`+

if (_PyIndex_Check(key)) {

`

245

246

`Py_ssize_t key_value;

`

246

247

`key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);

`

247

248

`if (key_value == -1 && PyErr_Occurred())

`

`@@ -1030,7 +1031,7 @@ static PyObject *

`

1030

1031

`sequence_repeat(ssizeargfunc repeatfunc, PyObject *seq, PyObject *n)

`

1031

1032

`{

`

1032

1033

`Py_ssize_t count;

`

1033

``

`-

if (PyIndex_Check(n)) {

`

``

1034

`+

if (_PyIndex_Check(n)) {

`

1034

1035

`count = PyNumber_AsSsize_t(n, PyExc_OverflowError);

`

1035

1036

`if (count == -1 && PyErr_Occurred())

`

1036

1037

`return NULL;

`

`@@ -1307,15 +1308,16 @@ PyNumber_Absolute(PyObject *o)

`

1307

1308

`return type_error("bad operand type for abs(): '%.200s'", o);

`

1308

1309

`}

`

1309

1310

``

``

1311

+

1310

1312

`#undef PyIndex_Check

`

1311

1313

``

1312

1314

`int

`

1313

1315

`PyIndex_Check(PyObject *obj)

`

1314

1316

`{

`

1315

``

`-

return Py_TYPE(obj)->tp_as_number != NULL &&

`

1316

``

`-

Py_TYPE(obj)->tp_as_number->nb_index != NULL;

`

``

1317

`+

return _PyIndex_Check(obj);

`

1317

1318

`}

`

1318

1319

``

``

1320

+

1319

1321

`/* Return a Python int from the object item.

`

1320

1322

` Raise TypeError if the result is not an int

`

1321

1323

` or if the object cannot be interpreted as an index.

`

`@@ -1332,7 +1334,7 @@ PyNumber_Index(PyObject *item)

`

1332

1334

`Py_INCREF(item);

`

1333

1335

`return item;

`

1334

1336

` }

`

1335

``

`-

if (!PyIndex_Check(item)) {

`

``

1337

`+

if (!_PyIndex_Check(item)) {

`

1336

1338

`PyErr_Format(PyExc_TypeError,

`

1337

1339

`"'%.200s' object cannot be interpreted "

`

1338

1340

`"as an integer", Py_TYPE(item)->tp_name);

`