Skip to content

Commit a11d3b3

Browse files
Merge branch 'main' into bugfix-150107-asyncio-ignore-0-offset
2 parents d2a9ce0 + f2fa291 commit a11d3b3

8 files changed

Lines changed: 20 additions & 9 deletions

File tree

Include/internal/pycore_magic_number.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ Known values:
298298
Python 3.15a8 3665 (Add FOR_ITER_VIRTUAL and GET_ITER specializations)
299299
Python 3.15b1 3666 (Add SEND_VIRTUAL and SEND_ASYNC_GEN specializations)
300300
Python 3.16a0 3700 (Initial version)
301+
Python 3.16a0 3701 (Add CONSTANT_EMPTY_TUPLE to LOAD_COMMON_CONSTANT)
301302
302303
303304
Python 3.17 will start with 3750
@@ -311,7 +312,7 @@ PC/launcher.c must also be updated.
311312
312313
*/
313314

314-
#define PYC_MAGIC_NUMBER 3700
315+
#define PYC_MAGIC_NUMBER 3701
315316
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
316317
(little-endian) and then appending b'\r\n'. */
317318
#define PYC_MAGIC_NUMBER_TOKEN \

Include/internal/pycore_opcode_utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ extern "C" {
8181
#define CONSTANT_FALSE 10
8282
#define CONSTANT_MINUS_ONE 11
8383
#define CONSTANT_BUILTIN_FROZENSET 12
84-
#define NUM_COMMON_CONSTANTS 13
84+
#define CONSTANT_EMPTY_TUPLE 13
85+
#define NUM_COMMON_CONSTANTS 14
8586

8687
/* Values used in the oparg for RESUME */
8788
#define RESUME_AT_FUNC_START 0

Lib/dis.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,8 @@ def _get_const_value(op, arg, co_consts):
698698
if op == LOAD_SMALL_INT:
699699
return arg
700700
if op == LOAD_COMMON_CONSTANT:
701-
# Opargs 0-6 are callables; 7-11 are literal values.
702-
if 7 <= arg <= 11:
701+
# Opargs 0-6 and 12 are callables; 7-11 and 13 are literal values.
702+
if 7 <= arg <= 11 or arg == 13:
703703
return _common_constants[arg]
704704
return UNKNOWN
705705
argval = UNKNOWN

Lib/opcode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
builtins.set,
4545
# Append-only — must match CONSTANT_* in
4646
# Include/internal/pycore_opcode_utils.h.
47-
None, "", True, False, -1, builtins.frozenset]
47+
None, "", True, False, -1, builtins.frozenset, ()]
4848
_nb_ops = _opcode.get_nb_ops()
4949

5050
hascompare = [opmap["COMPARE_OP"]]

Lib/test/test_peepholer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,10 +1307,10 @@ def test_build_empty_tuple(self):
13071307
('RETURN_VALUE', None, 0),
13081308
]
13091309
after = [
1310-
('LOAD_CONST', 0, 0),
1310+
('LOAD_COMMON_CONSTANT', opcode._common_constants.index(()), 0),
13111311
('RETURN_VALUE', None, 0),
13121312
]
1313-
self.cfg_optimization_test(before, after, consts=[], expected_consts=[()])
1313+
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
13141314

13151315
def test_fold_tuple_of_constants(self):
13161316
before = [
@@ -1365,10 +1365,10 @@ def test_fold_constant_intrinsic_list_to_tuple(self):
13651365
('RETURN_VALUE', None, 0)
13661366
]
13671367
after = [
1368-
('LOAD_CONST', 0, 0),
1368+
('LOAD_COMMON_CONSTANT', opcode._common_constants.index(()), 0),
13691369
('RETURN_VALUE', None, 0)
13701370
]
1371-
self.cfg_optimization_test(before, after, consts=[], expected_consts=[()])
1371+
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
13721372

13731373
# multiple BUILD_LIST 0: ([], 1, [], 2)
13741374
same = [
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The empty tuple ``()`` is now loaded via :opcode:`LOAD_COMMON_CONSTANT`
2+
instead of :opcode:`LOAD_CONST`, removing it from per-code-object
3+
:attr:`~codeobject.co_consts` tuples.

Python/flowgraph.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,10 @@ maybe_instr_make_load_common_const(cfg_instr *instr, PyObject *newconst)
14681468
&& PyUnicode_GET_LENGTH(newconst) == 0) {
14691469
oparg = CONSTANT_EMPTY_STR;
14701470
}
1471+
else if (PyTuple_CheckExact(newconst)
1472+
&& PyTuple_GET_SIZE(newconst) == 0) {
1473+
oparg = CONSTANT_EMPTY_TUPLE;
1474+
}
14711475
else if (PyLong_CheckExact(newconst)) {
14721476
int overflow;
14731477
long val = PyLong_AsLongAndOverflow(newconst, &overflow);

Python/pylifecycle.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,8 @@ pycore_init_builtins(PyThreadState *tstate)
893893
interp->common_consts[CONSTANT_MINUS_ONE] =
894894
(PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS - 1];
895895
interp->common_consts[CONSTANT_BUILTIN_FROZENSET] = (PyObject *)&PyFrozenSet_Type;
896+
interp->common_consts[CONSTANT_EMPTY_TUPLE] =
897+
Py_GetConstantBorrowed(Py_CONSTANT_EMPTY_TUPLE);
896898
for (int i = 0; i < NUM_COMMON_CONSTANTS; i++) {
897899
assert(interp->common_consts[i] != NULL);
898900
}

0 commit comments

Comments
 (0)