diff --git a/doc/build/changelog.rst b/doc/build/changelog.rst index b89ba661ed667c77c18cefbafabfcb046810d1fa..6b79b30cc6204af2739575a00f222b8f17c04d38 100644 --- a/doc/build/changelog.rst +++ b/doc/build/changelog.rst @@ -9,6 +9,14 @@ Changelog :version: 0.9.2 :released: + .. change:: + :tags: bug, py3k + :tickets: 227 + + Fixed bug in Python parsing logic which would fail on Python 3 + when a "try/except" targeted a tuple of exception types, rather + than a single exception. + .. change:: :tags: feature :pullreq: bitbucket:4 diff --git a/mako/pyparser.py b/mako/pyparser.py index aa2d882f235505bd66328f8778500f29742a95f1..db5c295a8c66d6dbf11fea58796050c4c465c6d6 100644 --- a/mako/pyparser.py +++ b/mako/pyparser.py @@ -102,7 +102,7 @@ if _ast: if node.name is not None: self._add_declared(node.name) if node.type is not None: - self.listener.undeclared_identifiers.add(node.type.id) + self.visit(node.type) for statement in node.body: self.visit(statement) diff --git a/test/test_ast.py b/test/test_ast.py index 9f9ec10c8af95e053d80183c9a30a2c59c5a123b..32a1d74c88518ae83b8b3a52776e25567befd25b 100644 --- a/test/test_ast.py +++ b/test/test_ast.py @@ -1,7 +1,8 @@ import unittest from mako import ast, exceptions, pyparser, util, compat -from test import eq_, requires_python_2, requires_python_3 +from test import eq_, requires_python_2, requires_python_3, \ + requires_python_26_or_greater exception_kwargs = { 'source': '', @@ -222,6 +223,28 @@ t2 = lambda (x,y):(x+5, y+4) eq_(parsed.declared_identifiers, set(['t1', 't2'])) eq_(parsed.undeclared_identifiers, set()) + @requires_python_26_or_greater + def test_locate_identifiers_16(self): + code = """ +try: + print(x) +except Exception as e: + print(y) +""" + parsed = ast.PythonCode(code, **exception_kwargs) + eq_(parsed.undeclared_identifiers, set(['x', 'y', 'Exception'])) + + @requires_python_26_or_greater + def test_locate_identifiers_17(self): + code = """ +try: + print(x) +except (Foo, Bar) as e: + print(y) +""" + parsed = ast.PythonCode(code, **exception_kwargs) + eq_(parsed.undeclared_identifiers, set(['x', 'y', 'Foo', 'Bar'])) + def test_no_global_imports(self): code = """ from foo import *