summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2009-03-02 23:31:26 (GMT)
committerBenjamin Peterson <benjamin@python.org>2009-03-02 23:31:26 (GMT)
commitf5b52246ed8a1191c3aa1da7d3c63bbe11aee020 (patch)
treedcac9a1daca7f583ff958cb61f73e9ed11b5564b /Python
parent0663a1ed793c164fb11d3dd62bebc677e260891e (diff)
downloadcpython-f5b52246ed8a1191c3aa1da7d3c63bbe11aee020.zip
cpython-f5b52246ed8a1191c3aa1da7d3c63bbe11aee020.tar.gz
cpython-f5b52246ed8a1191c3aa1da7d3c63bbe11aee020.tar.bz2
ignore the coding cookie in compile(), exec(), and eval() if the source is a string #4626
Diffstat (limited to 'Python')
-rw-r--r--Python/bltinmodule.c16
-rw-r--r--Python/pythonrun.c14
2 files changed, 20 insertions, 10 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 9805697..7a27fba 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -494,12 +494,13 @@ PyDoc_STR(
static char *
-source_as_string(PyObject *cmd, char *funcname, char *what)
+source_as_string(PyObject *cmd, char *funcname, char *what, PyCompilerFlags *cf)
{
char *str;
Py_ssize_t size;
if (PyUnicode_Check(cmd)) {
+ cf->cf_flags |= PyCF_IGNORE_COOKIE;
cmd = _PyUnicode_AsDefaultEncodedString(cmd, NULL);
if (cmd == NULL)
return NULL;
@@ -591,7 +592,7 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
return result;
}
- str = source_as_string(cmd, "compile", "string, bytes, AST or code");
+ str = source_as_string(cmd, "compile", "string, bytes, AST or code", &cf);
if (str == NULL)
return NULL;
@@ -703,14 +704,14 @@ builtin_eval(PyObject *self, PyObject *args)
return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
}
- str = source_as_string(cmd, "eval", "string, bytes or code");
+ cf.cf_flags = PyCF_SOURCE_IS_UTF8;
+ str = source_as_string(cmd, "eval", "string, bytes or code", &cf);
if (str == NULL)
return NULL;
while (*str == ' ' || *str == '\t')
str++;
- cf.cf_flags = PyCF_SOURCE_IS_UTF8;
(void)PyEval_MergeCompilerFlags(&cf);
result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
Py_XDECREF(tmp);
@@ -779,12 +780,13 @@ builtin_exec(PyObject *self, PyObject *args)
v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
}
else {
- char *str = source_as_string(prog, "exec",
- "string, bytes or code");
+ char *str;
PyCompilerFlags cf;
+ cf.cf_flags = PyCF_SOURCE_IS_UTF8;
+ str = source_as_string(prog, "exec",
+ "string, bytes or code", &cf);
if (str == NULL)
return NULL;
- cf.cf_flags = PyCF_SOURCE_IS_UTF8;
if (PyEval_MergeCompilerFlags(&cf))
v = PyRun_StringFlags(str, Py_file_input, globals,
locals, &cf);
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 65c6f5f..dee18b6 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -1002,9 +1002,17 @@ PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flag
}
/* compute parser flags based on compiler flags */
-#define PARSER_FLAGS(flags) \
- ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
- PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
+static int PARSER_FLAGS(PyCompilerFlags *flags)
+{
+ int parser_flags = 0;
+ if (!flags)
+ return 0;
+ if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
+ parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
+ if (flags->cf_flags & PyCF_IGNORE_COOKIE)
+ parser_flags |= PyPARSE_IGNORE_COOKIE;
+ return parser_flags;
+}
#if 0
/* Keep an example of flags with future keyword support. */