You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[C API] PyUnicodeWriter_WriteStr() and str subclasses #148250
Call PyObject_Str() on obj and write the output into writer.
On strings (str objects), it works as expected. But on str subclasses, the result can be surprising if the subclass overrides __str__() to return a different string. Example with the enum module from issue gh-148241:
str() and str.__str__() return different strings (AStringEnum.A vs a). PyUnicodeWriter_WriteStr() writes AStringEnum.A because it uses str().
One way to get the a string is to call PyUnicode_FromObject() to convert the str subclass to a str object, and then call PyUnicodeWriter_WriteStr() on it.
An alternative would be to provide a new PyUnicodeWriter function to write str subclasses, but I don't think that it's worth it. So I suppose that it's just a documentation issue (the doc should suggest using PyUnicode_FromObject()).
Activity
pythongh-148250: Mention str subclasses in PyUnicodeWriter_WriteStr()…
gh-148250: Mention str subclasses in PyUnicodeWriter_WriteStr() doc (#…
pythongh-148250: Mention str subclasses in PyUnicodeWriter_WriteStr()…
[3.14] gh-148250: Mention str subclasses in PyUnicodeWriter_WriteStr(…
pythongh-148250: Mention str subclasses in PyUnicodeWriter_WriteStr()…