Skip to content
Snippets Groups Projects
Commit c314ecf6 authored by Mike Bayer's avatar Mike Bayer
Browse files

- 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
parent 0b1cce61
No related branches found
No related tags found
No related merge requests found
...@@ -9,6 +9,14 @@ Changelog ...@@ -9,6 +9,14 @@ Changelog
:version: 0.9.2 :version: 0.9.2
:released: :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:: .. change::
:tags: feature :tags: feature
:pullreq: bitbucket:4 :pullreq: bitbucket:4
......
...@@ -102,7 +102,7 @@ if _ast: ...@@ -102,7 +102,7 @@ if _ast:
if node.name is not None: if node.name is not None:
self._add_declared(node.name) self._add_declared(node.name)
if node.type is not None: if node.type is not None:
self.listener.undeclared_identifiers.add(node.type.id) self.visit(node.type)
for statement in node.body: for statement in node.body:
self.visit(statement) self.visit(statement)
......
import unittest import unittest
from mako import ast, exceptions, pyparser, util, compat 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 = { exception_kwargs = {
'source': '', 'source': '',
...@@ -222,6 +223,28 @@ t2 = lambda (x,y):(x+5, y+4) ...@@ -222,6 +223,28 @@ t2 = lambda (x,y):(x+5, y+4)
eq_(parsed.declared_identifiers, set(['t1', 't2'])) eq_(parsed.declared_identifiers, set(['t1', 't2']))
eq_(parsed.undeclared_identifiers, set()) 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): def test_no_global_imports(self):
code = """ code = """
from foo import * from foo import *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment