summaryrefslogtreecommitdiffstats
path: root/Python/bltinmodule.c
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2008-03-29 13:24:23 (GMT)
committerGeorg Brandl <georg@python.org>2008-03-29 13:24:23 (GMT)
commitf2bfd54d6f2acde253ae24805edf116c372bd81e (patch)
tree30964d80bd483e0c2a18b7e80638883cc52ad565 /Python/bltinmodule.c
parentea13dc629caed2dd44b1ba5b611e1ba2ac1fd96f (diff)
downloadcpython-f2bfd54d6f2acde253ae24805edf116c372bd81e.zip
cpython-f2bfd54d6f2acde253ae24805edf116c372bd81e.tar.gz
cpython-f2bfd54d6f2acde253ae24805edf116c372bd81e.tar.bz2
Properly check for consistency with the third argument of
compile() when compiling an AST node.
Diffstat (limited to 'Python/bltinmodule.c')
-rw-r--r--Python/bltinmodule.c32
1 files changed, 16 insertions, 16 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index fff4fda..bcdcda6 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -466,7 +466,7 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
char *str;
char *filename;
char *startstr;
- int start;
+ int mode = -1;
int dont_inherit = 0;
int supplied_flags = 0;
PyCompilerFlags cf;
@@ -474,6 +474,7 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
Py_ssize_t length;
static char *kwlist[] = {"source", "filename", "mode", "flags",
"dont_inherit", NULL};
+ int start[] = {Py_file_input, Py_eval_input, Py_single_input};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
kwlist, &cmd, &filename, &startstr,
@@ -495,6 +496,18 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
PyEval_MergeCompilerFlags(&cf);
}
+ if (strcmp(startstr, "exec") == 0)
+ mode = 0;
+ else if (strcmp(startstr, "eval") == 0)
+ mode = 1;
+ else if (strcmp(startstr, "single") == 0)
+ mode = 2;
+ else {
+ PyErr_SetString(PyExc_ValueError,
+ "compile() arg 3 must be 'exec', 'eval' or 'single'");
+ return NULL;
+ }
+
if (PyAST_Check(cmd)) {
if (supplied_flags & PyCF_ONLY_AST) {
Py_INCREF(cmd);
@@ -505,7 +518,7 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
mod_ty mod;
arena = PyArena_New();
- mod = PyAST_obj2mod(cmd, arena);
+ mod = PyAST_obj2mod(cmd, arena, mode);
if (mod == NULL) {
PyArena_Free(arena);
return NULL;
@@ -526,19 +539,6 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
}
#endif
- /* XXX: is it possible to pass start to the PyAST_ branch? */
- if (strcmp(startstr, "exec") == 0)
- start = Py_file_input;
- else if (strcmp(startstr, "eval") == 0)
- start = Py_eval_input;
- else if (strcmp(startstr, "single") == 0)
- start = Py_single_input;
- else {
- PyErr_SetString(PyExc_ValueError,
- "compile() arg 3 must be 'exec'"
- "or 'eval' or 'single'");
- goto cleanup;
- }
if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
goto cleanup;
@@ -547,7 +547,7 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
"compile() expected string without null bytes");
goto cleanup;
}
- result = Py_CompileStringFlags(str, filename, start, &cf);
+ result = Py_CompileStringFlags(str, filename, start[mode], &cf);
cleanup:
Py_XDECREF(tmp);
return result;