Skip to content

Commit d95f295

Browse files
authored
gh-148603: Update docs now UTF-8 is default (PEP 686) (#157372)
1 parent cbb43b8 commit d95f295

5 files changed

Lines changed: 54 additions & 64 deletions

File tree

Doc/builtins/functions.rst

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. XXX document all delegations to __special__ methods
22
.. _built-in-funcs:
33

4-
Built-in Functions
4+
Built-in functions
55
==================
66

77
The Python interpreter has a number of functions and types built into it that
@@ -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/csv.rst

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
:mod:`!csv` --- CSV File Reading and Writing
1+
:mod:`!csv` --- CSV file reading and writing
22
============================================
33

44
.. module:: csv
@@ -42,7 +42,7 @@ using the :class:`DictReader` and :class:`DictWriter` classes.
4242

4343
.. _csv-contents:
4444

45-
Module Contents
45+
Module contents
4646
---------------
4747

4848
The :mod:`!csv` module defines the following functions:
@@ -451,7 +451,7 @@ The :mod:`!csv` module defines the following exception:
451451

452452
.. _csv-fmt-params:
453453

454-
Dialects and Formatting Parameters
454+
Dialects and formatting parameters
455455
----------------------------------
456456

457457
To make it easier to specify the format of input and output records, specific
@@ -557,7 +557,7 @@ with the specified formatting parameters replaced.
557557

558558
.. _reader-objects:
559559

560-
Reader Objects
560+
Reader objects
561561
--------------
562562

563563
Reader objects (:class:`DictReader` instances and objects returned by the
@@ -594,7 +594,7 @@ DictReader objects have the following public attribute:
594594

595595

596596

597-
Writer Objects
597+
Writer objects
598598
--------------
599599

600600
:class:`writer` objects (:class:`DictWriter` instances and objects returned by
@@ -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/io.rst

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -115,34 +115,21 @@ The raw stream API is described in detail in the docs of :class:`RawIOBase`.
115115

116116
.. _io-text-encoding:
117117

118-
Text Encoding
118+
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.
@@ -165,7 +152,7 @@ please consider using UTF-8 by default (i.e. ``encoding="utf-8"``) for
165152
new APIs.
166153

167154

168-
High-level Module Interface
155+
High-level module interface
169156
---------------------------
170157

171158
.. data:: DEFAULT_BUFFER_SIZE
@@ -315,7 +302,7 @@ ABC Inherits Stub Methods Mixin M
315302
========================= ================== ======================== ==================================================
316303

317304

318-
I/O Base Classes
305+
I/O base classes
319306
^^^^^^^^^^^^^^^^
320307

321308
.. class:: IOBase
@@ -660,7 +647,7 @@ I/O Base Classes
660647
so the implementation should only access *b* during the method call.
661648

662649

663-
Raw File I/O
650+
Raw file I/O
664651
^^^^^^^^^^^^
665652

666653
.. class:: FileIO(name, mode='r', closefd=True, opener=None)
@@ -728,7 +715,7 @@ Raw File I/O
728715
given in the constructor.
729716

730717

731-
Buffered Streams
718+
Buffered streams
732719
^^^^^^^^^^^^^^^^
733720

734721
Buffered I/O streams provide a higher-level interface to an I/O device
@@ -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

@@ -1187,7 +1174,7 @@ Text I/O
11871174
It inherits from :class:`codecs.IncrementalDecoder`.
11881175

11891176

1190-
Static Typing
1177+
Static typing
11911178
-------------
11921179

11931180
The following protocols can be used for annotating function and method

Doc/tutorial/inputoutput.rst

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. _tut-io:
22

33
****************
4-
Input and Output
4+
Input and output
55
****************
66

77
There are several ways to present the output of a program; data can be printed
@@ -11,7 +11,7 @@ discuss some of the possibilities.
1111

1212
.. _tut-formatting:
1313

14-
Fancier Output Formatting
14+
Fancier output formatting
1515
=========================
1616

1717
So far we've encountered two ways of writing values: *expression statements* and
@@ -111,7 +111,7 @@ This syntax is easy to use, although it offers much less control for formatting.
111111

112112
.. _tut-f-strings:
113113

114-
Formatted String Literals
114+
Formatted string literals
115115
-------------------------
116116

117117
:ref:`Formatted string literals <f-strings>` (also called f-strings for
@@ -163,7 +163,7 @@ the reference guide for the :ref:`formatspec`.
163163

164164
.. _tut-string-format:
165165

166-
The String format() Method
166+
The string format() method
167167
--------------------------
168168

169169
Basic usage of the :meth:`str.format` method looks like this::
@@ -240,7 +240,7 @@ For a complete overview of string formatting with :meth:`str.format`, see
240240
:ref:`formatstrings`.
241241

242242

243-
Manual String Formatting
243+
Manual string formatting
244244
------------------------
245245

246246
Here's the same table of squares and cubes, formatted manually::
@@ -303,20 +303,19 @@ More information can be found in the :ref:`old-string-formatting` section.
303303

304304
.. _tut-files:
305305

306-
Reading and Writing Files
306+
Reading and writing files
307307
=========================
308308

309309
.. index::
310310
pair: built-in function; open
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.
@@ -389,7 +385,7 @@ automatically fail. ::
389385

390386
.. _tut-filemethods:
391387

392-
Methods of File Objects
388+
Methods of file objects
393389
-----------------------
394390

395391
The rest of the examples in this section will assume that a file object called
@@ -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: 5 additions & 4 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.
1349-
:func:`locale.getencoding`).
1348+
Page). When the :ref:`Python UTF-8 Mode <utf8-mode>` is disabled, Python
1349+
uses the ANSI Code Page as the default encoding of text files, as
1350+
returned by :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)