summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_import.py
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2012-10-19 12:38:14 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2012-10-19 12:38:14 (GMT)
commit34937ce249864871bee129fbaa30a4dc0d317834 (patch)
tree07fd7a080cd86171f61c4ede807419d51048545b /Lib/test/test_import.py
parent2d51f687e133fb8141f1a6b5a6ac51c9d5eddf58 (diff)
downloadcpython-34937ce249864871bee129fbaa30a4dc0d317834.zip
cpython-34937ce249864871bee129fbaa30a4dc0d317834.tar.gz
cpython-34937ce249864871bee129fbaa30a4dc0d317834.tar.bz2
Issue #6074: Forward port Windows read-only source file fix from 2.7
Diffstat (limited to 'Lib/test/test_import.py')
-rw-r--r--Lib/test/test_import.py56
1 files changed, 51 insertions, 5 deletions
diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py
index 0f8f1f5..36c4f5e 100644
--- a/Lib/test/test_import.py
+++ b/Lib/test/test_import.py
@@ -20,12 +20,24 @@ from test.support import (
from test import script_helper
-def remove_files(name):
- for f in (name + ".py",
- name + ".pyc",
- name + ".pyo",
- name + ".pyw",
+def _iter_files(name):
+ for f in (name + os.extsep + "py",
+ name + os.extsep + "pyc",
+ name + os.extsep + "pyo",
+ name + os.extsep + "pyw",
name + "$py.class"):
+ yield f
+
+def chmod_files(name):
+ for f in _iter_files(name):
+ try:
+ os.chmod(f, 0o600)
+ except OSError as exc:
+ if exc.errno != errno.ENOENT:
+ raise
+
+def remove_files(name):
+ for f in _iter_files(name):
unlink(f)
rmtree('__pycache__')
@@ -122,6 +134,40 @@ class ImportTests(unittest.TestCase):
remove_files(TESTFN)
unload(TESTFN)
+ def test_rewrite_pyc_with_read_only_source(self):
+ # Issue 6074: a long time ago on posix, and more recently on Windows,
+ # a read only source file resulted in a read only pyc file, which
+ # led to problems with updating it later
+ sys.path.insert(0, os.curdir)
+ fname = TESTFN + os.extsep + "py"
+ try:
+ # Write a Python file, make it read-only and import it
+ with open(fname, 'w') as f:
+ f.write("x = 'original'\n")
+ # Tweak the mtime of the source to ensure pyc gets updated later
+ s = os.stat(fname)
+ os.utime(fname, (s.st_atime, s.st_mtime-100000000))
+ os.chmod(fname, 0o400)
+ m1 = __import__(TESTFN)
+ self.assertEqual(m1.x, 'original')
+ # Change the file and then reimport it
+ os.chmod(fname, 0o600)
+ with open(fname, 'w') as f:
+ f.write("x = 'rewritten'\n")
+ unload(TESTFN)
+ m2 = __import__(TESTFN)
+ self.assertEqual(m2.x, 'rewritten')
+ # Now delete the source file and check the pyc was rewritten
+ unlink(TESTFN)
+ unload(TESTFN)
+ m3 = __import__(TESTFN)
+ self.assertEqual(m3.x, 'rewritten')
+ finally:
+ chmod_files(TESTFN)
+ remove_files(TESTFN)
+ unload(TESTFN)
+ del sys.path[0]
+
def test_imp_module(self):
# Verify that the imp module can correctly load and find .py files
# XXX (ncoghlan): It would be nice to use support.CleanImport