Title: CJK StreamWriter.reset() does not write the shift-back sequence · Issue #160185 · python/cpython · GitHub
Open Graph Title: CJK StreamWriter.reset() does not write the shift-back sequence · Issue #160185 · python/cpython
X Title: CJK StreamWriter.reset() does not write the shift-back sequence · Issue #160185 · python/cpython
Description: Bug report Bug description: StreamWriter.reset() of the stateful CJK codecs (hz, iso2022_jp*, iso2022_kr) usually writes nothing. MultibyteStreamWriter.reset() returns early when self->pending is NULL, but pending only holds an unfinishe...
Open Graph Description: Bug report Bug description: StreamWriter.reset() of the stateful CJK codecs (hz, iso2022_jp*, iso2022_kr) usually writes nothing. MultibyteStreamWriter.reset() returns early when self->pending is N...
X Description: Bug report Bug description: StreamWriter.reset() of the stateful CJK codecs (hz, iso2022_jp*, iso2022_kr) usually writes nothing. MultibyteStreamWriter.reset() returns early when self->pending i...
Opengraph URL: https://github.com/python/cpython/issues/160185
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"CJK StreamWriter.reset() does not write the shift-back sequence","articleBody":"# Bug report\n\n### Bug description:\n\n`StreamWriter.reset()` of the stateful CJK codecs (`hz`, `iso2022_jp*`, `iso2022_kr`) usually writes nothing. `MultibyteStreamWriter.reset()` returns early when `self->pending` is NULL, but `pending` only holds an unfinished combining character, not the shift state, so the codec's `encreset` (which writes `ESC ( B`, `SI` or `~}`) is skipped and the writer stays in double-byte mode.\n\n```python\nimport codecs, io\nout = io.BytesIO()\nw = codecs.getwriter('iso2022_jp')(out)\nw.write('こんにちは。')\nw.reset()\nprint(out.getvalue()) # b'\\x1b$B$3$s$K$A$O!#'\n```\n\nExpected `b'\\x1b$B$3$s$K$A$O!#\\x1b(B'`, as `str.encode()` gives. Anything written after `reset()` is read in JIS X 0208 mode, and the next CJK text written by the same writer has no opening escape. `hz` gives `b'~{Dc:C'` for `'你好'` instead of `b'~{Dc:C~}'`.\n\nThis is a regression from the gh-67436 crash fix (2015, 3.4.4/3.5), which added the early return to avoid passing a NULL `pending` to the encoder; before that, `reset()` always ran the codec reset. The same function also leaks the result of `stream.write()`.\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-160186\n* gh-161067\n* gh-161068\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/gpshead","@type":"Person","name":"gpshead"},"datePublished":"2026-10-22T04:30:06Z","url":"https://github.com/python/cpython/issues/160185"}
| 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:50b3223e-f68d-708d-b10a-02b04683e2eb |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E144:BB75:1D68EB5:26D49F5:6B022F4B |
| html-safe-nonce | 97af4d91d59bc28341b5770ca3ee79d19c80b512070151ec790a8bf264184e63 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMTQ0OkJCNzU6MUQ2OEVCNToyNkQ0OUY1OjZCMDIyRjRCIiwidmlzaXRvcl9pZCI6IjE2NTgyOTE0ODc1NzU3ODExOTUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 36c2d467387cb6bf61c2239f4f14dde100df4345f72dea2892d190f5e43601e8 |
| hovercard-subject-tag | issue:5973078519 |
| 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/160185/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f5f7ccb4d3e9531b15009fbd79dd450686687c211dc16154221571cfc662bdf8/python/cpython/issues/160185 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f5f7ccb4d3e9531b15009fbd79dd450686687c211dc16154221571cfc662bdf8/python/cpython/issues/160185 |
| og:image:alt | Bug report Bug description: StreamWriter.reset() of the stateful CJK codecs (hz, iso2022_jp*, iso2022_kr) usually writes nothing. MultibyteStreamWriter.reset() returns early when self->pending is N... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gpshead |
| 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 | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width