| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8314690 commit b6e2556
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -794,6 +794,11 @@ find and load modules. | |||
| 794 | 794 | ||
| 795 | 795 | .. versionadded:: 3.4 | |
| 796 | 796 | ||
| 797 | + .. versionchanged:: 3.5 | ||
| 798 | + If the current working directory -- represented by an empty string -- | ||
| 799 | + is no longer valid then ``None`` is returned but no value is cached | ||
| 800 | + in :data:`sys.path_importer_cache`. | ||
| 801 | + | ||
| 797 | 802 | .. classmethod:: find_module(fullname, path=None) | |
| 798 | 803 | ||
| 799 | 804 | A legacy wrapper around :meth:`find_spec`. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -754,6 +754,15 @@ hook` callables on :data:`sys.path_hooks`, then the following protocol is used | |||
| 754 | 754 | to ask the finder for a module spec, which is then used when loading the | |
| 755 | 755 | module. | |
| 756 | 756 | ||
| 757 | + The current working directory -- denoted by an empty string -- is handled | ||
| 758 | + slightly differently from other entries on :data:`sys.path`. First, if the | ||
| 759 | + current working directory is found to not exist, no value is stored in | ||
| 760 | + :data:`sys.path_importer_cache`. Second, the value for the current working | ||
| 761 | + directory is looked up fresh for each module lookup. Third, the path used for | ||
| 762 | + :data:`sys.path_importer_cache` and returned by | ||
| 763 | + :meth:`importlib.machinery.PathFinder.find_spec` will be the actual current | ||
| 764 | + working directory and not the empty string. | ||
| 765 | + | ||
| 757 | 766 | Path entry finder protocol | |
| 758 | 767 | -------------------------- | |
| 759 | 768 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -436,6 +436,12 @@ Changes in the Python API | |||
| 436 | 436 | bytes-like object is required, not 'sometype'". (Contributed by Ezio Melotti | |
| 437 | 437 | in :issue:`16518`.) | |
| 438 | 438 | ||
| 439 | + * If the current directory is set to a directory that no longer exists then | ||
| 440 | + :exc:`FileNotFoundError` will no longer be raised and instead | ||
| 441 | + :meth:`~importlib.machinery.FileFinder.find_spec` will return ``None`` | ||
| 442 | + **without** caching ``None`` in :data:`sys.path_importer_cache` which is | ||
| 443 | + different than the typical case (:issue:`22834`). | ||
| 444 | + | ||
| 439 | 445 | Changes in the C API | |
| 440 | 446 | -------------------- | |
| 441 | 447 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1825,7 +1825,12 @@ def _path_importer_cache(cls, path): | |||
| 1825 | 1825 | ||
| 1826 | 1826 | """ | |
| 1827 | 1827 | if path == '': | |
| 1828 | - path = _os.getcwd() | ||
| 1828 | + try: | ||
| 1829 | + path = _os.getcwd() | ||
| 1830 | + except FileNotFoundError: | ||
| 1831 | + # Don't cache the failure as the cwd can easily change to | ||
| 1832 | + # a valid directory later on. | ||
| 1833 | + return None | ||
| 1829 | 1834 | try: | |
| 1830 | 1835 | finder = sys.path_importer_cache[path] | |
| 1831 | 1836 | except KeyError: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,7 @@ | |||
| 5 | 5 | ||
| 6 | 6 | import os | |
| 7 | 7 | import sys | |
| 8 | + import tempfile | ||
| 8 | 9 | from types import ModuleType | |
| 9 | 10 | import unittest | |
| 10 | 11 | import warnings | |
@@ -158,6 +159,17 @@ def find_spec(self, fullname, target=None): | |||
| 158 | 159 | got = self.machinery.PathFinder.find_spec('whatever', [path]) | |
| 159 | 160 | self.assertEqual(got, success_finder.spec) | |
| 160 | 161 | ||
| 162 | + def test_deleted_cwd(self): | ||
| 163 | + # Issue #22834 | ||
| 164 | + self.addCleanup(os.chdir, os.getcwd()) | ||
| 165 | + with tempfile.TemporaryDirectory() as path: | ||
| 166 | + os.chdir(path) | ||
| 167 | + with util.import_state(path=['']): | ||
| 168 | + # Do not want FileNotFoundError raised. | ||
| 169 | + self.assertIsNone(self.machinery.PathFinder.find_spec('whatever')) | ||
| 170 | + | ||
| 171 | + | ||
| 172 | + | ||
| 161 | 173 | ||
| 162 | 174 | (Frozen_FinderTests, | |
| 163 | 175 | Source_FinderTests | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,6 +10,9 @@ Release date: TBA | |||
| 10 | 10 | Core and Builtins | |
| 11 | 11 | ----------------- | |
| 12 | 12 | ||
| 13 | + - Issue #22834: If the current working directory ends up being set to a | ||
| 14 | + non-existent directory then import will no longer raise FileNotFoundError. | ||
| 15 | + | ||
| 13 | 16 | - Issue #22869: Move the interpreter startup & shutdown code to a new | |
| 14 | 17 | dedicated pylifecycle.c module | |
| 15 | 18 | ||
@@ -235,7 +238,7 @@ Library | |||
| 235 | 238 | ||
| 236 | 239 | - Issue #22776: Brought excluded code into the scope of a try block in | |
| 237 | 240 | SysLogHandler.emit(). | |
| 238 | - | ||
| 241 | + | ||
| 239 | 242 | - Issue #22665: Add missing get_terminal_size and SameFileError to | |
| 240 | 243 | shutil.__all__. | |
| 241 | 244 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments