Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b857ba2

Browse files
committed
Refactor future feature handling
Replace uses of PyCF_xxx with CO_xxx. Replace individual feature slots in PyFutureFeatures with single bitmask ff_features. When flags must be transfered among the three parts of the interpreter that care about them -- the pythonrun layer, the compiler, and the future feature parser -- can simply or (|) the definitions.
1 parent fdd12f6 commit b857ba2

4 files changed

Lines changed: 17 additions & 52 deletions

File tree

Python/ceval.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2937,11 +2937,11 @@ PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
29372937
const int codeflags = current_frame->f_code->co_flags;
29382938
if (codeflags & CO_NESTED) {
29392939
result = 1;
2940-
cf->cf_flags |= PyCF_NESTED_SCOPES;
2940+
cf->cf_flags |= CO_NESTED;
29412941
}
29422942
if (codeflags & CO_GENERATOR_ALLOWED) {
29432943
result = 1;
2944-
cf->cf_flags |= PyCF_GENERATORS;
2944+
cf->cf_flags |= CO_GENERATOR_ALLOWED;
29452945
}
29462946
}
29472947
return result;

Python/compile.c

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3967,22 +3967,8 @@ jcompile(node *n, char *filename, struct compiling *base,
39673967
com_free(&sc);
39683968
return NULL;
39693969
}
3970-
if (flags) {
3971-
if (flags->cf_flags & PyCF_NESTED_SCOPES)
3972-
sc.c_future->ff_nested_scopes = 1;
3973-
else if (sc.c_future->ff_nested_scopes)
3974-
flags->cf_flags |= PyCF_NESTED_SCOPES;
3975-
3976-
if (flags->cf_flags & PyCF_GENERATORS)
3977-
sc.c_future->ff_generators = 1;
3978-
else if (sc.c_future->ff_generators)
3979-
flags->cf_flags |= PyCF_GENERATORS;
3980-
3981-
if (flags->cf_flags & PyCF_DIVISION)
3982-
sc.c_future->ff_division = 1;
3983-
else if (sc.c_future->ff_division)
3984-
flags->cf_flags |= PyCF_DIVISION;
3985-
}
3970+
if (flags)
3971+
sc.c_future->ff_features |= flags->cf_flags;
39863972
if (symtable_build(&sc, n) < 0) {
39873973
com_free(&sc);
39883974
return NULL;
@@ -4150,8 +4136,6 @@ symtable_build(struct compiling *c, node *n)
41504136
if ((c->c_symtable = symtable_init()) == NULL)
41514137
return -1;
41524138
c->c_symtable->st_future = c->c_future;
4153-
if (c->c_future->ff_nested_scopes)
4154-
c->c_symtable->st_nested_scopes = 1;
41554139
c->c_symtable->st_filename = c->c_filename;
41564140
symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno);
41574141
if (c->c_symtable->st_errors > 0)
@@ -4451,14 +4435,8 @@ static int
44514435
symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste,
44524436
struct symbol_info *si)
44534437
{
4454-
if (c->c_future) {
4455-
if (c->c_future->ff_nested_scopes)
4456-
c->c_flags |= CO_NESTED;
4457-
if (c->c_future->ff_generators)
4458-
c->c_flags |= CO_GENERATOR_ALLOWED;
4459-
if (c->c_future->ff_division)
4460-
c->c_flags |= CO_FUTURE_DIVISION;
4461-
}
4438+
if (c->c_future)
4439+
c->c_flags |= c->c_future->ff_features;
44624440
if (ste->ste_generator)
44634441
c->c_flags |= CO_GENERATOR;
44644442
if (ste->ste_type != TYPE_MODULE)
@@ -4617,7 +4595,6 @@ symtable_init()
46174595
if (st == NULL)
46184596
return NULL;
46194597
st->st_pass = 1;
4620-
st->st_nested_scopes = NESTED_SCOPES_DEFAULT;
46214598
st->st_filename = NULL;
46224599
if ((st->st_stack = PyList_New(0)) == NULL)
46234600
goto fail;

Python/future.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ future_check_features(PyFutureFeatures *ff, node *n, char *filename)
3030
REQ(ch, import_as_name);
3131
feature = STR(CHILD(ch, 0));
3232
if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
33-
ff->ff_nested_scopes = 1;
33+
continue;
3434
} else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
35-
ff->ff_generators = 1;
35+
ff->ff_features |= CO_GENERATOR_ALLOWED;
3636
} else if (strcmp(feature, FUTURE_DIVISION) == 0) {
37-
ff->ff_division = 1;
37+
ff->ff_features |= CO_FUTURE_DIVISION;
3838
} else if (strcmp(feature, "braces") == 0) {
3939
PyErr_SetString(PyExc_SyntaxError,
4040
"not a chance");
@@ -234,9 +234,7 @@ PyNode_Future(node *n, char *filename)
234234
return NULL;
235235
ff->ff_found_docstring = 0;
236236
ff->ff_last_lineno = -1;
237-
ff->ff_nested_scopes = 0;
238-
ff->ff_generators = 0;
239-
ff->ff_division = 0;
237+
ff->ff_features = 0;
240238

241239
if (future_parse(ff, n, filename) < 0) {
242240
PyMem_Free((void *)ff);

Python/pythonrun.c

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ PyRun_InteractiveOneFlags(FILE *fp, char *filename, PyCompilerFlags *flags)
556556
n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
557557
Py_single_input, ps1, ps2, &err,
558558
(flags &&
559-
flags->cf_flags & PyCF_GENERATORS) ?
559+
flags->cf_flags & CO_GENERATOR_ALLOWED) ?
560560
PyPARSE_YIELD_IS_KEYWORD : 0);
561561
Py_XDECREF(v);
562562
Py_XDECREF(w);
@@ -1009,8 +1009,8 @@ PyRun_StringFlags(char *str, int start, PyObject *globals, PyObject *locals,
10091009
PyCompilerFlags *flags)
10101010
{
10111011
return run_err_node(PyParser_SimpleParseStringFlags(
1012-
str, start,
1013-
(flags && flags->cf_flags & PyCF_GENERATORS) ?
1012+
str, start,
1013+
(flags && flags->cf_flags & CO_GENERATOR_ALLOWED) ?
10141014
PyPARSE_YIELD_IS_KEYWORD : 0),
10151015
"<string>", globals, locals, flags);
10161016
}
@@ -1028,7 +1028,7 @@ PyRun_FileExFlags(FILE *fp, char *filename, int start, PyObject *globals,
10281028
PyObject *locals, int closeit, PyCompilerFlags *flags)
10291029
{
10301030
node *n = PyParser_SimpleParseFileFlags(fp, filename, start,
1031-
(flags && flags->cf_flags & PyCF_GENERATORS) ?
1031+
(flags && flags->cf_flags & CO_GENERATOR_ALLOWED) ?
10321032
PyPARSE_YIELD_IS_KEYWORD : 0);
10331033
if (closeit)
10341034
fclose(fp);
@@ -1085,18 +1085,8 @@ run_pyc_file(FILE *fp, char *filename, PyObject *globals, PyObject *locals,
10851085
}
10861086
co = (PyCodeObject *)v;
10871087
v = PyEval_EvalCode(co, globals, locals);
1088-
if (v && flags) {
1089-
if (co->co_flags & CO_NESTED)
1090-
flags->cf_flags |= PyCF_NESTED_SCOPES;
1091-
if (co->co_flags & CO_GENERATOR_ALLOWED)
1092-
flags->cf_flags |= PyCF_GENERATORS;
1093-
#if 0
1094-
fprintf(stderr, "run_pyc_file: nested_scopes: %d\n",
1095-
flags->cf_flags & PyCF_NESTED_SCOPES);
1096-
fprintf(stderr, "run_pyc_file: generators: %d\n",
1097-
flags->cf_flags & PyCF_GENERATORS);
1098-
#endif
1099-
}
1088+
if (v && flags)
1089+
flags->cf_flags |= (co->co_flags & PyCF_MASK);
11001090
Py_DECREF(co);
11011091
return v;
11021092
}
@@ -1114,7 +1104,7 @@ Py_CompileStringFlags(char *str, char *filename, int start,
11141104
node *n;
11151105
PyCodeObject *co;
11161106
n = PyParser_SimpleParseStringFlags(str, start,
1117-
(flags && flags->cf_flags & PyCF_GENERATORS) ?
1107+
(flags && flags->cf_flags & CO_GENERATOR_ALLOWED) ?
11181108
PyPARSE_YIELD_IS_KEYWORD : 0);
11191109
if (n == NULL)
11201110
return NULL;

0 commit comments

Comments
 (0)