8000 allow textfile to be overwritten on windows · prometheus/client_python@0be4d06 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0be4d06

Browse files
committed
allow textfile to be overwritten on windows
Signed-off-by: Sander Van de Moortel <sander.vandemoortel@gmail.com>
1 parent 9a6e21d commit 0be4d06

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

prometheus_client/exposition.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,20 @@ 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 == 'posix':
277+
os.rename(tmppath, path)
278+
else:
279+
if sys.version_info <= (3,3):
280+
# Unable to guarantee atomic rename on Windows and Python<3.3
281+
# Remove and rename instead (risks losing the file)
282+
os.remove(path)
283+
os.rename(tmppath, path)
284+
else:
285+
# os.replace is introduced in Python 3.3 but there is some dispute whether
286+
# it is a truly atomic file operation: https://bugs.python.org/issue8828
287+
os.replace(tmppath, path)
276288

277289

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

0 commit comments

Comments
 (0)
0