]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-145247: Use _PyTuple_FromPair in Parser and Python (#145842)
authorSergey Miryanov <sergey.miryanov@gmail.com>
Wed, 11 Mar 2026 21:08:18 +0000 (02:08 +0500)
committerGitHub <noreply@github.com>
Wed, 11 Mar 2026 21:08:18 +0000 (21:08 +0000)
Use _PyTuple_FromPair in Parser and Python

Parser/pegen_errors.c
Python/Python-tokenize.c
Python/_warnings.c
Python/bltinmodule.c
Python/compile.c
Python/hamt.c
Python/marshal.c
Python/pylifecycle.c

index 1c61524d60a1af82584331debb41287e9c2712ba..312699415efd9af0ecbf7b8ab3f4cf472f4a8774 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "pycore_pyerrors.h"      // _PyErr_ProgramDecodedTextObject()
 #include "pycore_runtime.h"       // _Py_ID()
+#include "pycore_tuple.h"         // _PyTuple_FromPair
 #include "lexer/state.h"
 #include "lexer/lexer.h"
 #include "pegen.h"
@@ -41,7 +42,7 @@ _PyPegen_raise_tokenizer_init_error(PyObject *filename)
         goto error;
     }
 
-    tuple = PyTuple_Pack(2, errstr, tmp);
+    tuple = _PyTuple_FromPair(errstr, tmp);
     Py_DECREF(tmp);
     if (!tuple) {
         goto error;
@@ -393,7 +394,7 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
     if (!tmp) {
         goto error;
     }
-    value = PyTuple_Pack(2, errstr, tmp);
+    value = _PyTuple_FromPair(errstr, tmp);
     Py_DECREF(tmp);
     if (!value) {
         goto error;
index 152d61c686722e84bd9fba7741411ad8eed7e20f..c50ff1190686c295382b2f4c1b7b68916606c29d 100644 (file)
@@ -1,6 +1,7 @@
 #include "Python.h"
 #include "errcode.h"
 #include "internal/pycore_critical_section.h"   // Py_BEGIN_CRITICAL_SECTION
+#include "internal/pycore_tuple.h"              // _PyTuple_FromPair
 #include "../Parser/lexer/state.h"
 #include "../Parser/lexer/lexer.h"
 #include "../Parser/tokenizer/tokenizer.h"
@@ -164,7 +165,7 @@ _tokenizer_error(tokenizeriterobject *it)
         goto exit;
     }
 
-    value = PyTuple_Pack(2, errstr, tmp);
+    value = _PyTuple_FromPair(errstr, tmp);
     if (!value) {
         result = -1;
         goto exit;
index 0ea785772f03b9d3103090071fc807bc9b1461d8..6b6ac238935765f943c6e21e77abb8e4be742e90 100644 (file)
@@ -7,6 +7,7 @@
 #include "pycore_pylifecycle.h"   // _Py_IsInterpreterFinalizing()
 #include "pycore_pystate.h"       // _PyThreadState_GET()
 #include "pycore_traceback.h"     // _Py_DisplaySourceLine()
+#include "pycore_tuple.h"         // _PyTuple_FromPair
 #include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()
 
 #include <stdbool.h>
@@ -634,7 +635,7 @@ update_registry(PyInterpreterState *interp, PyObject *registry, PyObject *text,
     if (add_zero)
         altkey = PyTuple_Pack(3, text, category, _PyLong_GetZero());
     else
-        altkey = PyTuple_Pack(2, text, category);
+        altkey = _PyTuple_FromPair(text, category);
 
     rc = already_warned(interp, registry, altkey, 1);
     Py_XDECREF(altkey);
index fc69f6372028a60d5dbc0c81a776133b4233b288..5680e8971576cd370e952bf0f99cbdf4248f6168 100644 (file)
@@ -3341,7 +3341,7 @@ zip_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
     if (lz->strict) {
         return PyTuple_Pack(3, Py_TYPE(lz), lz->ittuple, Py_True);
     }
-    return PyTuple_Pack(2, Py_TYPE(lz), lz->ittuple);
+    return _PyTuple_FromPair((PyObject *)Py_TYPE(lz), lz->ittuple);
 }
 
 static PyObject *
index 96779a0a219a55aba912c1db18039b8f1d408cfc..4cf178b06ae11dc59d1776596506a933fef6cf95 100644 (file)
@@ -23,6 +23,7 @@
 #include "pycore_runtime.h"       // _Py_ID()
 #include "pycore_setobject.h"     // _PySet_NextEntry()
 #include "pycore_stats.h"
+#include "pycore_tuple.h"         // _PyTuple_FromPair
 #include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()
 
 #include "cpython/code.h"
@@ -1697,7 +1698,7 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
         return NULL;
     }
     /* Allocate a copy of the instruction sequence on the heap */
-    res = PyTuple_Pack(2, _PyCompile_InstrSequence(c), metadata);
+    res = _PyTuple_FromPair((PyObject *)_PyCompile_InstrSequence(c), metadata);
 
 finally:
     Py_XDECREF(metadata);
index 881290a0e60db87b0815a68fb87a534dbd332b48..e4719e71a5259a57c67538615b388f7b2180d4a7 100644 (file)
@@ -4,6 +4,7 @@
 #include "pycore_initconfig.h"    // _PyStatus_OK()
 #include "pycore_long.h"          // _PyLong_Format()
 #include "pycore_object.h"        // _PyObject_GC_TRACK()
+#include "pycore_tuple.h"         // _PyTuple_FromPair
 
 #include <stddef.h>               // offsetof()
 
@@ -2542,7 +2543,7 @@ PyTypeObject _PyHamtItems_Type = {
 static PyObject *
 hamt_iter_yield_items(PyObject *key, PyObject *val)
 {
-    return PyTuple_Pack(2, key, val);
+    return _PyTuple_FromPair(key, val);
 }
 
 PyObject *
index 59db6456552c35c189953cbbd1a995b8d161628b..cc6a787ba75022b19a37d30f4f69226fa74d4660 100644 (file)
@@ -14,6 +14,7 @@
 #include "pycore_object.h"           // _PyObject_IsUniquelyReferenced
 #include "pycore_pystate.h"          // _PyInterpreterState_GET()
 #include "pycore_setobject.h"        // _PySet_NextEntryRef()
+#include "pycore_tuple.h"            // _PyTuple_FromPairSteal
 #include "pycore_unicodeobject.h"    // _PyUnicode_InternImmortal()
 
 #include "marshal.h"                 // Py_MARSHAL_VERSION
@@ -629,9 +630,7 @@ w_complex_object(PyObject *v, char flag, WFILE *p)
                 Py_DECREF(value);
                 break;
             }
-            PyObject *pair = PyTuple_Pack(2, dump, value);
-            Py_DECREF(dump);
-            Py_DECREF(value);
+            PyObject *pair = _PyTuple_FromPairSteal(dump, value);
             if (pair == NULL) {
                 p->error = WFERR_NOMEMORY;
                 break;
index 21d1e036d31540f7c93fac6386106a5a9b937e50..2b8e9a02cb6184ad32fb55c9b25bfed20a24aa39 100644 (file)
@@ -30,6 +30,7 @@
 #include "pycore_stats.h"         // _PyStats_InterpInit()
 #include "pycore_sysmodule.h"     // _PySys_ClearAttrString()
 #include "pycore_traceback.h"     // _Py_DumpTracebackThreads()
+#include "pycore_tuple.h"         // _PyTuple_FromPair
 #include "pycore_typeobject.h"    // _PyTypes_InitTypes()
 #include "pycore_typevarobject.h" // _Py_clear_generic_types()
 #include "pycore_unicodeobject.h" // _PyUnicode_InitTypes()
@@ -1613,7 +1614,7 @@ finalize_remove_modules(PyObject *modules, int verbose)
         if (weaklist != NULL) { \
             PyObject *wr = PyWeakref_NewRef(mod, NULL); \
             if (wr) { \
-                PyObject *tup = PyTuple_Pack(2, name, wr); \
+                PyObject *tup = _PyTuple_FromPair(name, wr); \
                 if (!tup || PyList_Append(weaklist, tup) < 0) { \
                     PyErr_FormatUnraisable("Exception ignored while removing modules"); \
                 } \