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

Skip to content

Commit 2be60af

Browse files
committed
Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.
1 parent 7fa5a99 commit 2be60af

4 files changed

Lines changed: 36 additions & 7 deletions

File tree

Lib/importlib/test/source/test_file_loader.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,23 @@ def test_file_from_empty_string_dir(self):
128128
pycache = os.path.dirname(imp.cache_from_source(file_path))
129129
shutil.rmtree(pycache)
130130

131+
def test_timestamp_overflow(self):
132+
# When a modification timestamp is larger than 2**32, it should be
133+
# truncated rather than raise an OverflowError.
134+
with source_util.create_modules('_temp') as mapping:
135+
source = mapping['_temp']
136+
compiled = imp.cache_from_source(source)
137+
with open(source, 'w') as f:
138+
f.write("x = 5")
139+
os.utime(source, (2 ** 33, 2 ** 33))
140+
loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
141+
mod = loader.load_module('_temp')
142+
# Sanity checks.
143+
self.assertEqual(mod.__cached__, compiled)
144+
self.assertEqual(mod.x, 5)
145+
# The pyc file was created.
146+
os.stat(compiled)
147+
131148

132149
class BadBytecodeTest(unittest.TestCase):
133150

Lib/test/test_import.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,18 @@ def __del__(self):
310310
"""))
311311
script_helper.assert_python_ok(testfn)
312312

313+
def test_timestamp_overflow(self):
314+
# A modification timestamp larger than 2**32 should not be a problem
315+
# when importing a module (issue #11235).
316+
source = TESTFN + ".py"
317+
compiled = imp.cache_from_source(source)
318+
with open(source, 'w') as f:
319+
pass
320+
os.utime(source, (2 ** 33, 2 ** 33))
321+
__import__(TESTFN)
322+
# The pyc file was created.
323+
os.stat(compiled)
324+
313325

314326
class PycRewritingTests(unittest.TestCase):
315327
# Test that the `co_filename` attribute on code objects always points

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.2.3?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #11235: Fix OverflowError when trying to import a source file whose
14+
modification time doesn't fit in a 32-bit timestamp.
15+
1316
- Fix the builtin module initialization code to store the init function for
1417
future reinitialization.
1518

Python/import.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,14 +1304,11 @@ load_source_module(char *name, char *pathname, FILE *fp)
13041304
}
13051305
#if SIZEOF_TIME_T > 4
13061306
/* Python's .pyc timestamp handling presumes that the timestamp fits
1307-
in 4 bytes. This will be fine until sometime in the year 2038,
1308-
when a 4-byte signed time_t will overflow.
1307+
in 4 bytes. Since the code only does an equality comparison,
1308+
ordering is not important and we can safely ignore the higher bits
1309+
(collisions are extremely unlikely).
13091310
*/
1310-
if (st.st_mtime >> 32) {
1311-
PyErr_SetString(PyExc_OverflowError,
1312-
"modification time overflows a 4 byte field");
1313-
return NULL;
1314-
}
1311+
st.st_mtime &= 0xFFFFFFFF;
13151312
#endif
13161313
cpathname = make_compiled_pathname(
13171314
pathname, buf, (size_t)MAXPATHLEN + 1, !Py_OptimizeFlag);

0 commit comments

Comments
 (0)