diff --git a/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java b/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java index 9aa1c5381e26..164e2ba57101 100644 --- a/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java +++ b/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java @@ -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; } diff --git a/python/testData/codeInsight/controlflow/tryexceptelsefinally.py b/python/testData/codeInsight/controlflow/tryexceptelsefinally.py new file mode 100644 index 000000000000..aff0bf097dcc --- /dev/null +++ b/python/testData/codeInsight/controlflow/tryexceptelsefinally.py @@ -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' \ No newline at end of file diff --git a/python/testData/codeInsight/controlflow/tryexceptelsefinally.txt b/python/testData/codeInsight/controlflow/tryexceptelsefinally.txt new file mode 100644 index 000000000000..fbee361d8659 --- /dev/null +++ b/python/testData/codeInsight/controlflow/tryexceptelsefinally.txt @@ -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 \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyControlFlowBuilderTest.java b/python/testSrc/com/jetbrains/python/PyControlFlowBuilderTest.java index 01994d34ad9a..993dddc21940 100644 --- a/python/testSrc/com/jetbrains/python/PyControlFlowBuilderTest.java +++ b/python/testSrc/com/jetbrains/python/PyControlFlowBuilderTest.java @@ -119,6 +119,10 @@ public class PyControlFlowBuilderTest extends LightMarkedTestCase { doTest(); } + public void testTryExceptElseFinally() throws Exception { + doTest(); + } + public void testTryFinally() throws Exception { doTest(); }