[Python-checkins] r57207 - sandbox/trunk/import_in_py/_importlib.py
brett.cannon
python-checkins at python.org
Mon Aug 20 03:31:46 CEST 2007
Author: brett.cannon
Date: Mon Aug 20 03:31:45 2007
New Revision: 57207
Modified:
sandbox/trunk/import_in_py/_importlib.py
Log:
Pass in the paths to the source and bytecode files for handle_py. Makes it
easier to set __file__.
Modified: sandbox/trunk/import_in_py/_importlib.py
==============================================================================
--- sandbox/trunk/import_in_py/_importlib.py (original)
+++ sandbox/trunk/import_in_py/_importlib.py Mon Aug 20 03:31:45 2007
@@ -303,9 +303,9 @@
if suffix[2] == suffix_type]
-def handle_py(loader, name, path, source_exists, bytecode_exists, is_pkg):
+def handle_py(loader, name, path, source_path, bytecode_path, is_pkg):
"""Handle the initialization of a module by Python source or bytecode."""
- assert source_exists or bytecode_exists
+ assert source_path or bytecode_path
source_timestamp = None
module = sys.modules.get(name)
if module is None:
@@ -314,14 +314,12 @@
# __file__, __path__, and __loader__ *must* be set on the module before
# any code is executed by the import. __name__ is set by new_module.
module.__loader__ = loader
- # XXX Will always be to the source if it exists, even if the module is
- # initialized from source thanks to the importer always searching for
- # source first.
- module.__file__ = path
+ # Just so that the attribute has some value to start with.
+ module.__file__ = source_path or bytecode_path
if is_pkg:
module.__path__ = [path.rsplit(path_sep, 1)[0]]
# Try to use bytecode if it is available.
- if bytecode_exists:
+ if bytecode_path:
data = loader.get_bytecode(name)
magic, pyc_timestamp, bytecode = data[:4], _r_long(data[4:8]), data[8:]
try:
@@ -329,7 +327,7 @@
if imp.get_magic() != magic:
raise ImportError("bad magic number")
# Verify that the bytecode is not stale.
- if source_exists:
+ if source_path:
# Keep in a variable so that if bytecode turns out to be bad
# then the value can be reused when re-generating bytecode.
source_timestamp = loader.mod_time(name)
@@ -338,23 +336,25 @@
# Bytecode seems fine, so try to use it.
try:
exec marshal.loads(bytecode) in module.__dict__
+ module.__file__ = bytecode_path
return module
except ValueError:
# XXX Does this have to be true?
# Since bad bytecode halts the import entirely, having the
# source code is useless. Signal this fact by setting
- # bytecode_exists to None.
- bytecode_exists = None
+ # bytecode_path to None.
+ bytecode_path = None
raise ImportError('Non-code object found')
except ImportError:
# If the exception is from the bytecode being corrupt, let it
# propagate.
- if not source_exists or bytecode_exists is None:
+ if not source_path or bytecode_path is None:
raise
# Use the source.
source = loader.get_source(name)
code_object = compile(source, path, 'exec')
exec code_object in module.__dict__
+ module.__file__ = source_path
# Generate bytecode and write it out.
if source_timestamp is None: # May have from bytecode verification.
source_timestamp = loader.mod_time(name)
@@ -400,10 +400,17 @@
@check_name
def load_module(self, fullname):
"""Load a Python source or bytecode file."""
+ if hasattr(self, '_source_path'):
+ source_path = self._source_path
+ else:
+ source_path = None
+ if hasattr(self, '_bytecode_path'):
+ bytecode_path = self._bytecode_path
+ else:
+ bytecode_path = None
try:
return self._handler(fullname, self._path,
- hasattr(self, '_source_path'),
- hasattr(self, '_bytecode_path'),
+ source_path, bytecode_path,
self._is_pkg)
except:
# Don't leave a partially initialized module in sys.modules.
More information about the Python-checkins
mailing list