Skip to content
Merged
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
48 changes: 48 additions & 0 deletions Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ast
import dis
import gc
from itertools import combinations, product
Expand Down Expand Up @@ -1119,6 +1120,53 @@ def trace(frame, event, arg):

class DirectCfgOptimizerTests(CfgOptimizationTestCase):

def test_optimize_cfg_const_index_out_of_range(self):
insts = [
('LOAD_CONST', 2, 0),
('RETURN_VALUE', None, 0),
]
seq = self.seq_from_insts(insts)
with self.assertRaisesRegex(ValueError, "out of range"):
_testinternalcapi.optimize_cfg(seq, [0, 1], 0)

def test_optimize_cfg_consts_must_be_list(self):
insts = [
('LOAD_CONST', 0, 0),
('RETURN_VALUE', None, 0),
]
seq = self.seq_from_insts(insts)
with self.assertRaisesRegex(TypeError, "consts must be a list"):
_testinternalcapi.optimize_cfg(seq, (0,), 0)

def test_compiler_codegen_metadata_consts_roundtrips_optimize_cfg(self):
tree = ast.parse("x = (1, 2)", mode="exec", optimize=1)
insts, meta = _testinternalcapi.compiler_codegen(tree, "<s>", 0)
consts = meta["consts"]
self.assertIsInstance(consts, list)
_testinternalcapi.optimize_cfg(insts, consts, 0)

def test_compiler_codegen_consts_include_none_required_for_implicit_return(self):
# Module "pass" only needs the const table entry for None once
# _PyCodegen_AddReturnAtEnd runs. If metadata["consts"] were taken
# before that, the list would not match LOAD_CONST opargs (here: 0
# for None), and optimize_cfg would read out of range.
tree = ast.parse("pass", mode="exec", optimize=1)
insts, meta = _testinternalcapi.compiler_codegen(tree, "<s>", 0)
consts = meta["consts"]
self.assertEqual(consts, [None])

load_const = opcode.opmap["LOAD_CONST"]
self.assertEqual(
[t[1] for t in insts.get_instructions() if t[0] == load_const],
[0],
)

# As if consts were snapshotted before AddReturnAtEnd: still LOAD_CONST 0, no row.
with self.assertRaisesRegex(ValueError, "out of range"):
_testinternalcapi.optimize_cfg(insts, [], 0)

_testinternalcapi.optimize_cfg(insts, list(consts), 0)

def cfg_optimization_test(self, insts, expected_insts,
consts=None, expected_consts=None,
nlocals=0):
Expand Down
11 changes: 9 additions & 2 deletions Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -732,13 +732,17 @@ _testinternalcapi.compiler_codegen -> object
compile_mode: int = 0

Apply compiler code generation to an AST.

Return (instruction_sequence, metadata). metadata maps "argcount",
"posonlyargcount", "kwonlyargcount" to ints and "consts" to the list of
constants in LOAD_CONST index order (for use with optimize_cfg).
[clinic start generated code]*/

static PyObject *
_testinternalcapi_compiler_codegen_impl(PyObject *module, PyObject *ast,
PyObject *filename, int optimize,
int compile_mode)
/*[clinic end generated code: output=40a68f6e13951cc8 input=a0e00784f1517cd7]*/
/*[clinic end generated code: output=40a68f6e13951cc8 input=e0c65e5c80efe30e]*/
{
PyCompilerFlags *flags = NULL;
return _PyCompile_CodeGen(ast, filename, flags, optimize, compile_mode);
Expand All @@ -754,12 +758,15 @@ _testinternalcapi.optimize_cfg -> object
nlocals: int

Apply compiler optimizations to an instruction list.

consts must be a list aligned with LOAD_CONST opargs (the "consts" entry
from the metadata dict returned by compiler_codegen for the same unit).
[clinic start generated code]*/

static PyObject *
_testinternalcapi_optimize_cfg_impl(PyObject *module, PyObject *instructions,
PyObject *consts, int nlocals)
/*[clinic end generated code: output=57c53c3a3dfd1df0 input=6a96d1926d58d7e5]*/
/*[clinic end generated code: output=57c53c3a3dfd1df0 input=905c3d935e063b27]*/
{
return _PyCompile_OptimizeCfg(instructions, consts, nlocals);
}
Expand Down
13 changes: 10 additions & 3 deletions Modules/clinic/_testinternalcapi.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1610,6 +1610,7 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
{
PyObject *res = NULL;
PyObject *metadata = NULL;
PyObject *consts_list = NULL;

if (!PyAST_Check(ast)) {
PyErr_SetString(PyExc_TypeError, "expected an AST");
Expand Down Expand Up @@ -1664,12 +1665,23 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
}

if (_PyInstructionSequence_ApplyLabelMap(_PyCompile_InstrSequence(c)) < 0) {
return NULL;
goto finally;
}

/* After AddReturnAtEnd: co_consts indices match the final instruction stream. */
consts_list = consts_dict_keys_inorder(umd->u_consts);
if (consts_list == NULL) {
goto finally;
}
if (PyDict_SetItemString(metadata, "consts", consts_list) < 0) {
goto finally;
}

/* Allocate a copy of the instruction sequence on the heap */
res = PyTuple_Pack(2, _PyCompile_InstrSequence(c), metadata);

finally:
Py_XDECREF(consts_list);
Py_XDECREF(metadata);
_PyCompile_ExitScope(c);
compiler_free(c);
Expand Down
15 changes: 15 additions & 0 deletions Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,14 @@ get_const_value(int opcode, int oparg, PyObject *co_consts)
PyObject *constant = NULL;
assert(loads_const(opcode));
if (opcode == LOAD_CONST) {
assert(PyList_Check(co_consts));
Py_ssize_t n = PyList_GET_SIZE(co_consts);
if (oparg < 0 || oparg >= n) {
PyErr_Format(PyExc_ValueError,
"LOAD_CONST index %d is out of range for consts (len=%zd)",
oparg, n);
return NULL;
}
constant = PyList_GET_ITEM(co_consts, oparg);
}
if (opcode == LOAD_SMALL_INT) {
Expand Down Expand Up @@ -2153,6 +2161,9 @@ basicblock_optimize_load_const(PyObject *const_cache, basicblock *bb, PyObject *
cfg_instr *inst = &bb->b_instr[i];
if (inst->i_opcode == LOAD_CONST) {
PyObject *constant = get_const_value(inst->i_opcode, inst->i_oparg, consts);
if (constant == NULL) {
return ERROR;
}
int res = maybe_instr_make_load_smallint(inst, constant, consts, const_cache);
Py_DECREF(constant);
if (res < 0) {
Expand Down Expand Up @@ -4073,6 +4084,10 @@ _PyCompile_OptimizeCfg(PyObject *seq, PyObject *consts, int nlocals)
PyErr_SetString(PyExc_ValueError, "expected an instruction sequence");
return NULL;
}
if (!PyList_Check(consts)) {
PyErr_SetString(PyExc_TypeError, "consts must be a list");
return NULL;
}
PyObject *const_cache = PyDict_New();
if (const_cache == NULL) {
return NULL;
Expand Down
Loading