Check the magic number of bytecode files
This commit is contained in:
parent
36324e9499
commit
a87b23b4e8
@ -86,14 +86,16 @@ def import_file_to_module(module_name, fpath, loader=None):
|
|||||||
try:
|
try:
|
||||||
source_mtime = int(os.stat(fpath).st_mtime)
|
source_mtime = int(os.stat(fpath).st_mtime)
|
||||||
with open(bytecode_path, 'rb') as bc_f:
|
with open(bytecode_path, 'rb') as bc_f:
|
||||||
# To get the bytecode file's internal timestamp, take the 4 bytes
|
# The first 4 bytes are the magic number for the version of Python
|
||||||
# after the first 4 bytes and interpret them as a little-endian
|
# that compiled this bytecode.
|
||||||
# 32-bit integer.
|
bytecode_magic = bc_f.read(4)
|
||||||
bytecode_mtime = struct.unpack('<i', bc_f.read(8)[4:])[0]
|
# The next 4 bytes, interpreted as a little-endian 32-bit integer,
|
||||||
|
# are the mtime of the corresponding source file.
|
||||||
|
bytecode_mtime, = struct.unpack('<i', bc_f.read(4))
|
||||||
except (IOError, OSError):
|
except (IOError, OSError):
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
if bytecode_mtime >= source_mtime:
|
if bytecode_magic == MAGIC and bytecode_mtime >= source_mtime:
|
||||||
# It's a cache hit. Load the byte-compiled version.
|
# It's a cache hit. Load the byte-compiled version.
|
||||||
if PY3:
|
if PY3:
|
||||||
# As of Python 3.6, imp.load_compiled still exists, but it's
|
# As of Python 3.6, imp.load_compiled still exists, but it's
|
||||||
|
Loading…
x
Reference in New Issue
Block a user