[Python-checkins] r78483 - in python/branches/release26-maint: Lib/codecs.py Lib/test/test_codecs.py Misc/NEWS

florent.xicluna python-checkins at python.org
Sat Feb 27 12:26:58 CET 2010


Author: florent.xicluna
Date: Sat Feb 27 12:26:58 2010
New Revision: 78483
Log:
Merged revisions 78461,78482 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/trunk
........
 r78461 | florent.xicluna | 2010年02月26日 11:40:58 +0100 (ven, 26 fév 2010) | 2 lines
 
 #691291: codecs.open() should not convert end of lines on reading and writing.
........
 r78482 | florent.xicluna | 2010年02月27日 12:19:18 +0100 (sam, 27 fév 2010) | 2 lines
 
 Add entry for issue #691291.
........
Modified:
 python/branches/release26-maint/ (props changed)
 python/branches/release26-maint/Lib/codecs.py
 python/branches/release26-maint/Lib/test/test_codecs.py
 python/branches/release26-maint/Misc/NEWS
Modified: python/branches/release26-maint/Lib/codecs.py
==============================================================================
--- python/branches/release26-maint/Lib/codecs.py	(original)
+++ python/branches/release26-maint/Lib/codecs.py	Sat Feb 27 12:26:58 2010
@@ -858,10 +858,15 @@
 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
Modified: python/branches/release26-maint/Lib/test/test_codecs.py
==============================================================================
--- python/branches/release26-maint/Lib/test/test_codecs.py	(original)
+++ python/branches/release26-maint/Lib/test/test_codecs.py	Sat Feb 27 12:26:58 2010
@@ -437,6 +437,21 @@
 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"
 
Modified: python/branches/release26-maint/Misc/NEWS
==============================================================================
--- python/branches/release26-maint/Misc/NEWS	(original)
+++ python/branches/release26-maint/Misc/NEWS	Sat Feb 27 12:26:58 2010
@@ -74,6 +74,9 @@
 Library
 -------
 
+- Issue #691291: codecs.open() should not convert end of lines on reading and
+ writing.
+
 - Issue #7975: correct regression in dict methods supported by bsddb.dbshelve.
 
 - Issue #7959: ctypes callback functions are now registered correctly


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /