-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
gh-148603: Update docs now UTF-8 is default (PEP 686) #157372
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -115,34 +115,21 @@ The raw stream API is described in detail in the docs of :class:`RawIOBase`. | |
|
|
||
| .. _io-text-encoding: | ||
|
|
||
| Text Encoding | ||
| Text encoding | ||
| ------------- | ||
|
|
||
| The default encoding of :class:`TextIOWrapper` and :func:`open` is | ||
| locale-specific (:func:`locale.getencoding`). | ||
|
|
||
| However, many developers forget to specify the encoding when opening text files | ||
| encoded in UTF-8 (e.g. JSON, TOML, Markdown, etc...) since most Unix | ||
| platforms use UTF-8 locale by default. This causes bugs because the locale | ||
| 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() | ||
|
|
||
| Accordingly, it is highly recommended that you specify the encoding | ||
| explicitly when opening text files. If you want to use UTF-8, pass | ||
| ``encoding="utf-8"``. To use the current locale encoding, | ||
| ``encoding="locale"`` is supported since Python 3.10. | ||
| The default encoding of :class:`TextIOWrapper` and :func:`open` is UTF-8. | ||
| If :ref:`Python UTF-8 Mode <utf8-mode>` is disabled, the default encoding | ||
| is locale-specific (:func:`locale.getencoding`). | ||
|
|
||
| .. seealso:: | ||
|
|
||
| :ref:`utf8-mode` | ||
| Python UTF-8 Mode can be used to change the default encoding to | ||
| UTF-8 from locale-specific encoding. | ||
| Python UTF-8 Mode ignores the locale encoding and forces the use | ||
| of UTF-8. | ||
|
|
||
| :pep:`686` | ||
| Python 3.15 will make :ref:`utf8-mode` default. | ||
| Python 3.15 made :ref:`utf8-mode` the default. | ||
|
|
||
| .. _io-encoding-warning: | ||
|
|
||
|
|
@@ -152,7 +139,7 @@ Opt-in EncodingWarning | |
| .. versionadded:: 3.10 | ||
| See :pep:`597` for more details. | ||
|
|
||
| To find where the default locale encoding is used, you can enable | ||
| To find where the default encoding is used, you can enable | ||
| the :option:`-X warn_default_encoding <-X>` command line option or set the | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @methane: It now looks wrong to emit EncodingWarning if open() is called with no encoding argument, since it defaults to UTF-8 which is fine, no? For me, the purpose of PEP 597 "Add optional EncodingWarning" is to detect when platform dependent locale encoding is used. Am I wrong? At least, I see one advantage of the current behavior, it helps writing new code which is not platform dependent when running on older Python versions.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Considering that some people may not start thinking about enabling UTF-8 Mode until Python 3.15, I think removing
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since |
||
| :envvar:`PYTHONWARNDEFAULTENCODING` environment variable, which will | ||
| emit an :exc:`EncodingWarning` when the default encoding is used. | ||
|
|
@@ -165,7 +152,7 @@ please consider using UTF-8 by default (i.e. ``encoding="utf-8"``) for | |
| new APIs. | ||
|
|
||
|
|
||
| High-level Module Interface | ||
| High-level module interface | ||
| --------------------------- | ||
|
|
||
| .. data:: DEFAULT_BUFFER_SIZE | ||
|
|
@@ -315,7 +302,7 @@ ABC Inherits Stub Methods Mixin M | |
| ========================= ================== ======================== ================================================== | ||
|
|
||
|
|
||
| I/O Base Classes | ||
| I/O base classes | ||
| ^^^^^^^^^^^^^^^^ | ||
|
|
||
| .. class:: IOBase | ||
|
|
@@ -660,7 +647,7 @@ I/O Base Classes | |
| so the implementation should only access *b* during the method call. | ||
|
|
||
|
|
||
| Raw File I/O | ||
| Raw file I/O | ||
| ^^^^^^^^^^^^ | ||
|
|
||
| .. class:: FileIO(name, mode='r', closefd=True, opener=None) | ||
|
|
@@ -728,7 +715,7 @@ Raw File I/O | |
| given in the constructor. | ||
|
|
||
|
|
||
| Buffered Streams | ||
| Buffered streams | ||
| ^^^^^^^^^^^^^^^^ | ||
|
|
||
| Buffered I/O streams provide a higher-level interface to an I/O device | ||
|
|
@@ -1004,8 +991,8 @@ Text I/O | |
| :class:`TextIOBase`. | ||
|
|
||
| *encoding* gives the name of the encoding that the stream will be decoded or | ||
| encoded with. In :ref:`UTF-8 Mode <utf8-mode>`, this defaults to UTF-8. | ||
| Otherwise, it defaults to :func:`locale.getencoding`. | ||
| encoded with. This defaults to UTF-8; if :ref:`UTF-8 Mode <utf8-mode>` is | ||
| disabled, it defaults to :func:`locale.getencoding`. | ||
| ``encoding="locale"`` can be used to specify the current locale's encoding | ||
| explicitly. See :ref:`io-text-encoding` for more information. | ||
|
|
||
|
|
@@ -1187,7 +1174,7 @@ Text I/O | |
| It inherits from :class:`codecs.IncrementalDecoder`. | ||
|
|
||
|
|
||
| Static Typing | ||
| Static typing | ||
| ------------- | ||
|
|
||
| The following protocols can be used for annotating function and method | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.