File tree Expand file tree Collapse file tree 7 files changed +654
-616
lines changed Expand file tree Collapse file tree 7 files changed +654
-616
lines changed Original file line number Diff line number Diff line change @@ -1081,7 +1081,12 @@ find and load modules.
1081
1081
.. classmethod :: invalidate_caches()
1082
1082
1083
1083
Calls :meth: `importlib.abc.PathEntryFinder.invalidate_caches ` on all
1084
- finders stored in :attr: `sys.path_importer_cache `.
1084
+ finders stored in :data: `sys.path_importer_cache ` that define the method.
1085
+ Otherwise entries in :data: `sys.path_importer_cache ` set to ``None `` are
1086
+ deleted.
1087
+
1088
+ .. versionchanged :: 3.7
1089
+ Entries of ``None `` in :data: `sys.path_importer_cache ` are deleted.
1085
1090
1086
1091
.. versionchanged :: 3.4
1087
1092
Calls objects in :data: `sys.path_hooks ` with the current working
Original file line number Diff line number Diff line change @@ -561,7 +561,7 @@ importlib
561
561
---------
562
562
563
563
The :class: `importlib.abc.ResourceReader ` ABC was introduced to
564
- support the loading of resource from packages.
564
+ support the loading of resources from packages.
565
565
566
566
locale
567
567
------
@@ -1274,6 +1274,11 @@ Changes in the Python API
1274
1274
previous behaviour, or use
1275
1275
:attr: `STARTUPINFO.lpAttributeList <subprocess.STARTUPINFO.lpAttributeList> `.
1276
1276
1277
+ * :meth: `importlib.machinery.PathFinder.invalidate_caches ` -- which implicitly
1278
+ affects :func: `importlib.invalidate_caches ` -- now deletes entries
1279
+ in :data: `sys.path_importer_cache ` which are set to ``None ``.
1280
+ (Contributed by Brett Cannon in :issue: `33169 `.)
1281
+
1277
1282
1278
1283
Changes in the C API
1279
1284
--------------------
Original file line number Diff line number Diff line change @@ -1179,8 +1179,10 @@ class PathFinder:
1179
1179
def invalidate_caches (cls ):
1180
1180
"""Call the invalidate_caches() method on all path entry finders
1181
1181
stored in sys.path_importer_caches (where implemented)."""
1182
- for finder in sys .path_importer_cache .values ():
1183
- if hasattr (finder , 'invalidate_caches' ):
1182
+ for name , finder in list (sys .path_importer_cache .items ()):
1183
+ if finder is None :
1184
+ del sys .path_importer_cache [name ]
1185
+ elif hasattr (finder , 'invalidate_caches' ):
1184
1186
finder .invalidate_caches ()
1185
1187
1186
1188
@classmethod
Original file line number Diff line number Diff line change @@ -184,6 +184,27 @@ def test_deleted_cwd(self):
184
184
# Do not want FileNotFoundError raised.
185
185
self .assertIsNone (self .machinery .PathFinder .find_spec ('whatever' ))
186
186
187
+ def test_invalidate_caches_finders (self ):
188
+ # Finders with an invalidate_caches() method have it called.
189
+ class FakeFinder :
190
+ def __init__ (self ):
191
+ self .called = False
192
+
193
+ def invalidate_caches (self ):
194
+ self .called = True
195
+
196
+ cache = {'leave_alone' : object (), 'finder_to_invalidate' : FakeFinder ()}
197
+ with util .import_state (path_importer_cache = cache ):
198
+ self .machinery .PathFinder .invalidate_caches ()
199
+ self .assertTrue (cache ['finder_to_invalidate' ].called )
200
+
201
+ def test_invalidate_caches_clear_out_None (self ):
202
+ # Clear out None in sys.path_importer_cache() when invalidating caches.
203
+ cache = {'clear_out' : None }
204
+ with util .import_state (path_importer_cache = cache ):
205
+ self .machinery .PathFinder .invalidate_caches ()
206
+ self .assertEqual (len (cache ), 0 )
207
+
187
208
188
209
class FindModuleTests (FinderTests ):
189
210
def find (self , * args , ** kwargs ):
Original file line number Diff line number Diff line change @@ -406,7 +406,7 @@ def test_method_lacking(self):
406
406
# There should be no issues if the method is not defined.
407
407
key = 'gobbledeegook'
408
408
sys .path_importer_cache [key ] = None
409
- self .addCleanup (lambda : sys .path_importer_cache .__delitem__ (key ))
409
+ self .addCleanup (lambda : sys .path_importer_cache .pop (key , None ))
410
410
self .init .invalidate_caches () # Shouldn't trigger an exception.
411
411
412
412
Original file line number Diff line number Diff line change
1
+ Delete entries of ``None `` in :data: `sys.path_importer_cache ` when
2
+ :meth: `importlib.machinery.invalidate_caches ` is called.
You can’t perform that action at this time.
0 commit comments