mirror of
https://github.com/jupyter/notebook.git
synced 2025-01-24 12:05:22 +08:00
Merge pull request #3305 from mdboom/atomic-write-notebook-json
Write notebook.json file atomically
This commit is contained in:
commit
e250b0b7bf
@ -91,12 +91,16 @@ class BaseJSONConfigManager(LoggingConfigurable):
|
|||||||
filename = self.file_name(section_name)
|
filename = self.file_name(section_name)
|
||||||
self.ensure_config_dir_exists()
|
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:
|
if PY3:
|
||||||
f = io.open(filename, 'w', encoding='utf-8')
|
f = io.open(filename, 'w', encoding='utf-8')
|
||||||
else:
|
else:
|
||||||
f = open(filename, 'wb')
|
f = open(filename, 'wb')
|
||||||
with f:
|
with f:
|
||||||
json.dump(data, f, indent=2)
|
f.write(json_content)
|
||||||
|
|
||||||
def update(self, section_name, new_data):
|
def update(self, section_name, new_data):
|
||||||
"""Modify the config section by recursively updating it with new_data.
|
"""Modify the config section by recursively updating it with new_data.
|
||||||
|
Loading…
Reference in New Issue
Block a user