Rework atomic_writing with tests & docstring

This commit is contained in:
Thomas Kluyver 2014-08-05 10:50:09 -07:00
parent 04cbce2a14
commit 8b7d4c1c2f
2 changed files with 30 additions and 3 deletions

View File

@ -313,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 atomic_writing(os_path, 'wb') as f:
with atomic_writing(os_path, text=False) as f:
f.write(bcontent)
def _save_directory(self, os_path, model, name='', path=''):

View File

@ -15,6 +15,7 @@ from __future__ import print_function
from __future__ import absolute_import
import io as stdlib_io
import os.path
import sys
from subprocess import Popen, PIPE
@ -23,8 +24,11 @@ import unittest
import nose.tools as nt
from IPython.testing.decorators import skipif
from IPython.utils.io import Tee, capture_output, unicode_std_stream
from IPython.utils.io import (Tee, capture_output, unicode_std_stream,
atomic_writing,
)
from IPython.utils.py3compat import doctest_refactor_print, PY3
from IPython.utils.tempdir import TemporaryDirectory
if PY3:
from io import StringIO
@ -121,4 +125,27 @@ def test_UnicodeStdStream_nowrap():
nt.assert_is(unicode_std_stream(), sys.stdout)
assert not sys.stdout.closed
finally:
sys.stdout = orig_stdout
sys.stdout = orig_stdout
def test_atomic_writing():
class CustomExc(Exception): pass
with TemporaryDirectory() as td:
f1 = os.path.join(td, 'penguin')
with stdlib_io.open(f1, 'w') as f:
f.write(u'Before')
with nt.assert_raises(CustomExc):
with atomic_writing(f1) as f:
f.write(u'Failing write')
raise CustomExc
# Because of the exception, the file should not have been modified
with stdlib_io.open(f1, 'r') as f:
nt.assert_equal(f.read(), u'Before')
with atomic_writing(f1) as f:
f.write(u'Overwritten')
with stdlib_io.open(f1, 'r') as f:
nt.assert_equal(f.read(), u'Overwritten')