Title: [C API] PyUnicodeWriter_WriteStr() and str subclasses · Issue #148250 · python/cpython · GitHub
Open Graph Title: [C API] PyUnicodeWriter_WriteStr() and str subclasses · Issue #148250 · python/cpython
X Title: [C API] PyUnicodeWriter_WriteStr() and str subclasses · Issue #148250 · python/cpython
Description: The PyUnicodeWriter_WriteStr() API is documented as: 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 overrid...
Open Graph Description: The PyUnicodeWriter_WriteStr() API is documented as: 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 c...
X Description: The PyUnicodeWriter_WriteStr() API is documented as: 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 c...
Opengraph URL: https://github.com/python/cpython/issues/148250
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[C API] PyUnicodeWriter_WriteStr() and str subclasses","articleBody":"The [`PyUnicodeWriter_WriteStr()` API](https://docs.python.org/dev/c-api/unicode.html#c.PyUnicodeWriter_WriteStr) is documented as:\n\n> Call `PyObject_Str()` on *obj* and write the output into *writer*.\n\nOn 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:\n\n```py\nfrom enum import Enum\n\nclass AStringEnum(str, Enum):\n A = \"a\"\n\nobj = AStringEnum.A\nprint(f\"{str(obj)=!r}\")\nprint(f\"{str.__str__(obj)=!r}\")\n```\n\nOutput (on Python 3.15):\n\n```\nstr(obj)='AStringEnum.A'\nstr.__str__(obj)='a'\n```\n\n`str()` and `str.__str__()` return different strings (`AStringEnum.A` vs `a`). `PyUnicodeWriter_WriteStr()` writes `AStringEnum.A` because it uses `str()`.\n\nOne 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.\n\nAn 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()`).\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-148251\n* gh-148293\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/vstinner","@type":"Person","name":"vstinner"},"datePublished":"2026-04-08T13:05:04Z","url":"https://github.com/python/cpython/issues/148250"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:837441e2-2b5e-7f60-169a-71dcd7bb471b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C414:34CAB:3AE3369:4B44018:6B020F69 |
| html-safe-nonce | ee4518e0f388ce3fd0f57e4f0881ec0ea0033795fea4f0952130f12819227f36 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNDE0OjM0Q0FCOjNBRTMzNjk6NEI0NDAxODo2QjAyMEY2OSIsInZpc2l0b3JfaWQiOiIzOTIzMjY2NDM1NTY0MDQ4MjMzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | e3754a5a6f43f6e722a59192c17b06f0fccfa643e173c1d157bb41723a19089e |
| hovercard-subject-tag | issue:4224715745 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/148250/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c1de7200d297f627937f42c6e329a8e6ee3c1a0d84cd294acf98c62fc0dcdd54/python/cpython/issues/148250 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c1de7200d297f627937f42c6e329a8e6ee3c1a0d84cd294acf98c62fc0dcdd54/python/cpython/issues/148250 |
| og:image:alt | The PyUnicodeWriter_WriteStr() API is documented as: 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 c... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | vstinner |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width