]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #18192: Introduce importlib.util.MAGIC_NUMBER and document the
authorBrett Cannon <brett@python.org>
Fri, 14 Jun 2013 23:02:34 +0000 (19:02 -0400)
committerBrett Cannon <brett@python.org>
Fri, 14 Jun 2013 23:02:34 +0000 (19:02 -0400)
deprecation of imp.get_magic().

Doc/library/imp.rst
Doc/library/importlib.rst
Lib/imp.py
Lib/importlib/_bootstrap.py
Lib/importlib/util.py
Lib/test/test_importlib/test_util.py
Misc/NEWS
Python/importlib.h

index 8a75d4c2ccbcc6986ee8c6796c1fa2d0cdc4a00d..890c171a15dfc9acebfd7309900696bb65c72562 100644 (file)
@@ -21,6 +21,9 @@ This module provides an interface to the mechanisms used to implement the
    Return the magic string value used to recognize byte-compiled code files
    (:file:`.pyc` files).  (This value may be different for each Python version.)
 
+   .. deprecated:: 3.4
+       Use :attr:`importlib.util.MAGIC_NUMBER` instead.
+
 
 .. function:: get_suffixes()
 
index 0caabaa188de835d5159682bd812371b9bc6ba21..e9b771758b4ebf972000c81640561d5dbf63b002 100644 (file)
@@ -879,6 +879,13 @@ find and load modules.
 This module contains the various objects that help in the construction of
 an :term:`importer`.
 
+.. attribute:: MAGIC_NUMBER
+
+   The bytes which represent the bytecode version number. If you need help with
+   loading/writing bytecode then consider :class:`importlib.abc.SourceLoader`.
+
+   .. versionadded:: 3.4
+
 .. function:: resolve_name(name, package)
 
    Resolve a relative module name to an absolute one.
index ad0fd6a4781cc13e168c091413476811b5a93443..e06fbe64c28ccab600ec955748cd4c81edf2dbba 100644 (file)
@@ -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():
index 9a82bd1ee5f0d7f1697a688f8c053bd735204d36..455805416f7aef5f5046402cb6b2c9201cffa57c 100644 (file)
@@ -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))
index 9cf0eb7e280afda135fcc5e5b8b64a275e15dcb9..09ec03c142498cfa7b00baf2205101024e66549c 100644 (file)
@@ -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
index e6b0084fa7fd3fa19d7f0015d88f4603865dcd70..78f5a3e99ac69025b0551a960fa838a58abeaa53 100644 (file)
@@ -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()
index a1b63b0c9068b0b55657d5ed6f6fc054c1b0ed13..f676dba49f157cf82fedb3e69df4cdf8aa5d2e3b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -123,6 +123,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #18192: Introduce importlib.util.MAGIC_NUMBER and document as deprecated
+  imp.get_magic().
+
 - Issue #18149: Add filecmp.clear_cache() to manually clear the filecmp cache.
   Patch by Mark Levitt
 
index e2f60c905ddc494df4bdf72ecbd89795cdcf407a..e1ec0e6962aebdb9a50dff145ca25380dd0c7ad2 100644 (file)
@@ -1273,8 +1273,8 @@ const unsigned char _Py_M__importlib[] = {
     0,98,121,116,101,99,111,100,101,32,105,115,32,115,116,97,
     108,101,32,102,111,114,32,123,33,114,125,244,4,0,0,0,
     115,105,122,101,108,3,0,0,0,255,127,255,127,3,0,40,
-    9,0,0,0,244,12,0,0,0,95,77,65,71,73,67,95,
-    66,89,84,69,83,114,46,0,0,0,114,140,0,0,0,114,
+    9,0,0,0,244,12,0,0,0,77,65,71,73,67,95,78,
+    85,77,66,69,82,114,46,0,0,0,114,140,0,0,0,114,
     156,0,0,0,114,31,0,0,0,244,8,0,0,0,69,79,
     70,69,114,114,111,114,114,14,0,0,0,114,93,0,0,0,
     114,19,0,0,0,40,11,0,0,0,114,52,0,0,0,114,