cpython: a2f3d6986bfa (original) (raw)
Mercurial > cpython
changeset 74596:a2f3d6986bfa 3.2
Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp. [#11235]
Antoine Pitrou solipsis@pitrou.net | |
---|---|
date | Tue, 24 Jan 2012 17:44:06 +0100 |
parents | f7d3a754bc02 |
children | cb13d8cff050 365d0e387179 |
files | Lib/importlib/test/source/test_file_loader.py Lib/test/test_import.py Misc/NEWS Python/import.c |
diffstat | 4 files changed, 36 insertions(+), 7 deletions(-)[+] [-] Lib/importlib/test/source/test_file_loader.py 17 Lib/test/test_import.py 12 Misc/NEWS 3 Python/import.c 11 |
line wrap: on
line diff
--- a/Lib/importlib/test/source/test_file_loader.py +++ b/Lib/importlib/test/source/test_file_loader.py @@ -128,6 +128,23 @@ class SimpleTest(unittest.TestCase): pycache = os.path.dirname(imp.cache_from_source(file_path)) shutil.rmtree(pycache)
- def test_timestamp_overflow(self):
# When a modification timestamp is larger than 2**32, it should be[](#l1.8)
# truncated rather than raise an OverflowError.[](#l1.9)
with source_util.create_modules('_temp') as mapping:[](#l1.10)
source = mapping['_temp'][](#l1.11)
compiled = imp.cache_from_source(source)[](#l1.12)
with open(source, 'w') as f:[](#l1.13)
f.write("x = 5")[](#l1.14)
os.utime(source, (2 ** 33, 2 ** 33))[](#l1.15)
loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])[](#l1.16)
mod = loader.load_module('_temp')[](#l1.17)
# Sanity checks.[](#l1.18)
self.assertEqual(mod.__cached__, compiled)[](#l1.19)
self.assertEqual(mod.x, 5)[](#l1.20)
# The pyc file was created.[](#l1.21)
os.stat(compiled)[](#l1.22)
+ class BadBytecodeTest(unittest.TestCase):
--- a/Lib/test/test_import.py +++ b/Lib/test/test_import.py @@ -310,6 +310,18 @@ class ImportTests(unittest.TestCase): """)) script_helper.assert_python_ok(testfn)
- def test_timestamp_overflow(self):
# A modification timestamp larger than 2**32 should not be a problem[](#l2.8)
# when importing a module (issue #11235).[](#l2.9)
source = TESTFN + ".py"[](#l2.10)
compiled = imp.cache_from_source(source)[](#l2.11)
with open(source, 'w') as f:[](#l2.12)
pass[](#l2.13)
os.utime(source, (2 ** 33, 2 ** 33))[](#l2.14)
__import__(TESTFN)[](#l2.15)
# The pyc file was created.[](#l2.16)
os.stat(compiled)[](#l2.17)
+
class PycRewritingTests(unittest.TestCase):
# Test that the co_filename
attribute on code objects always points
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 3.2.3? Core and Builtins ----------------- +- Issue #11235: Fix OverflowError when trying to import a source file whose
--- a/Python/import.c +++ b/Python/import.c @@ -1304,14 +1304,11 @@ load_source_module(char *name, char pat } #if SIZEOF_TIME_T > 4 / Python's .pyc timestamp handling presumes that the timestamp fits
in 4 bytes. This will be fine until sometime in the year 2038,[](#l4.7)
when a 4-byte signed time_t will overflow.[](#l4.8)
in 4 bytes. Since the code only does an equality comparison,[](#l4.9)
ordering is not important and we can safely ignore the higher bits[](#l4.10)
(collisions are extremely unlikely).[](#l4.11) */[](#l4.12)
- if (st.st_mtime >> 32) {
PyErr_SetString(PyExc_OverflowError,[](#l4.14)
"modification time overflows a 4 byte field");[](#l4.15)
return NULL;[](#l4.16)
- }
#endif cpathname = make_compiled_pathname( pathname, buf, (size_t)MAXPATHLEN + 1, !Py_OptimizeFlag);