PEP 686 – Make UTF-8 mode default | peps.python.org (2024)

Author:
Inada Naoki <songofacandy at gmail.com>
Discussions-To:
Discourse thread
Status:
Accepted
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
  • Motivation
  • Specification
    • Enable UTF-8 mode by default
    • locale.getencoding()
    • Fixing encoding="locale" option
  • Backward Compatibility
  • Preceding examples
  • Rejected Alternative
    • Deprecate implicit encoding
    • Use PYTHONIOENCODING for PIPEs
  • How to teach this
  • Copyright

Abstract

This PEP proposes enabling UTF-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 specify encoding="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 setting PYTHONUTF8=0 or-X utf8=0.

locale.getencoding()

Since UTF-8 mode affects locale.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.

When warn_default_encoding option is specified,locale.getpreferredencoding() will emit EncodingWarning likeopen() (see also PEP 597).

This API was added in Python 3.11.

Fixing encoding="locale" option

PEP 597 added the encoding="locale" option to the TextIOWrapper.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.

But TextIOWrapper uses "UTF-8" in UTF-8 mode even ifencoding="locale" is specified for now.This behavior is inconsistent with the PEP 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 when encoding="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. Use EncodingWarning (PEP 597) to find every places UTF-8 modeaffects.
    • If encoding option is omitted, consider using encoding="utf-8"or encoding="locale".
    • If locale.getpreferredencoding() is used, consider using"utf-8" or locale.getencoding().
  3. Test the application with UTF-8 mode.

Preceding examples

  • Ruby changed the default external_encodingto UTF-8 on Windows in Ruby 3.0 (2020).
  • Java changed 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 like PEP 597’s EncodingWarningin 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 the encoding everywhere is too painful.Emitting a lot of DeprecationWarning will lead users ignore warnings.

PEP 387 requires adding a warning for backward incompatible changes.But it doesn’t require using DeprecationWarning.So using optional EncodingWarning doesn’t violate the PEP 387.

Java also rejected this idea in JEP 400.

Use PYTHONIOENCODING for PIPEs

To ease backward compatibility issue, using PYTHONIOENCODING as thedefault encoding of PIPEs in the subprocess 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 with encoding="utf-8" or encoding="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 the Backward compatibility section.

Copyright

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

PEP 686 – Make UTF-8 mode default | peps.python.org (2024)
Top Articles
Best Blockchain Technology Courses in India, with Fees & Duration: 2024
Memory vs. Storage: A Look at What They Are and What They Do
Lowe's Garden Fence Roll
Moon Stone Pokemon Heart Gold
Tesla Supercharger La Crosse Photos
Craigslist Parsippany Nj Rooms For Rent
Practical Magic 123Movies
Tlc Africa Deaths 2021
Learn How to Use X (formerly Twitter) in 15 Minutes or Less
About Goodwill – Goodwill NY/NJ
Gina's Pizza Port Charlotte Fl
Valentina Gonzalez Leaked Videos And Images - EroThots
World Cup Soccer Wiki
Robot or human?
Bjork & Zhulkie Funeral Home Obituaries
Nick Pulos Height, Age, Net Worth, Girlfriend, Stunt Actor
Dallas Craigslist Org Dallas
Grimes County Busted Newspaper
Ford F-350 Models Trim Levels and Packages
8005607994
Craigslist Pennsylvania Poconos
Suspiciouswetspot
fft - Fast Fourier transform
Timeline of the September 11 Attacks
Meta Carevr
Cowboy Pozisyon
Pacman Video Guatemala
Maisons près d'une ville - Štanga - Location de vacances à proximité d'une ville - Štanga | Résultats 201
Login.castlebranch.com
Uncovering the Enigmatic Trish Stratus: From Net Worth to Personal Life
Insidious 5 Showtimes Near Cinemark Southland Center And Xd
Tu Housing Portal
Nurofen 400mg Tabletten (24 stuks) | De Online Drogist
King Soopers Cashiers Check
Ilabs Ucsf
Average weekly earnings in Great Britain
Joplin Pets Craigslist
Exploring TrippleThePotatoes: A Popular Game - Unblocked Hub
Watchdocumentaries Gun Mayhem 2
Keeper Of The Lost Cities Series - Shannon Messenger
Enjoy4Fun Uno
Pokemon Reborn Locations
Gun Mayhem Watchdocumentaries
M Life Insider
Gravel Racing
Sun Tracker Pontoon Wiring Diagram
Unit 11 Homework 3 Area Of Composite Figures
The Complete Uber Eats Delivery Driver Guide:
Dicks Mear Me
Makes A Successful Catch Maybe Crossword Clue
Treatise On Jewelcrafting
Fahrpläne, Preise und Anbieter von Bookaway
Latest Posts
Article information

Author: Lidia Grady

Last Updated:

Views: 5331

Rating: 4.4 / 5 (65 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.