Skip to content

bpo-42423: Accept single base class in PyType_FromModuleAndSpec() #23441

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

Merged
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
8 changes: 4 additions & 4 deletions Doc/c-api/type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,8 @@ The following functions and structs are used to create
Creates and returns a heap type object from the *spec*
(:const:`Py_TPFLAGS_HEAPTYPE`).

If *bases* is a tuple, the created heap type contains all types contained
in it as base types.

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.
If that also is ``NULL``, the *Py_tp_base* slot is used instead.
If that also is ``NULL``, the new type derives from :class:`object`.
Expand All @@ -174,7 +173,8 @@ The following functions and structs are used to create

.. versionchanged:: 3.10

The function now accepts NULL ``tp_doc`` slot.
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)

Expand Down
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,10 @@ New Features
reference count of an object and return the object.
(Contributed by Victor Stinner in :issue:`42262`.)

* The :c:func:`PyType_FromSpecWithBases` and :c:func:`PyType_FromModuleAndSpec`
functions now accept a single class as the *bases* argument.
(Contributed by Serhiy Storchaka in :issue:`42423`.)

* The :c:func:`PyType_FromModuleAndSpec` function now accepts NULL ``tp_doc``
slot.
(Contributed by Hai Shi in :issue:`41832`.)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The :c:func:`PyType_FromSpecWithBases` and
:c:func:`PyType_FromModuleAndSpec` functions now accept a single class as
the *bases* argument.
9 changes: 1 addition & 8 deletions Modules/_hashopenssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2038,21 +2038,14 @@ hashlib_init_evpxoftype(PyObject *module)
{
#ifdef PY_OPENSSL_HAS_SHAKE
_hashlibstate *state = get_hashlib_state(module);
PyObject *bases;

if (state->EVPtype == NULL) {
return -1;
}

bases = PyTuple_Pack(1, state->EVPtype);
if (bases == NULL) {
return -1;
}

state->EVPXOFtype = (PyTypeObject *)PyType_FromSpecWithBases(
&EVPXOFtype_spec, bases
&EVPXOFtype_spec, (PyObject *)state->EVPtype
);
Py_DECREF(bases);
if (state->EVPXOFtype == NULL) {
return -1;
}
Expand Down
7 changes: 1 addition & 6 deletions Modules/_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -5955,12 +5955,7 @@ do { \
if (PyModule_AddObjectRef(module, name, exc) < 0) goto error; \
} while(0)

bases = PyTuple_Pack(1, PyExc_OSError);
if (bases == NULL) {
goto error;
}
PySSLErrorObject = PyType_FromSpecWithBases(&sslerror_type_spec, bases);
Py_CLEAR(bases);
PySSLErrorObject = PyType_FromSpecWithBases(&sslerror_type_spec, PyExc_OSError);
if (PySSLErrorObject == NULL) {
goto error;
}
Expand Down
9 changes: 1 addition & 8 deletions Objects/structseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,6 @@ PyTypeObject *
PyStructSequence_NewType(PyStructSequence_Desc *desc)
{
PyMemberDef *members;
PyObject *bases;
PyTypeObject *type;
PyType_Slot slots[8];
PyType_Spec spec;
Expand Down Expand Up @@ -526,13 +525,7 @@ PyStructSequence_NewType(PyStructSequence_Desc *desc)
spec.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC;
spec.slots = slots;

bases = PyTuple_Pack(1, &PyTuple_Type);
if (bases == NULL) {
PyMem_FREE(members);
return NULL;
}
type = (PyTypeObject *)PyType_FromSpecWithBases(&spec, bases);
Py_DECREF(bases);
type = (PyTypeObject *)PyType_FromSpecWithBases(&spec, (PyObject *)&PyTuple_Type);
PyMem_FREE(members);
if (type == NULL) {
return NULL;
Expand Down
5 changes: 3 additions & 2 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2993,8 +2993,9 @@ PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
}
}
else if (!PyTuple_Check(bases)) {
PyErr_SetString(PyExc_SystemError, "bases is not a tuple");
goto fail;
bases = PyTuple_Pack(1, bases);
if (!bases)
goto fail;
}
else {
Py_INCREF(bases);
Expand Down