1616# anything specified at the class level.
1717
1818
19+ # Bootstrap-related code ######################################################
20+
1921# XXX Could also expose Modules/getpath.c:joinpath()
2022def _path_join (* args ):
2123 """Replacement for os.path.join."""
@@ -97,6 +99,8 @@ def _wrap(new, old):
9799 new .__dict__ .update (old .__dict__ )
98100
99101
102+ # Finder/loader utility code ##################################################
103+
100104def set_package (fxn ):
101105 """Set __package__ on the returned module."""
102106 def wrapper (* args , ** kwargs ):
@@ -121,6 +125,56 @@ def wrapper(self, *args, **kwargs):
121125 return wrapper
122126
123127
128+ def module_for_loader (fxn ):
129+ """Decorator to handle selecting the proper module for loaders.
130+
131+ Decorated modules are passed the module to use instead of the module name.
132+ The module is either from sys.modules if it already exists (for reloading)
133+ or is a new module which has __name__ set. If any exception is raised by
134+ the decorated method and the decorator added a module to sys.modules, then
135+ the module is deleted from sys.modules.
136+
137+ The decorator assumes that the decorated method takes self/cls as a first
138+ argument and the module as the second argument.
139+
140+ """
141+ def decorated (self , fullname ):
142+ module = sys .modules .get (fullname )
143+ is_reload = bool (module )
144+ if not is_reload :
145+ # This must be done before open() is called as the 'io' module
146+ # implicitly imports 'locale' and would otherwise trigger an
147+ # infinite loop.
148+ module = imp .new_module (fullname )
149+ sys .modules [fullname ] = module
150+ try :
151+ return fxn (self , module )
152+ except :
153+ if not is_reload :
154+ del sys .modules [fullname ]
155+ raise
156+ _wrap (decorated , fxn )
157+ return decorated
158+
159+
160+ def _check_name (method ):
161+ """Decorator to verify that the module being requested matches the one the
162+ loader can handle.
163+
164+ The first argument (self) must define _name which the second argument is
165+ comapred against. If the comparison fails then ImportError is raised.
166+
167+ """
168+ def inner (self , name , * args , ** kwargs ):
169+ if self ._name != name :
170+ raise ImportError ("loader cannot handle %s" % name )
171+ return method (self , name , * args , ** kwargs )
172+ _wrap (inner , method )
173+ return inner
174+
175+
176+ # Finders/loaders #############################################################
177+
124178class BuiltinImporter :
125179
126180 """Meta path loader for built-in modules.
@@ -224,21 +278,6 @@ def find_module(self, fullname, path=None):
224278 return None
225279
226280
227- def _check_name (method ):
228- """Decorator to verify that the module being requested matches the one the
229- loader can handle.
230-
231- The first argument (self) must define _name which the second argument is
232- comapred against. If the comparison fails then ImportError is raised.
233-
234- """
235- def inner (self , name , * args , ** kwargs ):
236- if self ._name != name :
237- raise ImportError ("loader cannot handle %s" % name )
238- return method (self , name , * args , ** kwargs )
239- _wrap (inner , method )
240- return inner
241-
242281
243282class _ExtensionFileLoader :
244283
@@ -295,37 +334,6 @@ def _suffix_list(suffix_type):
295334 if suffix [2 ] == suffix_type ]
296335
297336
298- def module_for_loader (fxn ):
299- """Decorator to handle selecting the proper module for loaders.
300-
301- Decorated modules are passed the module to use instead of the module name.
302- The module is either from sys.modules if it already exists (for reloading)
303- or is a new module which has __name__ set. If any exception is raised by
304- the decorated method and the decorator added a module to sys.modules, then
305- the module is deleted from sys.modules.
306-
307- The decorator assumes that the decorated method takes self/cls as a first
308- argument and the module as the second argument.
309-
310- """
311- def decorated (self , fullname ):
312- module = sys .modules .get (fullname )
313- is_reload = bool (module )
314- if not is_reload :
315- # This must be done before open() is called as the 'io' module
316- # implicitly imports 'locale' and would otherwise trigger an
317- # infinite loop.
318- module = imp .new_module (fullname )
319- sys .modules [fullname ] = module
320- try :
321- return fxn (self , module )
322- except :
323- if not is_reload :
324- del sys .modules [fullname ]
325- raise
326- _wrap (decorated , fxn )
327- return decorated
328-
329337
330338class PyLoader :
331339
@@ -738,6 +746,21 @@ def find_module(cls, fullname, path=None):
738746 return None
739747
740748
749+ # Import itself ###############################################################
750+
751+ class _ImportLockContext :
752+
753+ """Context manager for the import lock."""
754+
755+ def __enter__ (self ):
756+ """Acquire the import lock."""
757+ imp .acquire_lock ()
758+
759+ def __exit__ (self , exc_type , exc_value , exc_traceback ):
760+ """Release the import lock regardless of any raised exceptions."""
761+ imp .release_lock ()
762+
763+
741764_DEFAULT_PATH_HOOK = _chained_path_hook (ExtensionFileFinder , PyPycFileFinder )
742765
743766class _DefaultPathFinder (PathFinder ):
@@ -761,18 +784,6 @@ def _path_importer_cache(cls, path):
761784 return super ()._path_importer_cache (path , _DEFAULT_PATH_HOOK )
762785
763786
764- class _ImportLockContext :
765-
766- """Context manager for the import lock."""
767-
768- def __enter__ (self ):
769- """Acquire the import lock."""
770- imp .acquire_lock ()
771-
772- def __exit__ (self , exc_type , exc_value , exc_traceback ):
773- """Release the import lock regardless of any raised exceptions."""
774- imp .release_lock ()
775-
776787
777788_IMPLICIT_META_PATH = [BuiltinImporter , FrozenImporter , _DefaultPathFinder ]
778789
0 commit comments