mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 11:14:38 +08:00
Restrict mappingproxy ordering, use final codec registry normalization, and prefer index conversion before float conversion for sleep. Retain universal Python assertions and import the matching final codec/time canonical test hunks plus the non-ASCII codec-name deprecation warning. Upstream source: CPython d220409a13b77de7a6c30abe16ea90db01528edc. The existing codec normalization implementation and bytecode contract remain unchanged. Assisted-by: OpenAI Codex:model identifier unavailable
67 lines
2.2 KiB
Python
67 lines
2.2 KiB
Python
# Shared wide / escape codecs from rustpython-common.
|
|
|
|
assert "hi".encode("utf-16-le") == b"h\x00i\x00"
|
|
assert "hi".encode("utf-16-be") == b"\x00h\x00i"
|
|
assert b"h\x00i\x00".decode("utf-16-le") == "hi"
|
|
assert b"\x00h\x00i".decode("utf-16-be") == "hi"
|
|
|
|
# BOM form: native utf-16 writes a BOM and decodes it back.
|
|
assert "hi".encode("utf-16").decode("utf-16") == "hi"
|
|
assert "hi".encode("utf-32").decode("utf-32") == "hi"
|
|
assert "hi".encode("utf-32-le") == b"h\x00\x00\x00i\x00\x00\x00"
|
|
assert b"h\x00\x00\x00i\x00\x00\x00".decode("utf-32-le") == "hi"
|
|
|
|
assert "café".encode("unicode-escape") == b"caf\\xe9"
|
|
assert b"caf\\xe9".decode("unicode-escape") == "café"
|
|
assert "A\u0100".encode("raw-unicode-escape") == b"A\\u0100"
|
|
assert b"A\\u0100".decode("raw-unicode-escape") == "A\u0100"
|
|
|
|
assert "+".encode("utf-7") == b"+-"
|
|
assert b"+-".decode("utf-7") == "+"
|
|
|
|
import _codecs
|
|
|
|
assert _codecs.escape_encode(b"a\tb") == (b"a\\tb", 3)
|
|
assert _codecs.escape_decode(rb"a\tb") == (b"a\tb", 4)
|
|
assert _codecs.utf_16_ex_decode(b"\xff\xfeh\x00i\x00", "strict", 0, True)[0] == "hi"
|
|
assert (
|
|
_codecs.utf_32_ex_decode(b"\xff\xfe\x00\x00h\x00\x00\x00", "strict", 0, True)[0]
|
|
== "h"
|
|
)
|
|
|
|
|
|
# Lookup errors preserve the requested spelling, while search hooks receive
|
|
# the Python 3.15 normalized registry key.
|
|
import codecs
|
|
|
|
seen_encoding_names = []
|
|
|
|
|
|
def record_unknown_encoding(name):
|
|
seen_encoding_names.append(name)
|
|
return None
|
|
|
|
|
|
codecs.register(record_unknown_encoding)
|
|
try:
|
|
for requested, normalized in (
|
|
("NO Such Codec!", "no-such--codec!"),
|
|
("x_ weird--Ω .Ab", "x_-weird--Ω-.ab"),
|
|
):
|
|
for lookup in (
|
|
codecs.lookup,
|
|
lambda name: codecs.encode("value", name),
|
|
lambda name: codecs.decode(b"value", name),
|
|
lambda name: "value".encode(name),
|
|
lambda name: b"value".decode(name),
|
|
):
|
|
try:
|
|
lookup(requested)
|
|
except LookupError as error:
|
|
assert str(error) == "unknown encoding: " + requested
|
|
else:
|
|
assert False, "unknown encoding was accepted"
|
|
assert seen_encoding_names[-1] == normalized
|
|
finally:
|
|
codecs.unregister(record_unknown_encoding)
|