mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
* common: add PyPy CJK codec engines
Assisted-by: Codex:GPT-5
* common: mark CJK mapping tables generated
Assisted-by: Codex:GPT-5
* common: add reusable byte and text engines
Assisted-by: Codex:GPT-5
* common: record opt-in engine dependencies
Assisted-by: Codex:GPT-5
* stdlib: add `_multibytecodec` and the six `_codecs_*` modules
Wire the CJK codec engines in `rustpython-common` up to the VM. `_multibytecodec`
holds `MultibyteCodec`, `MultibyteIncrementalEncoder`, `MultibyteIncrementalDecoder`,
`MultibyteStreamReader` and `MultibyteStreamWriter`, the encode/decode drivers and
the error-handler protocol; each `_codecs_*` module exposes `getcodec` for the
encodings of its region.
`rustpython-stdlib` now enables the `cjk-codecs` feature of `rustpython-common`.
Add `cjk::decode_reset`, which the decoder `reset()` methods need. It differs from
building a fresh decoder state: ISO 2022 keeps its G1..G3 designations and the
escape-throughout flag, and resets only G0 and the shifted flag.
Drop the markers from the CJK codec tests and from the `test_codecs`, `test_io`,
`test_email`, `test_zipfile` and `test_traceback` cases that needed a CJK codec.
`test_codecs.BasicUnicodeTest.{test_basics,test_decoder_state}` stay expected
failures for an unrelated reason: `codecs.charmap_encode` rejects a `None` mapping.
Assisted-by: Claude Code:claude-opus-5
* stdlib: use the byte and text engines from `rustpython-common`
`binascii`, `_json` and `_socket`'s address converters now call the engines in
`rustpython-common` instead of carrying their own copies, and `rustpython-stdlib`
enables the `binascii`, `inet` and `json` features for them. That leaves
`base64` and `crc32fast` with no caller in `rustpython-stdlib`, and
`crates/stdlib/src/json/machinery.rs`, the last of the json_in_type-derived code,
goes away with its last caller.
The address converters change behaviour. `inet_aton` reads the lenient forms
(`127.1`, `16909060`, `0x7f.1`, `0177.0.0.1`), `inet_pton` rejects an octet with a
redundant leading zero and a group of five hex digits, `inet_ntop` writes the
IPv4-compatible form as `::c000:201`, and an address family with no converter
raises `EAFNOSUPPORT` instead of a bare `OSError`.
Give the JSON engine the closing-quote scan and the allocation-free `\uXXXX`
escape it needs to carry `_json`'s workload. Against the code it replaces,
`json.dumps` runs 2.3x faster with `ensure_ascii=True` and 1.7x without it, and
`json.loads` 1.2x.
`binascii.a2b_base64` now reports the messages `binascii.c` does: no
`error decoding base64: ` prefix, and the data-character count in parentheses.
Assisted-by: Claude Code:claude-opus-5
* test: enable the Windows cp932 codec case
The shared CJK engine now satisfies the Windows code-page adapter, so keeping this test marked as an expected failure turns its success into a CI failure.
Assisted-by: Codex:GPT-5
127 lines
4.8 KiB
Python
Vendored
127 lines
4.8 KiB
Python
Vendored
#
|
|
# test_codecencodings_jp.py
|
|
# Codec encoding tests for Japanese encodings.
|
|
#
|
|
|
|
from test import multibytecodec_support
|
|
import unittest
|
|
|
|
class Test_CP932(multibytecodec_support.TestBase, unittest.TestCase):
|
|
encoding = 'cp932'
|
|
tstring = multibytecodec_support.load_teststring('shift_jis')
|
|
codectests = (
|
|
# invalid bytes
|
|
(b"abc\x81\x00\x81\x00\x82\x84", "strict", None),
|
|
(b"abc\xf8", "strict", None),
|
|
(b"abc\x81\x00\x82\x84", "replace", "abc\ufffd\x00\uff44"),
|
|
(b"abc\x81\x00\x82\x84\x88", "replace", "abc\ufffd\x00\uff44\ufffd"),
|
|
(b"abc\x81\x00\x82\x84", "ignore", "abc\x00\uff44"),
|
|
(b"ab\xEBxy", "replace", "ab\uFFFDxy"),
|
|
(b"ab\xF0\x39xy", "replace", "ab\uFFFD9xy"),
|
|
(b"ab\xEA\xF0xy", "replace", 'ab\ufffd\ue038y'),
|
|
# sjis vs cp932
|
|
(b"\\\x7e", "replace", "\\\x7e"),
|
|
(b"\x81\x5f\x81\x61\x81\x7c", "replace", "\uff3c\u2225\uff0d"),
|
|
)
|
|
|
|
euc_commontests = (
|
|
# invalid bytes
|
|
(b"abc\x80\x80\xc1\xc4", "strict", None),
|
|
(b"abc\x80\x80\xc1\xc4", "replace", "abc\ufffd\ufffd\u7956"),
|
|
(b"abc\x80\x80\xc1\xc4\xc8", "replace", "abc\ufffd\ufffd\u7956\ufffd"),
|
|
(b"abc\x80\x80\xc1\xc4", "ignore", "abc\u7956"),
|
|
(b"abc\xc8", "strict", None),
|
|
(b"abc\x8f\x83\x83", "replace", "abc\ufffd\ufffd\ufffd"),
|
|
(b"\x82\xFCxy", "replace", "\ufffd\ufffdxy"),
|
|
(b"\xc1\x64", "strict", None),
|
|
(b"\xa1\xc0", "strict", "\uff3c"),
|
|
(b"\xa1\xc0\\", "strict", "\uff3c\\"),
|
|
(b"\x8eXY", "replace", "\ufffdXY"),
|
|
)
|
|
|
|
class Test_EUC_JIS_2004(multibytecodec_support.TestBase,
|
|
unittest.TestCase):
|
|
encoding = 'euc_jis_2004'
|
|
tstring = multibytecodec_support.load_teststring('euc_jisx0213')
|
|
codectests = euc_commontests
|
|
xmlcharnametest = (
|
|
"\xab\u211c\xbb = \u2329\u1234\u232a",
|
|
b"\xa9\xa8ℜ\xa9\xb2 = ⟨ሴ⟩"
|
|
)
|
|
|
|
class Test_EUC_JISX0213(multibytecodec_support.TestBase,
|
|
unittest.TestCase):
|
|
encoding = 'euc_jisx0213'
|
|
tstring = multibytecodec_support.load_teststring('euc_jisx0213')
|
|
codectests = euc_commontests
|
|
xmlcharnametest = (
|
|
"\xab\u211c\xbb = \u2329\u1234\u232a",
|
|
b"\xa9\xa8ℜ\xa9\xb2 = ⟨ሴ⟩"
|
|
)
|
|
|
|
class Test_EUC_JP_COMPAT(multibytecodec_support.TestBase,
|
|
unittest.TestCase):
|
|
encoding = 'euc_jp'
|
|
tstring = multibytecodec_support.load_teststring('euc_jp')
|
|
codectests = euc_commontests + (
|
|
("\xa5", "strict", b"\x5c"),
|
|
("\u203e", "strict", b"\x7e"),
|
|
)
|
|
|
|
shiftjis_commonenctests = (
|
|
(b"abc\x80\x80\x82\x84", "strict", None),
|
|
(b"abc\xf8", "strict", None),
|
|
(b"abc\x80\x80\x82\x84def", "ignore", "abc\uff44def"),
|
|
)
|
|
|
|
class Test_SJIS_COMPAT(multibytecodec_support.TestBase, unittest.TestCase):
|
|
encoding = 'shift_jis'
|
|
tstring = multibytecodec_support.load_teststring('shift_jis')
|
|
codectests = shiftjis_commonenctests + (
|
|
(b"abc\x80\x80\x82\x84", "replace", "abc\ufffd\ufffd\uff44"),
|
|
(b"abc\x80\x80\x82\x84\x88", "replace", "abc\ufffd\ufffd\uff44\ufffd"),
|
|
|
|
(b"\\\x7e", "strict", "\\\x7e"),
|
|
(b"\x81\x5f\x81\x61\x81\x7c", "strict", "\uff3c\u2016\u2212"),
|
|
(b"abc\x81\x39", "replace", "abc\ufffd9"),
|
|
(b"abc\xEA\xFC", "replace", "abc\ufffd\ufffd"),
|
|
(b"abc\xFF\x58", "replace", "abc\ufffdX"),
|
|
)
|
|
|
|
class Test_SJIS_2004(multibytecodec_support.TestBase, unittest.TestCase):
|
|
encoding = 'shift_jis_2004'
|
|
tstring = multibytecodec_support.load_teststring('shift_jis')
|
|
codectests = shiftjis_commonenctests + (
|
|
(b"\\\x7e", "strict", "\xa5\u203e"),
|
|
(b"\x81\x5f\x81\x61\x81\x7c", "strict", "\\\u2016\u2212"),
|
|
(b"abc\xEA\xFC", "strict", "abc\u64bf"),
|
|
(b"\x81\x39xy", "replace", "\ufffd9xy"),
|
|
(b"\xFF\x58xy", "replace", "\ufffdXxy"),
|
|
(b"\x80\x80\x82\x84xy", "replace", "\ufffd\ufffd\uff44xy"),
|
|
(b"\x80\x80\x82\x84\x88xy", "replace", "\ufffd\ufffd\uff44\u5864y"),
|
|
(b"\xFC\xFBxy", "replace", '\ufffd\u95b4y'),
|
|
)
|
|
xmlcharnametest = (
|
|
"\xab\u211c\xbb = \u2329\u1234\u232a",
|
|
b"\x85Gℜ\x85Q = ⟨ሴ⟩"
|
|
)
|
|
|
|
class Test_SJISX0213(multibytecodec_support.TestBase, unittest.TestCase):
|
|
encoding = 'shift_jisx0213'
|
|
tstring = multibytecodec_support.load_teststring('shift_jisx0213')
|
|
codectests = shiftjis_commonenctests + (
|
|
(b"abc\x80\x80\x82\x84", "replace", "abc\ufffd\ufffd\uff44"),
|
|
(b"abc\x80\x80\x82\x84\x88", "replace", "abc\ufffd\ufffd\uff44\ufffd"),
|
|
|
|
# sjis vs cp932
|
|
(b"\\\x7e", "replace", "\xa5\u203e"),
|
|
(b"\x81\x5f\x81\x61\x81\x7c", "replace", "\x5c\u2016\u2212"),
|
|
)
|
|
xmlcharnametest = (
|
|
"\xab\u211c\xbb = \u2329\u1234\u232a",
|
|
b"\x85Gℜ\x85Q = ⟨ሴ⟩"
|
|
)
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|