Skip to content

Commit d19de37

Browse files
GH-145247: Use _PyTuple_FromPair in Parser and Python (#145842)
Use _PyTuple_FromPair in Parser and Python
1 parent f062014 commit d19de37

File tree

8 files changed

+16
-11
lines changed

8 files changed

+16
-11
lines changed

Parser/pegen_errors.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "pycore_pyerrors.h" // _PyErr_ProgramDecodedTextObject()
55
#include "pycore_runtime.h" // _Py_ID()
6+
#include "pycore_tuple.h" // _PyTuple_FromPair
67
#include "lexer/state.h"
78
#include "lexer/lexer.h"
89
#include "pegen.h"
@@ -41,7 +42,7 @@ _PyPegen_raise_tokenizer_init_error(PyObject *filename)
4142
goto error;
4243
}
4344

44-
tuple = PyTuple_Pack(2, errstr, tmp);
45+
tuple = _PyTuple_FromPair(errstr, tmp);
4546
Py_DECREF(tmp);
4647
if (!tuple) {
4748
goto error;
@@ -393,7 +394,7 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
393394
if (!tmp) {
394395
goto error;
395396
}
396-
value = PyTuple_Pack(2, errstr, tmp);
397+
value = _PyTuple_FromPair(errstr, tmp);
397398
Py_DECREF(tmp);
398399
if (!value) {
399400
goto error;

Python/Python-tokenize.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "Python.h"
22
#include "errcode.h"
33
#include "internal/pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION
4+
#include "internal/pycore_tuple.h" // _PyTuple_FromPair
45
#include "../Parser/lexer/state.h"
56
#include "../Parser/lexer/lexer.h"
67
#include "../Parser/tokenizer/tokenizer.h"
@@ -164,7 +165,7 @@ _tokenizer_error(tokenizeriterobject *it)
164165
goto exit;
165166
}
166167

167-
value = PyTuple_Pack(2, errstr, tmp);
168+
value = _PyTuple_FromPair(errstr, tmp);
168169
if (!value) {
169170
result = -1;
170171
goto exit;

Python/_warnings.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing()
88
#include "pycore_pystate.h" // _PyThreadState_GET()
99
#include "pycore_traceback.h" // _Py_DisplaySourceLine()
10+
#include "pycore_tuple.h" // _PyTuple_FromPair
1011
#include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()
1112

1213
#include <stdbool.h>
@@ -634,7 +635,7 @@ update_registry(PyInterpreterState *interp, PyObject *registry, PyObject *text,
634635
if (add_zero)
635636
altkey = PyTuple_Pack(3, text, category, _PyLong_GetZero());
636637
else
637-
altkey = PyTuple_Pack(2, text, category);
638+
altkey = _PyTuple_FromPair(text, category);
638639

639640
rc = already_warned(interp, registry, altkey, 1);
640641
Py_XDECREF(altkey);

Python/bltinmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3341,7 +3341,7 @@ zip_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
33413341
if (lz->strict) {
33423342
return PyTuple_Pack(3, Py_TYPE(lz), lz->ittuple, Py_True);
33433343
}
3344-
return PyTuple_Pack(2, Py_TYPE(lz), lz->ittuple);
3344+
return _PyTuple_FromPair((PyObject *)Py_TYPE(lz), lz->ittuple);
33453345
}
33463346

33473347
static PyObject *

Python/compile.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "pycore_runtime.h" // _Py_ID()
2424
#include "pycore_setobject.h" // _PySet_NextEntry()
2525
#include "pycore_stats.h"
26+
#include "pycore_tuple.h" // _PyTuple_FromPair
2627
#include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()
2728

2829
#include "cpython/code.h"
@@ -1697,7 +1698,7 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
16971698
return NULL;
16981699
}
16991700
/* Allocate a copy of the instruction sequence on the heap */
1700-
res = PyTuple_Pack(2, _PyCompile_InstrSequence(c), metadata);
1701+
res = _PyTuple_FromPair((PyObject *)_PyCompile_InstrSequence(c), metadata);
17011702

17021703
finally:
17031704
Py_XDECREF(metadata);

Python/hamt.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "pycore_initconfig.h" // _PyStatus_OK()
55
#include "pycore_long.h" // _PyLong_Format()
66
#include "pycore_object.h" // _PyObject_GC_TRACK()
7+
#include "pycore_tuple.h" // _PyTuple_FromPair
78

89
#include <stddef.h> // offsetof()
910

@@ -2542,7 +2543,7 @@ PyTypeObject _PyHamtItems_Type = {
25422543
static PyObject *
25432544
hamt_iter_yield_items(PyObject *key, PyObject *val)
25442545
{
2545-
return PyTuple_Pack(2, key, val);
2546+
return _PyTuple_FromPair(key, val);
25462547
}
25472548

25482549
PyObject *

Python/marshal.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "pycore_object.h" // _PyObject_IsUniquelyReferenced
1515
#include "pycore_pystate.h" // _PyInterpreterState_GET()
1616
#include "pycore_setobject.h" // _PySet_NextEntryRef()
17+
#include "pycore_tuple.h" // _PyTuple_FromPairSteal
1718
#include "pycore_unicodeobject.h" // _PyUnicode_InternImmortal()
1819

1920
#include "marshal.h" // Py_MARSHAL_VERSION
@@ -629,9 +630,7 @@ w_complex_object(PyObject *v, char flag, WFILE *p)
629630
Py_DECREF(value);
630631
break;
631632
}
632-
PyObject *pair = PyTuple_Pack(2, dump, value);
633-
Py_DECREF(dump);
634-
Py_DECREF(value);
633+
PyObject *pair = _PyTuple_FromPairSteal(dump, value);
635634
if (pair == NULL) {
636635
p->error = WFERR_NOMEMORY;
637636
break;

Python/pylifecycle.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "pycore_stats.h" // _PyStats_InterpInit()
3131
#include "pycore_sysmodule.h" // _PySys_ClearAttrString()
3232
#include "pycore_traceback.h" // _Py_DumpTracebackThreads()
33+
#include "pycore_tuple.h" // _PyTuple_FromPair
3334
#include "pycore_typeobject.h" // _PyTypes_InitTypes()
3435
#include "pycore_typevarobject.h" // _Py_clear_generic_types()
3536
#include "pycore_unicodeobject.h" // _PyUnicode_InitTypes()
@@ -1613,7 +1614,7 @@ finalize_remove_modules(PyObject *modules, int verbose)
16131614
if (weaklist != NULL) { \
16141615
PyObject *wr = PyWeakref_NewRef(mod, NULL); \
16151616
if (wr) { \
1616-
PyObject *tup = PyTuple_Pack(2, name, wr); \
1617+
PyObject *tup = _PyTuple_FromPair(name, wr); \
16171618
if (!tup || PyList_Append(weaklist, tup) < 0) { \
16181619
PyErr_FormatUnraisable("Exception ignored while removing modules"); \
16191620
} \

0 commit comments

Comments
 (0)