8000 Merge pull request #650 from bluppfisk/fix_rename_windows · ethervoid/client_python@d8bc027 · GitHub
[go: up one dir, main page]

Skip to content

Commit d8bc027

Browse files
authored
Merge pull request prometheus#650 from bluppfisk/fix_rename_windows
allow textfile to be overwritten on windows
2 parents 9a6e21d + 20a31b4 commit d8bc027

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

prometheus_client/exposition.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,24 @@ def write_to_textfile(path, registry):
271271
tmppath = '%s.%s.%s' % (path, os.getpid(), threading.current_thread().ident)
272272
with open(tmppath, 'wb') as f:
273273
f.write(generate_latest(registry))
274-
# rename(2) is atomic.
275-
os.rename(tmppath, path)
274+
275+
# rename(2) is atomic but fails on Windows if the destination file exists
276+
if os.name == 'nt':
277+
if sys.version_info <= (3, 3):
278+
# Unable to guarantee atomic rename on Windows and Python<3.3
279+
# Remove and rename instead (risks losing the file)
280+
try:
281+
os.remove(path)
282+
except FileNotFoundError:
283+
pass
284+
285+
os.rename(tmppath, path)
286+
else:
287+
# os.replace is introduced in Python 3.3 but there is some dispute whether
288+
# it is a truly atomic file operation: https://bugs.python.org/issue8828
289+
os.replace(tmppath, path)
290+
else:
291+
os.rename(tmppath, path)
276292

277293

278294
def _make_handler(url, method, timeout, headers, data, base_handler):

0 commit comments

Comments
 (0)
0