Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
40 changes: 40 additions & 0 deletions Lib/test/test_codecencodings_jp.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,26 @@ class Test_SJIS_2004(multibytecodec_support.TestBase, unittest.TestCase):
b"\x85Gℜ\x85Q = ⟨ሴ⟩"
)

def test_null_terminator(self):
# see gh-101828
cases = (
"バルーンフルーツ",
"ライフアップキノコ",
"テスト",
"'Tis but a scratch!"
)
for case in cases:
with self.subTest(case=case):
encode_w_null = (case + "\0").encode(self.encoding)
encode_plus_null = case.encode(self.encoding) + "\0".encode(self.encoding)
self.assertTrue(encode_w_null.endswith(b'\x00'))
self.assertEqual(encode_w_null, encode_plus_null)

encode_w_null_2 = encode_w_null + encode_w_null
encode_plus_null_2 = encode_plus_null + encode_plus_null
self.assertEqual(encode_w_null_2.count(b'\x00'), 2)
self.assertEqual(encode_w_null_2, encode_plus_null_2)

class Test_SJISX0213(multibytecodec_support.TestBase, unittest.TestCase):
encoding = 'shift_jisx0213'
tstring = multibytecodec_support.load_teststring('shift_jisx0213')
Expand All @@ -122,5 +142,25 @@ class Test_SJISX0213(multibytecodec_support.TestBase, unittest.TestCase):
b"\x85Gℜ\x85Q = ⟨ሴ⟩"
)

def test_null_terminator(self):
# see gh-101828
cases = (
"バルーンフルーツ",
"ライフアップキノコ",
"テスト",
"'Tis but a scratch!"
)
for case in cases:
with self.subTest(case=case):
encode_w_null = (case + "\0").encode(self.encoding)
encode_plus_null = case.encode(self.encoding) + "\0".encode(self.encoding)
self.assertTrue(encode_w_null.endswith(b'\x00'))
self.assertEqual(encode_w_null, encode_plus_null)

encode_w_null_2 = encode_w_null + encode_w_null
encode_plus_null_2 = encode_plus_null + encode_plus_null
self.assertEqual(encode_w_null_2.count(b'\x00'), 2)
self.assertEqual(encode_w_null_2, encode_plus_null_2)

if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix ``'shift_jisx0213'`` and ``'shift_jis_2004'`` codecs truncating null chars
as they were treated as part of multi-character sequences.
13 changes: 12 additions & 1 deletion Modules/cjkcodecs/_codecs_jp.c
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,19 @@ ENCODER(shift_jis_2004)
if (code == DBCINV)
return 1;
}
else
else if (ch2 != 0) {
insize = 2;
}
else {
/* Don't consume null char as part of pair */
code = find_pairencmap(
(ucs2_t)c, 0,
jisx0213_pair_encmap,
JISX0213_ENCPAIRS);
if (code == DBCINV) {
return 1;
}
}
}
}
}
Expand Down
Loading