Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 16 additions & 0 deletions Lib/test/multibytecodec_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,22 @@ def test_incrementalencoder_del_segfault(self):
with self.assertRaises(AttributeError):
del e.errors

def test_null_terminator(self):
# see gh-101828
if any(enc in self.encoding for enc in ('shift', 'euc_jis')):
text = "バルーンフルーツ"
else:
text = "Spam"
encode_w_null = (text + "\0").encode(self.encoding)
encode_plus_null = text.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 = (text + "\0" + text + "\0").encode(self.encoding)
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 TestBase_Mapping(unittest.TestCase):
pass_enctest = []
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix ``'shift_jisx0213'``, ``'shift_jis_2004'``, ``'euc_jisx0213'`` and
``'euc_jis_2004'`` codecs truncating null chars
as they were treated as part of multi-character sequences.
9 changes: 7 additions & 2 deletions Modules/cjkcodecs/_codecs_jp.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,11 @@ ENCODER(euc_jis_2004)
JISX0213_ENCPAIRS);
if (code == DBCINV)
return 1;
} else
}
else if (c2 != 0) {
/* Don't consume null char as part of pair */
insize = 2;
}
}
}
}
Expand Down Expand Up @@ -611,8 +614,10 @@ ENCODER(shift_jis_2004)
if (code == DBCINV)
return 1;
}
else
else if (ch2 != 0) {
/* Don't consume null char as part of pair */
insize = 2;
}
}
}
}
Expand Down
Loading