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
3 changes: 2 additions & 1 deletion crates/vm/src/codecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ impl CodecsRegistry {
}

pub fn lookup(&self, encoding: &str, vm: &VirtualMachine) -> PyResult<PyCodec> {
let original_encoding = encoding;
let encoding = normalize_encoding_name(encoding);
let search_path = {
let inner = self.inner.read();
Expand All @@ -286,7 +287,7 @@ impl CodecsRegistry {
}
}

Err(vm.new_lookup_error(format!("unknown encoding: {encoding}")))
Err(vm.new_lookup_error(format!("unknown encoding: {original_encoding}")))
}

fn _lookup_text_encoding(
Expand Down
36 changes: 36 additions & 0 deletions extra_tests/snippets/stdlib_codecs_wide.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,39 @@
_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.14 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)
Loading