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`.
.. 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)
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`.)
--- /dev/null
+The :c:func:`PyType_FromSpecWithBases` and
+:c:func:`PyType_FromModuleAndSpec` functions now accept a single class as
+the *bases* argument.
{
#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;
}
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;
}
PyStructSequence_NewType(PyStructSequence_Desc *desc)
{
PyMemberDef *members;
- PyObject *bases;
PyTypeObject *type;
PyType_Slot slots[8];
PyType_Spec spec;
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;
}
}
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);