diff --git a/python/src/com/jetbrains/python/validation/CompatibilityVisitor.java b/python/src/com/jetbrains/python/validation/CompatibilityVisitor.java index de1a4d44a0d1..4441f7eca5a8 100644 --- a/python/src/com/jetbrains/python/validation/CompatibilityVisitor.java +++ b/python/src/com/jetbrains/python/validation/CompatibilityVisitor.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.jetbrains.python.validation; import com.google.common.collect.Maps; @@ -276,8 +262,8 @@ public abstract class CompatibilityVisitor extends PyAnnotator { public void visitPyRaiseStatement(PyRaiseStatement node) { super.visitPyRaiseStatement(node); - // empty raise - registerForAllMatchingVersions(level -> UnsupportedFeaturesUtil.raiseHasNoArgs(node, level), + // empty raise under finally + registerForAllMatchingVersions(level -> UnsupportedFeaturesUtil.raiseHasNoArgsUnderFinally(node, level), " not support this syntax. Raise with no arguments can only be used in an except block", node, null, diff --git a/python/src/com/jetbrains/python/validation/TryExceptAnnotator.java b/python/src/com/jetbrains/python/validation/TryExceptAnnotator.java index 8a448006a114..e1f54238fbae 100644 --- a/python/src/com/jetbrains/python/validation/TryExceptAnnotator.java +++ b/python/src/com/jetbrains/python/validation/TryExceptAnnotator.java @@ -1,22 +1,11 @@ -/* - * Copyright 2000-2014 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.jetbrains.python.validation; +import com.intellij.psi.util.PsiTreeUtil; import com.jetbrains.python.PyBundle; import com.jetbrains.python.psi.PyExceptPart; +import com.jetbrains.python.psi.PyFinallyPart; +import com.jetbrains.python.psi.PyRaiseStatement; import com.jetbrains.python.psi.PyTryExceptStatement; /** @@ -27,7 +16,7 @@ import com.jetbrains.python.psi.PyTryExceptStatement; public class TryExceptAnnotator extends PyAnnotator { @Override public void visitPyTryExceptStatement(final PyTryExceptStatement node) { - PyExceptPart[] exceptParts = node.getExceptParts(); + final PyExceptPart[] exceptParts = node.getExceptParts(); boolean haveDefaultExcept = false; for (PyExceptPart part : exceptParts) { if (haveDefaultExcept) { @@ -38,4 +27,11 @@ public class TryExceptAnnotator extends PyAnnotator { } } } + + @Override + public void visitPyRaiseStatement(PyRaiseStatement node) { + if (node.getExpressions().length == 0 && PsiTreeUtil.getParentOfType(node, PyExceptPart.class, PyFinallyPart.class) == null) { + markError(node, "No exception to reraise"); + } + } } diff --git a/python/src/com/jetbrains/python/validation/UnsupportedFeaturesUtil.java b/python/src/com/jetbrains/python/validation/UnsupportedFeaturesUtil.java index a62ebb696008..928f2f2fa53d 100644 --- a/python/src/com/jetbrains/python/validation/UnsupportedFeaturesUtil.java +++ b/python/src/com/jetbrains/python/validation/UnsupportedFeaturesUtil.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2014 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.jetbrains.python.validation; import com.intellij.openapi.diagnostic.Logger; @@ -22,6 +8,7 @@ import com.intellij.psi.util.PsiTreeUtil; import com.jetbrains.python.PyTokenTypes; import com.jetbrains.python.PythonHelpersLocator; import com.jetbrains.python.psi.*; +import org.jetbrains.annotations.NotNull; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -88,15 +75,10 @@ public class UnsupportedFeaturesUtil { } } - public static boolean raiseHasNoArgs(PyRaiseStatement node, LanguageLevel versionToProcess) { - final PyExpression[] expressions = node.getExpressions(); - if (expressions.length == 0 && versionToProcess.isPy3K()) { - final PyExceptPart exceptPart = PsiTreeUtil.getParentOfType(node, PyExceptPart.class); - if (exceptPart == null) { - return true; - } - } - return false; + public static boolean raiseHasNoArgsUnderFinally(@NotNull PyRaiseStatement node, @NotNull LanguageLevel versionToProcess) { + return node.getExpressions().length == 0 && + versionToProcess.isPython2() && + PsiTreeUtil.getParentOfType(node, PyFinallyPart.class) != null; } public static boolean raiseHasMoreThenOneArg(PyRaiseStatement node, LanguageLevel versionToProcess) { diff --git a/python/testData/highlighting/emptyRaise.py b/python/testData/highlighting/emptyRaise.py new file mode 100644 index 000000000000..9312486c19c5 --- /dev/null +++ b/python/testData/highlighting/emptyRaise.py @@ -0,0 +1,12 @@ +raise + +try: + raise ValueError +except: + raise + + +try: + raise ValueError +finally: + raise \ No newline at end of file diff --git a/python/testData/highlighting/unsupportedFeaturesInPython3.py b/python/testData/highlighting/unsupportedFeaturesInPython3.py index 5a4f399b3f62..d690cbfdd433 100644 --- a/python/testData/highlighting/unsupportedFeaturesInPython3.py +++ b/python/testData/highlighting/unsupportedFeaturesInPython3.py @@ -18,7 +18,7 @@ try: import __builtin__ -raise +raise try: pass diff --git a/python/testData/inspections/PyCompatibilityInspection/raiseMultipleArgs.py b/python/testData/inspections/PyCompatibilityInspection/raiseMultipleArgs.py new file mode 100644 index 000000000000..b938fc7a4914 --- /dev/null +++ b/python/testData/inspections/PyCompatibilityInspection/raiseMultipleArgs.py @@ -0,0 +1,4 @@ +try: + a +except : + raise ImportError, ImportWarning diff --git a/python/testData/inspections/PyCompatibilityInspection/raiseStatement.py b/python/testData/inspections/PyCompatibilityInspection/raiseStatement.py deleted file mode 100644 index 05e759eade12..000000000000 --- a/python/testData/inspections/PyCompatibilityInspection/raiseStatement.py +++ /dev/null @@ -1,6 +0,0 @@ -try: - a -except : - raise ImportError, ImportWarning - -raise diff --git a/python/testData/inspections/PyCompatibilityInspection/tryExceptEmptyRaise.py b/python/testData/inspections/PyCompatibilityInspection/tryExceptEmptyRaise.py new file mode 100644 index 000000000000..e59cdaadbd51 --- /dev/null +++ b/python/testData/inspections/PyCompatibilityInspection/tryExceptEmptyRaise.py @@ -0,0 +1,4 @@ +try: + raise ValueError +except: + raise \ No newline at end of file diff --git a/python/testData/inspections/PyCompatibilityInspection/tryFinallyEmptyRaisePy2.py b/python/testData/inspections/PyCompatibilityInspection/tryFinallyEmptyRaisePy2.py new file mode 100644 index 000000000000..447581d052a1 --- /dev/null +++ b/python/testData/inspections/PyCompatibilityInspection/tryFinallyEmptyRaisePy2.py @@ -0,0 +1,4 @@ +try: + raise ValueError +finally: + raise \ No newline at end of file diff --git a/python/testData/inspections/PyCompatibilityInspection/tryFinallyEmptyRaisePy3.py b/python/testData/inspections/PyCompatibilityInspection/tryFinallyEmptyRaisePy3.py new file mode 100644 index 000000000000..1dea5f56e6b4 --- /dev/null +++ b/python/testData/inspections/PyCompatibilityInspection/tryFinallyEmptyRaisePy3.py @@ -0,0 +1,4 @@ +try: + raise ValueError +finally: + raise \ No newline at end of file diff --git a/python/testData/inspections/PyUnreachableCodeInspection/Unreachable.py b/python/testData/inspections/PyUnreachableCodeInspection/Unreachable.py index 3bc2b9b0b780..92b5ef423e0e 100644 --- a/python/testData/inspections/PyUnreachableCodeInspection/Unreachable.py +++ b/python/testData/inspections/PyUnreachableCodeInspection/Unreachable.py @@ -88,7 +88,7 @@ def f(): # PY-4208 def f(g): try: - raise + raise ValueError finally: g() g() diff --git a/python/testSrc/com/jetbrains/python/PythonHighlightingTest.java b/python/testSrc/com/jetbrains/python/PythonHighlightingTest.java index 5c0fcc692fb7..6001dbad2686 100644 --- a/python/testSrc/com/jetbrains/python/PythonHighlightingTest.java +++ b/python/testSrc/com/jetbrains/python/PythonHighlightingTest.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.jetbrains.python; import com.intellij.openapi.editor.colors.EditorColorsManager; @@ -379,6 +365,11 @@ public class PythonHighlightingTest extends PyTestCase { runWithLanguageLevel(LanguageLevel.PYTHON30, this::doTest); } + // PY-26510 + public void testEmptyRaise() { + doTest(false, false); + } + @NotNull private static EditorColorsScheme createTemporaryColorScheme() { EditorColorsManager manager = EditorColorsManager.getInstance(); diff --git a/python/testSrc/com/jetbrains/python/inspections/PyCompatibilityInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyCompatibilityInspectionTest.java index f7e350fb58d0..49e4567ea113 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyCompatibilityInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/PyCompatibilityInspectionTest.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.jetbrains.python.inspections; import com.jetbrains.python.fixtures.PyInspectionTestCase; @@ -76,7 +62,7 @@ public class PyCompatibilityInspectionTest extends PyInspectionTestCase { doTest(); } - public void testRaiseStatement() { + public void testRaiseMultipleArgs() { doTest(); } @@ -233,6 +219,21 @@ public class PyCompatibilityInspectionTest extends PyInspectionTestCase { doTest(); } + // PY-26510 + public void testTryExceptEmptyRaise() { + doTest(); + } + + // PY-26510 + public void testTryFinallyEmptyRaisePy2() { + doTest(); + } + + // PY-26510 + public void testTryFinallyEmptyRaisePy3() { + doTest(LanguageLevel.PYTHON30); + } + private void doTest(@NotNull LanguageLevel level) { runWithLanguageLevel(level, this::doTest); }