Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

bpo-43510: Implement PEP 597 opt-in EncodingWarning.#19481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
methane merged 51 commits intopython:masterfrommethane:open-encoding
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
51 commits
Select commitHold shift + click to select a range
a3c014b
Raise a warning when encoding is omitted
methaneApr 7, 2020
050bd1b
add test
methaneApr 12, 2020
939f4a0
wrap encoding=None with text_encoding.
methaneApr 12, 2020
3c99777
Add io.LOCALE_ENCODING = "locale"
methaneJan 29, 2021
4016278
Add EncodingWarning.
methaneJan 29, 2021
c5c556c
Add sys.warn_default_encoding
methaneJan 29, 2021
d9a08c2
shorten option names
methaneJan 30, 2021
772648e
EncodingWarning extends Warning
methaneJan 30, 2021
1a8e305
make clinic
methaneJan 30, 2021
20966cd
fix test
methaneJan 30, 2021
2b80f42
remove wrong test case
methaneJan 30, 2021
760308c
fix exception_hierarchy.txt
methaneJan 30, 2021
a95dff2
Make sys.flags.encoding_warning int
methaneJan 31, 2021
31fb411
Fix text_embed.
methaneJan 31, 2021
096a0a3
Fix test_pickle
methaneJan 31, 2021
99fc938
configparser: use io.text_encoding()
methaneFeb 13, 2021
6fdbcbc
Rename option names
methaneFeb 22, 2021
3f362bc
Merge remote-tracking branch 'upstream/master' into open-encoding
methaneMar 16, 2021
674feff
Update docs
methaneMar 16, 2021
d9d850f
Add NEWS entry
methaneMar 16, 2021
16463ea
Add document for text_encoding and encoding="locale".
methaneMar 17, 2021
412d633
Suppress EncodingWarning from site.py
methaneMar 17, 2021
ee883d1
Remove io.LOCALE_ENCODING
methaneMar 18, 2021
6a15e2a
text_encoding() first argument is mandatory.
methaneMar 18, 2021
5d474b4
Apply suggestions from code review
methaneMar 18, 2021
c17016f
Simplify _PyPreCmdline and PyConfig
methaneMar 18, 2021
03f971c
Update EncodingWarning doc
methaneMar 18, 2021
9d26b7a
Update document
methaneMar 19, 2021
60e74cf
tweak warning message
methaneMar 19, 2021
a505b5f
Use stacklevel=2 for text_encoding() default
methaneMar 19, 2021
cbe22e2
fixup
methaneMar 19, 2021
a9f9f04
tweak for readability
methaneMar 19, 2021
3bea88f
make clinic
methaneMar 19, 2021
d260a4c
fix doc build error
methaneMar 19, 2021
049a269
tweak warning message
methaneMar 19, 2021
018ba64
fixup
methaneMar 19, 2021
3a9623e
Fix subprocess
methaneMar 23, 2021
737059e
Update Doc/library/io.rst
methaneMar 23, 2021
6a62211
Update Doc/library/io.rst
methaneMar 23, 2021
54c7dc6
Update Doc/library/io.rst
methaneMar 23, 2021
5b2830b
Update Doc/library/io.rst
methaneMar 23, 2021
14f2a6e
Apply suggestions from code review
methaneMar 23, 2021
06e2a32
Move EncodingWarnings
methaneMar 23, 2021
27d49d2
fix comment
methaneMar 23, 2021
80f4644
fix text_encoding() docstring
methaneMar 23, 2021
6ad0e7f
update what's new
methaneMar 23, 2021
73b27f1
fix doc build
methaneMar 23, 2021
c149d65
Update Doc/library/io.rst
methaneMar 24, 2021
4eb7655
Apply suggestions from code review
methaneMar 24, 2021
e3bce76
Apply suggestions from code review
methaneMar 24, 2021
c089fd7
Update Doc/library/io.rst
methaneMar 24, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
Update document
  • Loading branch information
@methane
methane committedMar 19, 2021
commit9d26b7a047493ddd0b02f297ed6683adcac62a55
4 changes: 2 additions & 2 deletionsDoc/c-api/init_config.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -586,8 +586,8 @@ PyConfig
.. c:member:: int warn_default_encoding

If non-zero, emit a :exc:`EncodingWarning` warning when :class:`io.TextIOWrapper`
uses its default encoding. See :pep:`597` fordetail.
uses its default encoding. See :ref:`io-encoding-warning` fordetails.

Default: ``0``.

.. versionadded:: 3.10
Expand Down
4 changes: 1 addition & 3 deletionsDoc/library/exceptions.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -692,9 +692,7 @@ The following exceptions are used as warning categories; see the

Base class for warnings about encodings.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

You might want to be more specific here, considering that (AFAIK) this isnot the base class forUnicodeWarning, which also has to do with encodings (yes, Unicode is a character set, not an encoding, but if developers were knowledgeable enough to understand that distinction, they certainly would know to specify their encoding and we wouldn't have this problem ^_^).


:class:`io.TextIOWrapper`, :func:`open`, and other functions having
``encoding=None`` option and locale specific default encoding may emit this
warning. See :pep:`597` for more details.
See :ref:`io-encoding-warning` for details.

.. versionadded:: 3.10

Expand Down
59 changes: 52 additions & 7 deletionsDoc/library/io.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -106,6 +106,54 @@ stream by opening a file in binary mode with buffering disabled::
The raw stream API is described in detail in the docs of :class:`RawIOBase`.


.. _io-text-encoding:

Text Encoding
-------------

The default encoding of :class:`TextIOWrapper` and :func:`open` is
locale-specific. (:func:`locale.getpreferredencoding(False) <locale.getpreferredencoding>`)

But many developers forget to specify encoding when opening text files
encoded in UTF-8 (e.g. JSON, TOML, Markdown, etc...) since most Unix
platforms uses UTF-8 locale by default. It cause a bug because locale-specific
encoding is not UTF-8 for most Windows users. For example::

# may not work on Windows when non-ASCII characters in the file.
with open("README.md") as f:
long_description = f.read()

Additionally, Python may change the default text encoding to UTF-8 in the
future, although there is no plan yet.

So it is highly recommended to specify encoding explicitly when opening text
files. If you want to use UTF-8, specify ``encoding="utf-8"``. If you need to
use locale-specific encoding, ``encoding="locale"`` is supported since Python
3.10.

When you need to run code using the default encoding to open UTF-8 files on
Windows, you can enable the UTF-8 mode. See also the
:ref:`UTF-8 mode on Windows <win-utf8-mode>`

.. _io-encoding-warning:

Opt-in EncodingWarning
^^^^^^^^^^^^^^^^^^^^^^

.. versionadded:: 3.10
See :pep:`597` for more details.

To find where the default encoding is used, you can use
a ``-X warn_default_encoding`` command line argument or a
:envvar:`PYTHONWARNDEFAULTENCODING` environment variable to emit
an :exc:`EncodingWarning` when the defaut encoding is used.

If you are providing APIs using :func:`open` or :class:`TextIOWrapper` and
having ``encoding=None`` parameter, you can use :func:`text_encoding` to emit
an :exc:`EncodingWarning` to the user too. But please consider using UTF-8
by default (i.e. ``encoding="utf-8"``).


High-level Module Interface
---------------------------

Expand DownExpand Up@@ -146,7 +194,7 @@ High-level Module Interface
.. function:: text_encoding(encoding, stacklevel=1)

This is a helper function for functions that use :func:`open` or
:class:`TextIOWrapper` and take ``encoding=None``option.
:class:`TextIOWrapper` and take ``encoding=None``argument.

This function returns *encoding* if it is not ``None`` and "locale" if
*encoding* is ``None``.
Expand All@@ -164,7 +212,7 @@ High-level Module Interface
``read_text()``. If *stacklevel* is greater than 1, more stack frames are
skipped.

See :envvar:`PYTHONWARNDEFAULTENCODING` and :pep:`597` for more information.
See :ref:`io-default-encoding` for more information.

.. versionadded:: 3.10

Expand DownExpand Up@@ -905,11 +953,8 @@ Text I/O
*encoding* gives the name of the encoding that the stream will be decoded or
encoded with. It defaults to
:func:`locale.getpreferredencoding(False) <locale.getpreferredencoding>`.

If ``sys.flags.warn_default_encoding`` is true and the default encoding
is used, this function emits an :class:`EncodingWarning`. You can suppress
the warning by using ``encoding="locale"`` option.
See :envvar:`PYTHONWARNDEFAULTENCODING` and :pep:`597` for more information.
``encoding="locale"`` can be used to specify the locale specific encoding
explicitly. See :ref:`io-text-encoding` for more information.

*errors* is an optional string that specifies how encoding and decoding
errors are to be handled. Pass ``'strict'`` to raise a :exc:`ValueError`
Expand Down
21 changes: 4 additions & 17 deletionsDoc/using/cmdline.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -453,8 +453,8 @@ Miscellaneous options
* ``-X pycache_prefix=PATH`` enables writing ``.pyc`` files to a parallel
tree rooted at the given directory instead of to the code tree. See also
:envvar:`PYTHONPYCACHEPREFIX`.
* ``-X warn_default_encoding`` issues a :class:`EncodingWarning` when
an ``encoding`` option is omitted and thedefault encoding islocale-specific.
* ``-X warn_default_encoding`` issues a :class:`EncodingWarning` when the
locale-specificdefault encoding isused.
See also :envvar:`PYTHONWARNDEFAULTENCODING`.

It also allows passing arbitrary values and retrieving them through the
Expand DownExpand Up@@ -916,22 +916,9 @@ conflict.
.. envvar:: PYTHONWARNDEFAULTENCODING

If this environment variable is set to a non-empty string, issue a
:class:`EncodingWarning` when an ``encoding`` option is omitted and
the default encoding is locale-specific.
:class:`EncodingWarning` when the locale-specific default encoding is used.

This option can be used to find bugs caused by not passing
``encoding="utf8"`` option. For example::

# This code may cause UnicodeDecodeError on Windows.
# encoding="utf8" or "b" mode must be used.
with open(path) as f:
data = json.load(f)

``encoding="locale"`` option can be used to specify locale-specific
encoding explicitly since Python 3.10. Python won't issue a
:class:`EncodingWarning` for it.

See :pep:`597` for detail.
See :ref:`io-encoding-warning` for details.

.. versionadded:: 3.10

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp