Fix SF Bug 564931: compile() traceback must include filename.

This commit is contained in:
Thomas Heller 2002-07-09 09:23:27 +00:00
parent 4254cbd29c
commit 6b17abf6c0
5 changed files with 55 additions and 4 deletions

View File

@ -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

View File

@ -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);

View File

@ -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"

View File

@ -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 = "<string>";
tok->filename = filename ? filename : "<string>";
tok->altwarning = (tok->filename != NULL);
if (Py_TabcheckFlag >= 2)
tok->alterror++;

View File

@ -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