--- /dev/null
+#ifndef Py_INTERNAL_ABSTRACT_H
+#define Py_INTERNAL_ABSTRACT_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifndef Py_BUILD_CORE
+# error "this header requires Py_BUILD_CORE define"
+#endif
+
+// Fast inlined version of PyIndex_Check()
+static inline int
+_PyIndex_Check(PyObject *obj)
+{
+ PyNumberMethods *tp_as_number = Py_TYPE(obj)->tp_as_number;
+ return (tp_as_number != NULL && tp_as_number->nb_index != NULL);
+}
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* !Py_INTERNAL_ABSTRACT_H */
$(srcdir)/Include/cpython/tupleobject.h \
$(srcdir)/Include/cpython/unicodeobject.h \
\
+ $(srcdir)/Include/internal/pycore_abstract.h \
$(srcdir)/Include/internal/pycore_accu.h \
$(srcdir)/Include/internal/pycore_atomic.h \
$(srcdir)/Include/internal/pycore_bytes_methods.h \
/* 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>
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())
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);
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())
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())
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;
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.
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);
#define PY_SSIZE_T_CLEAN
#include "Python.h"
+#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_bytes_methods.h"
#include "pycore_object.h"
#include "pycore_pymem.h"
static PyObject *
bytearray_subscript(PyByteArrayObject *self, PyObject *index)
{
- if (PyIndex_Check(index)) {
+ if (_PyIndex_Check(index)) {
Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
if (i == -1 && PyErr_Occurred())
char *buf, *bytes;
buf = PyByteArray_AS_STRING(self);
- if (PyIndex_Check(index)) {
+ if (_PyIndex_Check(index)) {
Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
if (i == -1 && PyErr_Occurred())
}
/* Is it an int? */
- if (PyIndex_Check(arg)) {
+ if (_PyIndex_Check(arg)) {
count = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
if (count == -1 && PyErr_Occurred()) {
if (!PyErr_ExceptionMatches(PyExc_TypeError))
#define PY_SSIZE_T_CLEAN
#include "Python.h"
+#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_bytes_methods.h"
PyDoc_STRVAR_shared(_Py_isspace__doc__,
return 1;
}
- if (!PyIndex_Check(tmp_subobj)) {
+ if (!_PyIndex_Check(tmp_subobj)) {
PyErr_Format(PyExc_TypeError,
"argument should be integer or bytes-like object, "
"not '%.200s'",
#define PY_SSIZE_T_CLEAN
#include "Python.h"
+#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_bytes_methods.h"
#include "pycore_object.h"
#include "pycore_pymem.h"
static PyObject*
bytes_subscript(PyBytesObject* self, PyObject* item)
{
- if (PyIndex_Check(item)) {
+ if (_PyIndex_Check(item)) {
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
if (i == -1 && PyErr_Occurred())
return NULL;
return NULL;
}
/* Is it an integer? */
- if (PyIndex_Check(x)) {
+ if (_PyIndex_Check(x)) {
size = PyNumber_AsSsize_t(x, PyExc_OverflowError);
if (size == -1 && PyErr_Occurred()) {
if (!PyErr_ExceptionMatches(PyExc_TypeError))
/* InterpreterID object */
#include "Python.h"
-#include "internal/pycore_pystate.h"
+#include "pycore_abstract.h" // _PyIndex_Check()
+#include "pycore_pystate.h"
#include "interpreteridobject.h"
if (PyObject_TypeCheck(arg, &_PyInterpreterID_Type)) {
id = ((interpid *)arg)->id;
}
- else if (PyIndex_Check(arg)) {
+ else if (_PyIndex_Check(arg)) {
id = PyLong_AsLongLong(arg);
if (id == -1 && PyErr_Occurred()) {
return 0;
/* List object implementation */
#include "Python.h"
+#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_object.h"
#include "pycore_pystate.h"
#include "pycore_tupleobject.h"
static PyObject *
list_subscript(PyListObject* self, PyObject* item)
{
- if (PyIndex_Check(item)) {
+ if (_PyIndex_Check(item)) {
Py_ssize_t i;
i = PyNumber_AsSsize_t(item, PyExc_IndexError);
if (i == -1 && PyErr_Occurred())
static int
list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
{
- if (PyIndex_Check(item)) {
+ if (_PyIndex_Check(item)) {
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
if (i == -1 && PyErr_Occurred())
return -1;
*/
#include "Python.h"
+#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_object.h"
#include "pycore_pymem.h"
#include "pycore_pystate.h"
size = PyTuple_GET_SIZE(key);
for (i = 0; i < size; i++) {
PyObject *x = PyTuple_GET_ITEM(key, i);
- if (!PyIndex_Check(x))
+ if (!_PyIndex_Check(x)) {
return 0;
+ }
}
return 1;
}
}
}
- if (PyIndex_Check(key)) {
+ if (_PyIndex_Check(key)) {
Py_ssize_t index;
index = PyNumber_AsSsize_t(key, PyExc_IndexError);
if (index == -1 && PyErr_Occurred())
}
}
- if (PyIndex_Check(key)) {
+ if (_PyIndex_Check(key)) {
Py_ssize_t index;
if (1 < view->ndim) {
PyErr_SetString(PyExc_NotImplementedError,
/* Range object implementation */
#include "Python.h"
-#include "structmember.h"
+#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_tupleobject.h"
+#include "structmember.h"
/* Support objects whose length is > PY_SSIZE_T_MAX.
static PyObject *
range_subscript(rangeobject* self, PyObject* item)
{
- if (PyIndex_Check(item)) {
+ if (_PyIndex_Check(item)) {
PyObject *i, *result;
i = PyNumber_Index(item);
if (!i)
*/
#include "Python.h"
+#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_object.h"
#include "pycore_pymem.h"
#include "pycore_pystate.h"
static PyObject*
evaluate_slice_index(PyObject *v)
{
- if (PyIndex_Check(v)) {
+ if (_PyIndex_Check(v)) {
return PyNumber_Index(v);
}
else {
/* Tuple object implementation */
#include "Python.h"
+#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_object.h"
#include "pycore_pystate.h"
#include "pycore_accu.h"
static PyObject*
tuplesubscript(PyTupleObject* self, PyObject* item)
{
- if (PyIndex_Check(item)) {
+ if (_PyIndex_Check(item)) {
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
if (i == -1 && PyErr_Occurred())
return NULL;
#define PY_SSIZE_T_CLEAN
#include "Python.h"
+#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_bytes_methods.h"
#include "pycore_fileutils.h"
#include "pycore_initconfig.h"
if (PyUnicode_READY(self) == -1)
return NULL;
- if (PyIndex_Check(item)) {
+ if (_PyIndex_Check(item)) {
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
if (i == -1 && PyErr_Occurred())
return NULL;
<ClInclude Include="..\Include\graminit.h" />
<ClInclude Include="..\Include\grammar.h" />
<ClInclude Include="..\Include\import.h" />
+ <ClInclude Include="..\Include\internal\pycore_abstract.h" />
<ClInclude Include="..\Include\internal\pycore_accu.h" />
<ClInclude Include="..\Include\internal\pycore_atomic.h" />
<ClInclude Include="..\Include\internal\pycore_bytes_methods.h" />
<ClInclude Include="..\Include\import.h">
<Filter>Include</Filter>
</ClInclude>
+ <ClInclude Include="..\Include\internal\pycore_abstract.h">
+ <Filter>Include</Filter>
+ </ClInclude>
<ClInclude Include="..\Include\internal\pycore_accu.h">
<Filter>Include</Filter>
</ClInclude>
#define PY_LOCAL_AGGRESSIVE
#include "Python.h"
+#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_call.h"
#include "pycore_ceval.h"
#include "pycore_code.h"
PyThreadState *tstate = _PyThreadState_GET();
if (v != Py_None) {
Py_ssize_t x;
- if (PyIndex_Check(v)) {
+ if (_PyIndex_Check(v)) {
x = PyNumber_AsSsize_t(v, NULL);
if (x == -1 && _PyErr_Occurred(tstate))
return 0;
{
PyThreadState *tstate = _PyThreadState_GET();
Py_ssize_t x;
- if (PyIndex_Check(v)) {
+ if (_PyIndex_Check(v)) {
x = PyNumber_AsSsize_t(v, NULL);
if (x == -1 && _PyErr_Occurred(tstate))
return 0;
/* Module support implementation */
#include "Python.h"
+#include "pycore_abstract.h" // _PyIndex_Check()
#define FLAG_SIZE_T 1
typedef double va_double;
if (obj == Py_None) {
return 1;
}
- else if (PyIndex_Check(obj)) {
+ else if (_PyIndex_Check(obj)) {
limit = PyNumber_AsSsize_t(obj, PyExc_OverflowError);
if (limit == -1 && PyErr_Occurred()) {
return 0;