GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-40170: Add PyType_SetBase() function by vstinner · Pull Request #23153 · python/cpython

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.

Sign up for GitHub

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
vstinner wants to merge 1 commit into python:master from vstinner:pytype_setbase

Uh oh!

There was an error while loading. Please reload this page.

Closed

bpo-40170: Add PyType_SetBase() function #23153

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
82bd889
bpo-40170: Add PyType_SetBase() function
vstinner Nov 4, 2020
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

22 changes: 22 additions & 0 deletions 22 Doc/c-api/type.rst
Show comments
View file
Open in desktop
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 2 Doc/includes/sublist.c
Show comments
View file
Open in desktop
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 4 Doc/whatsnew/3.10.rst
Show comments
View file
Open in desktop
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 3 Include/object.h
Show comments
View file
Open in desktop
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
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/C API/2020-11-04-20-43-15.bpo-40170.rvRWUD.rst
Show comments
View file
Open in desktop
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 2 Modules/_collectionsmodule.c
Show comments
View file
Open in desktop
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 28 Modules/_ctypes/_ctypes.c
Show comments
View file
Open in desktop
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 6 Modules/_datetimemodule.c
Show comments
View file
Open in desktop
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 8 Modules/_decimal/_decimal.c
Show comments
View file
Open in desktop
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 18 Modules/_io/_iomodule.c
Show comments
View file
Open in desktop
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 10 Modules/_testcapimodule.c
Show comments
View file
Open in desktop
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 2 Modules/_zoneinfo.c
Show comments
View file
Open in desktop
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 4 Modules/xxmodule.c
Show comments
View file
Open in desktop
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 4 Modules/xxsubtype.c
Show comments
View file
Open in desktop
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 2 Objects/structseq.c
Show comments
View file
Open in desktop
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
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.