C API: Add PyUnicodeWriter_WriteRawStr() function
Nobody has claimed this yet.
- 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
strsubclass 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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