Merge pull request #925 from Jongy/builtin-chr-raise-error

Raise error on out-of-range input in `chr()`
This commit is contained in:
Windel Bouwman
2019-05-06 17:38:25 +02:00
committed by GitHub
2 changed files with 11 additions and 3 deletions

View File

@@ -0,0 +1,8 @@
from testutils import assert_raises
assert "a" == chr(97)
assert "é" == chr(233)
assert "🤡" == chr(129313)
assert_raises(TypeError, lambda: chr(), "chr() takes exactly one argument (0 given)")
assert_raises(ValueError, lambda: chr(0x110005), "ValueError: chr() arg not in range(0x110000)")

View File

@@ -73,10 +73,10 @@ fn builtin_callable(obj: PyObjectRef, vm: &VirtualMachine) -> bool {
vm.is_callable(&obj)
}
fn builtin_chr(i: u32, _vm: &VirtualMachine) -> String {
fn builtin_chr(i: u32, vm: &VirtualMachine) -> PyResult<String> {
match char::from_u32(i) {
Some(value) => value.to_string(),
None => '_'.to_string(),
Some(value) => Ok(value.to_string()),
None => Err(vm.new_value_error("chr() arg not in range(0x110000)".to_string())),
}
}