diff options
author | Karthik Nadig <kanadig@microsoft.com> | 2022-08-09 05:25:47 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-09 08:25:47 -0400 |
commit | 6170086801ce3054e0d020028a5c539de626b757 (patch) | |
tree | e73311f0a91b8d5ac6dbd1b295d56a9e97b67b16 | |
parent | 684a1d6aa0a6791e20078bc524f97c8906332390 (diff) | |
download | pylint-git-6170086801ce3054e0d020028a5c539de626b757.tar.gz |
Check for `<cwd>` before removing first item from `sys.path` in `modify_sys_path`. (#7277)
-rw-r--r-- | doc/whatsnew/fragments/7231.bugfix | 3 | ||||
-rw-r--r-- | pylint/__init__.py | 5 | ||||
-rw-r--r-- | tests/test_self.py | 18 |
3 files changed, 24 insertions, 2 deletions
diff --git a/doc/whatsnew/fragments/7231.bugfix b/doc/whatsnew/fragments/7231.bugfix new file mode 100644 index 000000000..5c8837e42 --- /dev/null +++ b/doc/whatsnew/fragments/7231.bugfix @@ -0,0 +1,3 @@ +Check for `<cwd>` before removing first item from `sys.path` in `modify_sys_path`. + +Closes #7231 diff --git a/pylint/__init__.py b/pylint/__init__.py index 9f3dd74e6..8f1eaebe0 100644 --- a/pylint/__init__.py +++ b/pylint/__init__.py @@ -96,9 +96,10 @@ def modify_sys_path() -> None: if pylint is installed in an editable configuration (as the last item). https://github.com/PyCQA/pylint/issues/4161 """ - sys.path.pop(0) - env_pythonpath = os.environ.get("PYTHONPATH", "") cwd = os.getcwd() + if sys.path[0] in ("", ".", cwd): + sys.path.pop(0) + env_pythonpath = os.environ.get("PYTHONPATH", "") if env_pythonpath.startswith(":") and env_pythonpath not in (f":{cwd}", ":."): sys.path.pop(0) elif env_pythonpath.endswith(":") and env_pythonpath not in (f"{cwd}:", ".:"): diff --git a/tests/test_self.py b/tests/test_self.py index 7c11601a9..53c9fb11b 100644 --- a/tests/test_self.py +++ b/tests/test_self.py @@ -759,6 +759,24 @@ a.py:1:4: E0001: Parsing failed: 'invalid syntax (<unknown>, line 1)' (syntax-er modify_sys_path() assert sys.path == paths[1:] + paths = ["", *default_paths] + sys.path = copy(paths) + with _test_environ_pythonpath(): + modify_sys_path() + assert sys.path == paths[1:] + + paths = [".", *default_paths] + sys.path = copy(paths) + with _test_environ_pythonpath(): + modify_sys_path() + assert sys.path == paths[1:] + + paths = ["/do_not_remove", *default_paths] + sys.path = copy(paths) + with _test_environ_pythonpath(): + modify_sys_path() + assert sys.path == paths + paths = [cwd, cwd, *default_paths] sys.path = copy(paths) with _test_environ_pythonpath("."): |