summaryrefslogtreecommitdiffstats
path: root/Lib/importlib/_bootstrap.py
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2012-02-08 23:52:56 (GMT)
committerBrett Cannon <brett@python.org>2012-02-08 23:52:56 (GMT)
commitb4e63b3177f50d519aeac2f59d38af4503b82d62 (patch)
treeb94d1946b9b4889ed9d679667afe0a0bc517e819 /Lib/importlib/_bootstrap.py
parent354c26ecd6d1f6341bb1f65ea099d6952a29abd8 (diff)
downloadcpython-b4e63b3177f50d519aeac2f59d38af4503b82d62.zip
cpython-b4e63b3177f50d519aeac2f59d38af4503b82d62.tar.gz
cpython-b4e63b3177f50d519aeac2f59d38af4503b82d62.tar.bz2
Use the cwd when the empty string is found in sys.path. This leads to
__file__ being an absolute path when the module is found in the current directory.
Diffstat (limited to 'Lib/importlib/_bootstrap.py')
-rw-r--r--Lib/importlib/_bootstrap.py46
1 files changed, 45 insertions, 1 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index e5a9580..2569284 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -718,7 +718,7 @@ class PathFinder:
try:
finder = sys.path_importer_cache[path]
except KeyError:
- finder = cls._path_hooks(path)
+ finder = cls._path_hooks(path if path != '' else _os.getcwd())
sys.path_importer_cache[path] = finder
else:
if finder is None and default:
@@ -1039,3 +1039,47 @@ def _setup(sys_module, imp_module):
setattr(self_module, '_os', os_module)
setattr(self_module, 'path_sep', path_sep)
+
+
+def _setup(sys_module, imp_module):
+ """Setup importlib by importing needed built-in modules and injecting them
+ into the global namespace.
+
+ As sys is needed for sys.modules access and imp is needed to load built-in
+ modules those two modules must be explicitly passed in.
+
+ """
+ global imp, sys
+ imp = imp_module
+ sys = sys_module
+
+ for module in (imp, sys):
+ if not hasattr(module, '__loader__'):
+ module.__loader__ = BuiltinImporter
+
+ self_module = sys.modules[__name__]
+ for builtin_name in ('_io', '_warnings', 'builtins', 'marshal'):
+ if builtin_name not in sys.modules:
+ builtin_module = BuiltinImporter.load_module(builtin_name)
+ else:
+ builtin_module = sys.modules[builtin_name]
+ setattr(self_module, builtin_name, builtin_module)
+
+ for builtin_os, path_sep in [('posix', '/'), ('nt', '\\'), ('os2', '\\')]:
+ if builtin_os in sys.modules:
+ os_module = sys.modules[builtin_os]
+ break
+ else:
+ try:
+ os_module = BuiltinImporter.load_module(builtin_os)
+ # TODO: rip out os2 code after 3.3 is released as per PEP 11
+ if builtin_os == 'os2' and 'EMX GCC' in sys.version:
+ path_sep = '/'
+ break
+ except ImportError:
+ continue
+ else:
+ raise ImportError('importlib requires posix or nt')
+ setattr(self_module, '_os', os_module)
+ setattr(self_module, 'path_sep', path_sep)
+