From f4b6186d9c3677f96183d4c9e7267c0b960c9132 Mon Sep 17 00:00:00 2001 From: Florent Xicluna Date: Fri, 26 Feb 2010 10:40:58 +0000 Subject: #691291: codecs.open() should not convert end of lines on reading and writing. --- Lib/codecs.py | 13 +++++++++---- Lib/test/test_codecs.py | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Lib/codecs.py b/Lib/codecs.py index 557ccf7..a67240a 100644 --- a/Lib/codecs.py +++ b/Lib/codecs.py @@ -858,10 +858,15 @@ def open(filename, mode='rb', encoding=None, errors='strict', buffering=1): parameter. """ - if encoding is not None and \ - 'b' not in mode: - # Force opening of the file in binary mode - mode = mode + 'b' + if encoding is not None: + if 'U' in mode: + # No automatic conversion of '\n' is done on reading and writing + mode = mode.strip().replace('U', '') + if mode[:1] not in set('rwa'): + mode = 'r' + mode + if 'b' not in mode: + # Force opening of the file in binary mode + mode = mode + 'b' file = __builtin__.open(filename, mode, buffering) if encoding is None: return file diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 175f92b..8caf018 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -437,6 +437,21 @@ class UTF16Test(ReadTest): def test_errors(self): self.assertRaises(UnicodeDecodeError, codecs.utf_16_decode, "\xff", "strict", True) + def test_bug691291(self): + # Files are always opened in binary mode, even if no binary mode was + # specified. This means that no automatic conversion of '\n' is done + # on reading and writing. + s1 = u'Hello\r\nworld\r\n' + + s = s1.encode(self.encoding) + try: + with open(test_support.TESTFN, 'wb') as fp: + fp.write(s) + with codecs.open(test_support.TESTFN, 'U', encoding=self.encoding) as reader: + self.assertEqual(reader.read(), s1) + finally: + test_support.unlink(test_support.TESTFN) + class UTF16LETest(ReadTest): encoding = "utf-16-le" -- cgit v0.12