Skip to content

Commit 9e7d291

Browse files
authored
Add callable validation to codecs.register_error (RustPython#6229)
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.
1 parent 614028f commit 9e7d291

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

Lib/test/test_codeccallbacks.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -909,8 +909,6 @@ def handle(exc):
909909
self.assertEqual(exc.object, input)
910910
self.assertEqual(exc.reason, "surrogates not allowed")
911911

912-
# TODO: RUSTPYTHON
913-
@unittest.expectedFailure
914912
def test_badregistercall(self):
915913
# enhance coverage of:
916914
# Modules/_codecsmodule.c::register_error()

vm/src/stdlib/codecs.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,14 @@ mod _codecs {
6767
}
6868

6969
#[pyfunction]
70-
fn register_error(name: PyStrRef, handler: PyObjectRef, vm: &VirtualMachine) {
70+
fn register_error(name: PyStrRef, handler: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
71+
if !handler.is_callable() {
72+
return Err(vm.new_type_error("handler must be callable".to_owned()));
73+
}
7174
vm.state
7275
.codec_registry
7376
.register_error(name.as_str().to_owned(), handler);
77+
Ok(())
7478
}
7579

7680
#[pyfunction]

0 commit comments

Comments
 (0)