diff options
author | Tim Peters <tim.peters@gmail.com> | 2001-07-16 02:29:45 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2001-07-16 02:29:45 (GMT) |
commit | 5ba58662817b708a453020f0425fe4747ea6d5cb (patch) | |
tree | 7d53f1d02fdcc9f6ce4bd1e5130ba1f57269bb3f /Python | |
parent | 4dbf87152eb69e8c8c87d3081630c4bc02d7cace (diff) | |
download | cpython-5ba58662817b708a453020f0425fe4747ea6d5cb.zip cpython-5ba58662817b708a453020f0425fe4747ea6d5cb.tar.gz cpython-5ba58662817b708a453020f0425fe4747ea6d5cb.tar.bz2 |
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.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/bltinmodule.c | 16 | ||||
-rw-r--r-- | Python/ceval.c | 37 | ||||
-rw-r--r-- | Python/compile.c | 17 | ||||
-rw-r--r-- | Python/future.c | 3 | ||||
-rw-r--r-- | Python/pythonrun.c | 10 |
5 files changed, 54 insertions, 29 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 11d6f4c..fcc7d17 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -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; diff --git a/Python/ceval.c b/Python/ceval.c index f42165e..e614e17 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -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) diff --git a/Python/compile.c b/Python/compile.c index 92322fc..e950f0f 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -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) diff --git a/Python/future.c b/Python/future.c index 70be26b..f0960ae 100644 --- a/Python/future.c +++ b/Python/future.c @@ -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); diff --git a/Python/pythonrun.c b/Python/pythonrun.c index c67f50e..d5705b9 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -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); |