Skip to content

Commit 5a49cca

Browse files
bpo-39287: Doc: Add UTF-8 mode section in using/windows. (GH-17935)
Co-Authored-By: Kyle Stanley <[email protected]> (cherry picked from commit 148610d) Co-authored-by: Inada Naoki <[email protected]>
1 parent 005b059 commit 5a49cca

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

Doc/using/cmdline.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -908,8 +908,6 @@ conflict.
908908

909909
Also available as the :option:`-X` ``utf8`` option.
910910

911-
.. availability:: \*nix.
912-
913911
.. versionadded:: 3.7
914912
See :pep:`540` for more details.
915913

Doc/using/windows.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,50 @@ existed)::
605605

606606
C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Python 3.7
607607

608+
.. _win-utf8-mode:
609+
610+
UTF-8 mode
611+
==========
612+
613+
.. versionadded:: 3.7
614+
615+
Windows still uses legacy encodings for the system encoding (the ANSI Code
616+
Page). Python uses it for the default encoding of text files (e.g.
617+
:func:`locale.getpreferredencoding`).
618+
619+
This may cause issues because UTF-8 is widely used on the internet
620+
and most Unix systems, including WSL (Windows Subsystem for Linux).
621+
622+
You can use UTF-8 mode to change the default text encoding to UTF-8.
623+
You can enable UTF-8 mode via the ``-X utf8`` command line option, or
624+
the ``PYTHONUTF8=1`` environment variable. See :envvar:`PYTHONUTF8` for
625+
enabling UTF-8 mode, and :ref:`setting-envvars` for how to modify
626+
environment variables.
627+
628+
When UTF-8 mode is enabled:
629+
630+
* :func:`locale.getpreferredencoding` returns ``'UTF-8'`` instead of
631+
the system encoding. This function is used for the default text
632+
encoding in many places, including :func:`open`, :class:`Popen`,
633+
:meth:`Path.read_text`, etc.
634+
* :data:`sys.stdin`, :data:`sys.stdout`, and :data:`sys.stderr`
635+
all use UTF-8 as their text encoding.
636+
* You can still use the system encoding via the "mbcs" codec.
637+
638+
Note that adding ``PYTHONUTF8=1`` to the default environment variables
639+
will affect all Python 3.7+ applications on your system.
640+
If you have any Python 3.7+ applications which rely on the legacy
641+
system encoding, it is recommended to set the environment variable
642+
temporarily or use the ``-X utf8`` command line option.
643+
644+
.. note::
645+
Even when UTF-8 mode is disabled, Python uses UTF-8 by default
646+
on Windows for:
647+
648+
* Console I/O including standard I/O (see :pep:`528` for details).
649+
* The filesystem encoding (see :pep:`529` for details).
650+
651+
608652
.. _launcher:
609653

610654
Python Launcher for Windows

0 commit comments

Comments
 (0)