Movatterモバイル変換


[0]ホーム

URL:


Following system colour schemeSelected dark colour schemeSelected light colour scheme

Python Enhancement Proposals

PEP 686 – Make UTF-8 mode default

PEP 686 – Make UTF-8 mode default

Author:
Inada Naoki <songofacandy at gmail.com>
Discussions-To:
Discourse thread
Status:
Final
Type:
Standards Track
Created:
18-Mar-2022
Python-Version:
3.15
Post-History:
18-Mar-2022,31-Mar-2022
Resolution:
Discourse message

Table of Contents

Abstract

This PEP proposes enablingUTF-8 mode by default.

With this change, Python consistently uses UTF-8 for default encoding offiles, stdio, and pipes.

Motivation

UTF-8 becomes de facto standard text encoding.

  • The default encoding of Python source files is UTF-8.
  • JSON, TOML, YAML use UTF-8.
  • Most text editors, including Visual Studio Code and Windows Notepad useUTF-8 by default.
  • Most websites and text data on the internet use UTF-8.
  • And many other popular programming languages, including Node.js, Go, Rust,and Java uses UTF-8 by default.

Changing the default encoding to UTF-8 makes it easier for Python tointeroperate with them.

Additionally, many Python developers using Unix forget that the defaultencoding is platform dependent.They omit to specifyencoding="utf-8" when they read text files encodedin UTF-8 (e.g. JSON, TOML, Markdown, and Python source files).Inconsistent default encoding causes many bugs.

Specification

Enable UTF-8 mode by default

Python will enable UTF-8 mode by default from Python 3.15.

Users can still disable UTF-8 mode by settingPYTHONUTF8=0 or-Xutf8=0.

locale.getencoding()

Since UTF-8 mode affectslocale.getpreferredencoding(False),we need an API to get locale encoding regardless of UTF-8 mode.

locale.getencoding() will be added for this purpose.It returns locale encoding too, but ignores UTF-8 mode.

Whenwarn_default_encoding option is specified,locale.getpreferredencoding() will emitEncodingWarning likeopen() (see alsoPEP 597).

This API was added in Python 3.11.

Fixingencoding="locale" option

PEP 597 added theencoding="locale" option to theTextIOWrapper.This option is used to specify the locale encoding explicitly.TextIOWrapper should use locale encoding when the option is specified,regardless of default text encoding.

ButTextIOWrapper uses"UTF-8" in UTF-8 mode even ifencoding="locale" is specified for now.This behavior is inconsistent with thePEP 597 motivation.It is because we didn’t expect making UTF-8 mode default when Pythonchanges its default text encoding.

This inconsistency should be fixed before making UTF-8 mode default.TextIOWrapper should use locale encoding whenencoding="locale" ispassed even in UTF-8 mode.

This issue was fixed in Python 3.11.

Backward Compatibility

Most Unix systems use UTF-8 locale and Python enables UTF-8 mode when itslocale is C or POSIX.So this change mostly affects Windows users.

When a Python program depends on the default encoding, this change may causeUnicodeError, mojibake, or even silent data corruption.So this change should be announced loudly.

This is the guideline to fix this backward compatibility issue:

  1. Disable UTF-8 mode.
  2. UseEncodingWarning (PEP 597) to find every places UTF-8 modeaffects.
    • Ifencoding option is omitted, consider usingencoding="utf-8"orencoding="locale".
    • Iflocale.getpreferredencoding() is used, consider using"utf-8" orlocale.getencoding().
  3. Test the application with UTF-8 mode.

Preceding examples

  • Rubychanged the defaultexternal_encodingto UTF-8 on Windows in Ruby 3.0 (2020).
  • Javachanged the default text encodingto UTF-8 in JDK 18. (2022).

Both Ruby and Java have an option for backward compatibility.They don’t provide any warning likePEP 597’sEncodingWarningin Python for use of the default encoding.

Rejected Alternative

Deprecate implicit encoding

Deprecating the use of the default encoding is considered.

But there are many cases that the default encoding is used for reading/writingonly ASCII text.Additionally, such warnings are not useful for non-cross platform applicationsrun on Unix.

So forcing users to specify theencoding everywhere is too painful.Emitting a lot ofDeprecationWarning will lead users ignore warnings.

PEP 387 requires adding a warning for backward incompatible changes.But it doesn’t require usingDeprecationWarning.So using optionalEncodingWarning doesn’t violate thePEP 387.

Java also rejected this idea inJEP 400.

UsePYTHONIOENCODING for PIPEs

To ease backward compatibility issue, usingPYTHONIOENCODING as thedefault encoding of PIPEs in thesubprocess module is considered.

With this idea, users can use legacy encoding forsubprocess.Popen(text=True) even in UTF-8 mode.

But this idea makes “default encoding” complicated.And this idea is also backward incompatible.

So this idea is rejected. Users can disable UTF-8 mode until they replacetext=True withencoding="utf-8" orencoding="locale".

How to teach this

For new users, this change reduces things that need to teach.Users don’t need to learn about text encoding in their first year.They should learn it when they need to use non-UTF-8 text files.

For existing users, see theBackward compatibility section.

Copyright

This document is placed in the public domain or under theCC0-1.0-Universal license, whichever is more permissive.


Source:https://github.com/python/peps/blob/main/peps/pep-0686.rst

Last modified:2026-01-19 06:31:38 GMT


[8]ページ先頭

©2009-2026 Movatter.jp