Skip to content
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

bpo-41171: Added method "PyMetaType_FromSpec" to allow for specifying the metaclass for the type. #21238

Closed
wants to merge 7 commits 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
20 changes: 15 additions & 5 deletions Doc/c-api/type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ The following functions and structs are used to create

.. c:function:: PyObject* PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)

Creates and returns a heap type object from the *spec*
Equivalent to ``PyMetaType_FromSpec(module, &PyType_Type, spec, bases)``.

.. versionadded:: 3.9

.. c:function:: PyObject* PyMetaType_FromSpec(PyObject *module, PyTypeObject *meta_type, PyType_Spec *spec, PyObject *bases)

Creates and returns a heap type object from the *spec* whose's metaclass is *meta_type*
(:const:`Py_TPFLAGS_HEAPTYPE`).

If *bases* is a tuple, the created heap type contains all types contained
Expand All @@ -155,19 +161,23 @@ The following functions and structs are used to create
If not ``NULL``, the module is associated with the new type and can later be
retreived with :c:func:`PyType_GetModule`.

This function calls :c:func:`PyType_Ready` on the new type.
This function calls :c:func:`PyType_Ready` on the new type.'

.. versionadded:: 3.9
.. note::

This method DOES NOT invoke ``PyTypeObject.tp_new`` on 'meta_type'.

.. versionadded:: 3.10

.. c:function:: PyObject* PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)

Equivalent to ``PyType_FromModuleAndSpec(NULL, spec, bases)``.
Equivalent to ``PyMetaType_FromSpec(NULL, &PyType_Type, spec, bases)``.

.. versionadded:: 3.3

.. c:function:: PyObject* PyType_FromSpec(PyType_Spec *spec)

Equivalent to ``PyType_FromSpecWithBases(spec, NULL)``.
Equivalent to ``PyMetaType_FromSpec(NULL, &PyType_Type, spec, NULL)``.

.. c:type:: PyType_Spec

Expand Down
7 changes: 4 additions & 3 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,16 @@ typedef struct{
PyType_Slot *slots; /* terminated by slot==0. */
} PyType_Spec;

PyAPI_FUNC(PyObject*) PyType_FromSpec(PyType_Spec*);
#define PyType_FromSpec(spec) PyMetaType_FromSpec(NULL, &PyType_Type, spec, NULL)
PyAPI_FUNC(PyObject*) PyMetaType_FromSpec(PyObject*, PyTypeObject*, PyType_Spec*, PyObject*);
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
PyAPI_FUNC(PyObject*) PyType_FromSpecWithBases(PyType_Spec*, PyObject*);
#define PyType_FromSpecWithBases(spec, bases) PyMetaType_FromSpec(NULL, &PyType_Type, spec, bases)
#endif
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000
PyAPI_FUNC(void*) PyType_GetSlot(PyTypeObject*, int);
#endif
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
PyAPI_FUNC(PyObject*) PyType_FromModuleAndSpec(PyObject *, PyType_Spec *, PyObject *);
#define PyType_FromModuleAndSpec(module, spec, bases) PyMetaType_FromSpec(module, &PyType_Type, spec, bases)
PyAPI_FUNC(PyObject *) PyType_GetModule(struct _typeobject *);
PyAPI_FUNC(void *) PyType_GetModuleState(struct _typeobject *);
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added method "PyMetaType_FromSpec" to allow for specifying the metaclass for the type.
19 changes: 6 additions & 13 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2880,14 +2880,13 @@ static const short slotoffsets[] = {
};

PyObject *
PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
PyMetaType_FromSpec(PyObject *module, PyTypeObject *meta_type, PyType_Spec *spec, PyObject *bases)
{
return PyType_FromModuleAndSpec(NULL, spec, bases);
}
if (meta_type == NULL) {
PyErr_BadArgument();
return NULL;
}

PyObject *
PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
{
PyHeapTypeObject *res;
PyObject *modname;
PyTypeObject *type, *base;
Expand Down Expand Up @@ -2924,7 +2923,7 @@ PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
}
}

res = (PyHeapTypeObject*)PyType_GenericAlloc(&PyType_Type, nmembers);
res = (PyHeapTypeObject*)PyType_GenericAlloc(meta_type, nmembers);
if (res == NULL)
return NULL;
res_start = (char*)res;
Expand Down Expand Up @@ -3098,12 +3097,6 @@ PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
return NULL;
}

PyObject *
PyType_FromSpec(PyType_Spec *spec)
{
return PyType_FromSpecWithBases(spec, NULL);
}

void *
PyType_GetSlot(PyTypeObject *type, int slot)
{
Expand Down
3 changes: 1 addition & 2 deletions PC/python3dll.c
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,7 @@ EXPORT_FUNC(PyTuple_Pack)
EXPORT_FUNC(PyTuple_SetItem)
EXPORT_FUNC(PyTuple_Size)
EXPORT_FUNC(PyType_ClearCache)
EXPORT_FUNC(PyType_FromSpec)
EXPORT_FUNC(PyType_FromSpecWithBases)
EXPORT_FUNC(PyMetaType_FromSpec)
EXPORT_FUNC(PyType_GenericAlloc)
EXPORT_FUNC(PyType_GenericNew)
EXPORT_FUNC(PyType_GetFlags)
Expand Down