Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
575ad812
Commit
575ad812
authored
Jun 14, 2013
by
Brett Cannon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #18192: Introduce importlib.util.MAGIC_NUMBER and document the
deprecation of imp.get_magic().
parent
1aa049d2
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
37 additions
and
8 deletions
+37
-8
Doc/library/imp.rst
Doc/library/imp.rst
+3
-0
Doc/library/importlib.rst
Doc/library/importlib.rst
+7
-0
Lib/imp.py
Lib/imp.py
+6
-2
Lib/importlib/_bootstrap.py
Lib/importlib/_bootstrap.py
+4
-4
Lib/importlib/util.py
Lib/importlib/util.py
+1
-0
Lib/test/test_importlib/test_util.py
Lib/test/test_importlib/test_util.py
+11
-0
Misc/NEWS
Misc/NEWS
+3
-0
Python/importlib.h
Python/importlib.h
+2
-2
No files found.
Doc/library/imp.rst
View file @
575ad812
...
...
@@ -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()
...
...
Doc/library/importlib.rst
View file @
575ad812
...
...
@@ -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
.
...
...
Lib/imp.py
View file @
575ad812
...
...
@@ -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
():
...
...
Lib/importlib/_bootstrap.py
View file @
575ad812
...
...
@@ -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
))
...
...
Lib/importlib/util.py
View file @
575ad812
"""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
...
...
Lib/test/test_importlib/test_util.py
View file @
575ad812
...
...
@@ -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
()
Misc/NEWS
View file @
575ad812
...
...
@@ -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
...
...
Python/importlib.h
View file @
575ad812
...
...
@@ -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
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment