cpython: cb13d8cff050 (original) (raw)
Mercurial > cpython
changeset 74597:cb13d8cff050
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:45:50 +0100 |
parents | 75a3d35309cf(current diff)a2f3d6986bfa(diff) |
children | 454d1c52f3e0 |
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 @@ -123,6 +123,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 @@ -306,6 +306,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.3 Alpha 1? Core and Builtins ----------------- +- Issue #11235: Fix OverflowError when trying to import a source file whose
- Issue #12705: A SyntaxError exception is now raised when attempting to compile multiple statements as a single interactive statement.
--- a/Python/import.c +++ b/Python/import.c @@ -1478,14 +1478,11 @@ load_source_module(PyObject name, PyObj } #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)