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.trace import create_table
......@@ -28,35 +30,24 @@ class FixNestedExceptTrace(BaseFix):
['else' ':' (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):
parents_results = []
parent = node.parent
while parent is not None:
for r in self.results:
if r['node'] is parent:
parents_results.append(r)
parent = parent.parent
return parents_results
def list_except_clause_children(self, node):
for child in node.children:
if child.type == syms.except_clause:
yield child
# BBB: yield from
for c in self.list_except_clause_children(child):
yield c
def transform(self, node, results):
self.results.append(results)
# Find if the current node has parents, which are also a try_stmt
for parent_results in self.find_parents_results(node):
# Check if the node and its parent use the same variable name for the
# caught exception
parent_exception_node = parent_results['cleanup'][0]
exception_node = results['cleanup'][0]
try:
if exception_node.children[3] == parent_exception_node.children[3]:
trace(self.filename, parent_exception_node.get_lineno(), exception_node.get_lineno())
except_clause, _, body = results['cleanup']
except ValueError:
return
for child in self.list_except_clause_children(body):
try:
if except_clause.children[3] == child.children[3]:
trace(self.filename, except_clause.get_lineno(), child.get_lineno())
except Exception:
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