diff --git a/Include/parsetok.h b/Include/parsetok.h index bfe9f8f494b..ad337bd2a89 100644 --- a/Include/parsetok.h +++ b/Include/parsetok.h @@ -32,6 +32,10 @@ extern DL_IMPORT(node *) PyParser_ParseFileFlags(FILE *, char *, grammar *, int, char *, char *, perrdetail *, int); +extern DL_IMPORT(node *) PyParser_ParseStringFlagsFilename(char *, + char *, + grammar *, int, + perrdetail *, int); #ifdef __cplusplus } #endif diff --git a/Include/pythonrun.h b/Include/pythonrun.h index 3a79288ac04..33cf15c6cc9 100644 --- a/Include/pythonrun.h +++ b/Include/pythonrun.h @@ -45,6 +45,10 @@ DL_IMPORT(int) PyRun_InteractiveLoopFlags(FILE *, char *, PyCompilerFlags *); DL_IMPORT(struct _node *) PyParser_SimpleParseString(char *, int); DL_IMPORT(struct _node *) PyParser_SimpleParseFile(FILE *, char *, int); DL_IMPORT(struct _node *) PyParser_SimpleParseStringFlags(char *, int, int); +DL_IMPORT(struct _node *) PyParser_SimpleParseStringFlagsFilename(char *, + char *, + int, + int); DL_IMPORT(struct _node *) PyParser_SimpleParseFileFlags(FILE *, char *, int, int); diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index 3801cacd87f..c0638554310 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -22,6 +22,15 @@ try: except SyntaxError: pass +if verbose: + print "compiling string with syntax error" + +try: + compile("1+*3", "filename", "exec") +except SyntaxError, detail: + if not detail.filename == "filename": + raise TestFailed, "expected 'filename', got %r" % detail.filename + try: exec 'def f(a = 0, a = 1): pass' raise TestFailed, "duplicate keyword arguments" diff --git a/Parser/parsetok.c b/Parser/parsetok.c index 472b0f5d530..cd3887dff9c 100644 --- a/Parser/parsetok.c +++ b/Parser/parsetok.c @@ -26,10 +26,19 @@ PyParser_ParseString(char *s, grammar *g, int start, perrdetail *err_ret) node * PyParser_ParseStringFlags(char *s, grammar *g, int start, perrdetail *err_ret, int flags) +{ + return PyParser_ParseStringFlagsFilename(s, NULL, + g, start, err_ret, 0); +} + +node * +PyParser_ParseStringFlagsFilename(char *s, char *filename, + grammar *g, int start, + perrdetail *err_ret, int flags) { struct tok_state *tok; - initerr(err_ret, NULL); + initerr(err_ret, filename); if ((tok = PyTokenizer_FromString(s)) == NULL) { err_ret->error = E_NOMEM; @@ -37,7 +46,7 @@ PyParser_ParseStringFlags(char *s, grammar *g, int start, } if (Py_TabcheckFlag || Py_VerboseFlag) { - tok->filename = ""; + tok->filename = filename ? filename : ""; tok->altwarning = (tok->filename != NULL); if (Py_TabcheckFlag >= 2) tok->alterror++; diff --git a/Python/pythonrun.c b/Python/pythonrun.c index ec8291c06b1..88367ae1f88 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -1134,7 +1134,9 @@ Py_CompileStringFlags(char *str, char *filename, int start, { node *n; PyCodeObject *co; - n = PyParser_SimpleParseStringFlags(str, start, PARSER_FLAGS(flags)); + + n = PyParser_SimpleParseStringFlagsFilename(str, filename, start, + PARSER_FLAGS(flags)); if (n == NULL) return NULL; co = PyNode_CompileFlags(n, filename, flags); @@ -1147,7 +1149,8 @@ Py_SymtableString(char *str, char *filename, int start) { node *n; struct symtable *st; - n = PyParser_SimpleParseString(str, start); + n = PyParser_SimpleParseStringFlagsFilename(str, filename, + start, 0); if (n == NULL) return NULL; st = PyNode_CompileSymtable(n, filename); @@ -1195,6 +1198,28 @@ PyParser_SimpleParseString(char *str, int start) return PyParser_SimpleParseStringFlags(str, start, 0); } +node * +PyParser_SimpleParseStringFlagsFilename(char *str, char *filename, + int start, int flags) +{ + node *n; + perrdetail err; + + n = PyParser_ParseStringFlagsFilename(str, filename, + &_PyParser_Grammar, + start, &err, flags); + if (n == NULL) + err_input(&err); + return n; +} + +node * +PyParser_SimpleParseStringFilename(char *str, char *filename, int start) +{ + return PyParser_SimpleParseStringFlagsFilename(str, filename, + start, 0); +} + /* Set the error appropriate to the given input error code (see errcode.h) */ static void