Skip to content

Commit 02ad240

Browse files
committed
Update docs now UTF-8 is default (PEP 686)
1 parent 575fe39 commit 02ad240

5 files changed

Lines changed: 34 additions & 44 deletions

File tree

Doc/library/csv.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -673,17 +673,16 @@ The corresponding simplest possible writing example is::
673673
writer.writerows(someiterable)
674674

675675
Since :func:`open` is used to open a CSV file for reading, the file
676-
will by default be decoded into unicode using the system default
677-
encoding (see :func:`locale.getencoding`). To decode a file
676+
will by default be decoded into Unicode using UTF-8. To decode a file
678677
using a different encoding, use the ``encoding`` argument of open::
679678

680679
import csv
681-
with open('some.csv', newline='', encoding='utf-8') as f:
680+
with open('some.csv', newline='', encoding='latin-1') as f:
682681
reader = csv.reader(f)
683682
for row in reader:
684683
print(row)
685684

686-
The same applies to writing in something other than the system default
685+
The same applies to writing in something other than the default
687686
encoding: specify the encoding argument when opening the output file.
688687

689688
Registering a new dialect::

Doc/library/functions.rst

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,8 @@ are always available. They are listed here in alphabetical order.
14591459
already exists), ``'x'`` for exclusive creation, and ``'a'`` for appending
14601460
(which on *some* Unix systems, means that *all* writes append to the end of
14611461
the file regardless of the current seek position). In text mode, if
1462-
*encoding* is not specified the encoding used is platform-dependent:
1462+
*encoding* is not specified, UTF-8 is used by default; if
1463+
:ref:`Python UTF-8 Mode <utf8-mode>` is disabled,
14631464
:func:`locale.getencoding` is called to get the current locale encoding.
14641465
(For reading and writing raw bytes use binary mode and leave
14651466
*encoding* unspecified.) The available modes are:
@@ -1490,7 +1491,7 @@ are always available. They are listed here in alphabetical order.
14901491
argument) return contents as :class:`bytes` objects without any decoding. In
14911492
text mode (the default, or when ``'t'`` is included in the *mode* argument),
14921493
the contents of the file are returned as :class:`str`, the bytes having been
1493-
first decoded using a platform-dependent encoding or using the specified
1494+
first decoded using the default encoding or using the specified
14941495
*encoding* if given.
14951496

14961497
.. note::
@@ -1519,9 +1520,11 @@ are always available. They are listed here in alphabetical order.
15191520
described above for binary files.
15201521

15211522
*encoding* is the name of the encoding used to decode or encode the file.
1522-
This should only be used in text mode. The default encoding is platform
1523-
dependent (whatever :func:`locale.getencoding` returns), but any
1524-
:term:`text encoding` supported by Python can be used.
1523+
This should only be used in text mode. The default encoding is UTF-8;
1524+
if :ref:`Python UTF-8 Mode <utf8-mode>` is disabled, the default is
1525+
platform-dependent (whatever :func:`locale.getencoding` returns).
1526+
Any :term:`text encoding` supported by Python can be used, and
1527+
``encoding="locale"`` specifies the current locale encoding explicitly.
15251528
See the :mod:`codecs` module for the list of supported encodings.
15261529

15271530
*errors* is an optional string that specifies how encoding and decoding
@@ -1638,6 +1641,10 @@ are always available. They are listed here in alphabetical order.
16381641
.. versionchanged:: 3.11
16391642
The ``'U'`` mode has been removed.
16401643

1644+
.. versionchanged:: 3.15
1645+
UTF-8 is now the default encoding, instead of the
1646+
platform-dependent locale encoding (:pep:`686`).
1647+
16411648
.. function:: ord(character, /)
16421649

16431650
Return the ordinal value of a character.

Doc/library/io.rst

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -118,31 +118,18 @@ The raw stream API is described in detail in the docs of :class:`RawIOBase`.
118118
Text Encoding
119119
-------------
120120

121-
The default encoding of :class:`TextIOWrapper` and :func:`open` is
122-
locale-specific (:func:`locale.getencoding`).
123-
124-
However, many developers forget to specify the encoding when opening text files
125-
encoded in UTF-8 (e.g. JSON, TOML, Markdown, etc...) since most Unix
126-
platforms use UTF-8 locale by default. This causes bugs because the locale
127-
encoding is not UTF-8 for most Windows users. For example::
128-
129-
# May not work on Windows when non-ASCII characters in the file.
130-
with open("README.md") as f:
131-
long_description = f.read()
132-
133-
Accordingly, it is highly recommended that you specify the encoding
134-
explicitly when opening text files. If you want to use UTF-8, pass
135-
``encoding="utf-8"``. To use the current locale encoding,
136-
``encoding="locale"`` is supported since Python 3.10.
121+
The default encoding of :class:`TextIOWrapper` and :func:`open` is UTF-8.
122+
If :ref:`Python UTF-8 Mode <utf8-mode>` is disabled, the default encoding
123+
is locale-specific (:func:`locale.getencoding`).
137124

138125
.. seealso::
139126

140127
:ref:`utf8-mode`
141-
Python UTF-8 Mode can be used to change the default encoding to
142-
UTF-8 from locale-specific encoding.
128+
Python UTF-8 Mode ignores the locale encoding and forces the use
129+
of UTF-8.
143130

144131
:pep:`686`
145-
Python 3.15 will make :ref:`utf8-mode` default.
132+
Python 3.15 made :ref:`utf8-mode` the default.
146133

147134
.. _io-encoding-warning:
148135

@@ -152,7 +139,7 @@ Opt-in EncodingWarning
152139
.. versionadded:: 3.10
153140
See :pep:`597` for more details.
154141

155-
To find where the default locale encoding is used, you can enable
142+
To find where the default encoding is used, you can enable
156143
the :option:`-X warn_default_encoding <-X>` command line option or set the
157144
:envvar:`PYTHONWARNDEFAULTENCODING` environment variable, which will
158145
emit an :exc:`EncodingWarning` when the default encoding is used.
@@ -1004,8 +991,8 @@ Text I/O
1004991
:class:`TextIOBase`.
1005992

1006993
*encoding* gives the name of the encoding that the stream will be decoded or
1007-
encoded with. In :ref:`UTF-8 Mode <utf8-mode>`, this defaults to UTF-8.
1008-
Otherwise, it defaults to :func:`locale.getencoding`.
994+
encoded with. This defaults to UTF-8; if :ref:`UTF-8 Mode <utf8-mode>` is
995+
disabled, it defaults to :func:`locale.getencoding`.
1009996
``encoding="locale"`` can be used to specify the current locale's encoding
1010997
explicitly. See :ref:`io-text-encoding` for more information.
1011998

Doc/tutorial/inputoutput.rst

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,11 @@ Reading and Writing Files
311311
pair: object; file
312312

313313
:func:`open` returns a :term:`file object`, and is most commonly used with
314-
two positional arguments and one keyword argument:
315-
``open(filename, mode, encoding=None)``
314+
two positional arguments: ``open(filename, mode)``
316315

317316
::
318317

319-
>>> f = open('workfile', 'w', encoding="utf-8")
318+
>>> f = open('workfile', 'w')
320319

321320
.. XXX str(f) is <io.TextIOWrapper object at 0x82e8dc4>
322321
@@ -334,10 +333,7 @@ omitted.
334333

335334
Normally, files are opened in :dfn:`text mode`, that means, you read and write
336335
strings from and to the file, which are encoded in a specific *encoding*.
337-
If *encoding* is not specified, the default is platform dependent
338-
(see :func:`open`).
339-
Because UTF-8 is the modern de-facto standard, ``encoding="utf-8"`` is
340-
recommended unless you know that you need to use a different encoding.
336+
If *encoding* is not specified, the default is UTF-8 (see :func:`open`).
341337
Appending a ``'b'`` to the mode opens the file in :dfn:`binary mode`.
342338
Binary mode data is read and written as :class:`bytes` objects.
343339
You can not specify *encoding* when opening file in binary mode.
@@ -356,7 +352,7 @@ after its suite finishes, even if an exception is raised at some
356352
point. Using :keyword:`!with` is also much shorter than writing
357353
equivalent :keyword:`try`\ -\ :keyword:`finally` blocks::
358354

359-
>>> with open('workfile', encoding="utf-8") as f:
355+
>>> with open('workfile') as f:
360356
... read_data = f.read()
361357

362358
>>> # We can check that the file has been automatically closed.
@@ -532,8 +528,8 @@ To decode the object again, if ``f`` is a :term:`binary file` or
532528
x = json.load(f)
533529

534530
.. note::
535-
JSON files must be encoded in UTF-8. Use ``encoding="utf-8"`` when opening
536-
JSON file as a :term:`text file` for both of reading and writing.
531+
JSON files must be encoded in UTF-8, the default encoding for
532+
:term:`text files <text file>`.
537533

538534
This simple serialization technique can handle lists and dictionaries, but
539535
serializing arbitrary class instances in JSON requires a bit of extra effort.

Doc/using/windows.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,14 +1345,15 @@ UTF-8 mode
13451345
Python UTF-8 mode is now enabled by default (:pep:`686`).
13461346

13471347
Windows still uses legacy encodings for the system encoding (the ANSI Code
1348-
Page). Python uses it for the default encoding of text files (e.g.
1348+
Page). When the :ref:`Python UTF-8 Mode <utf8-mode>` is disabled, Python
1349+
uses it for the default encoding of text files (for example,
13491350
:func:`locale.getencoding`).
13501351

13511352
This may cause issues because UTF-8 is widely used on the internet
13521353
and most Unix systems, including WSL (Windows Subsystem for Linux).
13531354

1354-
The :ref:`Python UTF-8 Mode <utf8-mode>`, enabled by default, can help by
1355-
changing the default text encoding to UTF-8.
1355+
The :ref:`Python UTF-8 Mode <utf8-mode>`, enabled by default, ignores the
1356+
system encoding and uses UTF-8 as the default text encoding.
13561357
When the :ref:`UTF-8 mode <utf8-mode>` is enabled, you can still use the
13571358
system encoding (the ANSI Code Page) via the "mbcs" codec.
13581359

0 commit comments

Comments
 (0)