@@ -28,6 +28,7 @@ use crate::pyobject::{
28
28
} ;
29
29
use crate :: vm:: VirtualMachine ;
30
30
31
+ use crate :: obj:: objbyteinner:: PyByteInner ;
31
32
#[ cfg( not( target_arch = "wasm32" ) ) ]
32
33
use crate :: stdlib:: io:: io_open;
33
34
@@ -530,20 +531,39 @@ fn builtin_oct(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
530
531
}
531
532
532
533
fn builtin_ord ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
533
- arg_check ! ( vm, args, required = [ ( string, Some ( vm. ctx. str_type( ) ) ) ] ) ;
534
- let string = objstr:: borrow_value ( string) ;
535
- let string_len = string. chars ( ) . count ( ) ;
536
- if string_len != 1 {
537
- return Err ( vm. new_type_error ( format ! (
538
- "ord() expected a character, but string of length {} found" ,
539
- string_len
540
- ) ) ) ;
541
- }
542
- match string. chars ( ) . next ( ) {
543
- Some ( character) => Ok ( vm. context ( ) . new_int ( character as i32 ) ) ,
544
- None => Err ( vm. new_type_error (
545
- "ord() could not guess the integer representing this character" . to_string ( ) ,
546
- ) ) ,
534
+ arg_check ! ( vm, args, required = [ ( string, None ) ] ) ;
535
+ if objtype:: isinstance ( string, & vm. ctx . str_type ( ) ) {
536
+ let string = objstr:: borrow_value ( string) ;
537
+ let string_len = string. chars ( ) . count ( ) ;
538
+ if string_len != 1 {
539
+ return Err ( vm. new_type_error ( format ! (
540
+ "ord() expected a character, but string of length {} found" ,
541
+ string_len
542
+ ) ) ) ;
543
+ }
544
+ match string. chars ( ) . next ( ) {
545
+ Some ( character) => Ok ( vm. context ( ) . new_int ( character as i32 ) ) ,
546
+ None => Err ( vm. new_type_error (
547
+ "ord() could not guess the integer representing this character" . to_string ( ) ,
548
+ ) ) ,
549
+ }
550
+ } else if objtype:: isinstance ( string, & vm. ctx . bytearray_type ( ) )
551
+ || objtype:: isinstance ( string, & vm. ctx . bytes_type ( ) )
552
+ {
553
+ let inner = PyByteInner :: try_from_object ( vm, string. clone ( ) ) . unwrap ( ) ;
554
+ let bytes_len = inner. elements . len ( ) ;
555
+ if bytes_len != 1 {
556
+ return Err ( vm. new_type_error ( format ! (
557
+ "ord() expected a character, but string of length {} found" ,
558
+ bytes_len
559
+ ) ) ) ;
560
+ }
561
+ Ok ( vm. context ( ) . new_int ( inner. elements [ 0 ] ) )
562
+ } else {
563
+ Err ( vm. new_type_error ( format ! (
564
+ "ord() expected a string, bytes or bytearray, but found {}" ,
565
+ string. class( ) . name
566
+ ) ) )
547
567
}
548
568
}
549
569
0 commit comments