diff options
author | Brett Cannon <brett@python.org> | 2012-04-21 01:44:46 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2012-04-21 01:44:46 (GMT) |
commit | ea59dbff16e305aa07ef5896cc59fb36dc5edac3 (patch) | |
tree | e74f3a49c0328aca256bba40144acaac5cfc38c0 /Lib/importlib | |
parent | ed672d6872ac2b1d06a7723c9a544ee4d153e6ce (diff) | |
download | cpython-ea59dbff16e305aa07ef5896cc59fb36dc5edac3.zip cpython-ea59dbff16e305aa07ef5896cc59fb36dc5edac3.tar.gz cpython-ea59dbff16e305aa07ef5896cc59fb36dc5edac3.tar.bz2 |
Issue #13959: Re-implement imp.cache_from_source() in Lib/imp.py.
Diffstat (limited to 'Lib/importlib')
-rw-r--r-- | Lib/importlib/_bootstrap.py | 32 |
1 files changed, 27 insertions, 5 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index 28db02a..44bac39 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -178,6 +178,31 @@ def _new_module(name): # Finder/loader utility code ################################################## +PYCACHE = '__pycache__' + +DEBUG_BYTECODE_SUFFIX = '.pyc' +OPT_BYTECODE_SUFFIX = '.pyo' +BYTECODE_SUFFIX = DEBUG_BYTECODE_SUFFIX if __debug__ else OPT_BYTECODE_SUFFIX + +def _cache_from_source(path, debug_override=None): + """Given the path to a .py file, return the path to its .pyc/.pyo file. + + The .py file does not need to exist; this simply returns the path to the + .pyc/.pyo file calculated as if the .py file were imported. The extension + will be .pyc unless __debug__ is not defined, then it will be .pyo. + + If debug_override is not None, then it must be a boolean and is taken as + the value of __debug__ instead. + + """ + debug = __debug__ if debug_override is None else debug_override + suffix = DEBUG_BYTECODE_SUFFIX if debug else OPT_BYTECODE_SUFFIX + head, tail = _path_split(path) + base_filename, sep, _ = tail.partition('.') + filename = '{}{}{}{}'.format(base_filename, sep, _imp.get_tag(), suffix) + return _path_join(head, PYCACHE, filename) + + def verbose_message(message, *args): """Print the message to stderr if -v/PYTHONVERBOSE is turned on.""" if sys.flags.verbose: @@ -452,7 +477,7 @@ class _LoaderBasics: code_object = self.get_code(name) module.__file__ = self.get_filename(name) if not sourceless: - module.__cached__ = _imp.cache_from_source(module.__file__) + module.__cached__ = _cache_from_source(module.__file__) else: module.__cached__ = module.__file__ module.__package__ = name @@ -515,7 +540,7 @@ class SourceLoader(_LoaderBasics): """ source_path = self.get_filename(fullname) - bytecode_path = _imp.cache_from_source(source_path) + bytecode_path = _cache_from_source(source_path) source_mtime = None if bytecode_path is not None: try: @@ -554,9 +579,6 @@ class SourceLoader(_LoaderBasics): verbose_message('code object from {}', source_path) if (not sys.dont_write_bytecode and bytecode_path is not None and source_mtime is not None): - # If e.g. Jython ever implements imp.cache_from_source to have - # their own cached file format, this block of code will most likely - # throw an exception. data = bytearray(_MAGIC_NUMBER) data.extend(_w_long(source_mtime)) data.extend(_w_long(len(source_bytes))) |