Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Lib/test/test_codecencodings_cn.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,15 @@ class Test_HZ(multibytecodec_support.TestBase, unittest.TestCase):
(b'~{Dc~\n:C~}', 'strict', None), # ~\n only in ASCII mode
)

def test_escape_at_buffer_boundary(self):
# '~~' must not be cut in two when the output buffer is enlarged.
for pairs in range(40):
for letters in range(3):
text = 'a\u4e2d' * pairs + 'x' * letters + '~{'
with self.subTest(pairs=pairs, letters=letters):
encoded = text.encode('hz')
self.assertEndsWith(encoded, b'~~{')
self.assertEqual(encoded.decode('hz'), text)

if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix the ``hz`` codec sometimes encoding ``~`` as a single ``~`` instead of
``~~`` in text that mixes ASCII and Chinese characters, which produced data
that did not decode back to the original text.
10 changes: 6 additions & 4 deletions Modules/cjkcodecs/_codecs_cn.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,13 @@ ENCODER(hz)
NEXT_OUT(2);
state->c[CN_STATE_OFFSET] = 0;
}
WRITEBYTE1((unsigned char)c);
NEXT(1, 1);
if (c == '~') {
WRITEBYTE1('~');
NEXT_OUT(1);
WRITEBYTE2('~', '~');
NEXT(1, 2);
}
else {
WRITEBYTE1((unsigned char)c);
NEXT(1, 1);
}
continue;
}
Expand Down
Loading