Commit 98fe1a0c authored by Victor Stinner's avatar Victor Stinner

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.
parent c556e10b
...@@ -85,6 +85,9 @@ It defines the following functions: ...@@ -85,6 +85,9 @@ It defines the following functions:
In case a search function cannot find a given encoding, it should return In case a search function cannot find a given encoding, it should return
``None``. ``None``.
.. deprecated:: 3.3
*streamreader* and *streamwriter* attributes are now deprecated.
.. function:: lookup(encoding) .. function:: lookup(encoding)
...@@ -139,6 +142,8 @@ functions which use :func:`lookup` for the codec lookup: ...@@ -139,6 +142,8 @@ functions which use :func:`lookup` for the codec lookup:
Raises a :exc:`LookupError` in case the encoding cannot be found. Raises a :exc:`LookupError` in case the encoding cannot be found.
.. deprecated:: 3.3
.. function:: getwriter(encoding) .. function:: getwriter(encoding)
...@@ -147,6 +152,8 @@ functions which use :func:`lookup` for the codec lookup: ...@@ -147,6 +152,8 @@ functions which use :func:`lookup` for the codec lookup:
Raises a :exc:`LookupError` in case the encoding cannot be found. Raises a :exc:`LookupError` in case the encoding cannot be found.
.. deprecated:: 3.3
.. function:: register_error(name, error_handler) .. function:: register_error(name, error_handler)
...@@ -215,6 +222,11 @@ utility functions: ...@@ -215,6 +222,11 @@ utility functions:
providing transparent encoding/decoding. The default file mode is ``'r'`` providing transparent encoding/decoding. The default file mode is ``'r'``
meaning to open the file in read mode. meaning to open the file in read mode.
.. note::
This function is kept for backward compatibility with Python 2, the
builtin :func:`open` function should be used instead.
.. note:: .. note::
The wrapped version's methods will accept and return strings only. Bytes The wrapped version's methods will accept and return strings only. Bytes
...@@ -251,6 +263,8 @@ utility functions: ...@@ -251,6 +263,8 @@ utility functions:
``'strict'``, which causes :exc:`ValueError` to be raised in case an encoding ``'strict'``, which causes :exc:`ValueError` to be raised in case an encoding
error occurs. error occurs.
.. deprecated:: 3.3
.. function:: iterencode(iterator, encoding, errors='strict', **kwargs) .. function:: iterencode(iterator, encoding, errors='strict', **kwargs)
...@@ -563,6 +577,9 @@ The :class:`StreamWriter` class is a subclass of :class:`Codec` and defines the ...@@ -563,6 +577,9 @@ The :class:`StreamWriter` class is a subclass of :class:`Codec` and defines the
following methods which every stream writer must define in order to be following methods which every stream writer must define in order to be
compatible with the Python codec registry. compatible with the Python codec registry.
.. deprecated:: 3.3
Use the builtin the :class:`io.TextIOWrapper` class.
.. class:: StreamWriter(stream[, errors]) .. class:: StreamWriter(stream[, errors])
...@@ -628,6 +645,9 @@ The :class:`StreamReader` class is a subclass of :class:`Codec` and defines the ...@@ -628,6 +645,9 @@ The :class:`StreamReader` class is a subclass of :class:`Codec` and defines the
following methods which every stream reader must define in order to be following methods which every stream reader must define in order to be
compatible with the Python codec registry. compatible with the Python codec registry.
.. deprecated:: 3.3
Use the builtin the :class:`io.TextIOWrapper` class.
.. class:: StreamReader(stream[, errors]) .. class:: StreamReader(stream[, errors])
...@@ -728,6 +748,9 @@ and write modes. ...@@ -728,6 +748,9 @@ and write modes.
The design is such that one can use the factory functions returned by the The design is such that one can use the factory functions returned by the
:func:`lookup` function to construct the instance. :func:`lookup` function to construct the instance.
.. deprecated:: 3.3
Use the :class:`io.TextIOWrapper` class.
.. class:: StreamReaderWriter(stream, Reader, Writer, errors) .. class:: StreamReaderWriter(stream, Reader, Writer, errors)
...@@ -752,6 +775,8 @@ which is sometimes useful when dealing with different encoding environments. ...@@ -752,6 +775,8 @@ which is sometimes useful when dealing with different encoding environments.
The design is such that one can use the factory functions returned by the The design is such that one can use the factory functions returned by the
:func:`lookup` function to construct the instance. :func:`lookup` function to construct the instance.
.. deprecated:: 3.3
.. class:: StreamRecoder(stream, encode, decode, Reader, Writer, errors) .. class:: StreamRecoder(stream, encode, decode, Reader, Writer, errors)
......
...@@ -345,6 +345,8 @@ class StreamWriter(Codec): ...@@ -345,6 +345,8 @@ class StreamWriter(Codec):
The set of allowed parameter values can be extended via The set of allowed parameter values can be extended via
register_error. register_error.
""" """
import warnings
warnings.warn('use io.TextIOWrapper', DeprecationWarning, stacklevel=2)
self.stream = stream self.stream = stream
self.errors = errors self.errors = errors
...@@ -416,6 +418,8 @@ class StreamReader(Codec): ...@@ -416,6 +418,8 @@ class StreamReader(Codec):
The set of allowed parameter values can be extended via The set of allowed parameter values can be extended via
register_error. register_error.
""" """
import warnings
warnings.warn('use io.TextIOWrapper', DeprecationWarning, stacklevel=2)
self.stream = stream self.stream = stream
self.errors = errors self.errors = errors
self.bytebuffer = b"" self.bytebuffer = b""
...@@ -846,7 +850,7 @@ class StreamRecoder: ...@@ -846,7 +850,7 @@ class StreamRecoder:
### Shortcuts ### Shortcuts
def open(filename, mode='rb', encoding=None, errors='strict', buffering=1): def open(filename, mode='r', encoding=None, errors=None, buffering=1):
""" Open an encoded file using the given mode and return """ Open an encoded file using the given mode and return
a wrapped version providing transparent encoding/decoding. a wrapped version providing transparent encoding/decoding.
...@@ -877,18 +881,13 @@ def open(filename, mode='rb', encoding=None, errors='strict', buffering=1): ...@@ -877,18 +881,13 @@ def open(filename, mode='rb', encoding=None, errors='strict', buffering=1):
parameter. parameter.
""" """
if encoding is not None and \ if encoding is not None:
'b' not in mode: return builtins.open(filename, mode, buffering,
# Force opening of the file in binary mode encoding, errors, newline='')
mode = mode + 'b' else:
file = builtins.open(filename, mode, buffering) if 'b' not in mode:
if encoding is None: mode = mode + 'b'
return file return builtins.open(filename, mode, buffering, encoding, errors)
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'): def EncodedFile(file, data_encoding, file_encoding=None, errors='strict'):
......
This diff is collapsed.
...@@ -161,6 +161,11 @@ Core and Builtins ...@@ -161,6 +161,11 @@ Core and Builtins
Library Library
------- -------
- 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.
- Issue #12175: BufferedReader.read(-1) now calls raw.readall() if available. - Issue #12175: BufferedReader.read(-1) now calls raw.readall() if available.
- Issue #12175: FileIO.readall() now only reads the file position and size - Issue #12175: FileIO.readall() now only reads the file position and size
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment