mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Merge pull request #925 from Jongy/builtin-chr-raise-error
Raise error on out-of-range input in `chr()`
This commit is contained in:
8
tests/snippets/builtin_chr.py
Normal file
8
tests/snippets/builtin_chr.py
Normal 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)")
|
||||
@@ -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())),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user