summaryrefslogtreecommitdiffstats
path: root/Lib/codecs.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-05-27 14:50:40 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2011-05-27 14:50:40 (GMT)
commit05010706697ce9c18e7f8a8e571753b0bcfd6548 (patch)
tree2ab12cefd89264c49464dd7a1a6554637510ef11 /Lib/codecs.py
parent4f2dab5c337f202b8fe3058108efc40cb1a0a724 (diff)
downloadcpython-05010706697ce9c18e7f8a8e571753b0bcfd6548.zip
cpython-05010706697ce9c18e7f8a8e571753b0bcfd6548.tar.gz
cpython-05010706697ce9c18e7f8a8e571753b0bcfd6548.tar.bz2
Revert my commit 3555cf6f9c98: "Issue #8796: codecs.open() calls the builtin
open() function instead of using StreamReaderWriter. Deprecate StreamReader, StreamWriter, StreamReaderWriter, StreamRecoder and EncodedFile() of the codec module. Use the builtin open() function or io.TextIOWrapper instead." "It has not been approved !" wrote Marc-Andre Lemburg.
Diffstat (limited to 'Lib/codecs.py')
-rw-r--r--Lib/codecs.py25
1 files changed, 13 insertions, 12 deletions
diff --git a/Lib/codecs.py b/Lib/codecs.py
index ec7879f..b150d64 100644
--- a/Lib/codecs.py
+++ b/Lib/codecs.py
@@ -345,8 +345,6 @@ class StreamWriter(Codec):
The set of allowed parameter values can be extended via
register_error.
"""
- import warnings
- warnings.warn('use io.TextIOWrapper', DeprecationWarning, stacklevel=2)
self.stream = stream
self.errors = errors
@@ -418,8 +416,6 @@ class StreamReader(Codec):
The set of allowed parameter values can be extended via
register_error.
"""
- import warnings
- warnings.warn('use io.TextIOWrapper', DeprecationWarning, stacklevel=2)
self.stream = stream
self.errors = errors
self.bytebuffer = b""
@@ -850,7 +846,7 @@ class StreamRecoder:
### Shortcuts
-def open(filename, mode='r', encoding=None, errors=None, buffering=1):
+def open(filename, mode='rb', encoding=None, errors='strict', buffering=1):
""" Open an encoded file using the given mode and return
a wrapped version providing transparent encoding/decoding.
@@ -881,13 +877,18 @@ def open(filename, mode='r', encoding=None, errors=None, buffering=1):
parameter.
"""
- if encoding is not None:
- return builtins.open(filename, mode, buffering,
- encoding, errors, newline='')
- else:
- if 'b' not in mode:
- mode = mode + 'b'
- return builtins.open(filename, mode, buffering, encoding, errors)
+ if encoding is not None and \
+ 'b' not in mode:
+ # Force opening of the file in binary mode
+ mode = mode + 'b'
+ file = builtins.open(filename, mode, buffering)
+ if encoding is None:
+ return file
+ info = lookup(encoding)
+ srw = StreamReaderWriter(file, info.streamreader, info.streamwriter, errors)
+ # Add attributes to simplify introspection
+ srw.encoding = encoding
+ return srw
def EncodedFile(file, data_encoding, file_encoding=None, errors='strict'):