From 6b17abf6c0ddaed423a76e665124a80afbc39fe7 Mon Sep 17 00:00:00 2001 From: Thomas Heller Date: Tue, 9 Jul 2002 09:23:27 +0000 Subject: Fix SF Bug 564931: compile() traceback must include filename. --- Include/parsetok.h | 4 ++++ Include/pythonrun.h | 4 ++++ Lib/test/test_compile.py | 9 +++++++++ Parser/parsetok.c | 13 +++++++++++-- Python/pythonrun.c | 29 +++++++++++++++++++++++++++-- 5 files changed, 55 insertions(+), 4 deletions(-) diff --git a/Include/parsetok.h b/Include/parsetok.h index bfe9f8f..ad337bd 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 3a79288..33cf15c 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 3801cac..c063855 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 472b0f5..cd3887d 100644 --- a/Parser/parsetok.c +++ b/Parser/parsetok.c @@ -27,9 +27,18 @@ 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 ec8291c..88367ae 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 -- cgit v0.12