[Python-checkins] r52517 - in python/trunk: Lib/codecs.py Lib/test/test_codecs.py Misc/NEWS

georg.brandl python-checkins at python.org
Sun Oct 29 09:39:24 CET 2006


Author: georg.brandl
Date: Sun Oct 29 09:39:22 2006
New Revision: 52517
Modified:
 python/trunk/Lib/codecs.py
 python/trunk/Lib/test/test_codecs.py
 python/trunk/Misc/NEWS
Log:
Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
fix all codecs file wrappers to work correctly with the "with"
statement (bug #1586513).
Modified: python/trunk/Lib/codecs.py
==============================================================================
--- python/trunk/Lib/codecs.py	(original)
+++ python/trunk/Lib/codecs.py	Sun Oct 29 09:39:22 2006
@@ -329,6 +329,12 @@
 """
 return getattr(self.stream, name)
 
+ def __enter__(self):
+ return self
+
+ def __exit__(self, type, value, tb):
+ self.stream.close()
+
 ###
 
 class StreamReader(Codec):
@@ -568,6 +574,12 @@
 """
 return getattr(self.stream, name)
 
+ def __enter__(self):
+ return self
+
+ def __exit__(self, type, value, tb):
+ self.stream.close()
+
 ###
 
 class StreamReaderWriter:
@@ -641,6 +653,14 @@
 """
 return getattr(self.stream, name)
 
+ # these are needed to make "with codecs.open(...)" work properly
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, type, value, tb):
+ self.stream.close()
+
 ###
 
 class StreamRecoder:
@@ -751,6 +771,12 @@
 """
 return getattr(self.stream, name)
 
+ def __enter__(self):
+ return self
+
+ def __exit__(self, type, value, tb):
+ self.stream.close()
+
 ### Shortcuts
 
 def open(filename, mode='rb', encoding=None, errors='strict', buffering=1):
@@ -824,9 +850,10 @@
 """
 if file_encoding is None:
 file_encoding = data_encoding
- info = lookup(data_encoding)
- sr = StreamRecoder(file, info.encode, info.decode,
- info.streamreader, info.streamwriter, errors)
+ data_info = lookup(data_encoding)
+ file_info = lookup(file_encoding)
+ sr = StreamRecoder(file, data_info.encode, data_info.decode,
+ file_info.streamreader, file_info.streamwriter, errors)
 # Add attributes to simplify introspection
 sr.data_encoding = data_encoding
 sr.file_encoding = file_encoding
Modified: python/trunk/Lib/test/test_codecs.py
==============================================================================
--- python/trunk/Lib/test/test_codecs.py	(original)
+++ python/trunk/Lib/test/test_codecs.py	Sun Oct 29 09:39:22 2006
@@ -910,6 +910,18 @@
 f = self.reader(self.stream)
 self.assertEquals(f.readlines(), [u'\ud55c\n', u'\uae00'])
 
+class EncodedFileTest(unittest.TestCase):
+ 
+ def test_basic(self):
+ f = StringIO.StringIO('\xed\x95\x9c\n\xea\xb8\x80')
+ ef = codecs.EncodedFile(f, 'utf-16', 'utf-8')
+ self.assertEquals(ef.read(), '\xff\xfe\\\xd5\n\x00\x00\xae')
+
+ f = StringIO.StringIO()
+ ef = codecs.EncodedFile(f, 'utf-8', 'latin1')
+ ef.write('\xc3\xbc')
+ self.assertEquals(f.getvalue(), '\xfc')
+
 class Str2StrTest(unittest.TestCase):
 
 def test_read(self):
@@ -1214,6 +1226,19 @@
 (u"", len(allbytes))
 )
 
+class WithStmtTest(unittest.TestCase):
+ def test_encodedfile(self):
+ f = StringIO.StringIO("\xc3\xbc")
+ with codecs.EncodedFile(f, "latin-1", "utf-8") as ef:
+ self.assertEquals(ef.read(), "\xfc")
+
+ def test_streamreaderwriter(self):
+ f = StringIO.StringIO("\xc3\xbc")
+ info = codecs.lookup("utf-8")
+ with codecs.StreamReaderWriter(f, info.streamreader,
+ info.streamwriter, 'strict') as srw:
+ self.assertEquals(srw.read(), u"\xfc")
+
 
 def test_main():
 test_support.run_unittest(
@@ -1234,10 +1259,12 @@
 IDNACodecTest,
 CodecsModuleTest,
 StreamReaderTest,
+ EncodedFileTest,
 Str2StrTest,
 BasicUnicodeTest,
 BasicStrTest,
- CharmapTest
+ CharmapTest,
+ WithStmtTest,
 )
 
 
Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Sun Oct 29 09:39:22 2006
@@ -89,6 +89,10 @@
 Library
 -------
 
+- Fix codecs.EncodedFile which did not use file_encoding in 2.5.0, and
+ fix all codecs file wrappers to work correctly with the "with"
+ statement (bug #1586513).
+
 - Lib/modulefinder.py now handles absolute and relative imports
 correctly.
 


More information about the Python-checkins mailing list

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