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

Skip to content

Commit 778e265

Browse files
committed
Fix SF buf #480096: Assign to __debug__ still allowed
Easy enough to catch assignment in the compiler. The perverse user can still change the value of __debug__, but that may be the least he can do.
1 parent 734c7fb commit 778e265

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

Lib/test/test_compile.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
from test_support import verbose, TestFailed
22

3+
if verbose:
4+
print "Testing whether compiler catches assignment to __debug__"
5+
6+
try:
7+
compile('__debug__ = 1', '?', 'single')
8+
except SyntaxError:
9+
pass
10+
11+
import __builtin__
12+
setattr(__builtin__, '__debug__', 'sure')
13+
314
if verbose:
415
print 'Running tests on argument handling'
516

@@ -21,7 +32,8 @@
2132
except SyntaxError:
2233
pass
2334

24-
print "testing complex args"
35+
if verbose:
36+
print "testing complex args"
2537

2638
def comp_args((a, b)):
2739
print a,b

Python/compile.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5459,8 +5459,13 @@ symtable_assign(struct symtable *st, node *n, int def_flag)
54595459
n = CHILD(n, 1);
54605460
goto loop;
54615461
} else if (TYPE(tmp) == NAME) {
5462-
if (strcmp(STR(tmp), "__debug__") == 0)
5463-
symtable_warn(st, ASSIGN_DEBUG);
5462+
if (strcmp(STR(tmp), "__debug__") == 0) {
5463+
PyErr_SetString(PyExc_SyntaxError,
5464+
ASSIGN_DEBUG);
5465+
PyErr_SyntaxLocation(st->st_filename,
5466+
st->st_cur->ste_opt_lineno);
5467+
st->st_errors++;
5468+
}
54645469
symtable_add_def(st, STR(tmp), DEF_LOCAL | def_flag);
54655470
}
54665471
return;

0 commit comments

Comments
 (0)