]> granicus.if.org Git - python/commitdiff
Silence the FileExistsError which can be raised because of the O_EXCL flag
authorAntoine Pitrou <solipsis@pitrou.net>
Wed, 19 Oct 2011 21:28:40 +0000 (23:28 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Wed, 19 Oct 2011 21:28:40 +0000 (23:28 +0200)
(as in import.c)

Lib/importlib/_bootstrap.py

index 824a31c8d443a51f1d9b73b07705258a4b36dabc..1e22438996ec0c74ca523a858ebc50d5ecaaa0ad 100644 (file)
@@ -81,7 +81,9 @@ def _path_absolute(path):
 
 
 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 @@ class _SourceFileLoader(_FileLoader, SourceLoader):
                     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):