]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-42423: Accept single base class in PyType_FromModuleAndSpec() (GH-23441)
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 22 Nov 2020 11:25:02 +0000 (13:25 +0200)
committerGitHub <noreply@github.com>
Sun, 22 Nov 2020 11:25:02 +0000 (13:25 +0200)
Doc/c-api/type.rst
Doc/whatsnew/3.10.rst
Misc/NEWS.d/next/C API/2020-11-21-12-27-19.bpo-42423.ByJHhY.rst [new file with mode: 0644]
Modules/_hashopenssl.c
Modules/_ssl.c
Objects/structseq.c
Objects/typeobject.c

index a822b671ac4766bebb604068f8579c9e47168dec..a869859dd421d1545233ea5ceff9c9b1052838bb 100644 (file)
@@ -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`.
@@ -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)
 
index 16cb7efe2984eadc3599dca2a2a35dfb7104ee73..ce66b1de1b7f20b48d71284f831da46c7f6868dd 100644 (file)
@@ -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`.)
diff --git a/Misc/NEWS.d/next/C API/2020-11-21-12-27-19.bpo-42423.ByJHhY.rst b/Misc/NEWS.d/next/C API/2020-11-21-12-27-19.bpo-42423.ByJHhY.rst
new file mode 100644 (file)
index 0000000..046a89d
--- /dev/null
@@ -0,0 +1,3 @@
+The :c:func:`PyType_FromSpecWithBases` and
+:c:func:`PyType_FromModuleAndSpec` functions now accept a single class as
+the *bases* argument.
index 56d2a77049e3499127af16cb2b06d5aac193a123..7e176cf21d6297f5e525a55e594c59fa40091ca7 100644 (file)
@@ -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;
     }
index 130dce40eec71a6a9c34b04448920bb5f667508a..6f799ee66185200af4a9bcf3d1287fcf271f8e65 100644 (file)
@@ -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;
     }
index 5caa3bd52e4d4f9323ccf14988b0383dea0b83ff..bb28e113978b39a33b6e1e2a0b510c2a6a949ab0 100644 (file)
@@ -492,7 +492,6 @@ PyTypeObject *
 PyStructSequence_NewType(PyStructSequence_Desc *desc)
 {
     PyMemberDef *members;
-    PyObject *bases;
     PyTypeObject *type;
     PyType_Slot slots[8];
     PyType_Spec spec;
@@ -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;
index 9ebeeeb2c31682a5aa7e4bb2492e1bf0e5b45bce..3a6143a8ad6135dc42cadd594efa9b0384179c83 100644 (file)
@@ -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);