add bytearray and bytes input types for ord()
@@ -28,6 +28,7 @@ use crate::pyobject::{
};
use crate::vm::VirtualMachine;
+use crate::obj::objbyteinner::PyByteInner;
#[cfg(not(target_arch = "wasm32"))]
use crate::stdlib::io::io_open;
@@ -530,7 +531,8 @@ fn builtin_oct(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
}
fn builtin_ord(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
- arg_check!(vm, args, required = [(string, Some(vm.ctx.str_type()))]);
+ arg_check!(vm, args, required = [(string, None)]);
+ if objtype::isinstance(string, &vm.ctx.str_type()) {
let string = objstr::borrow_value(string);
let string_len = string.chars().count();
if string_len != 1 {
@@ -545,6 +547,24 @@ fn builtin_ord(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
"ord() could not guess the integer representing this character".to_string(),
)),
}
+ } else if objtype::isinstance(string, &vm.ctx.bytearray_type())
+ || objtype::isinstance(string, &vm.ctx.bytes_type())
+ {
+ let inner = PyByteInner::try_from_object(vm, string.clone()).unwrap();
+ let bytes_len = inner.elements.len();
+ if bytes_len != 1 {
+ return Err(vm.new_type_error(format!(
+ "ord() expected a character, but string of length {} found",
+ bytes_len
+ )));
+ }
+ Ok(vm.context().new_int(inner.elements[0]))
+ } else {
+ Err(vm.new_type_error(format!(
+ "ord() expected a string, bytes or bytearray, but found {}",
+ string.class().name
+ )))
+ }
}
fn builtin_pow(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
