From b0cc91290cfa12621cb51eb0c7d6a0f223722346 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola' Date: Fri, 19 Oct 2012 13:34:32 +0200 Subject: [PATCH 1/3] Fix issue #16270: urllib may hang when used for retrieving files via FTP by using a context manager. --- Lib/urllib/request.py | 6 ------ Misc/NEWS | 3 +++ 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 67b4c795b36..88f3ce1a26a 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -2326,13 +2326,7 @@ class ftpwrapper: return (ftpobj, retrlen) def endtransfer(self): - if not self.busy: - return self.busy = 0 - try: - self.ftp.voidresp() - except ftperrors(): - pass def close(self): self.keepalive = False diff --git a/Misc/NEWS b/Misc/NEWS index 228db08c6f0..98c8749847c 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -43,6 +43,9 @@ Core and Builtins Library ------- +- Issue #16270: urllib may hang when used for retrieving files via FTP by using + a context manager. Patch by Giampaolo Rodola'. + - Issue #16176: Properly identify Windows 8 via platform.platform() - Issue #16114: The subprocess module no longer provides a misleading error From 2d51f687e133fb8141f1a6b5a6ac51c9d5eddf58 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola' Date: Fri, 19 Oct 2012 13:40:28 +0200 Subject: [PATCH 2/3] Fix issue #16270: urllib may hang when used for retrieving files via FTP by using a context manager. --- Lib/urllib/request.py | 6 ------ Misc/NEWS | 3 +++ 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index d6f9f9a1bca..64092cf7d6c 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -2212,13 +2212,7 @@ class ftpwrapper: return (ftpobj, retrlen) def endtransfer(self): - if not self.busy: - return self.busy = 0 - try: - self.ftp.voidresp() - except ftperrors(): - pass def close(self): self.keepalive = False diff --git a/Misc/NEWS b/Misc/NEWS index 0efbefae70a..436a24967f7 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -129,6 +129,9 @@ Core and Builtins Library ------- +- Issue #16270: urllib may hang when used for retrieving files via FTP by using + a context manager. Patch by Giampaolo Rodola'. + - Issue #16176: Properly identify Windows 8 via platform.platform() - Issue #16114: The subprocess module no longer provides a misleading From 34937ce249864871bee129fbaa30a4dc0d317834 Mon Sep 17 00:00:00 2001 From: Nick Coghlan Date: Fri, 19 Oct 2012 22:38:14 +1000 Subject: [PATCH 3/3] Issue #6074: Forward port Windows read-only source file fix from 2.7 --- Lib/test/test_import.py | 56 +++++++++++++++++++++++++++++++++++++---- Misc/NEWS | 3 +++ Python/import.c | 6 +++++ 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py index 0f8f1f584f5..36c4f5e3b93 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 diff --git a/Misc/NEWS b/Misc/NEWS index 436a24967f7..107222f332c 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 3.2.4 Core and Builtins ----------------- +- Issue #6074: Ensure cached bytecode files can always be updated by the + user that created them, even when the source file is read-only. + - Issue #14783: Improve int() docstring and switch docstrings for str(), range(), and slice() to use multi-line signatures. diff --git a/Python/import.c b/Python/import.c index beb0eecb5a0..4695c969706 100644 --- a/Python/import.c +++ b/Python/import.c @@ -1174,6 +1174,12 @@ write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat) time_t mtime = srcstat->st_mtime; #ifdef MS_WINDOWS /* since Windows uses different permissions */ mode_t mode = srcstat->st_mode & ~S_IEXEC; + /* Issue #6074: We ensure user write access, so we can delete it later + * when the source file changes. (On POSIX, this only requires write + * access to the directory, on Windows, we need write access to the file + * as well) + */ + mode |= _S_IWRITE; #else mode_t mode = srcstat->st_mode & ~S_IXUSR & ~S_IXGRP & ~S_IXOTH; mode_t dirmode = (srcstat->st_mode |