Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Include/internal/pycore_magic_number.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ Known values:
Python 3.15a8 3665 (Add FOR_ITER_VIRTUAL and GET_ITER specializations)
Python 3.15b1 3666 (Add SEND_VIRTUAL and SEND_ASYNC_GEN specializations)
Python 3.16a0 3700 (Initial version)
Python 3.16a0 3701 (Add CONSTANT_EMPTY_TUPLE to LOAD_COMMON_CONSTANT)


Python 3.17 will start with 3750
Expand All @@ -311,7 +312,7 @@ PC/launcher.c must also be updated.

*/

#define PYC_MAGIC_NUMBER 3700
#define PYC_MAGIC_NUMBER 3701
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
(little-endian) and then appending b'\r\n'. */
#define PYC_MAGIC_NUMBER_TOKEN \
Expand Down
3 changes: 2 additions & 1 deletion Include/internal/pycore_opcode_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ extern "C" {
#define CONSTANT_TRUE 9
#define CONSTANT_FALSE 10
#define CONSTANT_MINUS_ONE 11
#define NUM_COMMON_CONSTANTS 12
#define CONSTANT_EMPTY_TUPLE 12
#define NUM_COMMON_CONSTANTS 13

/* Values used in the oparg for RESUME */
#define RESUME_AT_FUNC_START 0
Expand Down
4 changes: 2 additions & 2 deletions Lib/dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,8 @@ def _get_const_value(op, arg, co_consts):
if op == LOAD_SMALL_INT:
return arg
if op == LOAD_COMMON_CONSTANT:
# Opargs 0-6 are callables; 7-11 are literal values.
if 7 <= arg <= 11:
# Opargs 0-6 are callables; 7-12 are literal values.
if 7 <= arg <= 12:
return _common_constants[arg]
return UNKNOWN
argval = UNKNOWN
Expand Down
2 changes: 1 addition & 1 deletion Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
builtins.set,
# Append-only — must match CONSTANT_* in
# Include/internal/pycore_opcode_utils.h.
None, "", True, False, -1]
None, "", True, False, -1, ()]
_nb_ops = _opcode.get_nb_ops()

hascompare = [opmap["COMPARE_OP"]]
Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1307,10 +1307,10 @@ def test_build_empty_tuple(self):
('RETURN_VALUE', None, 0),
]
after = [
('LOAD_CONST', 0, 0),
('LOAD_COMMON_CONSTANT', opcode._common_constants.index(()), 0),
('RETURN_VALUE', None, 0),
]
self.cfg_optimization_test(before, after, consts=[], expected_consts=[()])
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])

def test_fold_tuple_of_constants(self):
before = [
Expand Down Expand Up @@ -1365,10 +1365,10 @@ def test_fold_constant_intrinsic_list_to_tuple(self):
('RETURN_VALUE', None, 0)
]
after = [
('LOAD_CONST', 0, 0),
('LOAD_COMMON_CONSTANT', opcode._common_constants.index(()), 0),
('RETURN_VALUE', None, 0)
]
self.cfg_optimization_test(before, after, consts=[], expected_consts=[()])
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])

# multiple BUILD_LIST 0: ([], 1, [], 2)
same = [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The empty tuple ``()`` is now loaded via :opcode:`LOAD_COMMON_CONSTANT`
instead of :opcode:`LOAD_CONST`, removing it from per-code-object
:attr:`~codeobject.co_consts` tuples.
4 changes: 4 additions & 0 deletions Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,10 @@ maybe_instr_make_load_common_const(cfg_instr *instr, PyObject *newconst)
&& PyUnicode_GET_LENGTH(newconst) == 0) {
oparg = CONSTANT_EMPTY_STR;
}
else if (PyTuple_CheckExact(newconst)
&& PyTuple_GET_SIZE(newconst) == 0) {
oparg = CONSTANT_EMPTY_TUPLE;
}
else if (PyLong_CheckExact(newconst)) {
int overflow;
long val = PyLong_AsLongAndOverflow(newconst, &overflow);
Expand Down
2 changes: 2 additions & 0 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,8 @@ pycore_init_builtins(PyThreadState *tstate)
interp->common_consts[CONSTANT_FALSE] = Py_False;
interp->common_consts[CONSTANT_MINUS_ONE] =
(PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS - 1];
interp->common_consts[CONSTANT_EMPTY_TUPLE] =
Py_GetConstantBorrowed(Py_CONSTANT_EMPTY_TUPLE);
for (int i = 0; i < NUM_COMMON_CONSTANTS; i++) {
assert(interp->common_consts[i] != NULL);
}
Expand Down
Loading