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-45383: Inherit MetaClass from bases in FromSpec API #28748

Merged
merged 19 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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
21 changes: 19 additions & 2 deletions Doc/c-api/type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,9 @@ The following functions and structs are used to create
(: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
When *metaclass* is ``NULL``, the metaclass is derived from *bases*
(or *Py_tp_base[s]* slots if *bases* is ``NULL``, see below).
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
Expand Down Expand Up @@ -228,17 +229,33 @@ The following functions and structs are used to create
The function now accepts a single class as the *bases* argument and
``NULL`` as the ``tp_doc`` slot.

.. versionchanged:: 3.12

The function now finds and uses a metaclass corresponding to the provided
base classes. Previously, only :class:`type` instances were returned.


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

Equivalent to ``PyType_FromMetaclass(NULL, NULL, spec, bases)``.

.. versionadded:: 3.3

.. versionchanged:: 3.12

The function now finds and uses a metaclass corresponding to the provided
base classes. Previously, only :class:`type` instances were returned.

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

Equivalent to ``PyType_FromMetaclass(NULL, NULL, spec, NULL)``.

.. versionchanged:: 3.12

The function now finds and uses a metaclass corresponding to the
base classes provided in *Py_tp_base[s]* slots.
Previously, only :class:`type` instances were returned.

.. c:type:: PyType_Spec

Structure defining a type's behavior.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The `PyType_FromSpec` API will now find and use a metaclass
based on the provided bases.
An error will be raised if there is a metaclass conflict.
144 changes: 144 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include <float.h> // FLT_MAX
#include <signal.h>

#include <ffi.h> /* required by ctypes.h */
#include "_ctypes/ctypes.h" /* To test metaclass inheritance */

#ifdef MS_WINDOWS
# include <winsock2.h> // struct timeval
#endif
Expand Down Expand Up @@ -1208,6 +1211,142 @@ test_get_type_name(PyObject *self, PyObject *Py_UNUSED(ignored))
}


static PyType_Slot empty_type_slots[] = {
{0, 0},
};

static PyType_Spec MinimalMetaclass_spec = {
.name = "_testcapi.MinimalMetaclass",
.basicsize = sizeof(PyHeapTypeObject),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.slots = empty_type_slots,
};

static PyType_Spec MinimalType_spec = {
.name = "_testcapi.MinimalSpecType",
.basicsize = sizeof(PyObject),
.flags = Py_TPFLAGS_DEFAULT,
.slots = empty_type_slots,
};

static PyObject *
test_from_spec_metatype_inheritance(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject *metaclass = NULL;
PyObject *class = NULL;
PyObject *new = NULL;
PyObject *result = NULL;

metaclass = PyType_FromSpecWithBases(&MinimalMetaclass_spec, (PyObject*)&PyType_Type);
if (metaclass == NULL) {
goto finally;
}
class = PyObject_CallFunction(metaclass, "s(){}", "TestClass");
if (class == NULL) {
goto finally;
}

new = PyType_FromSpecWithBases(&MinimalType_spec, class);
if (new == NULL) {
goto finally;
}
if (Py_TYPE(new) != (PyTypeObject*)metaclass) {
PyErr_SetString(PyExc_AssertionError,
"Metaclass not set properly!");
goto finally;
}
result = Py_NewRef(Py_None);

finally:
Py_XDECREF(metaclass);
Py_XDECREF(class);
Py_XDECREF(new);
return result;
}


static PyObject *
test_from_spec_invalid_metatype_inheritance(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject *metaclass_a = NULL;
PyObject *metaclass_b = NULL;
PyObject *class_a = NULL;
PyObject *class_b = NULL;
PyObject *bases = NULL;
PyObject *new = NULL;
PyObject *meta_error_string = NULL;
PyObject *exc_type = NULL;
PyObject *exc_value = NULL;
PyObject *exc_traceback = NULL;
PyObject *result = NULL;

metaclass_a = PyType_FromSpecWithBases(&MinimalMetaclass_spec, (PyObject*)&PyType_Type);
if (metaclass_a == NULL) {
goto finally;
}
metaclass_b = PyType_FromSpecWithBases(&MinimalMetaclass_spec, (PyObject*)&PyType_Type);
if (metaclass_b == NULL) {
goto finally;
}
class_a = PyObject_CallFunction(metaclass_a, "s(){}", "TestClassA");
if (class_a == NULL) {
goto finally;
}

class_b = PyObject_CallFunction(metaclass_b, "s(){}", "TestClassB");
if (class_b == NULL) {
goto finally;
}

bases = PyTuple_Pack(2, class_a, class_b);
if (bases == NULL) {
goto finally;
}

/*
* The following should raise a TypeError due to a MetaClass conflict.
*/
new = PyType_FromSpecWithBases(&MinimalType_spec, bases);
if (new != NULL) {
PyErr_SetString(PyExc_AssertionError,
"MetaType conflict not recognized by PyType_FromSpecWithBases");
goto finally;
}

// Assert that the correct exception was raised
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
PyErr_Fetch(&exc_type, &exc_value, &exc_traceback);

meta_error_string = PyUnicode_FromString("metaclass conflict:");
if (meta_error_string == NULL) {
goto finally;
}
int res = PyUnicode_Contains(exc_value, meta_error_string);
if (res < 0) {
goto finally;
}
if (res == 0) {
PyErr_SetString(PyExc_AssertionError,
"TypeError did not inlclude expected message.");
goto finally;
}
result = Py_NewRef(Py_None);
}
finally:
Py_XDECREF(metaclass_a);
Py_XDECREF(metaclass_b);
Py_XDECREF(bases);
Py_XDECREF(new);
Py_XDECREF(meta_error_string);
Py_XDECREF(exc_type);
Py_XDECREF(exc_value);
Py_XDECREF(exc_traceback);
Py_XDECREF(class_a);
Py_XDECREF(class_b);
return result;
}


static PyObject *
simple_str(PyObject *self) {
return PyUnicode_FromString("<test>");
Expand Down Expand Up @@ -5938,6 +6077,11 @@ static PyMethodDef TestMethods[] = {
{"test_get_type_name", test_get_type_name, METH_NOARGS},
{"test_get_type_qualname", test_get_type_qualname, METH_NOARGS},
{"test_type_from_ephemeral_spec", test_type_from_ephemeral_spec, METH_NOARGS},
{"test_from_spec_metatype_inheritance", test_from_spec_metatype_inheritance,
METH_NOARGS},
{"test_from_spec_invalid_metatype_inheritance",
test_from_spec_invalid_metatype_inheritance,
METH_NOARGS},
{"get_kwargs", _PyCFunction_CAST(get_kwargs),
METH_VARARGS|METH_KEYWORDS},
{"getargs_tuple", getargs_tuple, METH_VARARGS},
Expand Down
Loading