PY-1244 "Unreachable code" false positive with try/except/else/finally

This commit is contained in:
Oleg Shpynov
2010-07-09 16:33:51 +04:00
parent e476a0721d
commit 2dbae7c34b
4 changed files with 36 additions and 4 deletions
@@ -497,16 +497,16 @@ public class PyControlFlowBuilder extends PyRecursiveElementVisitor {
}
// handle return pending instructions inside try if final block exists
if (!finallyRef.isNull() &&
PsiTreeUtil.isAncestor(tryPart, pendingElement, false)) {
final boolean isPending = PsiTreeUtil.isAncestor(node, pendingElement, false) &&
(finallyPart == null || !PsiTreeUtil.isAncestor(finallyPart, pendingElement, false));
if (!finallyRef.isNull() && isPending) {
myBuilder.addEdge(instruction, finallyRef.get());
myBuilder.addPendingEdge(null, lastFinallyRef.get());
return;
}
// Handle pending instructions inside try with final block
if (finallyPart!=null && pendingScope !=finallyPart &&
PsiTreeUtil.isAncestor(tryPart, pendingElement, false)) {
if (finallyPart!=null && pendingScope !=finallyPart && isPending) {
myBuilder.addEdge(instruction, finallyRef.get());
return;
}
@@ -0,0 +1,9 @@
d = dict()
try:
v = d['key']
except KeyError:
print 'element not found'
else:
print 'element value {0}'.format(v)
finally:
print 'excuting finally clause'
@@ -0,0 +1,19 @@
0(1) element: null
1(2) element: PyAssignmentStatement
2(3) READ ACCESS: dict
3(4) WRITE ACCESS: d
4(5) element: PyTryExceptStatement
5(6) element: PyTryPart
6(7) element: PyAssignmentStatement
7(8) element: PySubscriptionExpression
8(9) READ ACCESS: d
9(10,13) WRITE ACCESS: v
10(11) element: PyElsePart
11(12) element: PyPrintStatement
12(16) READ ACCESS: v
13(14) element: PyExceptPart
14(15) READ ACCESS: KeyError
15(16) element: PyPrintStatement
16(17) element: PyFinallyPart
17(18) element: PyPrintStatement
18() element: null
@@ -119,6 +119,10 @@ public class PyControlFlowBuilderTest extends LightMarkedTestCase {
doTest();
}
public void testTryExceptElseFinally() throws Exception {
doTest();
}
public void testTryFinally() throws Exception {
doTest();
}