python / python/cpython

C API: Add PyUnicodeWriter_WriteRawStr() function

Open
#150,973 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core topic-C-API type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Feature or enhancement

Python 3.14 added PyUnicodeWriter_WriteStr() function. In general, writing str(obj) is fine. But there is one exception: writing a str subclass should not call str(obj), otherwise the output can be surprising if the subclass overrides __str__().

The _json and _io were impacted by the str subclass issue. They have been fixed by using the internal C API (_PyUnicodeWriter_WriteStr()). Problem: the internal C API is not usable outside CPython.

The PyUnicodeWriter_WriteStr() documentation has been modified to document a workaround for str subclasses:

To write a str subclass which overrides the __str__() method, PyUnicode_FromObject() can be used to get the original string.

Instead of the workaround, I propose adding a PyUnicodeWriter_WriteRawStr() function which handles str subclasses as expected (read the string content, don't use the __str__() method).

Linked PRs
  • gh-150974

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review linked PR gh-150974 first, then read the existing PyUnicodeWriter_WriteStr documentation and the _json and _io references to _PyUnicodeWriter_WriteStr. Done means a public PyUnicodeWriter_WriteRawStr() API handles str subclasses without invoking str(), with the documented behavior available outside CPython internals.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.