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.
This commit is contained in:
Jeremy Hylton 2001-08-10 21:41:33 +00:00
parent fdd12f66bb
commit b857ba261f
4 changed files with 17 additions and 52 deletions

View file

@ -2937,11 +2937,11 @@ PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
const int codeflags = current_frame->f_code->co_flags; const int codeflags = current_frame->f_code->co_flags;
if (codeflags & CO_NESTED) { if (codeflags & CO_NESTED) {
result = 1; result = 1;
cf->cf_flags |= PyCF_NESTED_SCOPES; cf->cf_flags |= CO_NESTED;
} }
if (codeflags & CO_GENERATOR_ALLOWED) { if (codeflags & CO_GENERATOR_ALLOWED) {
result = 1; result = 1;
cf->cf_flags |= PyCF_GENERATORS; cf->cf_flags |= CO_GENERATOR_ALLOWED;
} }
} }
return result; return result;

View file

@ -3967,22 +3967,8 @@ jcompile(node *n, char *filename, struct compiling *base,
com_free(&sc); com_free(&sc);
return NULL; return NULL;
} }
if (flags) { if (flags)
if (flags->cf_flags & PyCF_NESTED_SCOPES) sc.c_future->ff_features |= flags->cf_flags;
sc.c_future->ff_nested_scopes = 1;
else if (sc.c_future->ff_nested_scopes)
flags->cf_flags |= PyCF_NESTED_SCOPES;
if (flags->cf_flags & PyCF_GENERATORS)
sc.c_future->ff_generators = 1;
else if (sc.c_future->ff_generators)
flags->cf_flags |= PyCF_GENERATORS;
if (flags->cf_flags & PyCF_DIVISION)
sc.c_future->ff_division = 1;
else if (sc.c_future->ff_division)
flags->cf_flags |= PyCF_DIVISION;
}
if (symtable_build(&sc, n) < 0) { if (symtable_build(&sc, n) < 0) {
com_free(&sc); com_free(&sc);
return NULL; return NULL;
@ -4150,8 +4136,6 @@ symtable_build(struct compiling *c, node *n)
if ((c->c_symtable = symtable_init()) == NULL) if ((c->c_symtable = symtable_init()) == NULL)
return -1; return -1;
c->c_symtable->st_future = c->c_future; c->c_symtable->st_future = c->c_future;
if (c->c_future->ff_nested_scopes)
c->c_symtable->st_nested_scopes = 1;
c->c_symtable->st_filename = c->c_filename; c->c_symtable->st_filename = c->c_filename;
symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno); symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno);
if (c->c_symtable->st_errors > 0) if (c->c_symtable->st_errors > 0)
@ -4451,14 +4435,8 @@ static int
symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste, symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste,
struct symbol_info *si) struct symbol_info *si)
{ {
if (c->c_future) { if (c->c_future)
if (c->c_future->ff_nested_scopes) c->c_flags |= c->c_future->ff_features;
c->c_flags |= CO_NESTED;
if (c->c_future->ff_generators)
c->c_flags |= CO_GENERATOR_ALLOWED;
if (c->c_future->ff_division)
c->c_flags |= CO_FUTURE_DIVISION;
}
if (ste->ste_generator) if (ste->ste_generator)
c->c_flags |= CO_GENERATOR; c->c_flags |= CO_GENERATOR;
if (ste->ste_type != TYPE_MODULE) if (ste->ste_type != TYPE_MODULE)
@ -4617,7 +4595,6 @@ symtable_init()
if (st == NULL) if (st == NULL)
return NULL; return NULL;
st->st_pass = 1; st->st_pass = 1;
st->st_nested_scopes = NESTED_SCOPES_DEFAULT;
st->st_filename = NULL; st->st_filename = NULL;
if ((st->st_stack = PyList_New(0)) == NULL) if ((st->st_stack = PyList_New(0)) == NULL)
goto fail; goto fail;

View file

@ -30,11 +30,11 @@ future_check_features(PyFutureFeatures *ff, node *n, char *filename)
REQ(ch, import_as_name); REQ(ch, import_as_name);
feature = STR(CHILD(ch, 0)); feature = STR(CHILD(ch, 0));
if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) { if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
ff->ff_nested_scopes = 1; continue;
} else if (strcmp(feature, FUTURE_GENERATORS) == 0) { } else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
ff->ff_generators = 1; ff->ff_features |= CO_GENERATOR_ALLOWED;
} else if (strcmp(feature, FUTURE_DIVISION) == 0) { } else if (strcmp(feature, FUTURE_DIVISION) == 0) {
ff->ff_division = 1; ff->ff_features |= CO_FUTURE_DIVISION;
} else if (strcmp(feature, "braces") == 0) { } else if (strcmp(feature, "braces") == 0) {
PyErr_SetString(PyExc_SyntaxError, PyErr_SetString(PyExc_SyntaxError,
"not a chance"); "not a chance");
@ -234,9 +234,7 @@ PyNode_Future(node *n, char *filename)
return NULL; return NULL;
ff->ff_found_docstring = 0; ff->ff_found_docstring = 0;
ff->ff_last_lineno = -1; ff->ff_last_lineno = -1;
ff->ff_nested_scopes = 0; ff->ff_features = 0;
ff->ff_generators = 0;
ff->ff_division = 0;
if (future_parse(ff, n, filename) < 0) { if (future_parse(ff, n, filename) < 0) {
PyMem_Free((void *)ff); PyMem_Free((void *)ff);

View file

@ -556,7 +556,7 @@ PyRun_InteractiveOneFlags(FILE *fp, char *filename, PyCompilerFlags *flags)
n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar, n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
Py_single_input, ps1, ps2, &err, Py_single_input, ps1, ps2, &err,
(flags && (flags &&
flags->cf_flags & PyCF_GENERATORS) ? flags->cf_flags & CO_GENERATOR_ALLOWED) ?
PyPARSE_YIELD_IS_KEYWORD : 0); PyPARSE_YIELD_IS_KEYWORD : 0);
Py_XDECREF(v); Py_XDECREF(v);
Py_XDECREF(w); Py_XDECREF(w);
@ -1010,7 +1010,7 @@ PyRun_StringFlags(char *str, int start, PyObject *globals, PyObject *locals,
{ {
return run_err_node(PyParser_SimpleParseStringFlags( return run_err_node(PyParser_SimpleParseStringFlags(
str, start, str, start,
(flags && flags->cf_flags & PyCF_GENERATORS) ? (flags && flags->cf_flags & CO_GENERATOR_ALLOWED) ?
PyPARSE_YIELD_IS_KEYWORD : 0), PyPARSE_YIELD_IS_KEYWORD : 0),
"<string>", globals, locals, flags); "<string>", globals, locals, flags);
} }
@ -1028,7 +1028,7 @@ PyRun_FileExFlags(FILE *fp, char *filename, int start, PyObject *globals,
PyObject *locals, int closeit, PyCompilerFlags *flags) PyObject *locals, int closeit, PyCompilerFlags *flags)
{ {
node *n = PyParser_SimpleParseFileFlags(fp, filename, start, node *n = PyParser_SimpleParseFileFlags(fp, filename, start,
(flags && flags->cf_flags & PyCF_GENERATORS) ? (flags && flags->cf_flags & CO_GENERATOR_ALLOWED) ?
PyPARSE_YIELD_IS_KEYWORD : 0); PyPARSE_YIELD_IS_KEYWORD : 0);
if (closeit) if (closeit)
fclose(fp); fclose(fp);
@ -1085,18 +1085,8 @@ run_pyc_file(FILE *fp, char *filename, PyObject *globals, PyObject *locals,
} }
co = (PyCodeObject *)v; co = (PyCodeObject *)v;
v = PyEval_EvalCode(co, globals, locals); v = PyEval_EvalCode(co, globals, locals);
if (v && flags) { if (v && flags)
if (co->co_flags & CO_NESTED) flags->cf_flags |= (co->co_flags & PyCF_MASK);
flags->cf_flags |= PyCF_NESTED_SCOPES;
if (co->co_flags & CO_GENERATOR_ALLOWED)
flags->cf_flags |= PyCF_GENERATORS;
#if 0
fprintf(stderr, "run_pyc_file: nested_scopes: %d\n",
flags->cf_flags & PyCF_NESTED_SCOPES);
fprintf(stderr, "run_pyc_file: generators: %d\n",
flags->cf_flags & PyCF_GENERATORS);
#endif
}
Py_DECREF(co); Py_DECREF(co);
return v; return v;
} }
@ -1114,7 +1104,7 @@ Py_CompileStringFlags(char *str, char *filename, int start,
node *n; node *n;
PyCodeObject *co; PyCodeObject *co;
n = PyParser_SimpleParseStringFlags(str, start, n = PyParser_SimpleParseStringFlags(str, start,
(flags && flags->cf_flags & PyCF_GENERATORS) ? (flags && flags->cf_flags & CO_GENERATOR_ALLOWED) ?
PyPARSE_YIELD_IS_KEYWORD : 0); PyPARSE_YIELD_IS_KEYWORD : 0);
if (n == NULL) if (n == NULL)
return NULL; return NULL;