Skip to content

gh-63299: Escape unencodable characters in pprint() - #155192

Open
serhiy-storchaka wants to merge 2 commits into
python:mainfrom
serhiy-storchaka:gh-63299-pprint-backslashreplace
Open

gh-63299: Escape unencodable characters in pprint()#155192
serhiy-storchaka wants to merge 2 commits into
python:mainfrom
serhiy-storchaka:gh-63299-pprint-backslashreplace

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

pprint.pp(), pprint.pprint() and PrettyPrinter.pprint() failed with UnicodeEncodeError if the output could not be encoded in the encoding of the output stream. The output is intended to be read by humans, so it is better to escape unencodable characters than to fail. They are now escaped with backslashes, as sys.displayhook does.

The stream is only wrapped if it encodes the written text and uses the strict error handler, so streams which do not encode the text (like io.StringIO) and streams with their own error handler are unaffected. pformat() and saferepr(), which return strings, are unaffected too.

pprint.pp(), pprint.pprint() and PrettyPrinter.pprint() failed with
UnicodeEncodeError if the output could not be encoded in the encoding of
the output stream.  Such characters are now escaped with backslashes, as
sys.displayhook does.

The stream is only wrapped if it encodes the written text and uses the
strict error handler.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@serhiy-storchaka serhiy-storchaka added stdlib Standard Library Python modules in the Lib/ directory topic-unicode type-bug An unexpected behavior, bug, or error labels Aug 4, 2026
@read-the-docs-community

read-the-docs-community Bot commented Aug 4, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33922689 | 📁 Comparing 61c0946 against main (30ac23e)

  🔍 Preview build  

3 files changed
± library/os.html
± library/pprint.html
± whatsnew/changelog.html

TextIOWrapper translates "\n" to os.linesep if newline is None.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review stdlib Standard Library Python modules in the Lib/ directory topic-unicode type-bug An unexpected behavior, bug, or error

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant