2019-06-05 08:09:19 -04:00
|
|
|
PEP: 597
|
2020-04-16 19:34:21 -04:00
|
|
|
Title: Soft deprecation of omitting encoding
|
|
|
|
Last-Modified: 14-Apr-2020
|
2019-06-05 08:09:19 -04:00
|
|
|
Author: Inada Naoki <songofacandy@gmail.com>
|
2020-04-16 19:34:21 -04:00
|
|
|
Discussions-To: https://discuss.python.org/t/3880
|
2019-06-05 08:09:19 -04:00
|
|
|
Status: Draft
|
|
|
|
Type: Standards Track
|
|
|
|
Content-Type: text/x-rst
|
|
|
|
Created: 05-Jun-2019
|
2020-04-16 19:34:21 -04:00
|
|
|
Python-Version: 3.9
|
2019-06-05 08:09:19 -04:00
|
|
|
|
|
|
|
|
|
|
|
Abstract
|
|
|
|
========
|
|
|
|
|
2020-04-16 19:34:21 -04:00
|
|
|
This PEP proposes:
|
2019-06-11 23:22:09 -04:00
|
|
|
|
2020-04-16 19:34:21 -04:00
|
|
|
* ``TextIOWrapper`` raises a ``PendingDeprecationWarning`` when the
|
|
|
|
``encoding`` option is not specified, and dev mode is enabled.
|
|
|
|
|
|
|
|
* Add ``encoding="locale"`` option to ``TextIOWrapper``. It behaves
|
|
|
|
like ``encoding=None`` but don't raise a warning.
|
2019-06-05 08:09:19 -04:00
|
|
|
|
|
|
|
|
|
|
|
Motivation
|
|
|
|
==========
|
|
|
|
|
2020-04-16 19:34:21 -04:00
|
|
|
Omitting encoding is common mistake
|
|
|
|
------------------------------------
|
2019-06-11 23:22:09 -04:00
|
|
|
|
2020-02-04 04:35:06 -05:00
|
|
|
Developers using macOS or Linux may forget that the default encoding
|
|
|
|
is not always UTF-8.
|
2019-06-11 23:22:09 -04:00
|
|
|
|
|
|
|
For example, ``long_description = open("README.md").read()`` in
|
2020-02-04 04:35:06 -05:00
|
|
|
``setup.py`` is a common mistake. Many Windows users can not install
|
2020-04-16 19:34:21 -04:00
|
|
|
the package if there is at least one non-ASCII character (e.g. emoji)
|
|
|
|
in the ``README.md`` file.
|
|
|
|
|
|
|
|
For example, 489 packages of the 4000 most downloaded packages from
|
|
|
|
PyPI used non-ASCII characters in README. And 82 packages of them
|
|
|
|
can not be installed from source package when locale encoding is
|
|
|
|
ASCII. [1_] They used default encoding to read README or TOML file.
|
|
|
|
|
|
|
|
Another example is ``logging.basicConfig(filename="log.txt")``.
|
|
|
|
Some users expect UTF-8 is used by default, but locale encoding is
|
|
|
|
used actually. [2_]
|
2019-06-11 23:22:09 -04:00
|
|
|
|
2020-02-04 04:35:06 -05:00
|
|
|
Even Python experts assume that default encoding is UTF-8.
|
2020-04-16 19:34:21 -04:00
|
|
|
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.
|
2019-06-11 23:22:09 -04:00
|
|
|
|
2020-04-16 19:34:21 -04:00
|
|
|
|
|
|
|
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 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.
|
|
|
|
|
|
|
|
While this PEP doesn't cover the change, this PEP will help to reduce
|
|
|
|
the number of ``DeprecationWarning`` in the future.
|
2019-06-05 08:09:19 -04:00
|
|
|
|
|
|
|
|
|
|
|
Specification
|
|
|
|
=============
|
|
|
|
|
2020-04-16 19:34:21 -04:00
|
|
|
Raising a PendingDeprecationWarning
|
|
|
|
---------------------------------------
|
|
|
|
|
|
|
|
``TextIOWrapper`` raises the ``PendingDeprecationWarning`` when the
|
|
|
|
``encoding`` option is omitted, and dev mode is enabled.
|
2019-06-11 23:22:09 -04:00
|
|
|
|
|
|
|
|
2020-04-16 19:34:21 -04:00
|
|
|
``encoding="locale"`` option
|
|
|
|
----------------------------
|
2019-06-05 08:09:19 -04:00
|
|
|
|
2020-04-16 19:34:21 -04:00
|
|
|
When ``encoding="locale"`` is specified to the ``TextIOWrapper``, it
|
|
|
|
behaves same to ``encoding=None``. In detail, the encoding is
|
|
|
|
chosen by:
|
2019-06-05 08:09:19 -04:00
|
|
|
|
2020-04-16 19:34:21 -04:00
|
|
|
1. ``os.device_encoding(buffer.fileno())``
|
|
|
|
2. ``locale.getpreferredencoding(False)``
|
2019-06-05 08:09:19 -04:00
|
|
|
|
2020-04-16 19:34:21 -04:00
|
|
|
This option can be used to suppress the ``PendingDeprecationWarning``.
|
2019-06-05 08:09:19 -04:00
|
|
|
|
2019-06-11 23:22:09 -04:00
|
|
|
|
2020-04-16 19:34:21 -04:00
|
|
|
``io.text_encoding``
|
|
|
|
--------------------
|
2019-06-11 23:22:09 -04:00
|
|
|
|
2020-04-16 19:34:21 -04:00
|
|
|
``TextIOWrapper`` is used indirectly in most cases. For example, ``open``, and ``pathlib.Path.read_text()`` use it. Warning to these
|
|
|
|
functions doesn't make sense. Callers of these functions should be warned instead.
|
2019-06-05 08:09:19 -04:00
|
|
|
|
2020-04-16 19:34:21 -04:00
|
|
|
``io.text_encoding(encoding, stacklevel=1)`` is a helper function for it.
|
|
|
|
Pure Python implementation will be like this::
|
2019-06-11 23:22:09 -04:00
|
|
|
|
2020-04-16 19:34:21 -04:00
|
|
|
def text_encoding(encoding, stacklevel=1):
|
|
|
|
"""
|
|
|
|
Helper function to choose the text encoding.
|
2019-06-11 23:22:09 -04:00
|
|
|
|
2020-04-16 19:34:21 -04:00
|
|
|
When encoding is not None, just return it.
|
|
|
|
Otherwise, return the default text encoding ("locale" for now),
|
|
|
|
and raise a PendingDeprecationWarning in dev mode.
|
2019-06-05 08:09:19 -04:00
|
|
|
|
2020-04-16 19:34:21 -04:00
|
|
|
This function can be used in APIs having encoding=None option.
|
|
|
|
But please consider encoding="utf-8" for new APIs.
|
|
|
|
"""
|
|
|
|
if encoding is None:
|
|
|
|
if sys.flags.dev_mode:
|
|
|
|
import warnings
|
|
|
|
warnings.warn(
|
|
|
|
"'encoding' option is not specified. The default encoding "
|
|
|
|
"will be changed to 'utf-8' in the future",
|
|
|
|
PendingDeprecationWarning, stacklevel + 2)
|
|
|
|
encoding = "locale"
|
|
|
|
return encoding
|
2019-06-05 08:09:19 -04:00
|
|
|
|
2020-04-16 19:34:21 -04:00
|
|
|
``pathlib.Path.read_text()`` can use this function like this::
|
2019-06-05 08:09:19 -04:00
|
|
|
|
2020-04-16 19:34:21 -04:00
|
|
|
def read_text(self, encoding=None, errors=None):
|
|
|
|
"""
|
|
|
|
Open the file in text mode, read it, and close the file.
|
|
|
|
"""
|
|
|
|
encoding = io.text_encoding(encoding)
|
|
|
|
with self.open(mode='r', encoding=encoding, errors=errors) as f:
|
|
|
|
return f.read()
|
2019-06-05 08:09:19 -04:00
|
|
|
|
|
|
|
|
2020-04-16 19:34:21 -04:00
|
|
|
subprocess module doesn't warn
|
|
|
|
------------------------------
|
|
|
|
|
|
|
|
While the subprocess module uses TextIOWrapper, it doesn't raise
|
|
|
|
``PendingDeprecationWarning``. It uses the "locale" encoding
|
|
|
|
by default.
|
|
|
|
|
|
|
|
|
|
|
|
Rationale
|
|
|
|
=========
|
|
|
|
|
|
|
|
"locale" is not a codec alias
|
|
|
|
-----------------------------
|
|
|
|
|
|
|
|
We don't add the "locale" to the codec alias because locale can be
|
|
|
|
changed in runtime.
|
|
|
|
|
|
|
|
Additionally, ``TextIOWrapper`` checks ``os.device_encoding()``
|
|
|
|
when ``encoding=None``. This behavior can not be implemented in
|
|
|
|
the codec.
|
|
|
|
|
|
|
|
|
|
|
|
Use a PendingDeprecationWarning
|
|
|
|
-------------------------------
|
|
|
|
|
|
|
|
This PEP doesn't make decision about changing default text encoding.
|
|
|
|
So we use ``PendingDeprecationWarning`` instead of ``DeprecationWarning`` for now.
|
|
|
|
|
|
|
|
|
|
|
|
Raise warning only in dev mode
|
|
|
|
------------------------------
|
|
|
|
|
|
|
|
This PEP will produce a huge amount of ``PendingDeprecationWarning``.
|
|
|
|
It will be too noisy for most Python developers.
|
|
|
|
|
|
|
|
We need to fix warnings in standard library, pip, and major dev tools
|
|
|
|
like ``pytest`` before raise this warning by default.
|
|
|
|
|
|
|
|
|
|
|
|
subprocess module doesn't warn
|
|
|
|
------------------------------
|
|
|
|
|
|
|
|
The default encoding for PIPE is relating to the encoding of the stdio.
|
|
|
|
It should be discussed later.
|
2019-06-05 08:09:19 -04:00
|
|
|
|
|
|
|
|
2019-06-11 23:22:09 -04:00
|
|
|
Reference Implementation
|
|
|
|
========================
|
2019-06-05 08:09:19 -04:00
|
|
|
|
2020-04-16 19:34:21 -04:00
|
|
|
https://github.com/python/cpython/pull/19481
|
2019-06-05 08:09:19 -04:00
|
|
|
|
|
|
|
|
|
|
|
References
|
|
|
|
==========
|
|
|
|
|
2020-04-16 19:34:21 -04:00
|
|
|
.. [1] "Packages can't be installed when encoding is not UTF-8"
|
|
|
|
(https://github.com/methane/pep597-pypi-ascii)
|
|
|
|
|
|
|
|
.. [2] "Logging - Inconsistent behaviour when handling unicode"
|
|
|
|
(https://bugs.python.org/issue37111)
|
|
|
|
|
|
|
|
.. [3] Packaging tutorial in packaging.python.org didn't specify
|
|
|
|
encoding to read a ``README.md``
|
|
|
|
(https://github.com/pypa/packaging.python.org/pull/682)
|
|
|
|
|
|
|
|
.. [4] ``json.tool`` had used locale encoding to read JSON files.
|
|
|
|
(https://bugs.python.org/issue33684)
|
2019-06-11 23:22:09 -04:00
|
|
|
|
2019-06-05 08:09:19 -04:00
|
|
|
|
|
|
|
Copyright
|
|
|
|
=========
|
|
|
|
|
|
|
|
This document has been placed in the public domain.
|
|
|
|
|
2020-04-16 19:34:21 -04:00
|
|
|
|
2019-06-05 08:09:19 -04:00
|
|
|
..
|
|
|
|
Local Variables:
|
|
|
|
mode: indented-text
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
sentence-end-double-space: t
|
|
|
|
fill-column: 70
|
|
|
|
coding: utf-8
|
|
|
|
End:
|