Part way to allowing "from __future__ import generators" to communicate

that info to code dynamically compiled *by* code compiled with generators
enabled.  Doesn't yet work because there's still no way to tell the parser
that "yield" is OK (unlike nested_scopes, the parser has its fingers in
this too).
Replaced PyEval_GetNestedScopes by a more-general
PyEval_MergeCompilerFlags.  Perhaps I should not have?  I doubted it was
*intended* to be part of the public API, so just did.
This commit is contained in:
Tim Peters 2001-07-16 02:29:45 +00:00
parent 4dbf87152e
commit 5ba5866281
11 changed files with 77 additions and 33 deletions

View file

@ -393,6 +393,7 @@ builtin_compile(PyObject *self, PyObject *args)
char *filename;
char *startstr;
int start;
PyCompilerFlags cf;
if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr))
return NULL;
@ -407,11 +408,10 @@ builtin_compile(PyObject *self, PyObject *args)
"compile() arg 3 must be 'exec' or 'eval' or 'single'");
return NULL;
}
if (PyEval_GetNestedScopes()) {
PyCompilerFlags cf;
cf.cf_nested_scopes = 1;
cf.cf_flags = 0;
if (PyEval_MergeCompilerFlags(&cf))
return Py_CompileStringFlags(str, filename, start, &cf);
} else
else
return Py_CompileString(str, filename, start);
}
@ -822,6 +822,7 @@ builtin_execfile(PyObject *self, PyObject *args)
PyObject *globals = Py_None, *locals = Py_None;
PyObject *res;
FILE* fp;
PyCompilerFlags cf;
if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
&filename,
@ -847,12 +848,11 @@ builtin_execfile(PyObject *self, PyObject *args)
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
if (PyEval_GetNestedScopes()) {
PyCompilerFlags cf;
cf.cf_nested_scopes = 1;
cf.cf_flags = 0;
if (PyEval_MergeCompilerFlags(&cf))
res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
locals, 1, &cf);
} else
else
res = PyRun_FileEx(fp, filename, Py_file_input, globals,
locals, 1);
return res;

View file

@ -2524,7 +2524,7 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
if (co->co_flags & CO_GENERATOR) {
/* Don't need to keep the reference to f_back, it will be set
* when the generator is resumed. */
Py_DECREF(f->f_back);
Py_XDECREF(f->f_back);
f->f_back = NULL;
/* Create a new generator that owns the ready to run frame
@ -2906,11 +2906,23 @@ PyEval_GetRestricted(void)
}
int
PyEval_GetNestedScopes(void)
PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
{
PyFrameObject *current_frame = PyThreadState_Get()->frame;
return current_frame == NULL ? 0 :
current_frame->f_code->co_flags & CO_NESTED;
int result = 0;
if (current_frame != NULL) {
const int codeflags = current_frame->f_code->co_flags;
if (codeflags & CO_NESTED) {
result = 1;
cf->cf_flags |= PyCF_NESTED_SCOPES;
}
if (codeflags & CO_GENERATOR_ALLOWED) {
result = 1;
cf->cf_flags |= PyCF_GENERATORS;
}
}
return result;
}
int
@ -3730,26 +3742,25 @@ exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
else if (PyFile_Check(prog)) {
FILE *fp = PyFile_AsFile(prog);
char *name = PyString_AsString(PyFile_Name(prog));
if (PyEval_GetNestedScopes()) {
PyCompilerFlags cf;
cf.cf_nested_scopes = 1;
PyCompilerFlags cf;
cf.cf_flags = 0;
if (PyEval_MergeCompilerFlags(&cf))
v = PyRun_FileFlags(fp, name, Py_file_input, globals,
locals, &cf);
} else {
else
v = PyRun_File(fp, name, Py_file_input, globals,
locals);
}
}
else {
char *str;
PyCompilerFlags cf;
if (PyString_AsStringAndSize(prog, &str, NULL))
return -1;
if (PyEval_GetNestedScopes()) {
PyCompilerFlags cf;
cf.cf_nested_scopes = 1;
cf.cf_flags = 0;
if (PyEval_MergeCompilerFlags(&cf))
v = PyRun_StringFlags(str, Py_file_input, globals,
locals, &cf);
} else
else
v = PyRun_String(str, Py_file_input, globals, locals);
}
if (plain)

View file

@ -3953,10 +3953,15 @@ jcompile(node *n, char *filename, struct compiling *base,
return NULL;
}
if (flags) {
if (flags->cf_nested_scopes)
if (flags->cf_flags & PyCF_NESTED_SCOPES)
sc.c_future->ff_nested_scopes = 1;
else if (sc.c_future->ff_nested_scopes)
flags->cf_nested_scopes = 1;
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 (symtable_build(&sc, n) < 0) {
com_free(&sc);
@ -4426,8 +4431,12 @@ static int
symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste,
struct symbol_info *si)
{
if (c->c_future && c->c_future->ff_nested_scopes)
c->c_flags |= CO_NESTED;
if (c->c_future) {
if (c->c_future->ff_nested_scopes)
c->c_flags |= CO_NESTED;
if (c->c_future->ff_generators)
c->c_flags |= CO_GENERATOR_ALLOWED;
}
if (ste->ste_generator)
c->c_flags |= CO_GENERATOR;
if (ste->ste_type != TYPE_MODULE)

View file

@ -32,7 +32,7 @@ future_check_features(PyFutureFeatures *ff, node *n, char *filename)
if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
ff->ff_nested_scopes = 1;
} else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
/* OK; this is processed by the parser */
ff->ff_generators= 1;
} else if (strcmp(feature, "braces") == 0) {
PyErr_SetString(PyExc_SyntaxError,
"not a chance");
@ -233,6 +233,7 @@ PyNode_Future(node *n, char *filename)
ff->ff_found_docstring = 0;
ff->ff_last_lineno = -1;
ff->ff_nested_scopes = 0;
ff->ff_generators = 0;
if (future_parse(ff, n, filename) < 0) {
PyMem_Free((void *)ff);

View file

@ -494,7 +494,7 @@ PyRun_InteractiveLoopFlags(FILE *fp, char *filename, PyCompilerFlags *flags)
if (flags == NULL) {
flags = &local_flags;
local_flags.cf_nested_scopes = 0;
local_flags.cf_flags = 0;
}
v = PySys_GetObject("ps1");
if (v == NULL) {
@ -1075,10 +1075,14 @@ run_pyc_file(FILE *fp, char *filename, PyObject *globals, PyObject *locals,
v = PyEval_EvalCode(co, globals, locals);
if (v && flags) {
if (co->co_flags & CO_NESTED)
flags->cf_nested_scopes = 1;
flags->cf_flags |= PyCF_NESTED_SCOPES;
if (co->co_flags & CO_GENERATOR)
flags->cf_flags |= PyCF_GENERATORS;
#if 0
fprintf(stderr, "run_pyc_file: nested_scopes: %d\n",
flags->cf_nested_scopes);
flags->cf_flags & PyCF_NESTED_SCOPES);
fprintf(stderr, "run_pyc_file: generators: %d\n",
flags->cf_flags & PyCF_GENERATORS);
#endif
}
Py_DECREF(co);