Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0af681b

Browse files
miss-islingtonammaraskarmatrixise
authored
[3.10] bpo-34990: Treat the pyc header's mtime in compileall as an unsigned int (GH-19708)
(cherry picked from commit bb21e28) Co-authored-by: Ammar Askar <[email protected]> Co-authored-by: Stéphane Wirtel <[email protected]>
1 parent 4673dc2 commit 0af681b

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

Lib/compileall.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
221221
if not force:
222222
try:
223223
mtime = int(os.stat(fullname).st_mtime)
224-
expect = struct.pack('<4sll', importlib.util.MAGIC_NUMBER,
225-
0, mtime)
224+
expect = struct.pack('<4sLL', importlib.util.MAGIC_NUMBER,
225+
0, mtime & 0xFFFF_FFFF)
226226
for cfile in opt_cfiles.values():
227227
with open(cfile, 'rb') as chandle:
228228
actual = chandle.read(12)

Lib/test/test_compileall.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,28 @@ def timestamp_metadata(self):
8080
with open(self.bc_path, 'rb') as file:
8181
data = file.read(12)
8282
mtime = int(os.stat(self.source_path).st_mtime)
83-
compare = struct.pack('<4sll', importlib.util.MAGIC_NUMBER, 0, mtime)
83+
compare = struct.pack('<4sLL', importlib.util.MAGIC_NUMBER, 0,
84+
mtime & 0xFFFF_FFFF)
8485
return data, compare
8586

87+
def test_year_2038_mtime_compilation(self):
88+
# Test to make sure we can handle mtimes larger than what a 32-bit
89+
# signed number can hold as part of bpo-34990
90+
try:
91+
os.utime(self.source_path, (2**32 - 1, 2**32 - 1))
92+
except (OverflowError, OSError):
93+
self.skipTest("filesystem doesn't support timestamps near 2**32")
94+
self.assertTrue(compileall.compile_file(self.source_path))
95+
96+
def test_larger_than_32_bit_times(self):
97+
# This is similar to the test above but we skip it if the OS doesn't
98+
# support modification times larger than 32-bits.
99+
try:
100+
os.utime(self.source_path, (2**35, 2**35))
101+
except (OverflowError, OSError):
102+
self.skipTest("filesystem doesn't support large timestamps")
103+
self.assertTrue(compileall.compile_file(self.source_path))
104+
86105
def recreation_check(self, metadata):
87106
"""Check that compileall recreates bytecode when the new metadata is
88107
used."""
@@ -101,7 +120,7 @@ def recreation_check(self, metadata):
101120

102121
def test_mtime(self):
103122
# Test a change in mtime leads to a new .pyc.
104-
self.recreation_check(struct.pack('<4sll', importlib.util.MAGIC_NUMBER,
123+
self.recreation_check(struct.pack('<4sLL', importlib.util.MAGIC_NUMBER,
105124
0, 1))
106125

107126
def test_magic_number(self):

Lib/test/test_zipimport.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,9 @@ def get_file():
3737

3838
def make_pyc(co, mtime, size):
3939
data = marshal.dumps(co)
40-
if type(mtime) is type(0.0):
41-
# Mac mtimes need a bit of special casing
42-
if mtime < 0x7fffffff:
43-
mtime = int(mtime)
44-
else:
45-
mtime = int(-0x100000000 + int(mtime))
4640
pyc = (importlib.util.MAGIC_NUMBER +
47-
struct.pack("<iii", 0, int(mtime), size & 0xFFFFFFFF) + data)
41+
struct.pack("<iLL", 0,
42+
int(mtime) & 0xFFFF_FFFF, size & 0xFFFF_FFFF) + data)
4843
return pyc
4944

5045
def module_path_to_dotted_name(path):
@@ -256,6 +251,14 @@ def testBadMTime(self):
256251
TESTMOD + pyc_ext: (NOW, badtime_pyc)}
257252
self.doTest(".py", files, TESTMOD)
258253

254+
def test2038MTime(self):
255+
# Make sure we can handle mtimes larger than what a 32-bit signed number
256+
# can hold.
257+
twenty_thirty_eight_pyc = make_pyc(test_co, 2**32 - 1, len(test_src))
258+
files = {TESTMOD + ".py": (NOW, test_src),
259+
TESTMOD + pyc_ext: (NOW, twenty_thirty_eight_pyc)}
260+
self.doTest(".py", files, TESTMOD)
261+
259262
def testPackage(self):
260263
packdir = TESTPACK + os.sep
261264
files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed a Y2k38 bug in the compileall module where it would fail to compile
2+
files with a modification time after the year 2038.

0 commit comments

Comments
 (0)