Skip to content

Commit

Permalink
Merge pull request #3305 from mdboom/atomic-write-notebook-json
Browse files Browse the repository at this point in the history
Write notebook.json file atomically
  • Loading branch information
takluyver authored Feb 6, 2018
2 parents 364cd03 + dd5778b commit e250b0b
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion notebook/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,16 @@ def set(self, section_name, data):
filename = self.file_name(section_name)
self.ensure_config_dir_exists()

# Generate the JSON up front, since it could raise an exception,
# in order to avoid writing half-finished corrupted data to disk.
json_content = json.dumps(data, indent=2)

if PY3:
f = io.open(filename, 'w', encoding='utf-8')
else:
f = open(filename, 'wb')
with f:
json.dump(data, f, indent=2)
f.write(json_content)

def update(self, section_name, new_data):
"""Modify the config section by recursively updating it with new_data.
Expand Down

0 comments on commit e250b0b

Please sign in to comment.