Skip to content
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
7 changes: 7 additions & 0 deletions crates/vm/src/codecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,13 @@ impl FastCodec {
"latin",
"latin1",
];
// The spellings used most often, matched before normalizing allocates.
match encoding {
"utf-8" | "utf8" | "UTF-8" => return Some(Self::Utf8),
"ascii" => return Some(Self::Ascii),
"latin-1" | "latin1" | "iso-8859-1" => return Some(Self::Latin1),
_ => {}
}
let normalized = normalize_encoding_name(encoding);
let normalized = normalized.as_ref();
if UTF8_ALIASES.contains(&normalized) {
Expand Down
15 changes: 15 additions & 0 deletions extra_tests/snippets/builtin_str_encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,18 @@ def round_trip(s, encoding="utf-8"):
assert "\ud800\ud800".encode("unicode_escape") == b"\\ud800\\ud800"
assert "\ud800\udc00".encode("unicode_escape") == b"\\ud800\\udc00"
assert "\U00010000".encode("unicode_escape") == b"\\U00010000"

# Common encoding spellings pick the same codec, with its canonical name in errors
for spelling in ["utf-8", "utf8", "UTF-8", "Utf-8", "utf_8"]:
assert "é€".encode(spelling) == b"\xc3\xa9\xe2\x82\xac"
with assert_raises(UnicodeDecodeError, _msg=spelling) as cm:
b"\xff".decode(spelling)
assert str(cm.exception).startswith("'utf-8' codec"), str(cm.exception)
for spelling in ["latin-1", "latin1", "iso-8859-1", "l1"]:
assert b"\xe9".decode(spelling) == "é"
with assert_raises(UnicodeEncodeError) as cm:
"€".encode(spelling)
assert str(cm.exception).startswith("'latin-1' codec"), str(cm.exception)
with assert_raises(UnicodeEncodeError) as cm:
"é".encode("ascii")
assert str(cm.exception).startswith("'ascii' codec"), str(cm.exception)
Loading