[Python-checkins] cpython: Silence the FileExistsError which can be raised because of the O_EXCL flag

antoine.pitrou python-checkins at python.org
Wed Oct 19 23:32:38 CEST 2011


http://hg.python.org/cpython/rev/170ed6735d4b
changeset:   73016:170ed6735d4b
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Wed Oct 19 23:28:40 2011 +0200
summary:
  Silence the FileExistsError which can be raised because of the O_EXCL flag
(as in import.c)

files:
  Lib/importlib/_bootstrap.py |  14 +++++++-------
  1 files changed, 7 insertions(+), 7 deletions(-)


diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -81,7 +81,9 @@
 
 
 def _write_atomic(path, data):
-    """Best-effort function to write data to a path atomically."""
+    """Best-effort function to write data to a path atomically.
+    Be prepared to handle a FileExistsError if concurrent writing of the
+    temporary file is attempted."""
     if not sys.platform.startswith('win'):
         # On POSIX-like platforms, renaming is atomic
         path_tmp = path + '.tmp'
@@ -516,12 +518,10 @@
                     raise
         try:
             _write_atomic(path, data)
-        except OSError as exc:
-            # Don't worry if you can't write bytecode.
-            if exc.errno == errno.EACCES:
-                return
-            else:
-                raise
+        except (PermissionError, FileExistsError):
+            # Don't worry if you can't write bytecode or someone is writing
+            # it at the same time.
+            pass
 
 
 class _SourcelessFileLoader(_FileLoader, _LoaderBasics):

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list