From 5a8d149105b24ed1e3955824fd7fc74dc78939d1 Mon Sep 17 00:00:00 2001 From: Jiseok CHOI Date: Thu, 30 Oct 2025 18:17:23 +0900 Subject: [PATCH] Add callable validation to codecs.register_error Validate that the handler argument passed to codecs.register_error is callable, raising TypeError with message 'handler must be callable' if it is not. This matches CPython behavior. This fix enables test_badregistercall in test_codeccallbacks.py to pass. --- Lib/test/test_codeccallbacks.py | 2 -- vm/src/stdlib/codecs.rs | 6 +++++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_codeccallbacks.py b/Lib/test/test_codeccallbacks.py index bd1dbcd626..9ca02cea35 100644 --- a/Lib/test/test_codeccallbacks.py +++ b/Lib/test/test_codeccallbacks.py @@ -909,8 +909,6 @@ def handle(exc): self.assertEqual(exc.object, input) self.assertEqual(exc.reason, "surrogates not allowed") - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_badregistercall(self): # enhance coverage of: # Modules/_codecsmodule.c::register_error() diff --git a/vm/src/stdlib/codecs.rs b/vm/src/stdlib/codecs.rs index 468b5dda6e..46b1608f24 100644 --- a/vm/src/stdlib/codecs.rs +++ b/vm/src/stdlib/codecs.rs @@ -67,10 +67,14 @@ mod _codecs { } #[pyfunction] - fn register_error(name: PyStrRef, handler: PyObjectRef, vm: &VirtualMachine) { + fn register_error(name: PyStrRef, handler: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> { + if !handler.is_callable() { + return Err(vm.new_type_error("handler must be callable".to_owned())); + } vm.state .codec_registry .register_error(name.as_str().to_owned(), handler); + Ok(()) } #[pyfunction]