From 0595c1dfdb5650e950a46ed846a5329951018896 Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Tue, 28 Jun 2011 17:06:02 +0400 Subject: [PATCH 1/4] Fixed false positive for fallback classes in try-except ImportError (PY-3919) --- .../controlflow/PyControlFlowBuilder.java | 4 ++++ .../importExceptImportError.py | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java b/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java index d1bce00df3ec..90eaada7446d 100644 --- a/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java +++ b/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java @@ -52,6 +52,10 @@ public class PyControlFlowBuilder extends PyRecursiveElementVisitor { public void visitPyClass(final PyClass node) { // Create node and stop here myBuilder.startNode(node); + final ReadWriteInstruction instruction = ReadWriteInstruction.newInstruction(myBuilder, node, node.getName(), + ReadWriteInstruction.ACCESS.WRITE); + myBuilder.addNode(instruction); + myBuilder.checkPending(instruction); } @Override diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/importExceptImportError.py b/python/testData/inspections/PyUnresolvedReferencesInspection/importExceptImportError.py index c2cf55a09564..4edf12c6253a 100644 --- a/python/testData/inspections/PyUnresolvedReferencesInspection/importExceptImportError.py +++ b/python/testData/inspections/PyUnresolvedReferencesInspection/importExceptImportError.py @@ -42,8 +42,18 @@ def f(): # PY-3869 def f(x): try: - from foo import bar + from foo import bar #pass except ImportError: def bar(x): return x - return bar(x) \ No newline at end of file + return bar(x) + +# PY-3919 +def f(x): + try: + from foo import Bar #pass + except ImportError: + class Bar(object): + pass + return Bar() + From 5dab3927c8fc229ff1e6166cd9e2b14652ae0982 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Tue, 28 Jun 2011 14:07:14 +0400 Subject: [PATCH 2/4] optimization: do not encode whole file content to bytes on reparse --- .../jetbrains/python/psi/impl/PythonLanguageLevelPusher.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/src/com/jetbrains/python/psi/impl/PythonLanguageLevelPusher.java b/python/src/com/jetbrains/python/psi/impl/PythonLanguageLevelPusher.java index 8d8cb5b436b4..e66419056233 100644 --- a/python/src/com/jetbrains/python/psi/impl/PythonLanguageLevelPusher.java +++ b/python/src/com/jetbrains/python/psi/impl/PythonLanguageLevelPusher.java @@ -170,7 +170,7 @@ public class PythonLanguageLevelPusher implements FilePropertyPusher Date: Tue, 28 Jun 2011 18:53:26 +0400 Subject: [PATCH 3/4] fail() call breaks control flow (PY-3886) The original use case is TestCase.fail(), but other functions named fail() are probably also good candidates to break the control flow. --- .../codeInsight/controlflow/PyControlFlowBuilder.java | 3 ++- .../inspections/PyUnreachableCodeInspection/expected.xml | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java b/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java index 90eaada7446d..663f795a4429 100644 --- a/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java +++ b/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java @@ -68,7 +68,8 @@ public class PyControlFlowBuilder extends PyRecursiveElementVisitor { public void visitPyCallExpression(final PyCallExpression node) { final PyExpression callee = node.getCallee(); // Flow abrupted - if (callee != null && "sys.exit".equals(PyUtil.getReadableRepr(callee, true))) { + if (callee != null && ("sys.exit".equals(PyUtil.getReadableRepr(callee, true)) || + "fail".equals(callee.getName()))) { callee.accept(this); for (PyExpression expression : node.getArguments()) { expression.accept(this); diff --git a/python/testData/inspections/PyUnreachableCodeInspection/expected.xml b/python/testData/inspections/PyUnreachableCodeInspection/expected.xml index f455dfd911bb..5781925361ab 100644 --- a/python/testData/inspections/PyUnreachableCodeInspection/expected.xml +++ b/python/testData/inspections/PyUnreachableCodeInspection/expected.xml @@ -26,16 +26,19 @@ 30 This code is unreachable - unreachable.py 36 This code is unreachable - unreachable.py 46 This code is unreachable + + unreachable.py + 69 + This code is unreachable + From aaaf9ecfba5e42d567434c83bd402bcc87c496bc Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Tue, 28 Jun 2011 19:47:34 +0400 Subject: [PATCH 4/4] Don't offer to move outer functions/classes if an inner element is selected (PY-3883) --- .../move/PyMoveClassOrFunctionDelegate.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/python/src/com/jetbrains/python/refactoring/move/PyMoveClassOrFunctionDelegate.java b/python/src/com/jetbrains/python/refactoring/move/PyMoveClassOrFunctionDelegate.java index 8efe1f203d46..3731275a9ad7 100644 --- a/python/src/com/jetbrains/python/refactoring/move/PyMoveClassOrFunctionDelegate.java +++ b/python/src/com/jetbrains/python/refactoring/move/PyMoveClassOrFunctionDelegate.java @@ -109,15 +109,17 @@ public class PyMoveClassOrFunctionDelegate extends MoveHandlerDelegate { doMove(project, new PsiElement[] {element}, null, null); return true; } + if (element instanceof PsiNamedElement) { + return true; + } return false; } @Nullable private static PsiNamedElement getElementToMove(@NotNull PsiElement element) { - final PsiNamedElement result = PsiTreeUtil.getParentOfType(element, PsiNamedElement.class, false, PyClass.class, PyFunction.class); - if (result instanceof PyFunction && ((PyFunction)result).isTopLevel() || - result instanceof PyClass && ((PyClass)result).isTopLevel()) { - return result; + if (element instanceof PyFunction && ((PyFunction)element).isTopLevel() || + element instanceof PyClass && ((PyClass)element).isTopLevel()) { + return (PsiNamedElement)element; } return null; }