Skip to content

Commit 37d66d7

Browse files
authored
bpo-37253: Add _PyCompilerFlags_INIT macro (pythonGH-14018)
Add a new _PyCompilerFlags_INIT macro to initialize PyCompilerFlags variables, rather than initializing cf_flags and cf_feature_version explicitly in each variable.
1 parent 2c9b498 commit 37d66d7

File tree

8 files changed

+15
-29
lines changed

8 files changed

+15
-29
lines changed

Include/compile.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ typedef struct {
3030
int cf_flags; /* bitmask of CO_xxx flags relevant to future */
3131
int cf_feature_version; /* minor Python version (PyCF_ONLY_AST) */
3232
} PyCompilerFlags;
33+
34+
#define _PyCompilerFlags_INIT \
35+
(PyCompilerFlags){.cf_flags = 0, .cf_feature_version = PY_MINOR_VERSION}
3336
#endif
3437

3538
/* Future feature support */

Modules/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ pymain_run_python(int *exitcode)
524524
}
525525
}
526526

527-
PyCompilerFlags cf = {.cf_flags = 0, .cf_feature_version = PY_MINOR_VERSION};
527+
PyCompilerFlags cf = _PyCompilerFlags_INIT;
528528

529529
pymain_header(config);
530530
pymain_import_readline(config);

