Skip to content

Commit

Permalink
Merge pull request #1170 from Bastian-Krause/bst/atomic-replace-unbound
Browse files Browse the repository at this point in the history
util/atomic: fix UnboundLocalError in atomic_replace()
  • Loading branch information
jluebbe committed May 17, 2023
2 parents 2f00b39 + 7e0a144 commit 0dd63f9
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions labgrid/util/atomic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import tempfile

def atomic_replace(filename, data):
f = None
try:
with tempfile.NamedTemporaryFile(
mode='wb',
Expand All @@ -12,7 +13,8 @@ def atomic_replace(filename, data):
os.fsync(f.fileno())
os.replace(f.name, filename)
finally:
try:
os.unlink(f.name)
except FileNotFoundError:
pass
if f is not None:
try:
os.unlink(f.name)
except FileNotFoundError:
pass

0 comments on commit 0dd63f9

Please sign in to comment.