python / python/cpython

C API: Add PyUnicodeWriter_WriteRawStr() function

未關閉
#150,973 4 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

interpreter-core topic-C-API type-feature
主要語言
Python
星號
77.2k
分支
36k
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 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

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 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

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。