diff options
author | Georg Brandl <georg@python.org> | 2006-10-29 08:39:22 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2006-10-29 08:39:22 (GMT) |
commit | 8f99f81dfc5309205c2f93bd4dd205f97f586b0d (patch) | |
tree | 938fa01d5f2dc343366f439539805fd3bfd8f0d1 /Lib/codecs.py | |
parent | 4bb9b565011f2420649f851dc74e8fa3749046fa (diff) | |
download | cpython-8f99f81dfc5309205c2f93bd4dd205f97f586b0d.zip cpython-8f99f81dfc5309205c2f93bd4dd205f97f586b0d.tar.gz cpython-8f99f81dfc5309205c2f93bd4dd205f97f586b0d.tar.bz2 |
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).
Diffstat (limited to 'Lib/codecs.py')
-rw-r--r-- | Lib/codecs.py | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/Lib/codecs.py b/Lib/codecs.py index 1518d75..f834b8d 100644 --- a/Lib/codecs.py +++ b/Lib/codecs.py @@ -329,6 +329,12 @@ class StreamWriter(Codec): """ 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 @@ class StreamReader(Codec): """ 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 @@ class StreamReaderWriter: """ 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 @@ class StreamRecoder: """ 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 @@ def EncodedFile(file, data_encoding, file_encoding=None, errors='strict'): """ 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 |