Title: `hz` codec encodes `~` as a single `~` when the output buffer fills mid-escape · Issue #160518 · python/cpython · GitHub
Open Graph Title: `hz` codec encodes `~` as a single `~` when the output buffer fills mid-escape · Issue #160518 · python/cpython
X Title: `hz` codec encodes `~` as a single `~` when the output buffer fills mid-escape · Issue #160518 · python/cpython
Description: Bug report Bug description: The hz encoder writes the ~~ escape for a ~ as two separate bytes and consumes the character after the first one. If the output buffer becomes full between the two, the encoder is called again at the next char...
Open Graph Description: Bug report Bug description: The hz encoder writes the ~~ escape for a ~ as two separate bytes and consumes the character after the first one. If the output buffer becomes full between the two, the ...
X Description: Bug report Bug description: The hz encoder writes the ~~ escape for a ~ as two separate bytes and consumes the character after the first one. If the output buffer becomes full between the two, the ...
Opengraph URL: https://github.com/python/cpython/issues/160518
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`hz` codec encodes `~` as a single `~` when the output buffer fills mid-escape","articleBody":"# Bug report\n\n### Bug description:\n\nThe `hz` encoder writes the `~~` escape for a `~` as two separate bytes and consumes the character after the first one. If the output buffer becomes full between the two, the encoder is called again at the next character and the second `~` is never written. The result is a bare `~` that the decoder combines with the byte after it, so the data decodes to different text or fails to decode. It needs text that mixes ASCII and Chinese characters (plain ASCII or plain Chinese never fills the initial buffer) with a `~` at the wrong offset.\n\n```python\ntext = \"a中\" * 7 + \"xx\" + \"~{\"\ndata = text.encode(\"hz\")\nprint(data[-8:]) # b'VP~}xx~{', should end in b'xx~~{'\nprint(data.decode(\"hz\") == text) # False\n```\n\nWith `\"xxx\"` instead of `\"xx\"` the output is correct. The two-step write dates from gh-74189.\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-160519\n* gh-161002\n* gh-161003\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/gpshead","@type":"Person","name":"gpshead"},"datePublished":"2026-10-23T21:57:26Z","url":"https://github.com/python/cpython/issues/160518"}
| 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:9fe78c5b-09a0-966a-f52b-45d890277d63 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E48E:20AA01:48C39A9:5C314F2:6B023353 |
| html-safe-nonce | 8827073b1060fc1e036d7f7dc86c8b9df7fdfae19c8aecb643c2f8eba621e42b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNDhFOjIwQUEwMTo0OEMzOUE5OjVDMzE0RjI6NkIwMjMzNTMiLCJ2aXNpdG9yX2lkIjoiNDA1Mjg0MjY3NDAxMTY0ODg1MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | cde925ff6497f7ddb0b32152fbf00e96fe4382bd8487ac1e35d2de582258e22a |
| hovercard-subject-tag | issue:6006050135 |
| 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/160518/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b8c5b145dd9ce5dbdf14f3caf60389f3f6b2b6192d0959dbb4f5b30049e84ff0/python/cpython/issues/160518 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b8c5b145dd9ce5dbdf14f3caf60389f3f6b2b6192d0959dbb4f5b30049e84ff0/python/cpython/issues/160518 |
| og:image:alt | Bug report Bug description: The hz encoder writes the ~~ escape for a ~ as two separate bytes and consumes the character after the first one. If the output buffer becomes full between the two, the ... |
| 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