diff --git a/python/src/com/jetbrains/python/codeInsight/stdlib/StdlibTypes2.properties b/python/helpers/StdlibTypes2.properties similarity index 100% rename from python/src/com/jetbrains/python/codeInsight/stdlib/StdlibTypes2.properties rename to python/helpers/StdlibTypes2.properties diff --git a/python/src/com/jetbrains/python/codeInsight/stdlib/StdlibTypes3.properties b/python/helpers/StdlibTypes3.properties similarity index 100% rename from python/src/com/jetbrains/python/codeInsight/stdlib/StdlibTypes3.properties rename to python/helpers/StdlibTypes3.properties diff --git a/python/src/com/jetbrains/python/buildout/BuildoutFacet.java b/python/src/com/jetbrains/python/buildout/BuildoutFacet.java index de5a2ddab2bc..fecbb9ffa70e 100644 --- a/python/src/com/jetbrains/python/buildout/BuildoutFacet.java +++ b/python/src/com/jetbrains/python/buildout/BuildoutFacet.java @@ -97,6 +97,7 @@ public class BuildoutFacet extends Facet implements /** * Generates a sys.path[0:0] = [...] with paths that buildout script wants. + * * @param module to get a buildout facet from * @return the statement, or null if there's no buildout facet. */ @@ -158,7 +159,7 @@ public class BuildoutFacet extends Facet implements } return paths; } - catch(IOException e) { + catch (IOException e) { LOG.info(e); return null; } @@ -208,18 +209,20 @@ public class BuildoutFacet extends Facet implements String text = VfsUtil.loadText(vFile); String[] lines = LineTokenizer.tokenize(text, false); int index = 0; - while(index < lines.length && !lines [index].startsWith("def addsitepackages(")) + while (index < lines.length && !lines[index].startsWith("def addsitepackages(")) { index++; - while(index < lines.length && !lines [index].trim().startsWith("buildout_paths = [")) + } + while (index < lines.length && !lines[index].trim().startsWith("buildout_paths = [")) { index++; + } index++; - while(index < lines.length && !lines [index].trim().equals("]")) { - String line = lines [index].trim(); + while (index < lines.length && !lines[index].trim().equals("]")) { + String line = lines[index].trim(); if (line.endsWith(",")) { - line = line.substring(0, line.length()-1); + line = line.substring(0, line.length() - 1); } if (line.startsWith("'") && line.endsWith("'")) { - result.add(StringUtil.unescapeStringCharacters(line.substring(1, line.length()-1))); + result.add(StringUtil.unescapeStringCharacters(line.substring(1, line.length() - 1))); } index++; } @@ -243,10 +246,12 @@ public class BuildoutFacet extends Facet implements // alter execution script ParamsGroup script_params = params.getParamsGroup(PythonCommandLineState.GROUP_SCRIPT); assert script_params != null; - String normal_script = script_params.getParameters().get(0); // expect DjangoUtil.MANAGE_FILE - String engulfer_path = PythonHelpersLocator.getHelperPath("pycharm/buildout_engulfer.py"); - new_env.put("PYCHARM_ENGULF_SCRIPT", getConfiguration().getScriptName()); - script_params.getParametersList().replaceOrPrepend(normal_script, engulfer_path); + if (script_params.getParameters().size() > 0) { + String normal_script = script_params.getParameters().get(0); // expect DjangoUtil.MANAGE_FILE + String engulfer_path = PythonHelpersLocator.getHelperPath("pycharm/buildout_engulfer.py"); + new_env.put("PYCHARM_ENGULF_SCRIPT", getConfiguration().getScriptName()); + script_params.getParametersList().replaceOrPrepend(normal_script, engulfer_path); + } // add pycharm helpers to pythonpath so that fixGetpass is importable PythonEnvUtil.addToPythonPath(new_env, PythonHelpersLocator.getHelpersRoot().getAbsolutePath()); @@ -280,7 +285,7 @@ public class BuildoutFacet extends Facet implements String text = FileUtil.loadFile(cfg); final PsiFile configFile = PsiFileFactory .getInstance(getModule().getProject()).createFileFromText("buildout.cfg", - BuildoutCfgLanguage.INSTANCE, text); + BuildoutCfgLanguage.INSTANCE, text); if (configFile != null && configFile instanceof BuildoutCfgFile) { return (BuildoutCfgFile)configFile; } @@ -290,7 +295,7 @@ public class BuildoutFacet extends Facet implements } return null; } - + public static List getScripts(@Nullable BuildoutFacet buildoutFacet, final VirtualFile baseDir) { File rootPath = null; if (buildoutFacet != null) { diff --git a/python/src/com/jetbrains/python/buildout/config/BuildoutCfgProblemFileHighlightFilter.java b/python/src/com/jetbrains/python/buildout/config/BuildoutCfgProblemFileHighlightFilter.java index 41b1afb7455b..ae20d7b4f65a 100644 --- a/python/src/com/jetbrains/python/buildout/config/BuildoutCfgProblemFileHighlightFilter.java +++ b/python/src/com/jetbrains/python/buildout/config/BuildoutCfgProblemFileHighlightFilter.java @@ -1,7 +1,6 @@ package com.jetbrains.python.buildout.config; import com.intellij.openapi.fileTypes.FileType; -import com.intellij.openapi.fileTypes.FileTypeManager; import com.intellij.openapi.util.Condition; import com.intellij.openapi.vfs.VirtualFile; diff --git a/python/src/com/jetbrains/python/codeInsight/PyTargetElementEvaluator.java b/python/src/com/jetbrains/python/codeInsight/PyTargetElementEvaluator.java index 3f8a7b6ad37a..3c24ee89fe3e 100644 --- a/python/src/com/jetbrains/python/codeInsight/PyTargetElementEvaluator.java +++ b/python/src/com/jetbrains/python/codeInsight/PyTargetElementEvaluator.java @@ -6,6 +6,7 @@ import com.intellij.psi.PsiElement; import com.intellij.psi.PsiReference; import com.intellij.psi.util.PsiTreeUtil; import com.jetbrains.python.codeInsight.controlflow.ScopeOwner; +import com.jetbrains.python.psi.PyParameter; import com.jetbrains.python.psi.PyQualifiedExpression; import com.jetbrains.python.psi.PyReferenceExpression; import com.jetbrains.python.psi.PyTargetExpression; @@ -32,8 +33,9 @@ public class PyTargetElementEvaluator implements TargetElementEvaluator { PsiElement result = ref.resolve(); while (result instanceof PyReferenceExpression || result instanceof PyTargetExpression) { PsiElement nextResult = ((PyQualifiedExpression) result).getReference(PyResolveContext.noImplicits()).resolve(); - if (nextResult != null && nextResult != result && PsiTreeUtil.getParentOfType(element, ScopeOwner.class) == - PsiTreeUtil.getParentOfType(result, ScopeOwner.class)) { + if (nextResult != null && nextResult != result && + PsiTreeUtil.getParentOfType(element, ScopeOwner.class) == PsiTreeUtil.getParentOfType(result, ScopeOwner.class) && + (nextResult instanceof PyReferenceExpression || nextResult instanceof PyTargetExpression || nextResult instanceof PyParameter)) { result = nextResult; } else { diff --git a/python/src/com/jetbrains/python/codeInsight/completion/PyKeywordCompletionContributor.java b/python/src/com/jetbrains/python/codeInsight/completion/PyKeywordCompletionContributor.java index 6888b023c0a8..ac303b484ffb 100644 --- a/python/src/com/jetbrains/python/codeInsight/completion/PyKeywordCompletionContributor.java +++ b/python/src/com/jetbrains/python/codeInsight/completion/PyKeywordCompletionContributor.java @@ -51,12 +51,12 @@ public class PyKeywordCompletionContributor extends CompletionContributor { final ASTNode ctxNode = context.getNode(); if (ctxNode != null && PyTokenTypes.STRING_NODES.contains(ctxNode.getElementType())) return false; // no sense inside string PsiElement p = (PsiElement)element; - int first_offset = p.getTextOffset(); + int firstOffset = p.getTextRange().getStartOffset(); // we must be a stmt ourselves, not a part of another stmt // try to climb to the stmt level with the same offset while (true) { if (p == null) return false; - if (p.getTextOffset() != first_offset) return false; + if (p.getTextRange().getStartOffset() != firstOffset) return false; if (p instanceof PyStatement) break; p = p.getParent(); } @@ -73,7 +73,7 @@ public class PyKeywordCompletionContributor extends CompletionContributor { PsiDocumentManager docMgr = PsiDocumentManager.getInstance(p.getProject()); Document doc = docMgr.getDocument(p.getContainingFile().getOriginalFile()); if (doc != null) { - if (doc.getLineNumber(prev.getTextRange().getEndOffset()) == doc.getLineNumber(first_offset)) { + if (doc.getLineNumber(prev.getTextRange().getEndOffset()) == doc.getLineNumber(firstOffset)) { return false; // same line } } @@ -215,6 +215,9 @@ public class PyKeywordCompletionContributor extends CompletionContributor { private static final PsiElementPattern.Capture IN_STRING_LITERAL = psiElement().inside(PyStringLiteralExpression.class); + private static final PsiElementPattern.Capture IN_FUNCTION_HEADER = + psiElement().inside(PyFunction.class).andNot(psiElement().inside(PyStatementList.class)); + public static final PsiElementPattern.Capture AFTER_QUALIFIER = psiElement().afterLeaf(psiElement().withText(".").inside(PyReferenceExpression.class)); @@ -511,6 +514,7 @@ public class PyKeywordCompletionContributor extends CompletionContributor { .andNot(IN_COMMENT) .andNot(IN_IMPORT_STMT) .andNot(IN_PARAM_LIST) + .andNot(IN_FUNCTION_HEADER) .andNot(AFTER_QUALIFIER).andNot(IN_STRING_LITERAL) , new PyKeywordCompletionProvider("not", "lambda") @@ -526,6 +530,7 @@ public class PyKeywordCompletionContributor extends CompletionContributor { .andNot(IN_IMPORT_STMT) .and(NOT_PARAMETER_OR_DEFAULT_VALUE) .andNot(AFTER_QUALIFIER) + .andNot(IN_FUNCTION_HEADER) , new PyKeywordCompletionProvider(TailType.NONE, "True", "False", "None") ); diff --git a/python/src/com/jetbrains/python/codeInsight/stdlib/PyStdlibTypeProvider.java b/python/src/com/jetbrains/python/codeInsight/stdlib/PyStdlibTypeProvider.java index 1bcda3178fcf..4e03bd564f9e 100644 --- a/python/src/com/jetbrains/python/codeInsight/stdlib/PyStdlibTypeProvider.java +++ b/python/src/com/jetbrains/python/codeInsight/stdlib/PyStdlibTypeProvider.java @@ -5,6 +5,7 @@ import com.intellij.openapi.util.Ref; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiElement; import com.jetbrains.python.PyNames; +import com.jetbrains.python.PythonHelpersLocator; import com.jetbrains.python.documentation.StructuredDocString; import com.jetbrains.python.psi.*; import com.jetbrains.python.psi.impl.PyBuiltinCache; @@ -15,6 +16,7 @@ import com.jetbrains.python.psi.types.*; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Map; @@ -249,7 +251,7 @@ public class PyStdlibTypeProvider extends PyTypeProviderBase { final String name = level.isPy3K() ? "StdlibTypes3" : "StdlibTypes2"; if (result.isEmpty()) { try { - final InputStream s = getClass().getResourceAsStream(String.format("%s.properties", name)); + final InputStream s = new FileInputStream(PythonHelpersLocator.getHelperFile(String.format("%s.properties", name))); try { result.load(s); } diff --git a/python/src/com/jetbrains/python/console/PydevConsoleCommunication.java b/python/src/com/jetbrains/python/console/PydevConsoleCommunication.java index 61e65cae5f51..e7dbfd93992b 100644 --- a/python/src/com/jetbrains/python/console/PydevConsoleCommunication.java +++ b/python/src/com/jetbrains/python/console/PydevConsoleCommunication.java @@ -221,7 +221,7 @@ public class PydevConsoleCommunication extends AbstractConsoleCommunication impl lock.wait(10); } catch (InterruptedException e) { - LOG.error(e); + //pass } } } diff --git a/python/src/com/jetbrains/python/debugger/PyDebugProcess.java b/python/src/com/jetbrains/python/debugger/PyDebugProcess.java index 2210d8a59b32..46cdc7480740 100644 --- a/python/src/com/jetbrains/python/debugger/PyDebugProcess.java +++ b/python/src/com/jetbrains/python/debugger/PyDebugProcess.java @@ -38,10 +38,7 @@ import org.jetbrains.annotations.Nullable; import java.io.IOException; import java.net.ServerSocket; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; import static javax.swing.SwingUtilities.invokeLater; @@ -61,7 +58,7 @@ public class PyDebugProcess extends XDebugProcess implements IPyDebugProcess, Pr private final Map> myRegisteredExceptionBreakpoints = new ConcurrentHashMap>(); - private final List mySuspendedThreads = Lists.newArrayList(); + private final List mySuspendedThreads = Collections.synchronizedList(Lists.newArrayList()); private final Map myStackFrameCache = Maps.newHashMap(); private final Map myNewVariableValue = Maps.newHashMap(); diff --git a/python/src/com/jetbrains/python/debugger/PyDebuggerEditorsProvider.java b/python/src/com/jetbrains/python/debugger/PyDebuggerEditorsProvider.java index ba1fc8754f5d..e1b47b2285fa 100644 --- a/python/src/com/jetbrains/python/debugger/PyDebuggerEditorsProvider.java +++ b/python/src/com/jetbrains/python/debugger/PyDebuggerEditorsProvider.java @@ -46,15 +46,17 @@ public class PyDebuggerEditorsProvider extends XDebuggerEditorsProvider { final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document); if (psiFile != null) { int offset = sourcePosition.getOffset(); - final int lineEndOffset = document.getLineEndOffset(document.getLineNumber(offset)); - do { - PsiElement element = psiFile.findElementAt(offset); - if (element != null && !(element instanceof PsiWhiteSpace || element instanceof PsiComment)) { - return PyPsiUtils.getStatement(element); + if (offset >= 0 && offset < document.getTextLength()) { + final int lineEndOffset = document.getLineEndOffset(document.getLineNumber(offset)); + do { + PsiElement element = psiFile.findElementAt(offset); + if (element != null && !(element instanceof PsiWhiteSpace || element instanceof PsiComment)) { + return PyPsiUtils.getStatement(element); + } + offset = element.getTextRange().getEndOffset() + 1; } - offset = element.getTextRange().getEndOffset() + 1; + while (offset < lineEndOffset); } - while (offset < lineEndOffset); } } return null; diff --git a/python/src/com/jetbrains/python/debugger/PySignatureCacheManagerImpl.java b/python/src/com/jetbrains/python/debugger/PySignatureCacheManagerImpl.java index 009811ab0b2d..ac51515a5e33 100644 --- a/python/src/com/jetbrains/python/debugger/PySignatureCacheManagerImpl.java +++ b/python/src/com/jetbrains/python/debugger/PySignatureCacheManagerImpl.java @@ -141,7 +141,7 @@ public class PySignatureCacheManagerImpl extends PySignatureCacheManager { try { data = CALL_SIGNATURES_ATTRIBUTE.readAttributeBytes(file); } - catch (IOException e) { + catch (Exception e) { data = null; } diff --git a/python/src/com/jetbrains/python/documentation/PythonDocumentationProvider.java b/python/src/com/jetbrains/python/documentation/PythonDocumentationProvider.java index 25623b87c981..12b81ea4f41d 100644 --- a/python/src/com/jetbrains/python/documentation/PythonDocumentationProvider.java +++ b/python/src/com/jetbrains/python/documentation/PythonDocumentationProvider.java @@ -36,6 +36,7 @@ import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.io.File; import java.io.IOException; import java.util.Collections; import java.util.HashMap; @@ -385,16 +386,22 @@ public class PythonDocumentationProvider extends AbstractDocumentationProvider i } private static boolean pageExists(String url) { + if (new File(url).exists()) { + return true; + } HttpClient client = new HttpClient(); client.setTimeout(5 * 1000); client.setConnectionTimeout(5 * 1000); - HeadMethod method = new HeadMethod(url); try { + HeadMethod method = new HeadMethod(url); int rc = client.executeMethod(method); if (rc == 404) { return false; } } + catch (IllegalArgumentException e) { + return false; + } catch (IOException ignored) { } return true; diff --git a/python/src/com/jetbrains/python/sdk/PySkeletonGenerator.java b/python/src/com/jetbrains/python/sdk/PySkeletonGenerator.java index 64b22c1785ae..ccbf403fdc40 100644 --- a/python/src/com/jetbrains/python/sdk/PySkeletonGenerator.java +++ b/python/src/com/jetbrains/python/sdk/PySkeletonGenerator.java @@ -199,7 +199,7 @@ public class PySkeletonGenerator { return new ListBinariesResult(generatorVersion, binaries); } - public boolean deleteOrLog(File item) { + public boolean deleteOrLog(@NotNull File item) { boolean deleted = item.delete(); if (!deleted) LOG.warn("Failed to delete skeleton file " + item.getAbsolutePath()); return deleted; diff --git a/python/testData/refactoring/rename/renameShadowingVariable.py b/python/testData/refactoring/rename/renameShadowingVariable.py new file mode 100644 index 000000000000..bc7946d9ec3c --- /dev/null +++ b/python/testData/refactoring/rename/renameShadowingVariable.py @@ -0,0 +1,3 @@ +def lab(): pass +lab = 1 +print(lab) diff --git a/python/testData/refactoring/rename/renameShadowingVariable_after.py b/python/testData/refactoring/rename/renameShadowingVariable_after.py new file mode 100644 index 000000000000..1f26476ab745 --- /dev/null +++ b/python/testData/refactoring/rename/renameShadowingVariable_after.py @@ -0,0 +1,3 @@ +def lab(): pass +bar = 1 +print(bar) diff --git a/python/testSrc/com/jetbrains/python/refactoring/PyRenameTest.java b/python/testSrc/com/jetbrains/python/refactoring/PyRenameTest.java index a250d6f73199..ca5e06eb7be7 100644 --- a/python/testSrc/com/jetbrains/python/refactoring/PyRenameTest.java +++ b/python/testSrc/com/jetbrains/python/refactoring/PyRenameTest.java @@ -95,6 +95,10 @@ public class PyRenameTest extends PyTestCase { doTest("bar"); } + public void testRenameShadowingVariable() { // PY-7342 + doTest("bar"); + } + public void testRenameProperty() { // PY-5948 setLanguageLevel(LanguageLevel.PYTHON26); try {