Commit 44942257 authored by Bryton Lacquement's avatar Bryton Lacquement 🚪

nested_except_trace: fix false positives in "try", "else", "finally"

The except_clause which are in the "try", "else", or "finally" sections
were detected by mistake.
parent 889bbd5e
from lib2to3.pygram import python_symbols as syms
from my2to3.fixes import BaseFix from my2to3.fixes import BaseFix
from my2to3.trace import create_table from my2to3.trace import create_table
...@@ -28,35 +30,24 @@ class FixNestedExceptTrace(BaseFix): ...@@ -28,35 +30,24 @@ class FixNestedExceptTrace(BaseFix):
['else' ':' (simple_stmt | suite)] ['else' ':' (simple_stmt | suite)]
['finally' ':' (simple_stmt | suite)]) > ['finally' ':' (simple_stmt | suite)]) >
""" """
order = "pre"
def start_tree(self, tree, filename):
super(FixNestedExceptTrace, self).start_tree(tree, filename)
# Store the results of every node that matches the pattern
self.results = []
def find_parents_results(self, node): def list_except_clause_children(self, node):
parents_results = [] for child in node.children:
parent = node.parent if child.type == syms.except_clause:
while parent is not None: yield child
for r in self.results: # BBB: yield from
if r['node'] is parent: for c in self.list_except_clause_children(child):
parents_results.append(r) yield c
parent = parent.parent
return parents_results
def transform(self, node, results): def transform(self, node, results):
self.results.append(results) try:
except_clause, _, body = results['cleanup']
# Find if the current node has parents, which are also a try_stmt except ValueError:
for parent_results in self.find_parents_results(node): return
# Check if the node and its parent use the same variable name for the
# caught exception for child in self.list_except_clause_children(body):
parent_exception_node = parent_results['cleanup'][0]
exception_node = results['cleanup'][0]
try: try:
if exception_node.children[3] == parent_exception_node.children[3]: if except_clause.children[3] == child.children[3]:
trace(self.filename, parent_exception_node.get_lineno(), exception_node.get_lineno()) trace(self.filename, except_clause.get_lineno(), child.get_lineno())
except Exception: except Exception:
pass pass
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment