diff options
author | Brett Cannon <brett@python.org> | 2013-06-14 23:02:34 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2013-06-14 23:02:34 (GMT) |
commit | 05a647deedd11c227619f9463920526471db54f1 (patch) | |
tree | 0b7ac3a4d54cbd4fc5065484104385c61ef56fd9 /Lib | |
parent | 4d7056258b07df7cbb1b9b44e7a1a9bad04f7454 (diff) | |
download | cpython-05a647deedd11c227619f9463920526471db54f1.zip cpython-05a647deedd11c227619f9463920526471db54f1.tar.gz cpython-05a647deedd11c227619f9463920526471db54f1.tar.bz2 |
Issue #18192: Introduce importlib.util.MAGIC_NUMBER and document the
deprecation of imp.get_magic().
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/imp.py | 8 | ||||
-rw-r--r-- | Lib/importlib/_bootstrap.py | 8 | ||||
-rw-r--r-- | Lib/importlib/util.py | 1 | ||||
-rw-r--r-- | Lib/test/test_importlib/test_util.py | 11 |
4 files changed, 22 insertions, 6 deletions
@@ -23,6 +23,7 @@ from importlib._bootstrap import cache_from_source, source_from_cache from importlib import _bootstrap from importlib import machinery +from importlib import util import importlib import os import sys @@ -44,8 +45,11 @@ IMP_HOOK = 9 def get_magic(): - """Return the magic number for .pyc or .pyo files.""" - return _bootstrap._MAGIC_BYTES + """**DEPRECATED** + + Return the magic number for .pyc or .pyo files. + """ + return util.MAGIC_NUMBER def get_tag(): diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index 9a82bd1..4558054 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -383,8 +383,8 @@ def _call_with_frames_removed(f, *args, **kwds): # longer be understood by older implementations of the eval loop (usually # due to the addition of new opcodes). -_MAGIC_BYTES = (3280).to_bytes(2, 'little') + b'\r\n' -_RAW_MAGIC_NUMBER = int.from_bytes(_MAGIC_BYTES, 'little') # For import.c +MAGIC_NUMBER = (3280).to_bytes(2, 'little') + b'\r\n' +_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c _PYCACHE = '__pycache__' @@ -663,7 +663,7 @@ def _validate_bytecode_header(data, source_stats=None, name=None, path=None): magic = data[:4] raw_timestamp = data[4:8] raw_size = data[8:12] - if magic != _MAGIC_BYTES: + if magic != MAGIC_NUMBER: message = 'bad magic number in {!r}: {!r}'.format(name, magic) _verbose_message(message) raise ImportError(message, **exc_details) @@ -711,7 +711,7 @@ def _compile_bytecode(data, name=None, bytecode_path=None, source_path=None): def _code_to_bytecode(code, mtime=0, source_size=0): """Compile a code object into bytecode for writing out to a byte-compiled file.""" - data = bytearray(_MAGIC_BYTES) + data = bytearray(MAGIC_NUMBER) data.extend(_w_long(mtime)) data.extend(_w_long(source_size)) data.extend(marshal.dumps(code)) diff --git a/Lib/importlib/util.py b/Lib/importlib/util.py index 9cf0eb7..09ec03c 100644 --- a/Lib/importlib/util.py +++ b/Lib/importlib/util.py @@ -1,5 +1,6 @@ """Utility code for constructing importers, etc.""" +from ._bootstrap import MAGIC_NUMBER from ._bootstrap import module_to_load from ._bootstrap import set_loader from ._bootstrap import set_package diff --git a/Lib/test/test_importlib/test_util.py b/Lib/test/test_importlib/test_util.py index e6b0084..78f5a3e 100644 --- a/Lib/test/test_importlib/test_util.py +++ b/Lib/test/test_importlib/test_util.py @@ -313,5 +313,16 @@ class ResolveNameTests(unittest.TestCase): util.resolve_name('..bacon', 'spam') +class MagicNumberTests(unittest.TestCase): + + def test_length(self): + # Should be 4 bytes. + self.assertEqual(len(util.MAGIC_NUMBER), 4) + + def test_incorporates_rn(self): + # The magic number uses \r\n to come out wrong when splitting on lines. + self.assertTrue(util.MAGIC_NUMBER.endswith(b'\r\n')) + + if __name__ == '__main__': unittest.main() |