Skip to content

Commit c516ca9

Browse files
authored
Preserve requested encoding names in codec lookup errors (#9036)
Extract the Python 3.14-compatible lookup diagnostic fix from #8954 while retaining the existing registry normalization. Cover original error spelling and normalized search-hook names in the existing codec snippet. Assisted-by: OpenAI Codex:model identifier unavailable
1 parent 386cd86 commit c516ca9

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

‎crates/vm/src/codecs.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ impl CodecsRegistry {
261261
}
262262

263263
pub fn lookup(&self, encoding: &str, vm: &VirtualMachine) -> PyResult<PyCodec> {
264+
let original_encoding = encoding;
264265
let encoding = normalize_encoding_name(encoding);
265266
let search_path = {
266267
let inner = self.inner.read();
@@ -286,7 +287,7 @@ impl CodecsRegistry {
286287
}
287288
}
288289

289-
Err(vm.new_lookup_error(format!("unknown encoding: {encoding}")))
290+
Err(vm.new_lookup_error(format!("unknown encoding: {original_encoding}")))
290291
}
291292

292293
fn _lookup_text_encoding(

‎extra_tests/snippets/stdlib_codecs_wide.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,39 @@
2828
_codecs.utf_32_ex_decode(b"\xff\xfe\x00\x00h\x00\x00\x00", "strict", 0, True)[0]
2929
== "h"
3030
)
31+
32+
33+
# Lookup errors preserve the requested spelling, while search hooks receive
34+
# the Python 3.14 normalized registry key.
35+
import codecs
36+
37+
seen_encoding_names = []
38+
39+
40+
def record_unknown_encoding(name):
41+
seen_encoding_names.append(name)
42+
return None
43+
44+
45+
codecs.register(record_unknown_encoding)
46+
try:
47+
for requested, normalized in (
48+
("NO Such Codec!", "no_such_codec"),
49+
("x_ weird--Ω .Ab", "x_weird_.ab"),
50+
):
51+
for lookup in (
52+
codecs.lookup,
53+
lambda name: codecs.encode("value", name),
54+
lambda name: codecs.decode(b"value", name),
55+
lambda name: "value".encode(name),
56+
lambda name: b"value".decode(name),
57+
):
58+
try:
59+
lookup(requested)
60+
except LookupError as error:
61+
assert str(error) == "unknown encoding: " + requested
62+
else:
63+
assert False, "unknown encoding was accepted"
64+
assert seen_encoding_names[-1] == normalized
65+
finally:
66+
codecs.unregister(record_unknown_encoding)

0 commit comments

Comments
 (0)