summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2014-04-13 13:35:13 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2014-04-13 13:35:13 -0400
commitc314ecf67074218cae1ae0c8591a711f37758c72 (patch)
treed27af3ca90639c9070306c048117c9c826d8acaa
parent0b1cce614cfd7ad95083cf3aa8b41462ba932762 (diff)
downloadmako-c314ecf67074218cae1ae0c8591a711f37758c72.tar.gz
- 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. fixes #227
-rw-r--r--doc/build/changelog.rst8
-rw-r--r--mako/pyparser.py2
-rw-r--r--test/test_ast.py25
3 files changed, 33 insertions, 2 deletions
diff --git a/doc/build/changelog.rst b/doc/build/changelog.rst
index b89ba66..6b79b30 100644
--- a/doc/build/changelog.rst
+++ b/doc/build/changelog.rst
@@ -10,6 +10,14 @@ Changelog
: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 aa2d882..db5c295 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 9f9ec10..32a1d74 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 *