Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 280e6bd

Browse files
committed
Make error messages clearer for illegal combinations of nested
functions and import */exec.
1 parent 6e9c0ba commit 280e6bd

1 file changed

Lines changed: 36 additions & 15 deletions

File tree

Python/compile.c

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4191,32 +4191,53 @@ symtable_check_unoptimized(struct compiling *c,
41914191
|| (ste->ste_nested && si->si_nimplicit)))
41924192
return 0;
41934193

4194+
#define ILLEGAL_CONTAINS "contains a nested function with free variables"
4195+
4196+
#define ILLEGAL_IS "is a nested function"
4197+
41944198
#define ILLEGAL_IMPORT_STAR \
4195-
"import * is not allowed in function '%.100s' " \
4196-
"because it contains a nested function with free variables"
4199+
"import * is not allowed in function '%.100s' because it %s"
41974200

41984201
#define ILLEGAL_BARE_EXEC \
4199-
"unqualified exec is not allowed in function '%.100s' " \
4200-
"because it contains a nested function with free variables"
4202+
"unqualified exec is not allowed in function '%.100s' it %s"
42014203

42024204
#define ILLEGAL_EXEC_AND_IMPORT_STAR \
42034205
"function '%.100s' uses import * and bare exec, which are illegal" \
4204-
"because it contains a nested function with free variables"
4206+
"because it %s"
42054207

42064208
/* XXX perhaps the linenos for these opt-breaking statements
42074209
should be stored so the exception can point to them. */
42084210

4209-
if (ste->ste_optimized == OPT_IMPORT_STAR)
4210-
sprintf(buf, ILLEGAL_IMPORT_STAR,
4211-
PyString_AS_STRING(ste->ste_name));
4212-
else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
4213-
sprintf(buf, ILLEGAL_BARE_EXEC,
4214-
PyString_AS_STRING(ste->ste_name));
4215-
else {
4216-
sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR,
4217-
PyString_AS_STRING(ste->ste_name));
4211+
if (ste->ste_child_free) {
4212+
if (ste->ste_optimized == OPT_IMPORT_STAR)
4213+
sprintf(buf, ILLEGAL_IMPORT_STAR,
4214+
PyString_AS_STRING(ste->ste_name),
4215+
ILLEGAL_CONTAINS);
4216+
else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
4217+
sprintf(buf, ILLEGAL_BARE_EXEC,
4218+
PyString_AS_STRING(ste->ste_name),
4219+
ILLEGAL_CONTAINS);
4220+
else {
4221+
sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR,
4222+
PyString_AS_STRING(ste->ste_name),
4223+
ILLEGAL_CONTAINS);
4224+
}
4225+
} else {
4226+
if (ste->ste_optimized == OPT_IMPORT_STAR)
4227+
sprintf(buf, ILLEGAL_IMPORT_STAR,
4228+
PyString_AS_STRING(ste->ste_name),
4229+
ILLEGAL_IS);
4230+
else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
4231+
sprintf(buf, ILLEGAL_BARE_EXEC,
4232+
PyString_AS_STRING(ste->ste_name),
4233+
ILLEGAL_IS);
4234+
else {
4235+
sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR,
4236+
PyString_AS_STRING(ste->ste_name),
4237+
ILLEGAL_IS);
4238+
}
42184239
}
4219-
4240+
42204241
if (c->c_symtable->st_nested_scopes) {
42214242
PyErr_SetString(PyExc_SyntaxError, buf);
42224243
PyErr_SyntaxLocation(c->c_symtable->st_filename,

0 commit comments

Comments
 (0)