@@ -0,0 +1,41 @@
1+diff --git a/Lib/py_compile.py b/Lib/py_compile.py
2+index 978da73..3559eb9 100644
3+--- a/Lib/py_compile.py
4++++ b/Lib/py_compile.py
5+@@ -120,16 +120,27 @@ def compile(file, cfile=None, dfile=None, doraise=False):
6+ return
7+ if cfile is None:
8+ cfile = file + (__debug__ and 'c' or 'o')
9+- with open(cfile, 'wb') as fc:
10+- fc.write('\0\0\0\0')
11+- if "DETERMINISTIC_BUILD" in os.environ:
12++ # Atomically write the pyc/pyo file. Issue #13146.
13++ # id() is used to generate a pseudo-random filename.
14++ path_tmp = '{}.{}'.format(cfile, id(cfile))
15++ try:
16++ with open(path_tmp, 'wb') as fc:
17+ fc.write('\0\0\0\0')
18+- else:
19+- wr_long(fc, timestamp)
20+- marshal.dump(codeobject, fc)
21+- fc.flush()
22+- fc.seek(0, 0)
23+- fc.write(MAGIC)
24++ if "DETERMINISTIC_BUILD" in os.environ:
25++ fc.write('\0\0\0\0')
26++ else:
27++ wr_long(fc, timestamp)
28++ marshal.dump(codeobject, fc)
29++ fc.flush()
30++ fc.seek(0, 0)
31++ fc.write(MAGIC)
32++ os.rename(path_tmp, cfile)
33++ except OSError:
34++ try:
35++ os.unlink(path_tmp)
36++ except OSError:
37++ pass
38++ raise
39+
40+ def main(args=None):
41+ """Compile several source files.