Skip to content

Commit 9d3b6b2

Browse files
miss-islingtonammaraskarmatrixise
authored
[3.9] 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 834a2eb commit 9d3b6b2

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

Lib/compileall.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
219219
if not force:
220220
try:
221221
mtime = int(os.stat(fullname).st_mtime)
222-
expect = struct.pack('<4sll', importlib.util.MAGIC_NUMBER,
223-
0, mtime)
222+
expect = struct.pack('<4sLL', importlib.util.MAGIC_NUMBER,
223+
0, mtime & 0xFFFF_FFFF)
224224
for cfile in opt_cfiles.values():
225225
with open(cfile, 'rb') as chandle:
226226
actual = chandle.read(12)

Lib/test/test_compileall.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,28 @@ def timestamp_metadata(self):
7575
with open(self.bc_path, 'rb') as file:
7676
data = file.read(12)
7777
mtime = int(os.stat(self.source_path).st_mtime)
78-
compare = struct.pack('<4sll', importlib.util.MAGIC_NUMBER, 0, mtime)
78+
compare = struct.pack('<4sLL', importlib.util.MAGIC_NUMBER, 0,
79+
mtime & 0xFFFF_FFFF)
7980
return data, compare
8081

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

97116
def test_mtime(self):
98117
# Test a change in mtime leads to a new .pyc.
99-
self.recreation_check(struct.pack('<4sll', importlib.util.MAGIC_NUMBER,
118+
self.recreation_check(struct.pack('<4sLL', importlib.util.MAGIC_NUMBER,
100119
0, 1))
101120

102121
def test_magic_number(self):

Lib/test/test_zipimport.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,9 @@ def get_file():
3434

3535
def make_pyc(co, mtime, size):
3636
data = marshal.dumps(co)
37-
if type(mtime) is type(0.0):
38-
# Mac mtimes need a bit of special casing
39-
if mtime < 0x7fffffff:
40-
mtime = int(mtime)
41-
else:
42-
mtime = int(-0x100000000 + int(mtime))
4337
pyc = (importlib.util.MAGIC_NUMBER +
44-
struct.pack("<iii", 0, int(mtime), size & 0xFFFFFFFF) + data)
38+
struct.pack("<iLL", 0,
39+
int(mtime) & 0xFFFF_FFFF, size & 0xFFFF_FFFF) + data)
4540
return pyc
4641

4742
def module_path_to_dotted_name(path):
@@ -253,6 +248,14 @@ def testBadMTime(self):
253248
TESTMOD + pyc_ext: (NOW, badtime_pyc)}
254249
self.doTest(".py", files, TESTMOD)
255250

251+
def test2038MTime(self):
252+
# Make sure we can handle mtimes larger than what a 32-bit signed number
253+
# can hold.
254+
twenty_thirty_eight_pyc = make_pyc(test_co, 2**32 - 1, len(test_src))
255+
files = {TESTMOD + ".py": (NOW, test_src),
256+
TESTMOD + pyc_ext: (NOW, twenty_thirty_eight_pyc)}
257+
self.doTest(".py", files, TESTMOD)
258+
256259
def testPackage(self):
257260
packdir = TESTPACK + os.sep
258261
files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
Lines changed: 2 additions & 0 deletions
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)