From 47f307e4c8d4e5c3420691de148b15c95cbbad1f Mon Sep 17 00:00:00 2001 From: Min RK Date: Wed, 12 Nov 2014 14:08:41 -0800 Subject: [PATCH] don't use text mode in mkstemp causes double-encoding of newlines, preventing newline arg from having desired effect. --- IPython/utils/tests/test_io.py | 39 +++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/IPython/utils/tests/test_io.py b/IPython/utils/tests/test_io.py index a76179ff5..023c9641b 100644 --- a/IPython/utils/tests/test_io.py +++ b/IPython/utils/tests/test_io.py @@ -175,4 +175,41 @@ def test_atomic_writing(): f.write(u'written from symlink') with stdlib_io.open(f1, 'r') as f: - nt.assert_equal(f.read(), u'written from symlink') \ No newline at end of file + nt.assert_equal(f.read(), u'written from symlink') + +def test_atomic_writing_newlines(): + with TemporaryDirectory() as td: + path = os.path.join(td, 'testfile') + + lf = u'a\nb\nc\n' + plat = lf.replace(u'\n', os.linesep) + crlf = lf.replace(u'\n', u'\r\n') + + # test default + with stdlib_io.open(path, 'w') as f: + f.write(lf) + with stdlib_io.open(path, 'r', newline='') as f: + read = f.read() + nt.assert_equal(read, plat) + + # test newline=LF + with stdlib_io.open(path, 'w', newline='\n') as f: + f.write(lf) + with stdlib_io.open(path, 'r', newline='') as f: + read = f.read() + nt.assert_equal(read, lf) + + # test newline=CRLF + with atomic_writing(path, newline='\r\n') as f: + f.write(lf) + with stdlib_io.open(path, 'r', newline='') as f: + read = f.read() + nt.assert_equal(read, crlf) + + # test newline=no convert + text = u'crlf\r\ncr\rlf\n' + with atomic_writing(path, newline='') as f: + f.write(text) + with stdlib_io.open(path, 'r', newline='') as f: + read = f.read() + nt.assert_equal(read, text)