]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-145247: Use _PyTuple_FromPair in Modules (part 2) (#148003)
authorSergey Miryanov <sergey.miryanov@gmail.com>
Thu, 2 Apr 2026 21:24:03 +0000 (02:24 +0500)
committerGitHub <noreply@github.com>
Thu, 2 Apr 2026 21:24:03 +0000 (23:24 +0200)
Co-authored-by: Victor Stinner <vstinner@python.org>
Modules/_ctypes/_ctypes.c
Modules/_datetimemodule.c
Modules/_interpretersmodule.c
Modules/_ssl.c
Modules/cjkcodecs/multibytecodec.c
Modules/overlapped.c
Modules/posixmodule.c
Modules/selectmodule.c
Modules/signalmodule.c
Modules/socketmodule.c

index 9a6f377fdb0ed51ba471d1674aa239822c39f3d6..57d3d78969f5333aab847b8161af8ca9f4eebfdf 100644 (file)
@@ -111,6 +111,7 @@ bytes(cdata)
 #include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()
 #include "pycore_pyatomic_ft_wrappers.h"
 #include "pycore_object.h"
+#include "pycore_tuple.h"         // _PyTuple_FromPair
 #ifdef MS_WIN32
 #  include "pycore_modsupport.h"  // _PyArg_NoKeywords()
 #endif
@@ -3511,7 +3512,7 @@ _PyCData_set(ctypes_state *st,
           only it's object list.  So we create a tuple, containing
           b_objects list PLUS the array itself, and return that!
         */
-        return PyTuple_Pack(2, keep, value);
+        return _PyTuple_FromPair(keep, value);
     }
     PyErr_Format(PyExc_TypeError,
                  "incompatible types, %s instance instead of %s instance",
@@ -5332,8 +5333,7 @@ PyCArrayType_from_ctype(ctypes_state *st, PyObject *itemtype, Py_ssize_t length)
     len = PyLong_FromSsize_t(length);
     if (len == NULL)
         return NULL;
-    key = PyTuple_Pack(2, itemtype, len);
-    Py_DECREF(len);
+    key = _PyTuple_FromPairSteal(Py_NewRef(itemtype), len);
     if (!key)
         return NULL;
 
index 0db8bc675c4bb49584da6408a8c197758d4b9c0a..163e499d957b2e206d1e07110acbe356f15be8b5 100644 (file)
@@ -13,6 +13,7 @@
 #include "pycore_long.h"          // _PyLong_GetOne()
 #include "pycore_object.h"        // _PyObject_Init()
 #include "pycore_time.h"          // _PyTime_ObjectToTime_t()
+#include "pycore_tuple.h"         // _PyTuple_FromPair
 #include "pycore_unicodeobject.h" // _PyUnicode_Copy()
 #include "pycore_initconfig.h"    // _PyStatus_OK()
 #include "pycore_pyatomic_ft_wrappers.h"
@@ -2692,7 +2693,7 @@ delta_divmod(PyObject *left, PyObject *right)
         Py_DECREF(divmod);
         return NULL;
     }
-    result = PyTuple_Pack(2, PyTuple_GET_ITEM(divmod, 0), delta);
+    result = _PyTuple_FromPair(PyTuple_GET_ITEM(divmod, 0), delta);
     Py_DECREF(delta);
     Py_DECREF(divmod);
     return result;
@@ -4496,7 +4497,7 @@ timezone_getinitargs(PyObject *op, PyObject *Py_UNUSED(dummy))
     PyDateTime_TimeZone *self = PyTimeZone_CAST(op);
     if (self->name == NULL)
         return PyTuple_Pack(1, self->offset);
-    return PyTuple_Pack(2, self->offset, self->name);
+    return _PyTuple_FromPair(self->offset, self->name);
 }
 
 static PyMethodDef timezone_methods[] = {
@@ -5247,7 +5248,7 @@ time_getstate(PyDateTime_Time *self, int proto)
         if (! HASTZINFO(self) || self->tzinfo == Py_None)
             result = PyTuple_Pack(1, basestate);
         else
-            result = PyTuple_Pack(2, basestate, self->tzinfo);
+            result = _PyTuple_FromPair(basestate, self->tzinfo);
         Py_DECREF(basestate);
     }
     return result;
@@ -7169,7 +7170,7 @@ datetime_getstate(PyDateTime_DateTime *self, int proto)
         if (! HASTZINFO(self) || self->tzinfo == Py_None)
             result = PyTuple_Pack(1, basestate);
         else
-            result = PyTuple_Pack(2, basestate, self->tzinfo);
+            result = _PyTuple_FromPair(basestate, self->tzinfo);
         Py_DECREF(basestate);
     }
     return result;
index b65140e003b9c9a4bc18f19d9ba88480c7c734af..4c9be1d525d587177d4ef6466e201e5ab5bd0217 100644 (file)
@@ -15,6 +15,7 @@
 #include "pycore_pybuffer.h"      // _PyBuffer_ReleaseInInterpreterAndRawFree()
 #include "pycore_pylifecycle.h"   // _PyInterpreterConfig_AsDict()
 #include "pycore_pystate.h"       // _PyInterpreterState_IsRunningMain()
+#include "pycore_tuple.h"         // _PyTuple_FromPairSteal
 
 #include "marshal.h"              // PyMarshal_ReadObjectFromString()
 
@@ -796,10 +797,7 @@ get_summary(PyInterpreterState *interp)
         Py_DECREF(idobj);
         return NULL;
     }
-    PyObject *res = PyTuple_Pack(2, idobj, whenceobj);
-    Py_DECREF(idobj);
-    Py_DECREF(whenceobj);
-    return res;
+    return _PyTuple_FromPairSteal(idobj, whenceobj);
 }
 
 
index b93bbe8ddf5a836c45dedb48c357e76bd833716a..4e563379098eafbaa35b553f45ff36ef6ea9ff99 100644 (file)
@@ -30,6 +30,7 @@
 #include "pycore_long.h"          // _PyLong_UnsignedLongLong_Converter()
 #include "pycore_pyerrors.h"      // _PyErr_ChainExceptions1()
 #include "pycore_time.h"          // _PyDeadline_Init()
+#include "pycore_tuple.h"         // _PyTuple_FromPair
 
 /* Include symbols from _socket module */
 #include "socketmodule.h"
@@ -6796,7 +6797,7 @@ do {                                                                        \
     }
 
     /* ssl.CertificateError used to be a subclass of ValueError */
-    bases = PyTuple_Pack(2, state->PySSLErrorObject, PyExc_ValueError);
+    bases = _PyTuple_FromPair(state->PySSLErrorObject, PyExc_ValueError);
     if (bases == NULL) {
         goto error;
     }
index d774a4968b883631955eea764fd2bbade808b32e..f1124147e2b0a78b18ff9175b2c69d533e810acd 100644 (file)
@@ -12,6 +12,7 @@
 
 #include "multibytecodec.h"
 #include "clinic/multibytecodec.c.h"
+#include "pycore_tuple.h"         // _PyTuple_FromPairSteal
 
 #include <stddef.h>               // offsetof()
 
