Skip to content

bpo-2506: Experiment with adding a "-X noopt" flag #9693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions Include/coreconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ typedef struct {

const char *allocator; /* Memory allocator: PYTHONMALLOC */
int dev_mode; /* PYTHONDEVMODE, -X dev */
int noopt_mode; /* -X noopt */

/* Enable faulthandler?
Set to 1 by -X faulthandler and PYTHONFAULTHANDLER. -1 means unset. */
Expand Down
2 changes: 2 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4687,6 +4687,8 @@ get_coreconfig(PyObject *self, PyObject *Py_UNUSED(args))
FROM_STRING(config->allocator));
SET_ITEM("dev_mode",
PyLong_FromLong(config->dev_mode));
SET_ITEM("noopt_mode",
PyLong_FromLong(config->noopt_mode));
SET_ITEM("faulthandler",
PyLong_FromLong(config->faulthandler));
SET_ITEM("tracemalloc",
Expand Down
1 change: 1 addition & 0 deletions Programs/_testembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ dump_config(void)
printf("allocator = %s\n", config->allocator);

printf("dev_mode = %i\n", config->dev_mode);
printf("noopt_mode = %i\n", config->noopt_mode);
printf("faulthandler = %i\n", config->faulthandler);
printf("tracemalloc = %i\n", config->tracemalloc);
printf("import_time = %i\n", config->import_time);
Expand Down
37 changes: 28 additions & 9 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "node.h"
#include "ast.h"
#include "code.h"
#include "internal/pystate.h"
#include "symtable.h"
#include "opcode.h"
#include "wordcode_helpers.h"
Expand Down Expand Up @@ -158,6 +159,7 @@ struct compiler {
PyCompilerFlags *c_flags;

int c_optimize; /* optimization level */
int c_noopt;
int c_interactive; /* true if in interactive mode */
int c_nestlevel;

Expand Down Expand Up @@ -186,7 +188,7 @@ static int compiler_visit_slice(struct compiler *, slice_ty,
expr_context_ty);

static int inplace_binop(struct compiler *, operator_ty);
static int expr_constant(expr_ty);
static int expr_constant(struct compiler *, expr_ty);

static int compiler_with(struct compiler *, stmt_ty, int);
static int compiler_async_with(struct compiler *, stmt_ty, int);
Expand Down Expand Up @@ -300,6 +302,7 @@ PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
PyCodeObject *co = NULL;
PyCompilerFlags local_flags;
int merged;
const _PyCoreConfig *conf = &_PyInterpreterState_GET_UNSAFE()->core_config;

if (!__doc__) {
__doc__ = PyUnicode_InternFromString("__doc__");
Expand Down Expand Up @@ -330,8 +333,14 @@ PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize;
c.c_nestlevel = 0;

if (!_PyAST_Optimize(mod, arena, c.c_optimize)) {
goto finally;
if (conf->noopt_mode) {
c.c_noopt = 1;
}
else {
c.c_noopt = 0;
if (!_PyAST_Optimize(mod, arena, c.c_optimize)) {
goto finally;
}
}

c.c_st = PySymtable_BuildObject(mod, filename, c.c_future);
Expand Down Expand Up @@ -2365,7 +2374,7 @@ compiler_if(struct compiler *c, stmt_ty s)
if (end == NULL)
return 0;

constant = expr_constant(s->v.If.test);
constant = expr_constant(c, s->v.If.test);
/* constant = 0: "if 0"
* constant = 1: "if 1", "if 2", ...
* constant = -1: rest */
Expand Down Expand Up @@ -2478,7 +2487,8 @@ static int
compiler_while(struct compiler *c, stmt_ty s)
{
basicblock *loop, *orelse, *end, *anchor = NULL;
int constant = expr_constant(s->v.While.test);

int constant = expr_constant(c, s->v.While.test);

if (constant == 0) {
if (s->v.While.orelse)
Expand Down Expand Up @@ -4205,8 +4215,11 @@ compiler_visit_keyword(struct compiler *c, keyword_ty k)
*/

static int
expr_constant(expr_ty e)
expr_constant(struct compiler *c, expr_ty e)
{
if (c->c_noopt) {
return -1;
}
if (e->kind == Constant_kind) {
return PyObject_IsTrue(e->v.Constant.value);
}
Expand Down Expand Up @@ -5387,9 +5400,15 @@ makecode(struct compiler *c, struct assembler *a)
if (flags < 0)
goto error;

bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
if (!bytecode)
goto error;
if (!c->c_noopt) {
bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
if (!bytecode)
goto error;
}
else {
bytecode = a->a_bytecode;
Py_INCREF(bytecode);
}

tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
if (!tmp)
Expand Down
5 changes: 5 additions & 0 deletions Python/coreconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ _PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2)
COPY_ATTR(_install_importlib);
COPY_ATTR(allocator);
COPY_ATTR(dev_mode);
COPY_ATTR(noopt_mode);
COPY_ATTR(faulthandler);
COPY_ATTR(tracemalloc);
COPY_ATTR(import_time);
Expand Down Expand Up @@ -945,6 +946,10 @@ config_read_complex_options(_PyCoreConfig *config)
{
config->dev_mode = 1;
}
if (config_get_xoption(config, L"noopt"))
{
config->noopt_mode = 1;
}

_PyInitError err;
if (config->tracemalloc < 0) {
Expand Down
2 changes: 2 additions & 0 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2060,6 +2060,7 @@ static PyStructSequence_Field flags_fields[] = {
{"hash_randomization", "-R"},
{"isolated", "-I"},
{"dev_mode", "-X dev"},
{"noopt_mode", "-X noopt"},
{"utf8_mode", "-X utf8"},
{0}
};
Expand Down Expand Up @@ -2101,6 +2102,7 @@ make_flags(void)
SetFlag(config->use_hash_seed == 0 || config->hash_seed != 0);
SetFlag(config->isolated);
PyStructSequence_SET_ITEM(seq, pos++, PyBool_FromLong(config->dev_mode));
PyStructSequence_SET_ITEM(seq, pos++, PyBool_FromLong(config->noopt_mode));
SetFlag(config->utf8_mode);
#undef SetFlag

Expand Down