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

Skip to content

Commit 42d9016

Browse files
committed
SF patch 763201: handling of SyntaxErrors in symbol table build
Bug fix candidate.
1 parent 1955fcf commit 42d9016

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

Lib/test/test_symtable.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1-
from test.test_support import verify
1+
from test.test_support import vereq, TestFailed
22

33
import _symtable
44

55
symbols = _symtable.symtable("def f(x): return x", "?", "exec")
66

7-
verify(symbols[0].name == "global")
8-
verify(len([ste for ste in symbols.values() if ste.name == "f"]) == 1)
7+
vereq(symbols[0].name, "global")
8+
vereq(len([ste for ste in symbols.values() if ste.name == "f"]), 1)
9+
10+
# Bug tickler: SyntaxError file name correct whether error raised
11+
# while parsing or building symbol table.
12+
def checkfilename(brokencode):
13+
try:
14+
_symtable.symtable(brokencode, "spam", "exec")
15+
except SyntaxError, e:
16+
vereq(e.filename, "spam")
17+
else:
18+
raise TestFailed("no SyntaxError for %r" % (brokencode,))
19+
checkfilename("def f(x): foo)(") # parse-time
20+
checkfilename("def f(x): global x") # symtable-build-time

Lib/test/test_syntax.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import re
22
import unittest
3+
import warnings
34

45
from test import test_support
56

@@ -27,6 +28,20 @@ def test_assign_call(self):
2728
def test_assign_del(self):
2829
self._check_error("del f()", "delete")
2930

31+
def test_global_err_then_warn(self):
32+
# Bug tickler: The SyntaxError raised for one global statement
33+
# shouldn't be clobbered by a SyntaxWarning issued for a later one.
34+
source = re.sub('(?m)^ *:', '', """\
35+
:def error(a):
36+
: global a # SyntaxError
37+
:def warning():
38+
: b = 1
39+
: global b # SyntaxWarning
40+
:""")
41+
warnings.filterwarnings(action='ignore', category=SyntaxWarning)
42+
self._check_error(source, "global")
43+
warnings.filters.pop(0)
44+
3045
def test_main():
3146
test_support.run_unittest(SyntaxTestCase)
3247

0 commit comments

Comments
 (0)