summaryrefslogtreecommitdiff
path: root/checkers/utils.py
diff options
context:
space:
mode:
authorSylvain Th?nault <sylvain.thenault@logilab.fr>2011-10-24 09:06:59 +0200
committerSylvain Th?nault <sylvain.thenault@logilab.fr>2011-10-24 09:06:59 +0200
commitcd70fa86a4ab908eeb2fd9b61a6c2c869c1551a1 (patch)
treeb9cf35662c187e73150b704d7fee27c661a0d314 /checkers/utils.py
parent9fde18a9a891be5a5be152247c04defd75298cae (diff)
downloadpylint-cd70fa86a4ab908eeb2fd9b61a6c2c869c1551a1.tar.gz
Fix for #81078: Warn if names in exception handlers clobber overwrite existing names
Diffstat (limited to 'checkers/utils.py')
-rw-r--r--checkers/utils.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/checkers/utils.py b/checkers/utils.py
index 43d619f..81c1a73 100644
--- a/checkers/utils.py
+++ b/checkers/utils.py
@@ -25,6 +25,31 @@ BUILTINS_NAME = builtins.__name__
COMP_NODE_TYPES = astng.ListComp, astng.SetComp, astng.DictComp, astng.GenExpr
+def is_inside_except(node):
+ """Returns true if node is directly inside an exception handler"""
+ return isinstance(node.parent, astng.ExceptHandler)
+
+
+def clobber_in_except(node):
+ """Checks if an assignment node in an except handler clobbers an existing
+ variable.
+
+ Returns (True, args for W0623) if assignment clobbers an existing variable,
+ (False, None) otherwise.
+ """
+ if isinstance(node, astng.AssAttr):
+ return (True, (node.attrname, 'object %r' % (node.expr.name,)))
+ elif node is not None:
+ name = node.name
+ if is_builtin(name):
+ return (True, (name, 'builtins'))
+ else:
+ scope, stmts = node.lookup(name)
+ if stmts and not isinstance(stmts[0].ass_type(), (astng.Assign, astng.AugAssign)):
+ return (True, (name, 'outer scope (line %i)' % (stmts[0].lineno,)))
+ return (False, None)
+
+
def safe_infer(node):
"""return the inferred value for the given node.
Return None if inference failed or if there is some ambiguity (more than