PEP 597: Refinement (#1791)

This commit is contained in:
Inada Naoki 2021-01-31 12:44:30 +09:00 committed by GitHub
parent b2ce9f86f5
commit 6c41262962
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 38 additions and 27 deletions

View File

@ -14,11 +14,11 @@ Abstract
======== ========
Add a new warning category ``EncodingWarning``. It is emitted when Add a new warning category ``EncodingWarning``. It is emitted when
``encoding`` option is ommitted and the default encoding is locale ``encoding`` option is omitted and the default encoding is a locale
encoding. encoding.
The warning is disabled by default. New ``-X warn_encoding`` The warning is disabled by default. New ``-X warn_encoding``
commandline option and ``PYTHONWARNENCODING`` environment variable command-line option and ``PYTHONWARNENCODING`` environment variable
are used to enable the warnings. are used to enable the warnings.
@ -49,23 +49,24 @@ used actually. [2_]
Even Python experts assume that default encoding is UTF-8. Even Python experts assume that default encoding is UTF-8.
It creates bugs that happen only on Windows. See [3_] and [4_]. It creates bugs that happen only on Windows. See [3_] and [4_].
Raising a warning when the ``encoding`` option is omitted will Emitting a warning when the ``encoding`` option is omitted will help
help to find such mistakes. to find such mistakes.
Prepare to change the default encoding to UTF-8 Prepare to change the default encoding to UTF-8
----------------------------------------------- -----------------------------------------------
We chose to use locale encoding for the default text encoding We had chosen to use locale encoding for the default text encoding in
in Python 3.0. But UTF-8 has been adopted very widely since then. Python 3.0. But UTF-8 has been adopted very widely since then.
We might change the default text encoding to UTF-8 in the future. We might change the default text encoding to UTF-8 in the future.
But this change will affect many applications and libraries. But this change will affect many applications and libraries.
Many ``DeprecationWarning`` will be raised if we start raising Many ``DeprecationWarning`` will be emitted if we start emitting the
the warning by default. It will be too noisy. warning by default. It will be too noisy.
While this PEP doesn't cover the change, this PEP will help to Although this PEP doesn't propose to change the default encoding,
reduce the number of ``DeprecationWarning`` in the future. this PEP will help to reduce the warning in the future if we decide
to change the default encoding.
Specification Specification
@ -75,8 +76,8 @@ Specification
-------------------- --------------------
Add new ``EncodingWarning`` warning class which is a subclass of Add new ``EncodingWarning`` warning class which is a subclass of
``Warning``. It is used to warn when ``encoding`` option is omitted ``Warning``. It is used to warn when the ``encoding`` option is
and the default encoding is locale-specific. omitted and the default encoding is locale-specific.
Options to enable the warning Options to enable the warning
@ -86,11 +87,12 @@ Options to enable the warning
environment variable are added. They are used to enable the environment variable are added. They are used to enable the
``EncodingWarning``. ``EncodingWarning``.
``sys.flags.encoding_warning`` is also added. It is a boolean flag ``sys.flags.encoding_warning`` is also added. The flag represents
represents ``EncodingWarning`` is enabled. ``EncodingWarning`` is enabled.
When the option is enabled, ``io.TextIOWrapper()``, ``open()``, and When the option is enabled, ``io.TextIOWrapper()``, ``open()``, and
other modules emit ``EncodingWarning`` when ``encoding`` is omitted. other modules using them will emit ``EncodingWarning`` when
``encoding`` is omitted.
``encoding="locale"`` option ``encoding="locale"`` option
@ -120,7 +122,7 @@ supported too. For example,
----------------------- -----------------------
``io.text_encoding()`` is a helper function for functions having ``io.text_encoding()`` is a helper function for functions having
``encoding=None`` option and pass it to ``io.TextIOWrapper()`` or ``encoding=None`` option and passing it to ``io.TextIOWrapper()`` or
``open()``. ``open()``.
Pure Python implementation will be like this:: Pure Python implementation will be like this::
@ -155,17 +157,25 @@ For example, ``pathlib.Path.read_text()`` can use the function like:
with self.open(mode='r', encoding=encoding, errors=errors) as f: with self.open(mode='r', encoding=encoding, errors=errors) as f:
return f.read() return f.read()
By using ``io.text_encoding()``, ``EncodingWarning`` is emitted for
the caller of ``read_text()`` instead of ``read_text()``.
``subprocess`` module
----------------------
The default encoding for pipe in the subprocess module is changed Affected stdlibs
to ``io.LOCALE_ENCODING``. In other words, subprocess module doesn't -------------------
emit the ``EncodingWarning``.
The default encoding for PIPE is relating to the encoding of the Many stdlibs will be affected by this change.
stdio than the default encoding of ``TextIOWrapper``. So this PEP
doesn't propose to emit the warning for pipes. Most APIs accepting ``encoding=None`` will use ``io.text_encoding()``
as written in the previous section.
Where using locale encoding as the default encoding is reasonable,
``encoding=io.LOCALE_ENCODING`` will be used instead. For example,
``subprocess`` module will use locale encoding for the default
encoding of the pipes.
Many tests use ``open()`` without ``encoding`` specified to read
ASCII text files. They should be rewritten with ``encoding="ascii"``.
Rationale Rationale
@ -174,11 +184,12 @@ Rationale
Opt-in warning Opt-in warning
--------------- ---------------
Although ``DeprecationWarning`` is supressed by default, emitting Although ``DeprecationWarning`` is suppressed by default, emitting
``DeprecationWarning`` alwasy when ``encoding`` option is omitted ``DeprecationWarning`` always when ``encoding`` option is omitted
would be too noisy. would be too noisy.
Noisy warnings may leads developers to dismiss the ``DeprecationWarning``. Noisy warnings may lead developers to dismiss the
``DeprecationWarning``.
"locale" is not a codec alias "locale" is not a codec alias