bpo-33738: Fix macros which contradict PEP 384 (GH-7477) · pythoncapi/cpython@ea62ce7 · GitHub
Skip to content

Commit ea62ce7

Browse files
ctismerned-deily
authored andcommitted
bpo-33738: Fix macros which contradict PEP 384 (pythonGH-7477)
During development of the limited API support for PySide, we saw an error in a macro that accessed a type field. This patch fixes the 7 errors in the Python headers. Macros which were not written as capitals were implemented as function. To do the necessary analysis again, a script was included that parses all headers and looks for "->tp_" in serctions which can be reached with active limited API. It is easily possible to call this script as a test. Error listing: ../../Include/objimpl.h:243 #define PyObject_IS_GC(o) (PyType_IS_GC(Py_TYPE(o)) && \ (Py_TYPE(o)->tp_is_gc == NULL || Py_TYPE(o)->tp_is_gc(o))) Action: commented only ../../Include/objimpl.h:362 #define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0) Action: commented only ../../Include/objimpl.h:364 #define PyObject_GET_WEAKREFS_LISTPTR(o) \ ((PyObject **) (((char *) (o)) + Py_TYPE(o)->tp_weaklistoffset)) Action: commented only ../../Include/pyerrors.h:143 #define PyExceptionClass_Name(x) \ ((char *)(((PyTypeObject*)(x))->tp_name)) Action: implemented function ../../Include/abstract.h:593 #define PyIter_Check(obj) \ ((obj)->ob_type->tp_iternext != NULL && \ (obj)->ob_type->tp_iternext != &_PyObject_NextNotImplemented) Action: implemented function ../../Include/abstract.h:713 #define PyIndex_Check(obj) \ ((obj)->ob_type->tp_as_number != NULL && \ (obj)->ob_type->tp_as_number->nb_index != NULL) Action: implemented function ../../Include/abstract.h:924 #define PySequence_ITEM(o, i)\ ( Py_TYPE(o)->tp_as_sequence->sq_item(o, i) ) Action: commented only
1 parent 3f45f5d commit ea62ce7

8 files changed

Lines changed: 198 additions & 0 deletions

File tree

Include/abstract.h

Lines changed: 15 additions & 0 deletions

Include/objimpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,10 @@ PyAPI_FUNC(Py_ssize_t) _PyGC_CollectIfEnabled(void);
240240
#define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)
241241

242242
/* Test if an object has a GC head */
243+
#ifndef Py_LIMITED_API
243244
#define PyObject_IS_GC(o) (PyType_IS_GC(Py_TYPE(o)) && \
244245
(Py_TYPE(o)->tp_is_gc == NULL || Py_TYPE(o)->tp_is_gc(o)))
246+
#endif
245247

246248
PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t);
247249
#define PyObject_GC_Resize(type, op, n) \
@@ -359,10 +361,12 @@ PyAPI_FUNC(void) PyObject_GC_Del(void *);
359361

360362

361363
/* Test if a type supports weak references */
364+
#ifndef Py_LIMITED_API
362365
#define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0)
363366

364367
#define PyObject_GET_WEAKREFS_LISTPTR(o) \
365368
((PyObject **) (((char *) (o)) + Py_TYPE(o)->tp_weaklistoffset))
369+
#endif
366370

367371
#ifdef __cplusplus
368372
}

Include/pyerrors.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,12 @@ PyAPI_FUNC(void) _PyErr_ChainExceptions(PyObject *, PyObject *, PyObject *);
140140
#define PyExceptionInstance_Check(x) \
141141
PyType_FastSubclass((x)->ob_type, Py_TPFLAGS_BASE_EXC_SUBCLASS)
142142

143+
#ifndef Py_LIMITED_API
143144
#define PyExceptionClass_Name(x) \
144145
((char *)(((PyTypeObject*)(x))->tp_name))
146+
#else
147+
PyAPI_FUNC(char *) PyExceptionClass_Name(PyObject*);
148+
#endif
145149

146150
#define PyExceptionInstance_Class(x) ((PyObject*)((x)->ob_type))
147151

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Seven macro incompatibilities with the Limited API were fixed, and the
2+
macros PyIter_Check, PyIndex_Check and PyExceptionClass_Name were added as
3+
functions. A script for automatic macro checks was added.

Objects/abstract.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,14 @@ PyNumber_Absolute(PyObject *o)
12441244
return type_error("bad operand type for abs(): '%.200s'", o);
12451245
}
12461246

1247+
#undef PyIndex_Check
1248+
int
1249+
PyIndex_Check(PyObject *obj)
1250+
{
1251+
return obj->ob_type->tp_as_number != NULL &&
1252+
obj->ob_type->tp_as_number->nb_index != NULL;
1253+
}
1254+
12471255
/* Return a Python int from the object item.
12481256
Raise TypeError if the result is not an int
12491257
or if the object cannot be interpreted as an index.
@@ -2535,6 +2543,13 @@ PyObject_GetIter(PyObject *o)
25352543
}
25362544
}
25372545

2546+
#undef PyIter_Check
2547+
int PyIter_Check(PyObject *obj)
2548+
{
2549+
return obj->ob_type->tp_iternext != NULL &&
2550+
obj->ob_type->tp_iternext != &_PyObject_NextNotImplemented;
2551+
}
2552+
25382553
/* Return next item.
25392554
* If an error occurs, return NULL. PyErr_Occurred() will be true.
25402555
* If the iteration terminates normally, return NULL and clear the

Objects/exceptions.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,12 @@ PyException_SetContext(PyObject *self, PyObject *context)
342342
Py_XSETREF(((PyBaseExceptionObject *)self)->context, context);
343343
}
344344

345+
#undef PyExceptionClass_Name
346+
char *
347+
PyExceptionClass_Name(PyObject *ob)
348+
{
349+
return ((PyTypeObject*)ob)->tp_name;
350+
}
345351

346352
static struct PyMemberDef BaseException_members[] = {
347353
{"__suppress_context__", T_BOOL,

PC/python3.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ EXPORTS
248248
PyExc_Warning=python38.PyExc_Warning DATA
249249
PyExc_WindowsError=python38.PyExc_WindowsError DATA
250250
PyExc_ZeroDivisionError=python38.PyExc_ZeroDivisionError DATA
251+
PyExceptionClass_Name=python38.PyExceptionClass_Name
251252
PyException_GetCause=python38.PyException_GetCause
252253
PyException_GetContext=python38.PyException_GetContext
253254
PyException_GetTraceback=python38.PyException_GetTraceback
@@ -294,9 +295,11 @@ EXPORTS
294295
PyImport_ImportModuleLevelObject=python38.PyImport_ImportModuleLevelObject
295296
PyImport_ImportModuleNoBlock=python38.PyImport_ImportModuleNoBlock
296297
PyImport_ReloadModule=python38.PyImport_ReloadModule
298+
PyIndex_Check=python38.PyIndex_Check
297299
PyInterpreterState_Clear=python38.PyInterpreterState_Clear
298300
PyInterpreterState_Delete=python38.PyInterpreterState_Delete
299301
PyInterpreterState_New=python38.PyInterpreterState_New
302+
PyIter_Check=python38.PyIter_Check
300303
PyIter_Next=python38.PyIter_Next
301304
PyListIter_Type=python38.PyListIter_Type DATA
302305
PyListRevIter_Type=python38.PyListRevIter_Type DATA

Tools/scripts/pep384_macrocheck.py

Lines changed: 148 additions & 0 deletions

0 commit comments

Comments
 (0)