summaryrefslogtreecommitdiffstats
path: root/Lib/importlib/_bootstrap.py
diff options
context:
space:
mode:
authorCharles-François Natali <neologix@free.fr>2011-10-31 19:47:31 (GMT)
committerCharles-François Natali <neologix@free.fr>2011-10-31 19:47:31 (GMT)
commite695eec24addd2a39c9af7456b8218c0810d742c (patch)
treef6e5c0cbb2aaa4d6a3ddbec9425fda2e4d885c12 /Lib/importlib/_bootstrap.py
parent59142db6d35f00142cd9982878e75d43cbda7a68 (diff)
downloadcpython-e695eec24addd2a39c9af7456b8218c0810d742c.zip
cpython-e695eec24addd2a39c9af7456b8218c0810d742c.tar.gz
cpython-e695eec24addd2a39c9af7456b8218c0810d742c.tar.bz2
Issue #13303: Fix a race condition in the bytecode file creation.
Diffstat (limited to 'Lib/importlib/_bootstrap.py')
-rw-r--r--Lib/importlib/_bootstrap.py7
1 files changed, 4 insertions, 3 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index 4161b3e..775fa85 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -85,10 +85,11 @@ def _write_atomic(path, data):
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'
+ # On POSIX-like platforms, renaming is atomic. id() is used to generate
+ # a pseudo-random filename.
+ path_tmp = '{}.{}'.format(path, id(path))
+ fd = _os.open(path_tmp, _os.O_EXCL | _os.O_CREAT | _os.O_WRONLY)
try:
- fd = _os.open(path_tmp, _os.O_EXCL | _os.O_CREAT | _os.O_WRONLY)
with _io.FileIO(fd, 'wb') as file:
file.write(data)
_os.rename(path_tmp, path)