https://github.com/python/cpython/commit/c1b20a6d96eadf1c49c511f1e480aff82b6477a4
commit: c1b20a6d96eadf1c49c511f1e480aff82b6477a4
branch: main
author: Sergey Miryanov <[email protected]>
committer: vstinner <[email protected]>
date: 2026-04-02T23:24:03+02:00
summary:

GH-145247: Use _PyTuple_FromPair in Modules (part 2) (#148003)

Co-authored-by: Victor Stinner <[email protected]>

files:
M Modules/_ctypes/_ctypes.c
M Modules/_datetimemodule.c
M Modules/_interpretersmodule.c
M Modules/_ssl.c
M Modules/cjkcodecs/multibytecodec.c
M Modules/overlapped.c
M Modules/posixmodule.c
M Modules/selectmodule.c
M Modules/signalmodule.c
M Modules/socketmodule.c

diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 9a6f377fdb0ed5..57d3d78969f533 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -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;
 
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index 0db8bc675c4bb4..163e499d957b2e 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -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;
diff --git a/Modules/_interpretersmodule.c b/Modules/_interpretersmodule.c
index b65140e003b9c9..4c9be1d525d587 100644
--- a/Modules/_interpretersmodule.c
+++ b/Modules/_interpretersmodule.c
@@ -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);
 }
 
 
diff --git a/Modules/_ssl.c b/Modules/_ssl.c
index b93bbe8ddf5a83..4e563379098eaf 100644
--- a/Modules/_ssl.c
+++ b/Modules/_ssl.c
@@ -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;
     }
diff --git a/Modules/cjkcodecs/multibytecodec.c 
b/Modules/cjkcodecs/multibytecodec.c
index d774a4968b8836..f1124147e2b0a7 100644
--- a/Modules/cjkcodecs/multibytecodec.c
+++ b/Modules/cjkcodecs/multibytecodec.c
@@ -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 *
diff --git a/Modules/overlapped.c b/Modules/overlapped.c
index 8d2bd87ddb3c2e..822e1ce4bdc28d 100644
--- a/Modules/overlapped.c
+++ b/Modules/overlapped.c
@@ -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);
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 07c2b73575f14e..e5ce487723b25b 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -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 *
diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c
index 32bd9ab0873ea1..eb3148ef24631b 100644
--- a/Modules/selectmodule.c
+++ b/Modules/selectmodule.c
@@ -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);
diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
index 42ec786f953ab6..fb548b8ca00f24 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -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
 
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index a97b09a4f5df0e..f1a55db229e115 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -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,

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to