From 42d90161e27cf7bf4cfdab44b5b13b6da1ef901c Mon Sep 17 00:00:00 2001 From: Jeremy Hylton Date: Tue, 15 Jul 2003 20:24:27 +0000 Subject: SF patch 763201: handling of SyntaxErrors in symbol table build Bug fix candidate. --- Lib/test/test_symtable.py | 18 +++++++++++++++--- Lib/test/test_syntax.py | 15 +++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_symtable.py b/Lib/test/test_symtable.py index 44f156e..5961102 100644 --- a/Lib/test/test_symtable.py +++ b/Lib/test/test_symtable.py @@ -1,8 +1,20 @@ -from test.test_support import verify +from test.test_support import vereq, TestFailed import _symtable symbols = _symtable.symtable("def f(x): return x", "?", "exec") -verify(symbols[0].name == "global") -verify(len([ste for ste in symbols.values() if ste.name == "f"]) == 1) +vereq(symbols[0].name, "global") +vereq(len([ste for ste in symbols.values() if ste.name == "f"]), 1) + +# Bug tickler: SyntaxError file name correct whether error raised +# while parsing or building symbol table. +def checkfilename(brokencode): + try: + _symtable.symtable(brokencode, "spam", "exec") + except SyntaxError, e: + vereq(e.filename, "spam") + else: + raise TestFailed("no SyntaxError for %r" % (brokencode,)) +checkfilename("def f(x): foo)(") # parse-time +checkfilename("def f(x): global x") # symtable-build-time diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py index c5c4f63..a40190e 100644 --- a/Lib/test/test_syntax.py +++ b/Lib/test/test_syntax.py @@ -1,5 +1,6 @@ import re import unittest +import warnings from test import test_support @@ -27,6 +28,20 @@ class SyntaxTestCase(unittest.TestCase): def test_assign_del(self): self._check_error("del f()", "delete") + def test_global_err_then_warn(self): + # Bug tickler: The SyntaxError raised for one global statement + # shouldn't be clobbered by a SyntaxWarning issued for a later one. + source = re.sub('(?m)^ *:', '', """\ + :def error(a): + : global a # SyntaxError + :def warning(): + : b = 1 + : global b # SyntaxWarning + :""") + warnings.filterwarnings(action='ignore', category=SyntaxWarning) + self._check_error(source, "global") + warnings.filters.pop(0) + def test_main(): test_support.run_unittest(SyntaxTestCase) -- cgit v0.12