PEP 597: Refinement (#1791)
This commit is contained in:
parent
b2ce9f86f5
commit
6c41262962
65
pep-0597.rst
65
pep-0597.rst
|
@ -14,11 +14,11 @@ Abstract
|
|||
========
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
|
||||
|
@ -49,23 +49,24 @@ used actually. [2_]
|
|||
Even Python experts assume that default encoding is UTF-8.
|
||||
It creates bugs that happen only on Windows. See [3_] and [4_].
|
||||
|
||||
Raising a warning when the ``encoding`` option is omitted will
|
||||
help to find such mistakes.
|
||||
Emitting a warning when the ``encoding`` option is omitted will help
|
||||
to find such mistakes.
|
||||
|
||||
|
||||
Prepare to change the default encoding to UTF-8
|
||||
-----------------------------------------------
|
||||
|
||||
We chose to use locale encoding for the default text encoding
|
||||
in Python 3.0. But UTF-8 has been adopted very widely since then.
|
||||
We had chosen to use locale encoding for the default text encoding in
|
||||
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.
|
||||
But this change will affect many applications and libraries.
|
||||
Many ``DeprecationWarning`` will be raised if we start raising
|
||||
the warning by default. It will be too noisy.
|
||||
Many ``DeprecationWarning`` will be emitted if we start emitting the
|
||||
warning by default. It will be too noisy.
|
||||
|
||||
While this PEP doesn't cover the change, this PEP will help to
|
||||
reduce the number of ``DeprecationWarning`` in the future.
|
||||
Although this PEP doesn't propose to change the default encoding,
|
||||
this PEP will help to reduce the warning in the future if we decide
|
||||
to change the default encoding.
|
||||
|
||||
|
||||
Specification
|
||||
|
@ -75,8 +76,8 @@ Specification
|
|||
--------------------
|
||||
|
||||
Add new ``EncodingWarning`` warning class which is a subclass of
|
||||
``Warning``. It is used to warn when ``encoding`` option is omitted
|
||||
and the default encoding is locale-specific.
|
||||
``Warning``. It is used to warn when the ``encoding`` option is
|
||||
omitted and the default encoding is locale-specific.
|
||||
|
||||
|
||||
Options to enable the warning
|
||||
|
@ -86,11 +87,12 @@ Options to enable the warning
|
|||
environment variable are added. They are used to enable the
|
||||
``EncodingWarning``.
|
||||
|
||||
``sys.flags.encoding_warning`` is also added. It is a boolean flag
|
||||
represents ``EncodingWarning`` is enabled.
|
||||
``sys.flags.encoding_warning`` is also added. The flag represents
|
||||
``EncodingWarning`` is enabled.
|
||||
|
||||
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
|
||||
|
@ -120,7 +122,7 @@ supported too. For example,
|
|||
-----------------------
|
||||
|
||||
``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()``.
|
||||
|
||||
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:
|
||||
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
|
||||
to ``io.LOCALE_ENCODING``. In other words, subprocess module doesn't
|
||||
emit the ``EncodingWarning``.
|
||||
Affected stdlibs
|
||||
-------------------
|
||||
|
||||
The default encoding for PIPE is relating to the encoding of the
|
||||
stdio than the default encoding of ``TextIOWrapper``. So this PEP
|
||||
doesn't propose to emit the warning for pipes.
|
||||
Many stdlibs will be affected by this change.
|
||||
|
||||
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
|
||||
|
@ -174,11 +184,12 @@ Rationale
|
|||
Opt-in warning
|
||||
---------------
|
||||
|
||||
Although ``DeprecationWarning`` is supressed by default, emitting
|
||||
``DeprecationWarning`` alwasy when ``encoding`` option is omitted
|
||||
Although ``DeprecationWarning`` is suppressed by default, emitting
|
||||
``DeprecationWarning`` always when ``encoding`` option is omitted
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue