Skip to content

bpo-40170: Add PyType_SetBase() function #23153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Doc/c-api/type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,28 @@ Type Objects

.. versionadded:: 3.9

.. c:function:: void PyType_SetBase(PyTypeObject *type, PyTypeObject *base)

Set the base type of the given type. The *type* type stores a strong
reference to *base*.

If the *type* type already has a base type, decrement the reference count of
the old base type.

The *type* type must be a heap type.

.. versionadded:: 3.10

.. c:function:: void PyType_SetBaseStatic(PyTypeObject *type, PyTypeObject *base)

Similar to :c:func:`PyType_SetBase`, but the *type* type stores a borrowed
reference to *base* and leaves the reference count of the old base type
unchanged.

The *type* type must be a static type.

.. versionadded:: 3.10


Creating Heap-Allocated Types
.............................
Expand Down
2 changes: 1 addition & 1 deletion Doc/includes/sublist.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ PyMODINIT_FUNC
PyInit_sublist(void)
{
PyObject *m;
SubListType.tp_base = &PyList_Type;
PyType_SetBaseStatic(&SubListType, &PyList_Type);
if (PyType_Ready(&SubListType) < 0)
return NULL;

Expand Down
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ New Features
slot.
(Contributed by Hai Shi in :issue:`41832`.)

* Added :c:func:`PyType_SetBase` and :c:func:`PyType_SetBaseStatic`
functions to set the base type of a type.
(Contributed by Victor Stinner in :issue:`40170`.)


Porting to Python 3.10
----------------------
Expand Down
3 changes: 3 additions & 0 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,9 @@ static inline int _PyType_CheckExact(PyObject *op) {
}
#define PyType_CheckExact(op) _PyType_CheckExact(_PyObject_CAST(op))

PyAPI_FUNC(void) PyType_SetBase(PyTypeObject *type, PyTypeObject *base);
PyAPI_FUNC(void) PyType_SetBaseStatic(PyTypeObject *type, PyTypeObject *base);

#ifdef __cplusplus
}
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added :c:func:`PyType_SetBase` and :c:func:`PyType_SetBaseStatic` functions
to set the base type of a type. Patch by Victor Stinner.
2 changes: 1 addition & 1 deletion Modules/_collectionsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2565,7 +2565,7 @@ collections_exec(PyObject *module) {
&tuplegetter_type
};

defdict_type.tp_base = &PyDict_Type;
PyType_SetBaseStatic(&defdict_type, &PyDict_Type);

for (size_t i = 0; i < Py_ARRAY_LENGTH(typelist); i++) {
if (PyModule_AddType(module, typelist[i]) < 0) {
Expand Down
28 changes: 14 additions & 14 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -5560,7 +5560,7 @@ static PyTypeObject PyComError_Type = {
static int
create_comerror(void)
{
PyComError_Type.tp_base = (PyTypeObject*)PyExc_Exception;
PyType_SetBaseStatic(&PyComError_Type, (PyTypeObject*)PyExc_Exception);
if (PyType_Ready(&PyComError_Type) < 0)
return -1;
Py_INCREF(&PyComError_Type);
Expand Down Expand Up @@ -5719,7 +5719,7 @@ PyInit__ctypes(void)
return NULL;

/* StgDict is derived from PyDict_Type */
PyCStgDict_Type.tp_base = &PyDict_Type;
PyType_SetBaseStatic(&PyCStgDict_Type, &PyDict_Type);
if (PyType_Ready(&PyCStgDict_Type) < 0)
return NULL;

Expand All @@ -5728,27 +5728,27 @@ PyInit__ctypes(void)
* Metaclasses
*/

PyCStructType_Type.tp_base = &PyType_Type;
PyType_SetBaseStatic(&PyCStructType_Type, &PyType_Type);
if (PyType_Ready(&PyCStructType_Type) < 0)
return NULL;

UnionType_Type.tp_base = &PyType_Type;
PyType_SetBaseStatic(&UnionType_Type, &PyType_Type);
if (PyType_Ready(&UnionType_Type) < 0)
return NULL;

PyCPointerType_Type.tp_base = &PyType_Type;
PyType_SetBaseStatic(&PyCPointerType_Type, &PyType_Type);
if (PyType_Ready(&PyCPointerType_Type) < 0)
return NULL;

PyCArrayType_Type.tp_base = &PyType_Type;
PyType_SetBaseStatic(&PyCArrayType_Type, &PyType_Type);
if (PyType_Ready(&PyCArrayType_Type) < 0)
return NULL;

PyCSimpleType_Type.tp_base = &PyType_Type;
PyType_SetBaseStatic(&PyCSimpleType_Type, &PyType_Type);
if (PyType_Ready(&PyCSimpleType_Type) < 0)
return NULL;

PyCFuncPtrType_Type.tp_base = &PyType_Type;
PyType_SetBaseStatic(&PyCFuncPtrType_Type, &PyType_Type);
if (PyType_Ready(&PyCFuncPtrType_Type) < 0)
return NULL;

Expand All @@ -5761,42 +5761,42 @@ PyInit__ctypes(void)
return NULL;

Py_SET_TYPE(&Struct_Type, &PyCStructType_Type);
Struct_Type.tp_base = &PyCData_Type;
PyType_SetBaseStatic(&Struct_Type, &PyCData_Type);
if (PyType_Ready(&Struct_Type) < 0)
return NULL;
Py_INCREF(&Struct_Type);
PyModule_AddObject(m, "Structure", (PyObject *)&Struct_Type);

Py_SET_TYPE(&Union_Type, &UnionType_Type);
Union_Type.tp_base = &PyCData_Type;
PyType_SetBaseStatic(&Union_Type, &PyCData_Type);
if (PyType_Ready(&Union_Type) < 0)
return NULL;
Py_INCREF(&Union_Type);
PyModule_AddObject(m, "Union", (PyObject *)&Union_Type);

Py_SET_TYPE(&PyCPointer_Type, &PyCPointerType_Type);
PyCPointer_Type.tp_base = &PyCData_Type;
PyType_SetBaseStatic(&PyCPointer_Type, &PyCData_Type);
if (PyType_Ready(&PyCPointer_Type) < 0)
return NULL;
Py_INCREF(&PyCPointer_Type);
PyModule_AddObject(m, "_Pointer", (PyObject *)&PyCPointer_Type);

Py_SET_TYPE(&PyCArray_Type, &PyCArrayType_Type);
PyCArray_Type.tp_base = &PyCData_Type;
PyType_SetBaseStatic(&PyCArray_Type, &PyCData_Type);
if (PyType_Ready(&PyCArray_Type) < 0)
return NULL;
Py_INCREF(&PyCArray_Type);
PyModule_AddObject(m, "Array", (PyObject *)&PyCArray_Type);

Py_SET_TYPE(&Simple_Type, &PyCSimpleType_Type);
Simple_Type.tp_base = &PyCData_Type;
PyType_SetBaseStatic(&Simple_Type, &PyCData_Type);
if (PyType_Ready(&Simple_Type) < 0)
return NULL;
Py_INCREF(&Simple_Type);
PyModule_AddObject(m, "_SimpleCData", (PyObject *)&Simple_Type);

Py_SET_TYPE(&PyCFuncPtr_Type, &PyCFuncPtrType_Type);
PyCFuncPtr_Type.tp_base = &PyCData_Type;
PyType_SetBaseStatic(&PyCFuncPtr_Type, &PyCData_Type);
if (PyType_Ready(&PyCFuncPtr_Type) < 0)
return NULL;
Py_INCREF(&PyCFuncPtr_Type);
Expand Down
6 changes: 3 additions & 3 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -6523,9 +6523,9 @@ PyInit__datetime(void)
// `&...` is not a constant expression according to a strict reading
// of C standards. Fill tp_base at run-time rather than statically.
// See https://bugs.python.org/issue40777
PyDateTime_IsoCalendarDateType.tp_base = &PyTuple_Type;
PyDateTime_TimeZoneType.tp_base = &PyDateTime_TZInfoType;
PyDateTime_DateTimeType.tp_base = &PyDateTime_DateType;
PyType_SetBaseStatic(&PyDateTime_IsoCalendarDateType, &PyTuple_Type);
PyType_SetBaseStatic(&PyDateTime_TimeZoneType, &PyDateTime_TZInfoType);
PyType_SetBaseStatic(&PyDateTime_DateTimeType, &PyDateTime_DateType);

PyTypeObject *types[] = {
&PyDateTime_DateType,
Expand Down
8 changes: 4 additions & 4 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -5867,10 +5867,10 @@ PyInit__decimal(void)


/* Init types */
PyDec_Type.tp_base = &PyBaseObject_Type;
PyDecContext_Type.tp_base = &PyBaseObject_Type;
PyDecContextManager_Type.tp_base = &PyBaseObject_Type;
PyDecSignalDictMixin_Type.tp_base = &PyBaseObject_Type;
PyType_SetBaseStatic(&PyDec_Type, &PyBaseObject_Type);
PyType_SetBaseStatic(&PyDecContext_Type, &PyBaseObject_Type);
PyType_SetBaseStatic(&PyDecContextManager_Type, &PyBaseObject_Type);
PyType_SetBaseStatic(&PyDecSignalDictMixin_Type, &PyBaseObject_Type);

CHECK_INT(PyType_Ready(&PyDec_Type));
CHECK_INT(PyType_Ready(&PyDecContext_Type));
Expand Down
18 changes: 9 additions & 9 deletions Modules/_io/_iomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -691,43 +691,43 @@ PyInit__io(void)

/* Implementation of concrete IO objects. */
/* FileIO */
PyFileIO_Type.tp_base = &PyRawIOBase_Type;
PyType_SetBaseStatic(&PyFileIO_Type, &PyRawIOBase_Type);
ADD_TYPE(&PyFileIO_Type);

/* BytesIO */
PyBytesIO_Type.tp_base = &PyBufferedIOBase_Type;
PyType_SetBaseStatic(&PyBytesIO_Type, &PyBufferedIOBase_Type);
ADD_TYPE(&PyBytesIO_Type);
if (PyType_Ready(&_PyBytesIOBuffer_Type) < 0)
goto fail;

/* StringIO */
PyStringIO_Type.tp_base = &PyTextIOBase_Type;
PyType_SetBaseStatic(&PyStringIO_Type, &PyTextIOBase_Type);
ADD_TYPE(&PyStringIO_Type);

#ifdef MS_WINDOWS
/* WindowsConsoleIO */
PyWindowsConsoleIO_Type.tp_base = &PyRawIOBase_Type;
PyType_SetBaseStatic(&PyWindowsConsoleIO_Type, &PyRawIOBase_Type);
ADD_TYPE(&PyWindowsConsoleIO_Type);
#endif

/* BufferedReader */
PyBufferedReader_Type.tp_base = &PyBufferedIOBase_Type;
PyType_SetBaseStatic(&PyBufferedReader_Type, &PyBufferedIOBase_Type);
ADD_TYPE(&PyBufferedReader_Type);

/* BufferedWriter */
PyBufferedWriter_Type.tp_base = &PyBufferedIOBase_Type;
PyType_SetBaseStatic(&PyBufferedWriter_Type, &PyBufferedIOBase_Type);
ADD_TYPE(&PyBufferedWriter_Type);

/* BufferedRWPair */
PyBufferedRWPair_Type.tp_base = &PyBufferedIOBase_Type;
PyType_SetBaseStatic(&PyBufferedRWPair_Type, &PyBufferedIOBase_Type);
ADD_TYPE(&PyBufferedRWPair_Type);

/* BufferedRandom */
PyBufferedRandom_Type.tp_base = &PyBufferedIOBase_Type;
PyType_SetBaseStatic(&PyBufferedRandom_Type, &PyBufferedIOBase_Type);
ADD_TYPE(&PyBufferedRandom_Type);

/* TextIOWrapper */
PyTextIOWrapper_Type.tp_base = &PyTextIOBase_Type;
PyType_SetBaseStatic(&PyTextIOWrapper_Type, &PyTextIOBase_Type);
ADD_TYPE(&PyTextIOWrapper_Type);

/* IncrementalNewlineDecoder */
Expand Down
10 changes: 5 additions & 5 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -7093,7 +7093,7 @@ PyInit__testcapi(void)
Py_INCREF(&awaitType);
PyModule_AddObject(m, "awaitType", (PyObject *)&awaitType);

MyList_Type.tp_base = &PyList_Type;
PyType_SetBaseStatic(&MyList_Type, &PyList_Type);
if (PyType_Ready(&MyList_Type) < 0)
return NULL;
Py_INCREF(&MyList_Type);
Expand All @@ -7104,19 +7104,19 @@ PyInit__testcapi(void)
Py_INCREF(&MethodDescriptorBase_Type);
PyModule_AddObject(m, "MethodDescriptorBase", (PyObject *)&MethodDescriptorBase_Type);

MethodDescriptorDerived_Type.tp_base = &MethodDescriptorBase_Type;
PyType_SetBaseStatic(&MethodDescriptorDerived_Type, &MethodDescriptorBase_Type);
if (PyType_Ready(&MethodDescriptorDerived_Type) < 0)
return NULL;
Py_INCREF(&MethodDescriptorDerived_Type);
PyModule_AddObject(m, "MethodDescriptorDerived", (PyObject *)&MethodDescriptorDerived_Type);

MethodDescriptorNopGet_Type.tp_base = &MethodDescriptorBase_Type;
PyType_SetBaseStatic(&MethodDescriptorNopGet_Type, &MethodDescriptorBase_Type);
if (PyType_Ready(&MethodDescriptorNopGet_Type) < 0)
return NULL;
Py_INCREF(&MethodDescriptorNopGet_Type);
PyModule_AddObject(m, "MethodDescriptorNopGet", (PyObject *)&MethodDescriptorNopGet_Type);

MethodDescriptor2_Type.tp_base = &MethodDescriptorBase_Type;
PyType_SetBaseStatic(&MethodDescriptor2_Type, &MethodDescriptorBase_Type);
if (PyType_Ready(&MethodDescriptor2_Type) < 0)
return NULL;
Py_INCREF(&MethodDescriptor2_Type);
Expand Down Expand Up @@ -7147,7 +7147,7 @@ PyInit__testcapi(void)
Py_INCREF(&MethStatic_Type);
PyModule_AddObject(m, "MethStatic", (PyObject *)&MethStatic_Type);

PyRecursingInfinitelyError_Type.tp_base = (PyTypeObject *)PyExc_Exception;
PyType_SetBaseStatic(&PyRecursingInfinitelyError_Type, (PyTypeObject *)PyExc_Exception);
if (PyType_Ready(&PyRecursingInfinitelyError_Type) < 0) {
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/_zoneinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -2619,7 +2619,7 @@ static int
zoneinfomodule_exec(PyObject *m)
{
PyDateTime_IMPORT;
PyZoneInfo_ZoneInfoType.tp_base = PyDateTimeAPI->TZInfoType;
PyType_SetBaseStatic(&PyZoneInfo_ZoneInfoType, PyDateTimeAPI->TZInfoType);
if (PyType_Ready(&PyZoneInfo_ZoneInfoType) < 0) {
goto error;
}
Expand Down
4 changes: 2 additions & 2 deletions Modules/xxmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ xx_exec(PyObject *m)
Both compilers are strictly standard conforming in this particular
behavior.
*/
Null_Type.tp_base = &PyBaseObject_Type;
Str_Type.tp_base = &PyUnicode_Type;
PyType_SetBaseStatic(&Null_Type, &PyBaseObject_Type);
PyType_SetBaseStatic(&Str_Type, &PyUnicode_Type);

/* Finalize the type object including setting type of the new type
* object; doing it here is required for portability, too. */
Expand Down
4 changes: 2 additions & 2 deletions Modules/xxsubtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,11 @@ xxsubtype_exec(PyObject* m)
PyType_Ready() is called. Note that PyType_Ready() automatically
initializes the ob.ob_type field to &PyType_Type if it's NULL,
so it's not necessary to fill in ob_type first. */
spamdict_type.tp_base = &PyDict_Type;
PyType_SetBaseStatic(&spamdict_type, &PyDict_Type);
if (PyType_Ready(&spamdict_type) < 0)
return -1;

spamlist_type.tp_base = &PyList_Type;
PyType_SetBaseStatic(&spamlist_type, &PyList_Type);
if (PyType_Ready(&spamlist_type) < 0)
return -1;

Expand Down
2 changes: 1 addition & 1 deletion Objects/structseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc)
type->tp_dealloc = (destructor)structseq_dealloc;
type->tp_repr = (reprfunc)structseq_repr;
type->tp_doc = desc->doc;
type->tp_base = &PyTuple_Type;
PyType_SetBaseStatic(type, &PyTuple_Type);
type->tp_methods = structseq_methods;
type->tp_new = structseq_new;
type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC;
Expand Down
Loading