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

Skip to content

Commit abaf89b

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.
2 parents a10999d + 2be60af commit abaf89b

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
@@ -123,6 +123,23 @@ def test_file_from_empty_string_dir(self):
123123
pycache = os.path.dirname(imp.cache_from_source(file_path))
124124
shutil.rmtree(pycache)
125125

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

127144
class BadBytecodeTest(unittest.TestCase):
128145

Lib/test/test_import.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,18 @@ def __del__(self):
306306
"""))
307307
script_helper.assert_python_ok(testfn)
308308

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

310322
class PycRewritingTests(unittest.TestCase):
311323
# 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.3 Alpha 1?
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
- Issue #12705: A SyntaxError exception is now raised when attempting to
1417
compile multiple statements as a single interactive statement.
1518

Python/import.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,14 +1478,11 @@ load_source_module(PyObject *name, PyObject *pathname, FILE *fp)
14781478
}
14791479
#if SIZEOF_TIME_T > 4
14801480
/* Python's .pyc timestamp handling presumes that the timestamp fits
1481-
in 4 bytes. This will be fine until sometime in the year 2038,
1482-
when a 4-byte signed time_t will overflow.
1481+
in 4 bytes. Since the code only does an equality comparison,
1482+
ordering is not important and we can safely ignore the higher bits
1483+
(collisions are extremely unlikely).
14831484
*/
1484-
if (st.st_mtime >> 32) {
1485-
PyErr_SetString(PyExc_OverflowError,
1486-
"modification time overflows a 4 byte field");
1487-
goto error;
1488-
}
1485+
st.st_mtime &= 0xFFFFFFFF;
14891486
#endif
14901487
if (PyUnicode_READY(pathname) < 0)
14911488
return NULL;

0 commit comments

Comments
 (0)