Skip to content

hz codec encodes ~ as a single ~ when the output buffer fills mid-escape #160518

Description

@gpshead

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 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.

text = "a中" * 7 + "xx" + "~{"
data = text.encode("hz")
print(data[-8:])                  # b'VP~}xx~{', should end in b'xx~~{'
print(data.decode("hz") == text)  # False

With "xxx" instead of "xx" the output is correct. The two-step write dates from gh-74189.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

Labels

extension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions