Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
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
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
a3c014b050bd1b939f4a03c997774016278c5c556cd9a08c2772648e1a8e30520966cd2b80f42760308ca95dff231fb411096a0a399fc9386fdbcbc3f362bc674feffd9d850f16463ea412d633ee883d16a15e2a5d474b4c17016f03f971c9d26b7a60e74cfa505b5fcbe22e2a9f9f043bea88fd260a4c049a269018ba643a9623e737059e6a6221154c7dc65b2830b14f2a6e06e2a3227d49d280f46446ad0e7f73b27f1c149d654eb7655e3bce76c089fd7File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -692,9 +692,7 @@ The following exceptions are used as warning categories; see the | ||
| Base class for warnings about encodings. | ||
| ||
| See :ref:`io-encoding-warning` for details. | ||
| .. versionadded:: 3.10 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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:: | ||
methane marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| # may not work on Windows when non-ASCII characters in the file. | ||
methane marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| 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. | ||
methane marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| 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. | ||
methane marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| 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>` | ||
methane marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| .. _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. | ||
methane marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| 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"``). | ||
methane marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| High-level Module Interface | ||
| --------------------------- | ||
| @@ -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``argument. | ||
| This function returns *encoding* if it is not ``None`` and "locale" if | ||
| *encoding* is ``None``. | ||
| @@ -164,7 +212,7 @@ High-level Module Interface | ||
| ``read_text()``. If *stacklevel* is greater than 1, more stack frames are | ||
| skipped. | ||
| See :ref:`io-default-encoding` for more information. | ||
| .. versionadded:: 3.10 | ||
| @@ -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>`. | ||
| ``encoding="locale"`` can be used to specify the locale specific encoding | ||
| explicitly. See :ref:`io-text-encoding` for more information. | ||
methane marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| *errors* is an optional string that specifies how encoding and decoding | ||
| errors are to be handled. Pass ``'strict'`` to raise a :exc:`ValueError` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 the | ||
| locale-specificdefault encoding isused. | ||
methane marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| See also :envvar:`PYTHONWARNDEFAULTENCODING`. | ||
| It also allows passing arbitrary values and retrieving them through the | ||
| @@ -916,22 +916,9 @@ conflict. | ||
| .. envvar:: PYTHONWARNDEFAULTENCODING | ||
| If this environment variable is set to a non-empty string, issue a | ||
| :class:`EncodingWarning` when the locale-specific default encoding is used. | ||
| See :ref:`io-encoding-warning` for details. | ||
| .. versionadded:: 3.10 | ||