Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 98fe1a0

Browse files
author
Victor Stinner
committed
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.
1 parent c556e10 commit 98fe1a0

4 files changed

Lines changed: 148 additions & 59 deletions

File tree

Doc/library/codecs.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ It defines the following functions:
8585
In case a search function cannot find a given encoding, it should return
8686
``None``.
8787

88+
.. deprecated:: 3.3
89+
*streamreader* and *streamwriter* attributes are now deprecated.
90+
8891

8992
.. function:: lookup(encoding)
9093

@@ -139,6 +142,8 @@ functions which use :func:`lookup` for the codec lookup:
139142

140143
Raises a :exc:`LookupError` in case the encoding cannot be found.
141144

145+
.. deprecated:: 3.3
146+
142147

143148
.. function:: getwriter(encoding)
144149

@@ -147,6 +152,8 @@ functions which use :func:`lookup` for the codec lookup:
147152

148153
Raises a :exc:`LookupError` in case the encoding cannot be found.
149154

155+
.. deprecated:: 3.3
156+
150157

151158
.. function:: register_error(name, error_handler)
152159

@@ -215,6 +222,11 @@ utility functions:
215222
providing transparent encoding/decoding. The default file mode is ``'r'``
216223
meaning to open the file in read mode.
217224

225+
.. note::
226+
227+
This function is kept for backward compatibility with Python 2, the
228+
builtin :func:`open` function should be used instead.
229+
218230
.. note::
219231

220232
The wrapped version's methods will accept and return strings only. Bytes
@@ -251,6 +263,8 @@ utility functions:
251263
``'strict'``, which causes :exc:`ValueError` to be raised in case an encoding
252264
error occurs.
253265

266+
.. deprecated:: 3.3
267+
254268

255269
.. function:: iterencode(iterator, encoding, errors='strict', **kwargs)
256270

@@ -563,6 +577,9 @@ The :class:`StreamWriter` class is a subclass of :class:`Codec` and defines the
563577
following methods which every stream writer must define in order to be
564578
compatible with the Python codec registry.
565579

580+
.. deprecated:: 3.3
581+
Use the builtin the :class:`io.TextIOWrapper` class.
582+
566583

567584
.. class:: StreamWriter(stream[, errors])
568585

@@ -628,6 +645,9 @@ The :class:`StreamReader` class is a subclass of :class:`Codec` and defines the
628645
following methods which every stream reader must define in order to be
629646
compatible with the Python codec registry.
630647

648+
.. deprecated:: 3.3
649+
Use the builtin the :class:`io.TextIOWrapper` class.
650+
631651

632652
.. class:: StreamReader(stream[, errors])
633653

@@ -728,6 +748,9 @@ and write modes.
728748
The design is such that one can use the factory functions returned by the
729749
:func:`lookup` function to construct the instance.
730750

751+
.. deprecated:: 3.3
752+
Use the :class:`io.TextIOWrapper` class.
753+
731754

732755
.. class:: StreamReaderWriter(stream, Reader, Writer, errors)
733756

@@ -752,6 +775,8 @@ which is sometimes useful when dealing with different encoding environments.
752775
The design is such that one can use the factory functions returned by the
753776
:func:`lookup` function to construct the instance.
754777

778+
.. deprecated:: 3.3
779+
755780

756781
.. class:: StreamRecoder(stream, encode, decode, Reader, Writer, errors)
757782

Lib/codecs.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ def __init__(self, stream, errors='strict'):
345345
The set of allowed parameter values can be extended via
346346
register_error.
347347
"""
348+
import warnings
349+
warnings.warn('use io.TextIOWrapper', DeprecationWarning, stacklevel=2)
348350
self.stream = stream
349351
self.errors = errors
350352

@@ -416,6 +418,8 @@ def __init__(self, stream, errors='strict'):
416418
The set of allowed parameter values can be extended via
417419
register_error.
418420
"""
421+
import warnings
422+
warnings.warn('use io.TextIOWrapper', DeprecationWarning, stacklevel=2)
419423
self.stream = stream
420424
self.errors = errors
421425
self.bytebuffer = b""
@@ -846,7 +850,7 @@ def __exit__(self, type, value, tb):
846850

847851
### Shortcuts
848852

849-
def open(filename, mode='rb', encoding=None, errors='strict', buffering=1):
853+
def open(filename, mode='r', encoding=None, errors=None, buffering=1):
850854

851855
""" Open an encoded file using the given mode and return
852856
a wrapped version providing transparent encoding/decoding.
@@ -877,18 +881,13 @@ def open(filename, mode='rb', encoding=None, errors='strict', buffering=1):
877881
parameter.
878882
879883
"""
880-
if encoding is not None and \
881-
'b' not in mode:
882-
# Force opening of the file in binary mode
883-
mode = mode + 'b'
884-
file = builtins.open(filename, mode, buffering)
885-
if encoding is None:
886-
return file
887-
info = lookup(encoding)
888-
srw = StreamReaderWriter(file, info.streamreader, info.streamwriter, errors)
889-
# Add attributes to simplify introspection
890-
srw.encoding = encoding
891-
return srw
884+
if encoding is not None:
885+
return builtins.open(filename, mode, buffering,
886+
encoding, errors, newline='')
887+
else:
888+
if 'b' not in mode:
889+
mode = mode + 'b'
890+
return builtins.open(filename, mode, buffering, encoding, errors)
892891

893892
def EncodedFile(file, data_encoding, file_encoding=None, errors='strict'):
894893

0 commit comments

Comments
 (0)