From: Daira Hopwood Date: Thu, 5 Sep 2013 18:12:00 +0000 (+0100) Subject: Sun Oct 9 05:40:22 BST 2011 david-sarah@jacaranda.org X-Git-Url: https://git.rkrishnan.org/(%5B%5E?a=commitdiff_plain;h=ada7a28545d547dd90087189da6339bd3178a3c5;p=tahoe-lafs%2Ftahoe-lafs.git Sun Oct 9 05:40:22 BST 2011 david-sarah@jacaranda.org * check-miscaptures.py: check while loops and list comprehensions as well as for loops. Also fix a pyflakes warning. refs #1555 --- diff --git a/misc/coding_tools/check-miscaptures.py b/misc/coding_tools/check-miscaptures.py index 9820a8a3..07b94a5b 100644 --- a/misc/coding_tools/check-miscaptures.py +++ b/misc/coding_tools/check-miscaptures.py @@ -1,7 +1,7 @@ #! /usr/bin/python -import os, sys, compiler, traceback -from compiler.ast import Node, For, AssName, Name, Lambda, Function +import os, sys, compiler +from compiler.ast import Node, For, While, ListComp, AssName, Name, Lambda, Function def check_source(source): @@ -21,16 +21,25 @@ def check_thing(parser, thing): return results def check_ast(ast, results): - """Check a node outside a 'for' loop.""" - if isinstance(ast, For): - check_for(ast, results) + """Check a node outside a loop.""" + if isinstance(ast, (For, While, ListComp)): + check_loop(ast, results) else: for child in ast.getChildNodes(): if isinstance(ast, Node): check_ast(child, results) -def check_for(ast, results): - """Check a particular outer 'for' loop.""" +def check_loop(ast, results): + """Check a particular outer loop.""" + + # List comprehensions have a poorly designed AST of the form + # ListComp(exprNode, [ListCompFor(...), ...]), in which the + # result expression is outside the ListCompFor node even though + # it is logically inside the loop(s). + # There may be multiple ListCompFor nodes (in cases such as + # [lambda: (a,b) for a in ... for b in ...] + # ), and that case they are not nested in the AST. But these + # warts (nonobviously) happen not to matter for our analysis. assigned = {} # maps name to lineno of topmost assignment nested = set()