mirror of https://github.com/python/cpython
bpo-39573: Use Py_TYPE() macro in Python and Include directories (GH-18391)
Replace direct access to PyObject.ob_type with Py_TYPE().
This commit is contained in:
parent
38aaaaac80
commit
a102ed7d2f
|
@ -19,7 +19,7 @@ typedef struct {
|
||||||
|
|
||||||
PyAPI_DATA(PyTypeObject) PyMethod_Type;
|
PyAPI_DATA(PyTypeObject) PyMethod_Type;
|
||||||
|
|
||||||
#define PyMethod_Check(op) ((op)->ob_type == &PyMethod_Type)
|
#define PyMethod_Check(op) (Py_TYPE(op)== &PyMethod_Type)
|
||||||
|
|
||||||
PyAPI_FUNC(PyObject *) PyMethod_New(PyObject *, PyObject *);
|
PyAPI_FUNC(PyObject *) PyMethod_New(PyObject *, PyObject *);
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ typedef struct {
|
||||||
|
|
||||||
PyAPI_DATA(PyTypeObject) PyInstanceMethod_Type;
|
PyAPI_DATA(PyTypeObject) PyInstanceMethod_Type;
|
||||||
|
|
||||||
#define PyInstanceMethod_Check(op) ((op)->ob_type == &PyInstanceMethod_Type)
|
#define PyInstanceMethod_Check(op) (Py_TYPE(op) == &PyInstanceMethod_Type)
|
||||||
|
|
||||||
PyAPI_FUNC(PyObject *) PyInstanceMethod_New(PyObject *);
|
PyAPI_FUNC(PyObject *) PyInstanceMethod_New(PyObject *);
|
||||||
PyAPI_FUNC(PyObject *) PyInstanceMethod_Function(PyObject *);
|
PyAPI_FUNC(PyObject *) PyInstanceMethod_Function(PyObject *);
|
||||||
|
|
|
@ -246,8 +246,8 @@ PyAPI_FUNC(Py_ssize_t) PyObject_LengthHint(PyObject *o, Py_ssize_t);
|
||||||
|
|
||||||
/* Return 1 if the getbuffer function is available, otherwise return 0. */
|
/* Return 1 if the getbuffer function is available, otherwise return 0. */
|
||||||
#define PyObject_CheckBuffer(obj) \
|
#define PyObject_CheckBuffer(obj) \
|
||||||
(((obj)->ob_type->tp_as_buffer != NULL) && \
|
((Py_TYPE(obj)->tp_as_buffer != NULL) && \
|
||||||
((obj)->ob_type->tp_as_buffer->bf_getbuffer != NULL))
|
(Py_TYPE(obj)->tp_as_buffer->bf_getbuffer != NULL))
|
||||||
|
|
||||||
/* This is a C-API version of the getbuffer function call. It checks
|
/* This is a C-API version of the getbuffer function call. It checks
|
||||||
to make sure object has the required function pointer and issues the
|
to make sure object has the required function pointer and issues the
|
||||||
|
@ -315,14 +315,14 @@ PyAPI_FUNC(void) PyBuffer_Release(Py_buffer *view);
|
||||||
/* ==== Iterators ================================================ */
|
/* ==== Iterators ================================================ */
|
||||||
|
|
||||||
#define PyIter_Check(obj) \
|
#define PyIter_Check(obj) \
|
||||||
((obj)->ob_type->tp_iternext != NULL && \
|
(Py_TYPE(obj)->tp_iternext != NULL && \
|
||||||
(obj)->ob_type->tp_iternext != &_PyObject_NextNotImplemented)
|
Py_TYPE(obj)->tp_iternext != &_PyObject_NextNotImplemented)
|
||||||
|
|
||||||
/* === Number Protocol ================================================== */
|
/* === Number Protocol ================================================== */
|
||||||
|
|
||||||
#define PyIndex_Check(obj) \
|
#define PyIndex_Check(obj) \
|
||||||
((obj)->ob_type->tp_as_number != NULL && \
|
(Py_TYPE(obj)->tp_as_number != NULL && \
|
||||||
(obj)->ob_type->tp_as_number->nb_index != NULL)
|
Py_TYPE(obj)->tp_as_number->nb_index != NULL)
|
||||||
|
|
||||||
/* === Sequence protocol ================================================ */
|
/* === Sequence protocol ================================================ */
|
||||||
|
|
||||||
|
|
|
@ -54,11 +54,11 @@ PyAPI_FUNC(void) PyException_SetContext(PyObject *, PyObject *);
|
||||||
PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
|
PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
|
||||||
|
|
||||||
#define PyExceptionInstance_Check(x) \
|
#define PyExceptionInstance_Check(x) \
|
||||||
PyType_FastSubclass((x)->ob_type, Py_TPFLAGS_BASE_EXC_SUBCLASS)
|
PyType_FastSubclass(Py_TYPE(x), Py_TPFLAGS_BASE_EXC_SUBCLASS)
|
||||||
|
|
||||||
PyAPI_FUNC(const char *) PyExceptionClass_Name(PyObject *);
|
PyAPI_FUNC(const char *) PyExceptionClass_Name(PyObject *);
|
||||||
|
|
||||||
#define PyExceptionInstance_Class(x) ((PyObject*)((x)->ob_type))
|
#define PyExceptionInstance_Class(x) ((PyObject*)Py_TYPE(x))
|
||||||
|
|
||||||
|
|
||||||
/* Predefined exceptions */
|
/* Predefined exceptions */
|
||||||
|
|
|
@ -650,7 +650,7 @@ warn_explicit(PyObject *category, PyObject *message,
|
||||||
text = PyObject_Str(message);
|
text = PyObject_Str(message);
|
||||||
if (text == NULL)
|
if (text == NULL)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
category = (PyObject*)message->ob_type;
|
category = (PyObject*)Py_TYPE(message);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
text = message;
|
text = message;
|
||||||
|
@ -906,7 +906,7 @@ get_category(PyObject *message, PyObject *category)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (rc == 1)
|
if (rc == 1)
|
||||||
category = (PyObject*)message->ob_type;
|
category = (PyObject*)Py_TYPE(message);
|
||||||
else if (category == NULL || category == Py_None)
|
else if (category == NULL || category == Py_None)
|
||||||
category = PyExc_UserWarning;
|
category = PyExc_UserWarning;
|
||||||
|
|
||||||
|
|
|
@ -170,7 +170,7 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
|
||||||
/* else get the type of the first base */
|
/* else get the type of the first base */
|
||||||
else {
|
else {
|
||||||
PyObject *base0 = PyTuple_GET_ITEM(bases, 0);
|
PyObject *base0 = PyTuple_GET_ITEM(bases, 0);
|
||||||
meta = (PyObject *) (base0->ob_type);
|
meta = (PyObject *)Py_TYPE(base0);
|
||||||
}
|
}
|
||||||
Py_INCREF(meta);
|
Py_INCREF(meta);
|
||||||
isclass = 1; /* meta is really a class */
|
isclass = 1; /* meta is really a class */
|
||||||
|
@ -1002,13 +1002,13 @@ builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
|
||||||
|
|
||||||
if (!PyDict_Check(globals)) {
|
if (!PyDict_Check(globals)) {
|
||||||
PyErr_Format(PyExc_TypeError, "exec() globals must be a dict, not %.100s",
|
PyErr_Format(PyExc_TypeError, "exec() globals must be a dict, not %.100s",
|
||||||
globals->ob_type->tp_name);
|
Py_TYPE(globals)->tp_name);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (!PyMapping_Check(locals)) {
|
if (!PyMapping_Check(locals)) {
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"locals must be a mapping or None, not %.100s",
|
"locals must be a mapping or None, not %.100s",
|
||||||
locals->ob_type->tp_name);
|
Py_TYPE(locals)->tp_name);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (_PyDict_GetItemIdWithError(globals, &PyId___builtins__) == NULL) {
|
if (_PyDict_GetItemIdWithError(globals, &PyId___builtins__) == NULL) {
|
||||||
|
@ -1383,11 +1383,11 @@ builtin_next(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
|
||||||
if (!PyIter_Check(it)) {
|
if (!PyIter_Check(it)) {
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"'%.200s' object is not an iterator",
|
"'%.200s' object is not an iterator",
|
||||||
it->ob_type->tp_name);
|
Py_TYPE(it)->tp_name);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
res = (*it->ob_type->tp_iternext)(it);
|
res = (*Py_TYPE(it)->tp_iternext)(it);
|
||||||
if (res != NULL) {
|
if (res != NULL) {
|
||||||
return res;
|
return res;
|
||||||
} else if (nargs > 1) {
|
} else if (nargs > 1) {
|
||||||
|
@ -1788,7 +1788,7 @@ builtin_ord(PyObject *module, PyObject *c)
|
||||||
else {
|
else {
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"ord() expected string of length 1, but " \
|
"ord() expected string of length 1, but " \
|
||||||
"%.200s found", c->ob_type->tp_name);
|
"%.200s found", Py_TYPE(c)->tp_name);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1856,7 +1856,7 @@ builtin_print(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject
|
||||||
else if (sep && !PyUnicode_Check(sep)) {
|
else if (sep && !PyUnicode_Check(sep)) {
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"sep must be None or a string, not %.200s",
|
"sep must be None or a string, not %.200s",
|
||||||
sep->ob_type->tp_name);
|
Py_TYPE(sep)->tp_name);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (end == Py_None) {
|
if (end == Py_None) {
|
||||||
|
@ -1865,7 +1865,7 @@ builtin_print(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject
|
||||||
else if (end && !PyUnicode_Check(end)) {
|
else if (end && !PyUnicode_Check(end)) {
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"end must be None or a string, not %.200s",
|
"end must be None or a string, not %.200s",
|
||||||
end->ob_type->tp_name);
|
Py_TYPE(end)->tp_name);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2633,7 +2633,7 @@ main_loop:
|
||||||
PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
|
PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
|
||||||
if (none_val == NULL) {
|
if (none_val == NULL) {
|
||||||
if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
|
if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
|
||||||
(iterable->ob_type->tp_iter == NULL && !PySequence_Check(iterable)))
|
(Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
|
||||||
{
|
{
|
||||||
_PyErr_Clear(tstate);
|
_PyErr_Clear(tstate);
|
||||||
_PyErr_Format(tstate, PyExc_TypeError,
|
_PyErr_Format(tstate, PyExc_TypeError,
|
||||||
|
@ -2803,7 +2803,7 @@ main_loop:
|
||||||
if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
|
if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
|
||||||
_PyErr_Format(tstate, PyExc_TypeError,
|
_PyErr_Format(tstate, PyExc_TypeError,
|
||||||
"'%.200s' object is not a mapping",
|
"'%.200s' object is not a mapping",
|
||||||
update->ob_type->tp_name);
|
Py_TYPE(update)->tp_name);
|
||||||
}
|
}
|
||||||
Py_DECREF(update);
|
Py_DECREF(update);
|
||||||
goto error;
|
goto error;
|
||||||
|
@ -3158,7 +3158,7 @@ main_loop:
|
||||||
PREDICTED(FOR_ITER);
|
PREDICTED(FOR_ITER);
|
||||||
/* before: [iter]; after: [iter, iter()] *or* [] */
|
/* before: [iter]; after: [iter, iter()] *or* [] */
|
||||||
PyObject *iter = TOP();
|
PyObject *iter = TOP();
|
||||||
PyObject *next = (*iter->ob_type->tp_iternext)(iter);
|
PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
|
||||||
if (next != NULL) {
|
if (next != NULL) {
|
||||||
PUSH(next);
|
PUSH(next);
|
||||||
PREDICT(STORE_FAST);
|
PREDICT(STORE_FAST);
|
||||||
|
@ -4369,11 +4369,11 @@ unpack_iterable(PyThreadState *tstate, PyObject *v,
|
||||||
it = PyObject_GetIter(v);
|
it = PyObject_GetIter(v);
|
||||||
if (it == NULL) {
|
if (it == NULL) {
|
||||||
if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
|
if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
|
||||||
v->ob_type->tp_iter == NULL && !PySequence_Check(v))
|
Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
|
||||||
{
|
{
|
||||||
_PyErr_Format(tstate, PyExc_TypeError,
|
_PyErr_Format(tstate, PyExc_TypeError,
|
||||||
"cannot unpack non-iterable %.200s object",
|
"cannot unpack non-iterable %.200s object",
|
||||||
v->ob_type->tp_name);
|
Py_TYPE(v)->tp_name);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -4790,7 +4790,7 @@ PyEval_GetFuncName(PyObject *func)
|
||||||
else if (PyCFunction_Check(func))
|
else if (PyCFunction_Check(func))
|
||||||
return ((PyCFunctionObject*)func)->m_ml->ml_name;
|
return ((PyCFunctionObject*)func)->m_ml->ml_name;
|
||||||
else
|
else
|
||||||
return func->ob_type->tp_name;
|
return Py_TYPE(func)->tp_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *
|
const char *
|
||||||
|
@ -5197,7 +5197,7 @@ import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
|
||||||
static int
|
static int
|
||||||
check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
|
check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
|
||||||
{
|
{
|
||||||
if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
|
if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
|
||||||
/* check_args_iterable() may be called with a live exception:
|
/* check_args_iterable() may be called with a live exception:
|
||||||
* clear it to prevent calling _PyObject_FunctionStr() with an
|
* clear it to prevent calling _PyObject_FunctionStr() with an
|
||||||
* exception set. */
|
* exception set. */
|
||||||
|
|
|
@ -658,7 +658,7 @@ static void wrong_exception_type(PyObject *exc)
|
||||||
{
|
{
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"don't know how to handle %.200s in error callback",
|
"don't know how to handle %.200s in error callback",
|
||||||
exc->ob_type->tp_name);
|
Py_TYPE(exc)->tp_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *PyCodec_StrictErrors(PyObject *exc)
|
PyObject *PyCodec_StrictErrors(PyObject *exc)
|
||||||
|
|
|
@ -3988,7 +3988,7 @@ infer_type(expr_ty e)
|
||||||
case FormattedValue_kind:
|
case FormattedValue_kind:
|
||||||
return &PyUnicode_Type;
|
return &PyUnicode_Type;
|
||||||
case Constant_kind:
|
case Constant_kind:
|
||||||
return e->v.Constant.value->ob_type;
|
return Py_TYPE(e->v.Constant.value);
|
||||||
default:
|
default:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1447,7 +1447,7 @@ _PyUnicode_FormatAdvancedWriter(_PyUnicodeWriter *writer,
|
||||||
return format_string_internal(obj, &format, writer);
|
return format_string_internal(obj, &format, writer);
|
||||||
default:
|
default:
|
||||||
/* unknown */
|
/* unknown */
|
||||||
unknown_presentation_type(format.type, obj->ob_type->tp_name);
|
unknown_presentation_type(format.type, Py_TYPE(obj)->tp_name);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1505,7 +1505,7 @@ _PyLong_FormatAdvancedWriter(_PyUnicodeWriter *writer,
|
||||||
|
|
||||||
default:
|
default:
|
||||||
/* unknown */
|
/* unknown */
|
||||||
unknown_presentation_type(format.type, obj->ob_type->tp_name);
|
unknown_presentation_type(format.type, Py_TYPE(obj)->tp_name);
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1549,7 +1549,7 @@ _PyFloat_FormatAdvancedWriter(_PyUnicodeWriter *writer,
|
||||||
|
|
||||||
default:
|
default:
|
||||||
/* unknown */
|
/* unknown */
|
||||||
unknown_presentation_type(format.type, obj->ob_type->tp_name);
|
unknown_presentation_type(format.type, Py_TYPE(obj)->tp_name);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1587,7 +1587,7 @@ _PyComplex_FormatAdvancedWriter(_PyUnicodeWriter *writer,
|
||||||
|
|
||||||
default:
|
default:
|
||||||
/* unknown */
|
/* unknown */
|
||||||
unknown_presentation_type(format.type, obj->ob_type->tp_name);
|
unknown_presentation_type(format.type, Py_TYPE(obj)->tp_name);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -531,7 +531,7 @@ converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
|
||||||
toplevel ? "expected %d arguments, not %.50s" :
|
toplevel ? "expected %d arguments, not %.50s" :
|
||||||
"must be %d-item sequence, not %.50s",
|
"must be %d-item sequence, not %.50s",
|
||||||
n,
|
n,
|
||||||
arg == Py_None ? "None" : arg->ob_type->tp_name);
|
arg == Py_None ? "None" : Py_TYPE(arg)->tp_name);
|
||||||
return msgbuf;
|
return msgbuf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -621,7 +621,7 @@ _PyArg_BadArgument(const char *fname, const char *displayname,
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"%.200s() %.200s must be %.50s, not %.50s",
|
"%.200s() %.200s must be %.50s, not %.50s",
|
||||||
fname, displayname, expected,
|
fname, displayname, expected,
|
||||||
arg == Py_None ? "None" : arg->ob_type->tp_name);
|
arg == Py_None ? "None" : Py_TYPE(arg)->tp_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
|
@ -636,7 +636,7 @@ converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
|
||||||
else {
|
else {
|
||||||
PyOS_snprintf(msgbuf, bufsize,
|
PyOS_snprintf(msgbuf, bufsize,
|
||||||
"must be %.50s, not %.50s", expected,
|
"must be %.50s, not %.50s", expected,
|
||||||
arg == Py_None ? "None" : arg->ob_type->tp_name);
|
arg == Py_None ? "None" : Py_TYPE(arg)->tp_name);
|
||||||
}
|
}
|
||||||
return msgbuf;
|
return msgbuf;
|
||||||
}
|
}
|
||||||
|
@ -1331,7 +1331,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
|
||||||
type = va_arg(*p_va, PyTypeObject*);
|
type = va_arg(*p_va, PyTypeObject*);
|
||||||
p = va_arg(*p_va, PyObject **);
|
p = va_arg(*p_va, PyObject **);
|
||||||
format++;
|
format++;
|
||||||
if (PyType_IsSubtype(arg->ob_type, type))
|
if (PyType_IsSubtype(Py_TYPE(arg), type))
|
||||||
*p = arg;
|
*p = arg;
|
||||||
else
|
else
|
||||||
return converterr(type->tp_name, arg, msgbuf, bufsize);
|
return converterr(type->tp_name, arg, msgbuf, bufsize);
|
||||||
|
|
|
@ -1696,7 +1696,7 @@ marshal_load(PyObject *module, PyObject *file)
|
||||||
if (!PyBytes_Check(data)) {
|
if (!PyBytes_Check(data)) {
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"file.read() returned not bytes but %.100s",
|
"file.read() returned not bytes but %.100s",
|
||||||
data->ob_type->tp_name);
|
Py_TYPE(data)->tp_name);
|
||||||
result = NULL;
|
result = NULL;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -840,7 +840,7 @@ sys_intern_impl(PyObject *module, PyObject *s)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
_PyErr_Format(tstate, PyExc_TypeError,
|
_PyErr_Format(tstate, PyExc_TypeError,
|
||||||
"can't intern %.400s", s->ob_type->tp_name);
|
"can't intern %.400s", Py_TYPE(s)->tp_name);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue