Skip to content

Commit

Permalink
Changed handling of empty files and name of tempfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsMichelsen committed Sep 29, 2016
1 parent 2ce54cb commit f17ee3a
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,12 @@ def load_data_from_file(path, default=None, lock=False):

try:
try:
return ast.literal_eval(file(path).read())
content = file(path).read().strip()
if not content:
# May be created empty during locking
return default
else:
return ast.literal_eval(file(path).read())
except IOError, e:
if e.errno == 2: # IOError: [Errno 2] No such file or directory
return default
Expand All @@ -212,7 +217,7 @@ def save_file(path, content, mode=0660):
try:
tmp_path = None
with tempfile.NamedTemporaryFile("w", dir=os.path.dirname(path),
prefix=os.path.basename(path)+".new",
prefix=".%s.new" % os.path.basename(path),
delete=False) as tmp:
tmp_path = tmp.name
os.chmod(tmp_path, mode)
Expand Down

0 comments on commit f17ee3a

Please sign in to comment.