bpo-37253: Add _PyCompilerFlags_INIT macro (GH-14018)

Add a new _PyCompilerFlags_INIT macro to initialize PyCompilerFlags
variables, rather than initializing cf_flags and cf_feature_version
explicitly in each variable.
This commit is contained in:
Victor Stinner 2019-06-13 02:16:41 +02:00 committed by GitHub
parent 2c9b498759
commit 37d66d7d4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 15 additions and 29 deletions

View file

@ -30,6 +30,9 @@ typedef struct {
int cf_flags; /* bitmask of CO_xxx flags relevant to future */ int cf_flags; /* bitmask of CO_xxx flags relevant to future */
int cf_feature_version; /* minor Python version (PyCF_ONLY_AST) */ int cf_feature_version; /* minor Python version (PyCF_ONLY_AST) */
} PyCompilerFlags; } PyCompilerFlags;
#define _PyCompilerFlags_INIT \
(PyCompilerFlags){.cf_flags = 0, .cf_feature_version = PY_MINOR_VERSION}
#endif #endif
/* Future feature support */ /* Future feature support */

View file

@ -524,7 +524,7 @@ pymain_run_python(int *exitcode)
} }
} }
PyCompilerFlags cf = {.cf_flags = 0, .cf_feature_version = PY_MINOR_VERSION}; PyCompilerFlags cf = _PyCompilerFlags_INIT;
pymain_header(config); pymain_header(config);
pymain_import_readline(config); pymain_import_readline(config);

View file

@ -336,8 +336,7 @@ parser_newstobject(node *st, int type)
if (o != 0) { if (o != 0) {
o->st_node = st; o->st_node = st;
o->st_type = type; o->st_type = type;
o->st_flags.cf_flags = 0; o->st_flags = _PyCompilerFlags_INIT;
o->st_flags.cf_feature_version = PY_MINOR_VERSION;
} }
else { else {
PyNode_Free(st); PyNode_Free(st);

View file

@ -30,11 +30,10 @@ _symtable_symtable_impl(PyObject *module, PyObject *source,
struct symtable *st; struct symtable *st;
PyObject *t; PyObject *t;
int start; int start;
PyCompilerFlags cf; PyCompilerFlags cf = _PyCompilerFlags_INIT;
PyObject *source_copy = NULL; PyObject *source_copy = NULL;
cf.cf_flags = PyCF_SOURCE_IS_UTF8; cf.cf_flags = PyCF_SOURCE_IS_UTF8;
cf.cf_feature_version = PY_MINOR_VERSION;
const char *str = _Py_SourceAsString(source, "symtable", "string or bytes", &cf, &source_copy); const char *str = _Py_SourceAsString(source, "symtable", "string or bytes", &cf, &source_copy);
if (str == NULL) { if (str == NULL) {

View file

@ -4845,7 +4845,6 @@ fstring_compile_expr(const char *expr_start, const char *expr_end,
struct compiling *c, const node *n) struct compiling *c, const node *n)
{ {
PyCompilerFlags cf;
node *mod_n; node *mod_n;
mod_ty mod; mod_ty mod;
char *str; char *str;
@ -4887,8 +4886,8 @@ fstring_compile_expr(const char *expr_start, const char *expr_end,
str[len+1] = ')'; str[len+1] = ')';
str[len+2] = 0; str[len+2] = 0;
PyCompilerFlags cf = _PyCompilerFlags_INIT;
cf.cf_flags = PyCF_ONLY_AST; cf.cf_flags = PyCF_ONLY_AST;
cf.cf_feature_version = PY_MINOR_VERSION;
mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>", mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
Py_eval_input, 0); Py_eval_input, 0);
if (!mod_n) { if (!mod_n) {

View file

@ -723,12 +723,11 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
const char *str; const char *str;
int compile_mode = -1; int compile_mode = -1;
int is_ast; int is_ast;
PyCompilerFlags cf;
int start[] = {Py_file_input, Py_eval_input, Py_single_input, Py_func_type_input}; int start[] = {Py_file_input, Py_eval_input, Py_single_input, Py_func_type_input};
PyObject *result; PyObject *result;
PyCompilerFlags cf = _PyCompilerFlags_INIT;
cf.cf_flags = flags | PyCF_SOURCE_IS_UTF8; cf.cf_flags = flags | PyCF_SOURCE_IS_UTF8;
cf.cf_feature_version = PY_MINOR_VERSION;
if (feature_version >= 0 && (flags & PyCF_ONLY_AST)) { if (feature_version >= 0 && (flags & PyCF_ONLY_AST)) {
cf.cf_feature_version = feature_version; cf.cf_feature_version = feature_version;
} }
@ -889,7 +888,6 @@ builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
{ {
PyObject *result, *source_copy; PyObject *result, *source_copy;
const char *str; const char *str;
PyCompilerFlags cf;
if (locals != Py_None && !PyMapping_Check(locals)) { if (locals != Py_None && !PyMapping_Check(locals)) {
PyErr_SetString(PyExc_TypeError, "locals must be a mapping"); PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
@ -941,8 +939,8 @@ builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
return PyEval_EvalCode(source, globals, locals); return PyEval_EvalCode(source, globals, locals);
} }
PyCompilerFlags cf = _PyCompilerFlags_INIT;
cf.cf_flags = PyCF_SOURCE_IS_UTF8; cf.cf_flags = PyCF_SOURCE_IS_UTF8;
cf.cf_feature_version = PY_MINOR_VERSION;
str = _Py_SourceAsString(source, "eval", "string, bytes or code", &cf, &source_copy); str = _Py_SourceAsString(source, "eval", "string, bytes or code", &cf, &source_copy);
if (str == NULL) if (str == NULL)
return NULL; return NULL;
@ -1032,9 +1030,8 @@ builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
else { else {
PyObject *source_copy; PyObject *source_copy;
const char *str; const char *str;
PyCompilerFlags cf; PyCompilerFlags cf = _PyCompilerFlags_INIT;
cf.cf_flags = PyCF_SOURCE_IS_UTF8; cf.cf_flags = PyCF_SOURCE_IS_UTF8;
cf.cf_feature_version = PY_MINOR_VERSION;
str = _Py_SourceAsString(source, "exec", str = _Py_SourceAsString(source, "exec",
"string, bytes or code", &cf, "string, bytes or code", &cf,
&source_copy); &source_copy);

View file

@ -309,7 +309,7 @@ PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
{ {
struct compiler c; struct compiler c;
PyCodeObject *co = NULL; PyCodeObject *co = NULL;
PyCompilerFlags local_flags; PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
int merged; int merged;
PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config; PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;
@ -332,8 +332,6 @@ PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
if (c.c_future == NULL) if (c.c_future == NULL)
goto finally; goto finally;
if (!flags) { if (!flags) {
local_flags.cf_flags = 0;
local_flags.cf_feature_version = PY_MINOR_VERSION;
flags = &local_flags; flags = &local_flags;
} }
merged = c.c_future->ff_features | flags->cf_flags; merged = c.c_future->ff_features | flags->cf_flags;

View file

@ -91,7 +91,7 @@ PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *
{ {
PyObject *filename, *v; PyObject *filename, *v;
int ret, err; int ret, err;
PyCompilerFlags local_flags; PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
int nomem_count = 0; int nomem_count = 0;
#ifdef Py_REF_DEBUG #ifdef Py_REF_DEBUG
int show_ref_count = _PyInterpreterState_Get()->config.show_ref_count; int show_ref_count = _PyInterpreterState_Get()->config.show_ref_count;
@ -105,8 +105,6 @@ PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *
if (flags == NULL) { if (flags == NULL) {
flags = &local_flags; flags = &local_flags;
local_flags.cf_flags = 0;
local_flags.cf_feature_version = PY_MINOR_VERSION;
} }
v = _PySys_GetObjectId(&PyId_ps1); v = _PySys_GetObjectId(&PyId_ps1);
if (v == NULL) { if (v == NULL) {
@ -1283,10 +1281,7 @@ _Py_SourceAsString(PyObject *cmd, const char *funcname, const char *what, PyComp
struct symtable * struct symtable *
Py_SymtableStringObject(const char *str, PyObject *filename, int start) Py_SymtableStringObject(const char *str, PyObject *filename, int start)
{ {
PyCompilerFlags flags; PyCompilerFlags flags = _PyCompilerFlags_INIT;
flags.cf_flags = 0;
flags.cf_feature_version = PY_MINOR_VERSION;
return _Py_SymtableStringObjectFlags(str, filename, start, &flags); return _Py_SymtableStringObjectFlags(str, filename, start, &flags);
} }
@ -1331,7 +1326,7 @@ PyParser_ASTFromStringObject(const char *s, PyObject *filename, int start,
PyCompilerFlags *flags, PyArena *arena) PyCompilerFlags *flags, PyArena *arena)
{ {
mod_ty mod; mod_ty mod;
PyCompilerFlags localflags; PyCompilerFlags localflags = _PyCompilerFlags_INIT;
perrdetail err; perrdetail err;
int iflags = PARSER_FLAGS(flags); int iflags = PARSER_FLAGS(flags);
if (flags && flags->cf_feature_version < 7) if (flags && flags->cf_feature_version < 7)
@ -1341,8 +1336,6 @@ PyParser_ASTFromStringObject(const char *s, PyObject *filename, int start,
&_PyParser_Grammar, start, &err, &_PyParser_Grammar, start, &err,
&iflags); &iflags);
if (flags == NULL) { if (flags == NULL) {
localflags.cf_flags = 0;
localflags.cf_feature_version = PY_MINOR_VERSION;
flags = &localflags; flags = &localflags;
} }
if (n) { if (n) {
@ -1379,7 +1372,7 @@ PyParser_ASTFromFileObject(FILE *fp, PyObject *filename, const char* enc,
PyArena *arena) PyArena *arena)
{ {
mod_ty mod; mod_ty mod;
PyCompilerFlags localflags; PyCompilerFlags localflags = _PyCompilerFlags_INIT;
perrdetail err; perrdetail err;
int iflags = PARSER_FLAGS(flags); int iflags = PARSER_FLAGS(flags);
@ -1387,8 +1380,6 @@ PyParser_ASTFromFileObject(FILE *fp, PyObject *filename, const char* enc,
&_PyParser_Grammar, &_PyParser_Grammar,
start, ps1, ps2, &err, &iflags); start, ps1, ps2, &err, &iflags);
if (flags == NULL) { if (flags == NULL) {
localflags.cf_flags = 0;
localflags.cf_feature_version = PY_MINOR_VERSION;
flags = &localflags; flags = &localflags;
} }
if (n) { if (n) {