Skip to content

bpo-30003: Fix handling escape characters in HZ codec #1556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2017
Merged
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
4 changes: 4 additions & 0 deletions Lib/test/test_codecencodings_cn.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ class Test_HZ(multibytecodec_support.TestBase, unittest.TestCase):
(b'ab~{\x81\x81\x41\x44~}cd', 'replace', 'ab\uFFFD\uFFFD\u804Acd'),
(b'ab~{\x41\x44~}cd', 'replace', 'ab\u804Acd'),
(b"ab~{\x79\x79\x41\x44~}cd", "replace", "ab\ufffd\ufffd\u804acd"),
# issue 30003
('ab~cd', 'strict', b'ab~~cd'), # escape ~
(b'~{Dc~~:C~}', 'strict', None), # ~~ only in ASCII mode
(b'~{Dc~\n:C~}', 'strict', None), # ~\n only in ASCII mode
)

if __name__ == "__main__":
Expand Down
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@ Extension Modules
Library
-------

- bpo-30003: Fix handling escape characters in HZ codec. Based on patch
by Ma Lin.

- bpo-30149: inspect.signature() now supports callables with
variable-argument parameters wrapped with partialmethod.
Patch by Dong-hee Na.
Expand Down
25 changes: 12 additions & 13 deletions Modules/cjkcodecs/_codecs_cn.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,15 +350,17 @@ ENCODER(hz)
DBCHAR code;

if (c < 0x80) {
if (state->i == 0) {
WRITEBYTE1((unsigned char)c);
NEXT(1, 1);
}
else {
WRITEBYTE3('~', '}', (unsigned char)c);
NEXT(1, 3);
if (state->i) {
WRITEBYTE2('~', '}');
NEXT_OUT(2);
state->i = 0;
}
WRITEBYTE1((unsigned char)c);
NEXT(1, 1);
if (c == '~') {
WRITEBYTE1('~');
NEXT_OUT(1);
}
continue;
}

Expand Down Expand Up @@ -409,17 +411,14 @@ DECODER(hz)
unsigned char c2 = INBYTE2;

REQUIRE_INBUF(2);
if (c2 == '~') {
if (c2 == '~' && state->i == 0)
OUTCHAR('~');
NEXT_IN(2);
continue;
}
else if (c2 == '{' && state->i == 0)
state->i = 1; /* set GB */
else if (c2 == '\n' && state->i == 0)
; /* line-continuation */
else if (c2 == '}' && state->i == 1)
state->i = 0; /* set ASCII */
else if (c2 == '\n')
; /* line-continuation */
else
return 1;
NEXT_IN(2);
Expand Down