C API: Add PyUnicodeWriter_WriteRawStr() function
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
先查看关联的 PR gh-150974,然后阅读现有的 PyUnicodeWriter_WriteStr 文档,以及 _json 和 _io 中对 _PyUnicodeWriter_WriteStr 的引用。当公共 API PyUnicodeWriter_WriteRawStr() 能够处理 str 子类而不调用 str(),并且文档所述行为可在 CPython 内部实现之外使用时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- c, python
- 领域
- api
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 30/100