@@ -102,26 +103,17 @@ static PyObject *multibytecodec_encode(const MultibyteCodec *,
 static PyObject *
 make_tuple(PyObject *object, Py_ssize_t len)
 {
-    PyObject *v, *w;
-
-    if (object == NULL)
-        return NULL;
-
-    v = PyTuple_New(2);
-    if (v == NULL) {
-        Py_DECREF(object);
+    if (object == NULL) {
         return NULL;
     }
-    PyTuple_SET_ITEM(v, 0, object);
 
-    w = PyLong_FromSsize_t(len);
-    if (w == NULL) {
-        Py_DECREF(v);
+    PyObject* len_obj = PyLong_FromSsize_t(len);
+    if (len_obj == NULL) {
+        Py_DECREF(object);
         return NULL;
     }
-    PyTuple_SET_ITEM(v, 1, w);
 
-    return v;
+    return _PyTuple_FromPairSteal(object, len_obj);
 }
 
 static PyObject *
index 8d2bd87ddb3c2ed5ad5057e3f60aa388dccac87b..822e1ce4bdc28d749800da3dd703bd466570ef99 100644 (file)
@@ -12,6 +12,7 @@
 #endif
 
 #include "Python.h"
+#include "pycore_tuple.h"           // _PyTuple_FromPairSteal
 
 #define WINDOWS_LEAN_AND_MEAN
 #include <winsock2.h>
@@ -896,6 +897,7 @@ _overlapped_Overlapped_getresult_impl(OverlappedObject *self, BOOL wait)
     BOOL ret;
     DWORD err;
     PyObject *addr;
+    PyObject *transferred_obj;
 
     if (self->type == TYPE_NONE) {
         PyErr_SetString(PyExc_ValueError, "operation not yet attempted");
@@ -964,18 +966,12 @@ _overlapped_Overlapped_getresult_impl(OverlappedObject *self, BOOL wait)
             }
 
             // The result is a two item tuple: (message, address)
-            self->read_from.result = PyTuple_New(2);
+            self->read_from.result = _PyTuple_FromPairSteal(
+                Py_NewRef(self->read_from.allocated_buffer), addr);
             if (self->read_from.result == NULL) {
-                Py_CLEAR(addr);
                 return NULL;
             }
 
-            // first item: message
-            PyTuple_SET_ITEM(self->read_from.result, 0,
-                             Py_NewRef(self->read_from.allocated_buffer));
-            // second item: address
-            PyTuple_SET_ITEM(self->read_from.result, 1, addr);
-
             return Py_NewRef(self->read_from.result);
         case TYPE_READ_FROM_INTO:
             // unparse the address
@@ -986,19 +982,19 @@ _overlapped_Overlapped_getresult_impl(OverlappedObject *self, BOOL wait)
                 return NULL;
             }
 
+            transferred_obj = PyLong_FromUnsignedLong((unsigned long)transferred);
+            if (transferred_obj == NULL) {
+                Py_DECREF(addr);
+                return NULL;
+            }
+
             // The result is a two item tuple: (number of bytes read, address)
-            self->read_from_into.result = PyTuple_New(2);
+            self->read_from_into.result = _PyTuple_FromPairSteal(
+                transferred_obj, addr);
             if (self->read_from_into.result == NULL) {
-                Py_CLEAR(addr);
                 return NULL;
             }
 
-            // first item: number of bytes read
-            PyTuple_SET_ITEM(self->read_from_into.result, 0,
-                PyLong_FromUnsignedLong((unsigned long)transferred));
-            // second item: address
-            PyTuple_SET_ITEM(self->read_from_into.result, 1, addr);
-
             return Py_NewRef(self->read_from_into.result);
         default:
             return PyLong_FromUnsignedLong((unsigned long) transferred);
index 07c2b73575f14e53688eb1cd3a05496b7ffb053b..e5ce487723b25bec7ccc460bdf608e48319c2492 100644 (file)
@@ -27,6 +27,7 @@
 #include "pycore_pystate.h"       // _PyInterpreterState_GET()
 #include "pycore_signal.h"        // Py_NSIG
 #include "pycore_time.h"          // _PyLong_FromTime_t()
+#include "pycore_tuple.h"         // _PyTuple_FromPairSteal
 #include "pycore_typeobject.h"    // _PyType_AddMethod()
 
 #ifndef MS_WINDOWS
@@ -11288,10 +11289,7 @@ build_itimerspec(const struct itimerspec* curr_value)
         Py_DECREF(value);
         return NULL;
     }
-    PyObject *tuple = PyTuple_Pack(2, value, interval);
-    Py_DECREF(interval);
-    Py_DECREF(value);
-    return tuple;
+    return _PyTuple_FromPairSteal(value, interval);
 }
 
 static PyObject *
index 32bd9ab0873ea10ec47cba41dc2d5f33edfa605e..eb3148ef24631bb7e60c08d1afbe5eb61cc3d4c8 100644 (file)
@@ -15,6 +15,7 @@
 #include "Python.h"
 #include "pycore_fileutils.h"     // _Py_set_inheritable()
 #include "pycore_time.h"          // _PyTime_FromSecondsObject()
+#include "pycore_tuple.h"         // _PyTuple_FromPairSteal
 
 #include <stdbool.h>
 #include <stddef.h>               // offsetof()
@@ -1075,9 +1076,7 @@ select_devpoll_poll_impl(devpollObject *self, PyObject *timeout_obj)
             Py_XDECREF(num2);
             goto error;
         }
-        value = PyTuple_Pack(2, num1, num2);
-        Py_DECREF(num1);
-        Py_DECREF(num2);
+        value = _PyTuple_FromPairSteal(num1, num2);
         if (value == NULL)
             goto error;
         PyList_SET_ITEM(result_list, i, value);
index 42ec786f953ab6c30ec9b79669eaaaeda0bb68ce..fb548b8ca00f24e5e0809d4abbabdb0f58247f00 100644 (file)
@@ -14,6 +14,7 @@
 #include "pycore_pystate.h"       // _PyThreadState_GET()
 #include "pycore_signal.h"        // _Py_RestoreSignals()
 #include "pycore_time.h"          // _PyTime_FromSecondsObject()
+#include "pycore_tuple.h"         // _PyTuple_FromPairSteal
 
 #ifndef MS_WINDOWS
 #  include "posixmodule.h"        // _PyLong_FromUid()
@@ -193,27 +194,16 @@ double_from_timeval(struct timeval *tv)
 static PyObject *
 itimer_retval(struct itimerval *iv)
 {
-    PyObject *r, *v;
-
-    r = PyTuple_New(2);
-    if (r == NULL)
-        return NULL;
-
-    if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
-        Py_DECREF(r);
+    PyObject *value = PyFloat_FromDouble(double_from_timeval(&iv->it_value));
+    if (value == NULL) {
         return NULL;
     }
-
-    PyTuple_SET_ITEM(r, 0, v);
-
-    if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
-        Py_DECREF(r);
+    PyObject *interval = PyFloat_FromDouble(double_from_timeval(&iv->it_interval));
+    if (interval == NULL) {
+        Py_DECREF(value);
         return NULL;
     }
-
-    PyTuple_SET_ITEM(r, 1, v);
-
-    return r;
+    return _PyTuple_FromPairSteal(value, interval);
 }
 #endif
 
index a97b09a4f5df0e2a95873ef7bbe2459b7db19a88..f1a55db229e115a83d3b1b7dfd5d3565e32a6150 100644 (file)
@@ -111,6 +111,7 @@ Local naming conventions:
 #include "pycore_moduleobject.h"  // _PyModule_GetState
 #include "pycore_object.h"        // _PyObject_VisitType()
 #include "pycore_time.h"          // _PyTime_AsMilliseconds()
+#include "pycore_tuple.h"         // _PyTuple_FromPairSteal
 #include "pycore_pystate.h"       // _Py_AssertHoldsTstate()
 
 #ifdef _Py_MEMORY_SANITIZER
@@ -3076,7 +3077,6 @@ sock_accept(PyObject *self, PyObject *Py_UNUSED(ignored))
     socklen_t addrlen;
     PyObject *sock = NULL;
     PyObject *addr = NULL;
-    PyObject *res = NULL;
     struct sock_accept ctx;
 
     if (!getsockaddrlen(s, &addrlen))
@@ -3102,7 +3102,7 @@ sock_accept(PyObject *self, PyObject *Py_UNUSED(ignored))
     if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) {
         PyErr_SetFromWindowsErr(0);
         SOCKETCLOSE(newfd);
-        goto finally;
+        goto error;
     }
 #endif
 #else
@@ -3113,7 +3113,7 @@ sock_accept(PyObject *self, PyObject *Py_UNUSED(ignored))
     {
         if (_Py_set_inheritable(newfd, 0, NULL) < 0) {
             SOCKETCLOSE(newfd);
-            goto finally;
+            goto error;
         }
     }
 #endif
@@ -3121,20 +3121,20 @@ sock_accept(PyObject *self, PyObject *Py_UNUSED(ignored))
     sock = PyLong_FromSocket_t(newfd);
     if (sock == NULL) {
         SOCKETCLOSE(newfd);
-        goto finally;
+        goto error;
     }
 
     addr = makesockaddr(get_sock_fd(s), SAS2SA(&addrbuf),
                         addrlen, s->sock_proto);
     if (addr == NULL)
-        goto finally;
+        goto error;
 
-    res = PyTuple_Pack(2, sock, addr);
+    return _PyTuple_FromPairSteal(sock, addr);
 
-finally:
+error:
     Py_XDECREF(sock);
     Py_XDECREF(addr);
-    return res;
+    return NULL;
 }
 
 PyDoc_STRVAR(accept_doc,
@@ -4166,7 +4166,6 @@ sock_recvfrom(PyObject *self, PyObject *args)
     PySocketSockObject *s = _PySocketSockObject_CAST(self);
 
     PyObject *addr = NULL;
-    PyObject *ret = NULL;
     int flags = 0;
     Py_ssize_t recvlen, outlen;
 
@@ -4188,20 +4187,19 @@ sock_recvfrom(PyObject *self, PyObject *args)
                                 recvlen, flags, &addr);
     if (outlen < 0) {
         PyBytesWriter_Discard(writer);
-        goto finally;
+        goto error;
     }
 
     PyObject *buf = PyBytesWriter_FinishWithSize(writer, outlen);
     if (buf == NULL) {
-        goto finally;
+        goto error;
     }
 
-    ret = PyTuple_Pack(2, buf, addr);
-    Py_DECREF(buf);
+    return _PyTuple_FromPairSteal(buf, addr);
 
-finally:
+error:
     Py_XDECREF(addr);
-    return ret;
+    return NULL;
 }
 
 PyDoc_STRVAR(recvfrom_doc,
@@ -6543,7 +6541,6 @@ socket_socketpair(PyObject *self, PyObject *args)
     PySocketSockObject *s0 = NULL, *s1 = NULL;
     SOCKET_T sv[2];
     int family, type = SOCK_STREAM, proto = 0;
-    PyObject *res = NULL;
     socket_state *state = get_module_state(self);
 #ifdef SOCK_CLOEXEC
     int *atomic_flag_works = &sock_cloexec_works;
@@ -6588,28 +6585,26 @@ socket_socketpair(PyObject *self, PyObject *args)
         return set_error();
 
     if (_Py_set_inheritable(sv[0], 0, atomic_flag_works) < 0)
-        goto finally;
+        goto error;
     if (_Py_set_inheritable(sv[1], 0, atomic_flag_works) < 0)
-        goto finally;
+        goto error;
 
     s0 = new_sockobject(state, sv[0], family, type, proto);
     if (s0 == NULL)
-        goto finally;
+        goto error;
     s1 = new_sockobject(state, sv[1], family, type, proto);
     if (s1 == NULL)
-        goto finally;
-    res = PyTuple_Pack(2, s0, s1);
+        goto error;
+    return _PyTuple_FromPairSteal((PyObject *)s0, (PyObject *)s1);
 
-finally:
-    if (res == NULL) {
-        if (s0 == NULL)
-            SOCKETCLOSE(sv[0]);
-        if (s1 == NULL)
-            SOCKETCLOSE(sv[1]);
-    }
+error:
+    if (s0 == NULL)
+        SOCKETCLOSE(sv[0]);
+    if (s1 == NULL)
+        SOCKETCLOSE(sv[1]);
     Py_XDECREF(s0);
     Py_XDECREF(s1);
-    return res;
+    return NULL;
 }
 
 PyDoc_STRVAR(socketpair_doc,