From 04cbce2a1413e66bc993358e6f768f9fd605adff Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Mon, 4 Aug 2014 11:22:42 -0700 Subject: [PATCH] Implement atomic save Ping @fperez, this should avoid issues with corrupted/lost notebooks when the disk is full, though I haven't worked out how to test it just yet. Closes gh-6254 --- IPython/html/services/contents/filemanager.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/IPython/html/services/contents/filemanager.py b/IPython/html/services/contents/filemanager.py index 59da30917..ad36f3963 100644 --- a/IPython/html/services/contents/filemanager.py +++ b/IPython/html/services/contents/filemanager.py @@ -13,6 +13,7 @@ from tornado import web from .manager import ContentsManager from IPython.nbformat import current +from IPython.utils.io import atomic_writing from IPython.utils.path import ensure_dir_exists from IPython.utils.traitlets import Unicode, Bool, TraitError from IPython.utils.py3compat import getcwd @@ -295,7 +296,7 @@ class FileContentsManager(ContentsManager): if 'name' in nb['metadata']: nb['metadata']['name'] = u'' - with io.open(os_path, 'w', encoding='utf-8') as f: + with atomic_writing(os_path, encoding='utf-8') as f: current.write(nb, f, u'json') def _save_file(self, os_path, model, name='', path=''): @@ -312,7 +313,7 @@ class FileContentsManager(ContentsManager): bcontent = base64.decodestring(b64_bytes) except Exception as e: raise web.HTTPError(400, u'Encoding error saving %s: %s' % (os_path, e)) - with io.open(os_path, 'wb') as f: + with atomic_writing(os_path, 'wb') as f: f.write(bcontent) def _save_directory(self, os_path, model, name='', path=''):