GeezerVison is showing github.com — gh-60074: add new stable API function PyType_FromMetaclass by wjakob · Pull Request #93012 · python/cpython
gh-60074: add new stable API function PyType_FromMetaclass
#93012
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
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.c
(3)
.dat
(1)
.h
(1)
.py
(2)
.rst
(4)
.toml
(1)
All 6 file types selected
Viewed files
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
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -190,11 +190,16 @@ Creating Heap-Allocated Types
The following functions and structs are used to create
:ref:`heap types <heap-types>`.
.. c:function:: PyObject* PyType_FromModuleAndSpec( PyObject *module, PyType_Spec *spec, PyObject *bases)
.. c:function:: PyObject* PyType_FromMetaclass(PyTypeObject *metaclass, PyObject *module, PyType_Spec *spec, PyObject *bases)
Creates and returns a :ref:`heap type <heap-types>` from the *spec*
Create and return a :ref:`heap type <heap-types>` from the *spec*
(:const:`Py_TPFLAGS_HEAPTYPE`).
The metaclass *metaclass* is used to construct the resulting type object.
When *metaclass* is ``NULL``, the default :c:type:`PyType_Type` is used
instead. Note that metaclasses that override
:c:member:`~PyTypeObject.tp_new` are not supported.
The *bases* argument can be used to specify base classes; it can either
be only one class or a tuple of classes.
If *bases* is ``NULL``, the *Py_tp_bases* slot is used instead.
Expand All
@@ -210,22 +215,29 @@ The following functions and structs are used to create
This function calls :c:func:`PyType_Ready` on the new type.
.. versionadded:: 3.12
.. c:function:: PyObject* PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
Equivalent to ``PyType_FromMetaclass(NULL, module, spec, bases)``.
.. versionadded:: 3.9
.. versionchanged:: 3.10
The function now accepts a single class as the *bases* argument and
``NULL`` as the ``tp_doc`` slot.
.. c:function:: PyObject* PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
Equivalent to ``PyType_FromModuleAndSpec( NULL, spec, bases)``.
Equivalent to ``PyType_FromMetaclass(NULL, NULL, spec, bases)``.
.. versionadded:: 3.3
.. c:function:: PyObject* PyType_FromSpec(PyType_Spec *spec)
Equivalent to ``PyType_FromSpecWithBases( spec, NULL)``.
Equivalent to ``PyType_FromMetaclass(NULL, NULL, spec, NULL)``.
.. c:type:: PyType_Spec
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -2071,7 +2071,7 @@ flag set.
This is done by filling a :c:type:`PyType_Spec` structure and calling
:c:func:`PyType_FromSpec`, :c:func:`PyType_FromSpecWithBases`,
or :c:func:`PyType_FromModuleAndSpec `.
:c:func:`PyType_FromModuleAndSpec`, or :c:func:`PyType_FromMetaclass `.
.. _number-structs:
Expand Down
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Original file line number
Diff line number
Diff line change
Expand Up
@@ -151,6 +151,11 @@ C API Changes
New Features
------------
* Added the new limited C API function :c:func:`PyType_FromMetaclass`,
which generalizes the existing :c:func:`PyType_FromModuleAndSpec` using
an additional metaclass argument.
(Contributed by Wenzel Jakob in :gh:`93012`.)
Porting to Python 3.12
----------------------
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -257,6 +257,9 @@ PyAPI_FUNC(void *) PyType_GetModuleState(PyTypeObject *);
PyAPI_FUNC(PyObject *) PyType_GetName(PyTypeObject *);
PyAPI_FUNC(PyObject *) PyType_GetQualName(PyTypeObject *);
#endif
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000
PyAPI_FUNC(PyObject *) PyType_FromMetaclass(PyTypeObject*, PyObject*, PyType_Spec*, PyObject*);
Comment thread
wjakob marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
#endif
/* Generic type check */
PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -608,6 +608,19 @@ def test_heaptype_with_setattro(self):
del obj.value
self.assertEqual(obj.pvalue, 0)
def test_heaptype_with_custom_metaclass(self):
self.assertTrue(issubclass(_testcapi.HeapCTypeMetaclass, type))
self.assertTrue(issubclass(_testcapi.HeapCTypeMetaclassCustomNew, type))
t = _testcapi.pytype_fromspec_meta(_testcapi.HeapCTypeMetaclass)
self.assertIsInstance(t, type)
self.assertEqual(t.__name__, "HeapCTypeViaMetaclass")
self.assertIs(type(t), _testcapi.HeapCTypeMetaclass)
msg = "Metaclasses with custom tp_new are not supported."
with self.assertRaisesRegex(TypeError, msg):
t = _testcapi.pytype_fromspec_meta(_testcapi.HeapCTypeMetaclassCustomNew)
def test_pynumber_tobase(self):
from _testcapi import pynumber_tobase
self.assertEqual(pynumber_tobase(123, 2), '0b1111011')
Expand Down
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,8 @@
Added the new function :c:func:`PyType_FromMetaclass`, which generalizes the
existing :c:func:`PyType_FromModuleAndSpec` using an additional metaclass
argument. This is useful for language binding tools, where it can be used to
intercept type-related operations like subclassing or static attribute access
by specifying a metaclass with custom slots.
Importantly, :c:func:`PyType_FromMetaclass` is available in the Limited API,
which provides a path towards migrating more binding tools onto the Stable ABI.
Original file line number
Diff line number
Diff line change
Expand Up
@@ -2275,3 +2275,5 @@
added = '3.11'
[function.PyErr_SetHandledException]
added = '3.11'
[function.PyType_FromMetaclass]
added = '3.12'
Original file line number
Diff line number
Diff line change
Expand Up
@@ -308,6 +308,32 @@ test_dict_inner(int count)
}
}
static PyObject *pytype_fromspec_meta(PyObject* self, PyObject *meta)
{
if (!PyType_Check(meta)) {
PyErr_SetString(
TestError,
"pytype_fromspec_meta: must be invoked with a type argument!");
return NULL;
}
Comment thread
encukou marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
PyType_Slot HeapCTypeViaMetaclass_slots[] = {
{0},
};
PyType_Spec HeapCTypeViaMetaclass_spec = {
"_testcapi.HeapCTypeViaMetaclass",
sizeof(PyObject),
0,
Py_TPFLAGS_DEFAULT,
HeapCTypeViaMetaclass_slots
};
return PyType_FromMetaclass(
(PyTypeObject *) meta, NULL, &HeapCTypeViaMetaclass_spec, NULL);
}
static PyObject*
test_dict_iteration(PyObject* self, PyObject *Py_UNUSED(ignored))
{
Expand Down
Expand Up
@@ -5886,6 +5912,7 @@ static PyMethodDef TestMethods[] = {
{"test_long_numbits", test_long_numbits, METH_NOARGS},
{"test_k_code", test_k_code, METH_NOARGS},
{"test_empty_argparse", test_empty_argparse, METH_NOARGS},
{"pytype_fromspec_meta", pytype_fromspec_meta, METH_O},
{"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS},
{"pyobject_repr_from_null", pyobject_repr_from_null, METH_NOARGS},
{"pyobject_str_from_null", pyobject_str_from_null, METH_NOARGS},
Expand Down
Expand Up
@@ -7078,6 +7105,38 @@ static PyType_Spec HeapCTypeSubclassWithFinalizer_spec = {
HeapCTypeSubclassWithFinalizer_slots
};
static PyType_Slot HeapCTypeMetaclass_slots[] = {
{0},
};
static PyType_Spec HeapCTypeMetaclass_spec = {
"_testcapi.HeapCTypeMetaclass",
sizeof(PyHeapTypeObject),
sizeof(PyMemberDef),
Py_TPFLAGS_DEFAULT,
HeapCTypeMetaclass_slots
};
static PyObject *
heap_ctype_metaclass_custom_tp_new(PyTypeObject *tp, PyObject *args, PyObject *kwargs)
{
return PyType_Type.tp_new(tp, args, kwargs);
}
static PyType_Slot HeapCTypeMetaclassCustomNew_slots[] = {
{ Py_tp_new, heap_ctype_metaclass_custom_tp_new },
{0},
};
static PyType_Spec HeapCTypeMetaclassCustomNew_spec = {
"_testcapi.HeapCTypeMetaclassCustomNew",
sizeof(PyHeapTypeObject),
sizeof(PyMemberDef),
Py_TPFLAGS_DEFAULT,
HeapCTypeMetaclassCustomNew_slots
};
typedef struct {
PyObject_HEAD
PyObject *dict;
Expand Down
Expand Up
@@ -7591,6 +7650,20 @@ PyInit__testcapi(void)
Py_DECREF(subclass_with_finalizer_bases);
PyModule_AddObject(m, "HeapCTypeSubclassWithFinalizer", HeapCTypeSubclassWithFinalizer);
PyObject *HeapCTypeMetaclass = PyType_FromMetaclass(
&PyType_Type, m, &HeapCTypeMetaclass_spec, (PyObject *) &PyType_Type);
if (HeapCTypeMetaclass == NULL) {
return NULL;
}
PyModule_AddObject(m, "HeapCTypeMetaclass", HeapCTypeMetaclass);
PyObject *HeapCTypeMetaclassCustomNew = PyType_FromMetaclass(
&PyType_Type, m, &HeapCTypeMetaclassCustomNew_spec, (PyObject *) &PyType_Type);
if (HeapCTypeMetaclassCustomNew == NULL) {
return NULL;
}
PyModule_AddObject(m, "HeapCTypeMetaclassCustomNew", HeapCTypeMetaclassCustomNew);
if (PyType_Ready(&ContainerNoGC_type) < 0) {
return NULL;
}
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -3366,13 +3366,8 @@ static const PySlot_Offset pyslot_offsets[] = {
};
PyObject *
PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
{
return PyType_FromModuleAndSpec(NULL, spec, bases);
}
PyObject *
PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
PyType_FromMetaclass(PyTypeObject *metaclass, PyObject *module,
PyType_Spec *spec, PyObject *bases)
{
PyHeapTypeObject *res;
PyObject *modname;
Expand All
@@ -3384,6 +3379,16 @@ PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
char *res_start;
short slot_offset, subslot_offset;
if (!metaclass) {
metaclass = &PyType_Type;
}
if (metaclass->tp_new != PyType_Type.tp_new) {
PyErr_SetString(PyExc_TypeError,
"Metaclasses with custom tp_new are not supported.");
return NULL;
}
Comment thread
wjakob marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
nmembers = weaklistoffset = dictoffset = vectorcalloffset = 0;
for (slot = spec->slots; slot->slot; slot++) {
if (slot->slot == Py_tp_members) {
Expand Down
Expand Up
@@ -3412,7 +3417,7 @@ PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
}
}
res = (PyHeapTypeObject*)PyType_GenericAlloc(&PyType_Type , nmembers);
res = (PyHeapTypeObject*)metaclass->tp_alloc(metaclass , nmembers);
if (res == NULL)
return NULL;
res_start = (char*)res;
Expand Down
Expand Up
@@ -3639,10 +3644,22 @@ PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
return NULL;
}
PyObject *
PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
{
return PyType_FromMetaclass(NULL, module, spec, bases);
}
PyObject *
PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
{
return PyType_FromMetaclass(NULL, NULL, spec, bases);
}
PyObject *
PyType_FromSpec(PyType_Spec *spec)
{
return PyType_FromSpecWithBases( spec, NULL);
return PyType_FromMetaclass(NULL, NULL, spec, NULL);
}
PyObject *
Expand Down
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.