[Python-checkins] r52924 - sandbox/trunk/import_in_py/importer.py
brett.cannon
python-checkins at python.org
Tue Dec 5 23:36:13 CET 2006
Author: brett.cannon
Date: Tue Dec 5 23:36:13 2006
New Revision: 52924
Modified:
sandbox/trunk/import_in_py/importer.py
Log:
Add in usage of the import lock.
Modified: sandbox/trunk/import_in_py/importer.py
==============================================================================
--- sandbox/trunk/import_in_py/importer.py (original)
+++ sandbox/trunk/import_in_py/importer.py Tue Dec 5 23:36:13 2006
@@ -744,64 +744,68 @@
"""
is_pkg = True if '__path__' in globals else False
caller_name = globals.get('__name__')
- if level and caller_name:
- # Handle the classic style of import: relative first, then
- # absolute.
- if level == -1:
- relative_name = self.classic_resolve_name(name, caller_name,
- is_pkg)
- imported_name = relative_name
- try:
- # Check that a redirection entry does not already exist for
- # the relative import name.
- if (relative_name in sys.modules and
- sys.modules[relative_name] is None):
- raise ImportError("import redirection")
- # Try a relative import.
+ imp.acquire_lock()
+ try:
+ if level and caller_name:
+ # Handle the classic style of import: relative first, then
+ # absolute.
+ if level == -1:
+ relative_name = self.classic_resolve_name(name, caller_name,
+ is_pkg)
+ imported_name = relative_name
+ try:
+ # Check that a redirection entry does not already exist for
+ # the relative import name.
+ if (relative_name in sys.modules and
+ sys.modules[relative_name] is None):
+ raise ImportError("import redirection")
+ # Try a relative import.
+ self.import_full_module(imported_name)
+ except ImportError:
+ # If the relative import fails (or is redirected), try an
+ # absolute import.
+ imported_name = name
+ self.import_full_module(name)
+ # Redirection entry for resolved relative name to instead
+ # redirect to the absolute import.
+ sys.modules[relative_name] = None
+ # If using absolute imports with a relative path, only attempt with
+ # the fully-resolved module name.
+ else:
+ imported_name = self.resolve_name(name, caller_name, is_pkg,
+ level)
+ # This call will also handle setting the attribute on the
+ # package.
self.import_full_module(imported_name)
- except ImportError:
- # If the relative import fails (or is redirected), try an
- # absolute import.
- imported_name = name
- self.import_full_module(name)
- # Redirection entry for resolved relative name to instead
- # redirect to the absolute import.
- sys.modules[relative_name] = None
- # If using absolute imports with a relative path, only attempt with
- # the fully-resolved module name.
else:
- imported_name = self.resolve_name(name, caller_name, is_pkg,
- level)
- # This call will also handle setting the attribute on the
- # package.
- self.import_full_module(imported_name)
- else:
- imported_name = name
- self.import_full_module(name)
- # When fromlist is not specified, return the root module (i.e., module
- # up to first dot).
- if not fromlist:
- return sys.modules[imported_name.split('.', 1)[0]]
- # When fromlist is not empty, return the actual module specified in
- # the import.
- else:
- module = sys.modules[imported_name]
- if hasattr(module, '__path__') and hasattr(module, '__name__'):
- # When fromlist has a value and the imported module is a
- # package, then if a name in fromlist is not found as an
- # attribute on module, try a relative import to find it.
- # Failure is fine and is the exception is suppressed.
- check_for = fromlist[:]
- if '*' in check_for and hasattr(module, '__all__'):
- check_for.extend(module.__all__)
- for item in check_for:
- if item == '*':
- continue
- if not hasattr(module, item):
- resolved_name = self.resolve_name(item, module.__name__,
- True, 1)
- try:
- self.import_full_module(resolved_name)
- except ImportError:
- pass
- return module
+ imported_name = name
+ self.import_full_module(name)
+ # When fromlist is not specified, return the root module (i.e., module
+ # up to first dot).
+ if not fromlist:
+ return sys.modules[imported_name.split('.', 1)[0]]
+ # When fromlist is not empty, return the actual module specified in
+ # the import.
+ else:
+ module = sys.modules[imported_name]
+ if hasattr(module, '__path__') and hasattr(module, '__name__'):
+ # When fromlist has a value and the imported module is a
+ # package, then if a name in fromlist is not found as an
+ # attribute on module, try a relative import to find it.
+ # Failure is fine and is the exception is suppressed.
+ check_for = fromlist[:]
+ if '*' in check_for and hasattr(module, '__all__'):
+ check_for.extend(module.__all__)
+ for item in check_for:
+ if item == '*':
+ continue
+ if not hasattr(module, item):
+ resolved_name = self.resolve_name(item, module.__name__,
+ True, 1)
+ try:
+ self.import_full_module(resolved_name)
+ except ImportError:
+ pass
+ return module
+ finally:
+ imp.release_lock()
More information about the Python-checkins
mailing list