Skip to content

Commit 3597c12

Browse files
authored
gh-89546: Clean up PyType_FromMetaclass (GH-93686)
When changing PyType_FromMetaclass recently (GH-93012, GH-93466, GH-28748) I found a bunch of opportunities to improve the code. Here they are. Fixes: #89546 Automerge-Triggered-By: GH:encukou
1 parent 5bcf33d commit 3597c12

File tree

3 files changed

+192
-91
lines changed

3 files changed

+192
-91
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:c:func:`PyType_FromMetaclass` (and other ``PyType_From*`` functions) now
2+
check that offsets and the base class's
3+
:c:member:`~PyTypeObject.tp_basicsize` fit in the new class's
4+
``tp_basicsize``.

Modules/_testcapimodule.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,7 @@ static PyType_Spec MinimalMetaclass_spec = {
12211221

12221222
static PyType_Spec MinimalType_spec = {
12231223
.name = "_testcapi.MinimalSpecType",
1224-
.basicsize = sizeof(PyObject),
1224+
.basicsize = 0, // Updated later
12251225
.flags = Py_TPFLAGS_DEFAULT,
12261226
.slots = empty_type_slots,
12271227
};
@@ -1245,6 +1245,7 @@ test_from_spec_metatype_inheritance(PyObject *self, PyObject *Py_UNUSED(ignored)
12451245
goto finally;
12461246
}
12471247

1248+
MinimalType_spec.basicsize = (int)(((PyTypeObject*)class)->tp_basicsize);
12481249
new = PyType_FromSpecWithBases(&MinimalType_spec, class);
12491250
if (new == NULL) {
12501251
goto finally;

0 commit comments

Comments
 (0)