File bpo34990-2038-problem-compileall.patch of Package python (Revision 381d91ea0ab10fc5235c75951d42564a)
Currently displaying revision 381d91ea0ab10fc5235c75951d42564a , Show latest
xxxxxxxxxx
1
From 9d3b6b2472f7c7ef841e652825de652bc8af85d7 Mon Sep 17 00:00:00 2001
2
From: "Miss Islington (bot)"
3
<31488909+miss-islington@users.noreply.github.com>
4
Date: Tue, 24 Aug 2021 08:07:31 -0700
5
Subject: [PATCH] [3.9] bpo-34990: Treat the pyc header's mtime in compileall
6
as an unsigned int (GH-19708)
7
MIME-Version: 1.0
8
Content-Type: text/plain; charset=UTF-8
9
Content-Transfer-Encoding: 8bit
10
11
(cherry picked from commit bb21e28fd08f894ceff2405544a2f257d42b1354)
12
13
Co-authored-by: Ammar Askar <ammar@ammaraskar.com>
14
Co-authored-by: Stéphane Wirtel <stephane@wirtel.be>
15
16
ported to python-2.7 by Bernhard M. Wiedemann <bwiedemann suse de>
17
18
diff --git a/Lib/compileall.py b/Lib/compileall.py
19
index 5cfa8be..193147e 100644
20
--- a/Lib/compileall.py
21
+++ b/Lib/compileall.py
22
23
if not force:
24
try:
25
mtime = int(os.stat(fullname).st_mtime)
26
- expect = struct.pack('<4sl', imp.get_magic(), mtime)
27
+ expect = struct.pack('<4sL', imp.get_magic(), mtime & 0xFFFFFFFF)
28
cfile = fullname + (__debug__ and 'c' or 'o')
29
with open(cfile, 'rb') as chandle:
30
actual = chandle.read(8)
31
diff --git a/Lib/test/test_compileall.py b/Lib/test/test_compileall.py
32
index d3a26db..0907f59 100644
33
--- a/Lib/test/test_compileall.py
34
+++ b/Lib/test/test_compileall.py
35
36
with open(self.bc_path, 'rb') as file:
37
data = file.read(8)
38
mtime = int(os.stat(self.source_path).st_mtime)
39
- compare = struct.pack('<4sl', imp.get_magic(), mtime)
40
+ compare = struct.pack('<4sL', imp.get_magic(), mtime & 0xFFFFFFFF)
41
return data, compare
42
43
@unittest.skipUnless(hasattr(os, 'stat'), 'test needs os.stat()')
44
45
46
def test_mtime(self):
47
# Test a change in mtime leads to a new .pyc.
48
- self.recreation_check(struct.pack('<4sl', imp.get_magic(), 1))
49
+ self.recreation_check(struct.pack('<4sL', imp.get_magic(), 1))
50
51
def test_magic_number(self):
52
# Test a change in mtime leads to a new .pyc.
53
diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py
54
index a66738a..e333582 100644
55
--- a/Lib/test/test_zipimport.py
56
+++ b/Lib/test/test_zipimport.py
57
58
59
def make_pyc(co, mtime):
60
data = marshal.dumps(co)
61
- if type(mtime) is type(0.0):
62
- # Mac mtimes need a bit of special casing
63
- if mtime < 0x7fffffff:
64
- mtime = int(mtime)
65
- else:
66
- mtime = int(-0x100000000L + long(mtime))
67
- pyc = imp.get_magic() + struct.pack("<i", int(mtime)) + data
68
+ pyc = imp.get_magic() + struct.pack("<L", int(mtime) & 0xFFFFFFFF) + data
69
return pyc
70
71
def module_path_to_dotted_name(path):
72
73
TESTMOD + pyc_ext: (NOW, badtime_pyc)}
74
self.doTest(".py", files, TESTMOD)
75
76
+ def test2038MTime(self):
77
+ # Make sure we can handle mtimes larger than what a 32-bit signed number
78
+ # can hold.
79
+ twenty_thirty_eight_pyc = make_pyc(test_co, 2**32 - 1)
80
+ files = {TESTMOD + ".py": (NOW, test_src),
81
+ TESTMOD + pyc_ext: (NOW, twenty_thirty_eight_pyc)}
82
+ self.doTest(".py", files, TESTMOD)
83
+
84
def testPackage(self):
85
packdir = TESTPACK + os.sep
86
files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
87
88
==========
89
90
Author: Bernhard M. Wiedemann <bwiedemann suse de>
91
Date: 2022-09-13
92
93
More y2038 fixes that are only needed for python2.7
94
95
diff --git a/Lib/compiler/pycodegen.py b/Lib/compiler/pycodegen.py
96
index 6515945..21d52bb 100644
97
--- a/Lib/compiler/pycodegen.py
98
+++ b/Lib/compiler/pycodegen.py
99
100
# to indicate the type of the value. simplest way to get the
101
# same effect is to call marshal and then skip the code.
102
mtime = os.path.getmtime(self.filename)
103
- mtime = struct.pack('<i', mtime)
104
+ mtime = struct.pack('<L', mtime & 0xFFFFFFFF)
105
return self.MAGIC + mtime
106
107
class LocalNameFinder:
108
diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py
109
index cdb1af5..6344ef2 100644
110
--- a/Lib/test/test_gzip.py
111
+++ b/Lib/test/test_gzip.py
112
113
self.assertEqual(flagsByte, '\x08') # only the FNAME flag is set
114
115
mtimeBytes = fRead.read(4)
116
- self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian
117
+ self.assertEqual(mtimeBytes, struct.pack('<L', mtime & 0xFFFFFFFF)) # little-endian
118
119
xflByte = fRead.read(1)
120
self.assertEqual(xflByte, '\x02') # maximum compression
121
diff --git a/Python/import.c b/Python/import.c
122
index b79354b..3efd17f 100644
123
--- a/Python/import.c
124
+++ b/Python/import.c
125
126
{
127
FILE *fp;
128
long magic;
129
- long pyc_mtime;
130
+ unsigned long pyc_mtime;
131
132
fp = fopen(cpathname, "rb");
133
if (fp == NULL)
134
135
return NULL;
136
}
137
pyc_mtime = PyMarshal_ReadLongFromFile(fp);
138
- if (pyc_mtime != mtime) {
139
+ if ((pyc_mtime&0xFFFFFFFF) != (((unsigned long)mtime)&0xFFFFFFFF)) {
140
if (Py_VerboseFlag)
141
PySys_WriteStderr("# %s has bad mtime\n", cpathname);
142
fclose(fp);
143