Modules/parsermodule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,7 @@ parser_newstobject(node *st, int type)
336336
if (o != 0) {
337337
o->st_node = st;
338338
o->st_type = type;
339-
o->st_flags.cf_flags = 0;
340-
o->st_flags.cf_feature_version = PY_MINOR_VERSION;
339+
o->st_flags = _PyCompilerFlags_INIT;
341340
}
342341
else {
343342
PyNode_Free(st);

Modules/symtablemodule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ _symtable_symtable_impl(PyObject *module, PyObject *source,
3030
struct symtable *st;
3131
PyObject *t;
3232
int start;
33-
PyCompilerFlags cf;
33+
PyCompilerFlags cf = _PyCompilerFlags_INIT;
3434
PyObject *source_copy = NULL;
3535

3636
cf.cf_flags = PyCF_SOURCE_IS_UTF8;
37-
cf.cf_feature_version = PY_MINOR_VERSION;
3837

3938
const char *str = _Py_SourceAsString(source, "symtable", "string or bytes", &cf, &source_copy);
4039
if (str == NULL) {

Python/ast.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4845,7 +4845,6 @@ fstring_compile_expr(const char *expr_start, const char *expr_end,
48454845
struct compiling *c, const node *n)
48464846

48474847
{
4848-
PyCompilerFlags cf;
48494848
node *mod_n;
48504849
mod_ty mod;
48514850
char *str;
@@ -4887,8 +4886,8 @@ fstring_compile_expr(const char *expr_start, const char *expr_end,
48874886
str[len+1] = ')';
48884887
str[len+2] = 0;
48894888

4889+
PyCompilerFlags cf = _PyCompilerFlags_INIT;
48904890
cf.cf_flags = PyCF_ONLY_AST;
4891-
cf.cf_feature_version = PY_MINOR_VERSION;
48924891
mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
48934892
Py_eval_input, 0);
48944893
if (!mod_n) {

Python/bltinmodule.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -723,12 +723,11 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
723723
const char *str;
724724
int compile_mode = -1;
725725
int is_ast;
726-
PyCompilerFlags cf;
727726
int start[] = {Py_file_input, Py_eval_input, Py_single_input, Py_func_type_input};
728727
PyObject *result;
729728

729+
PyCompilerFlags cf = _PyCompilerFlags_INIT;
730730
cf.cf_flags = flags | PyCF_SOURCE_IS_UTF8;
731-
cf.cf_feature_version = PY_MINOR_VERSION;
732731
if (feature_version >= 0 && (flags & PyCF_ONLY_AST)) {
733732
cf.cf_feature_version = feature_version;
734733
}
@@ -889,7 +888,6 @@ builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
889888
{
890889
PyObject *result, *source_copy;
891890
const char *str;
892-
PyCompilerFlags cf;
893891

894892
if (locals != Py_None && !PyMapping_Check(locals)) {
895893
PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
@@ -941,8 +939,8 @@ builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
941939
return PyEval_EvalCode(source, globals, locals);
942940
}
943941

942+
PyCompilerFlags cf = _PyCompilerFlags_INIT;
944943
cf.cf_flags = PyCF_SOURCE_IS_UTF8;
945-
cf.cf_feature_version = PY_MINOR_VERSION;
946944
str = _Py_SourceAsString(source, "eval", "string, bytes or code", &cf, &source_copy);
947945
if (str == NULL)
948946
return NULL;
@@ -1032,9 +1030,8 @@ builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
10321030
else {
10331031
PyObject *source_copy;
10341032
const char *str;
1035-
PyCompilerFlags cf;
1033+
PyCompilerFlags cf = _PyCompilerFlags_INIT;
10361034
cf.cf_flags = PyCF_SOURCE_IS_UTF8;
1037-
cf.cf_feature_version = PY_MINOR_VERSION;
10381035
str = _Py_SourceAsString(source, "exec",
10391036
"string, bytes or code", &cf,
10401037
&source_copy);

Python/compile.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
309309
{
310310
struct compiler c;
311311
PyCodeObject *co = NULL;
312-
PyCompilerFlags local_flags;
312+
PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
313313
int merged;
314314
PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
315315

@@ -332,8 +332,6 @@ PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
332332
if (c.c_future == NULL)
333333
goto finally;
334334
if (!flags) {
335-
local_flags.cf_flags = 0;
336-
local_flags.cf_feature_version = PY_MINOR_VERSION;
337335
flags = &local_flags;
338336
}
339337
merged = c.c_future->ff_features | flags->cf_flags;

Python/pythonrun.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *
9191
{
9292
PyObject *filename, *v;
9393
int ret, err;
94-
PyCompilerFlags local_flags;
94+
PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
9595
int nomem_count = 0;
9696
#ifdef Py_REF_DEBUG
9797
int show_ref_count = _PyInterpreterState_Get()->config.show_ref_count;
@@ -105,8 +105,6 @@ PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *
105105

106106
if (flags == NULL) {
107107
flags = &local_flags;
108-
local_flags.cf_flags = 0;
109-
local_flags.cf_feature_version = PY_MINOR_VERSION;
110108
}
111109
v = _PySys_GetObjectId(&PyId_ps1);
112110
if (v == NULL) {
@@ -1283,10 +1281,7 @@ _Py_SourceAsString(PyObject *cmd, const char *funcname, const char *what, PyComp
12831281
struct symtable *
12841282
Py_SymtableStringObject(const char *str, PyObject *filename, int start)
12851283
{
1286-
PyCompilerFlags flags;
1287-
1288-
flags.cf_flags = 0;
1289-
flags.cf_feature_version = PY_MINOR_VERSION;
1284+
PyCompilerFlags flags = _PyCompilerFlags_INIT;
12901285
return _Py_SymtableStringObjectFlags(str, filename, start, &flags);
12911286
}
12921287

@@ -1331,7 +1326,7 @@ PyParser_ASTFromStringObject(const char *s, PyObject *filename, int start,
13311326
PyCompilerFlags *flags, PyArena *arena)
13321327
{
13331328
mod_ty mod;
1334-
PyCompilerFlags localflags;
1329+
PyCompilerFlags localflags = _PyCompilerFlags_INIT;
13351330
perrdetail err;
13361331
int iflags = PARSER_FLAGS(flags);
13371332
if (flags && flags->cf_feature_version < 7)
@@ -1341,8 +1336,6 @@ PyParser_ASTFromStringObject(const char *s, PyObject *filename, int start,
13411336
&_PyParser_Grammar, start, &err,
13421337
&iflags);
13431338
if (flags == NULL) {
1344-
localflags.cf_flags = 0;
1345-
localflags.cf_feature_version = PY_MINOR_VERSION;
13461339
flags = &localflags;
13471340
}
13481341
if (n) {
@@ -1379,16 +1372,14 @@ PyParser_ASTFromFileObject(FILE *fp, PyObject *filename, const char* enc,
13791372
PyArena *arena)
13801373
{
13811374
mod_ty mod;
1382-
PyCompilerFlags localflags;
1375+
PyCompilerFlags localflags = _PyCompilerFlags_INIT;
13831376
perrdetail err;
13841377
int iflags = PARSER_FLAGS(flags);
13851378

13861379
node *n = PyParser_ParseFileObject(fp, filename, enc,
13871380
&_PyParser_Grammar,
13881381
start, ps1, ps2, &err, &iflags);
13891382
if (flags == NULL) {
1390-
localflags.cf_flags = 0;
1391-
localflags.cf_feature_version = PY_MINOR_VERSION;
13921383
flags = &localflags;
13931384
}
13941385
if (n) {

0 commit comments

Comments
 (0)