8000 bpo-42423: Accept single base class in PyType_FromModuleAndSpec() (GH… · Ringdingcoder/cpython@686c203 · GitHub
[go: up one dir, main page]

Skip to content

Commit 686c203

Browse files
bpo-42423: Accept single base class in PyType_FromModuleAndSpec() (pythonGH-23441)
1 parent c4d45ee commit 686c203

File tree

7 files changed

+17
-28
lines changed

7 files changed

+17
-28
lines changed

Doc/c-api/type.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,8 @@ The following functions and structs are used to create
154154
Creates and returns a heap type object from the *spec*
155155
(:const:`Py_TPFLAGS_HEAPTYPE`).
156156
157-
If *bases* is a tuple, the created heap type contains all types contained
158-
in it as base types.
159-
157+
The *bases* argument can be used to specify base classes; it can either
158+
be only one class or a tuple of classes.
160159
If *bases* is ``NULL``, the *Py_tp_bases* slot is used instead.
161160
If that also is ``NULL``, the *Py_tp_base* slot is used instead.
162161
If that also is ``NULL``, the new type derives from :class:`object`.
@@ -174,7 +173,8 @@ The following functions and structs are used to create
174173
175174
.. versionchanged:: 3.10
176175
177-
The function now accepts NULL ``tp_doc`` slot.
176+
The function now accepts a single class as the *bases* argument and
177+
``NULL`` as the ``tp_doc`` slot.
178178
179179
.. c:function:: PyObject* PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
180180

Doc/whatsnew/3.10.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,10 @@ New Features
512512
reference count of an object and return the object.
513513
(Contributed by Victor Stinner in :issue:`42262`.)
514514

515+
* The :c:func:`PyType_FromSpecWithBases` and :c:func:`PyType_FromModuleAndSpec`
516+
functions now accept a single class as the *bases* argument.
517+
(Contributed by Serhiy Storchaka in :issue:`42423`.)
518+
515519
* The :c:func:`PyType_FromModuleAndSpec` function now accepts NULL ``tp_doc``
516520
slot.
517521
(Contributed by Hai Shi in :issue:`41832`.)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The :c:func:`PyType_FromSpecWithBases` and
2+
:c:func:`PyType_FromModuleAndSpec` functions now accept a single class as
3+
the *bases* argument.

Modules/_hashopenssl.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,21 +2038,14 @@ hashlib_init_evpxoftype(PyObject *module)
20382038
{
20392039
#ifdef PY_OPENSSL_HAS_SHAKE
20402040
_hashlibstate *state = get_hashlib_state(module);
2041-
PyObject *bases;
20422041

20432042
if (state->EVPtype == NULL) {
20442043
return -1;
20452044
}
20462045

2047-
bases = PyTuple_Pack(1, state->EVPtype);
2048-
if (bases == NULL) {
2049-
return -1;
2050-
}
2051-
20522046
state->EVPXOFtype = (PyTypeObject *)PyType_FromSpecWithBases(
2053-
&EVPXOFtype_spec, bases
2047+
&EVPXOFtype_spec, (PyObject *)state->EVPtype
20542048
);
2055-
Py_DECREF(bases);
20562049
if (state->EVPXOFtype == NULL) {
20572050
return -1;
20582051
}

Modules/_ssl.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5955,12 +5955,7 @@ do { \
59555955
if (PyModule_AddObjectRef(module, name, exc) < 0) goto error; \
59565956
} while(0)
59575957

5958-
bases = PyTuple_Pack(1, PyExc_OSError);
5959-
if (bases == NULL) {
5960-
goto error;
5961-
}
5962-
PySSLErrorObject = PyType_FromSpecWithBases(&sslerror_type_spec, bases);
5963-
Py_CLEAR(bases);
5958+
PySSLErrorObject = PyType_FromSpecWithBases(&sslerror_type_spec, PyExc_OSError);
59645959
if (PySSLErrorObject == NULL) {
59655960
goto error;
59665961
}

Objects/structseq.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,6 @@ PyTypeObject *
492492
PyStructSequence_NewType(PyStructSequence_Desc *desc)
493493
{
494494
PyMemberDef *members;
495-
PyObject *bases;
496495
PyTypeObject *type;
497496
PyType_Slot slots[8];
498497
PyType_Spec spec;
@@ -526,13 +525,7 @@ PyStructSequence_NewType(PyStructSequence_Desc *desc)
526525
spec.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC;
527526
spec.slots = slots;
528527

529-
bases = PyTuple_Pack(1, &PyTuple_Type);
530-
if (bases == NULL) {
531-
PyMem_FREE(members);
532-
return NULL;
533-
}
534-
type = (PyTypeObject *)PyType_FromSpecWithBases(&spec, bases);
535-
Py_DECREF(bases);
528+
type = (PyTypeObject *)PyType_FromSpecWithBases(&spec, (PyObject *)&PyTuple_Type);
536529
PyMem_FREE(members);
537530
if (type == NULL) {
538531
return NULL;

Objects/typeobject.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2993,8 +2993,9 @@ PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
29932993
}
29942994
}
29952995
else if (!PyTuple_Check(bases)) {
2996-
PyErr_SetString(PyExc_SystemError, "bases is not a tuple");
2997-
goto fail;
2996+
bases = PyTuple_Pack(1, bases);
2997+
if (!bases)
2998+
goto fail;
29982999
}
29993000
else {
30003001
Py_INCREF(bases);

0 commit comments

Comments
 (0)
0