@@ -16,7 +16,7 @@ use crate::obj::objcode::PyCodeRef;
16
16
use crate :: obj:: objdict:: PyDictRef ;
17
17
use crate :: obj:: objint:: { self , PyIntRef } ;
18
18
use crate :: obj:: objiter;
19
- use crate :: obj:: objstr:: { self , PyString , PyStringRef } ;
19
+ use crate :: obj:: objstr:: { PyString , PyStringRef } ;
20
20
use crate :: obj:: objtype:: { self , PyClassRef } ;
21
21
#[ cfg( feature = "rustpython-compiler" ) ]
22
22
use rustpython_compiler:: compile;
@@ -499,40 +499,34 @@ fn builtin_oct(number: PyIntRef, vm: &VirtualMachine) -> PyResult {
499
499
Ok ( vm. new_str ( s) )
500
500
}
501
501
502
- fn builtin_ord ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
503
- arg_check ! ( vm, args, required = [ ( string, None ) ] ) ;
504
- if objtype:: isinstance ( string, & vm. ctx . str_type ( ) ) {
505
- let string = objstr:: borrow_value ( string) ;
506
- let string_len = string. chars ( ) . count ( ) ;
507
- if string_len != 1 {
508
- return Err ( vm. new_type_error ( format ! (
509
- "ord() expected a character, but string of length {} found" ,
510
- string_len
511
- ) ) ) ;
512
- }
513
- match string. chars ( ) . next ( ) {
514
- Some ( character) => Ok ( vm. context ( ) . new_int ( character as i32 ) ) ,
515
- None => Err ( vm. new_type_error (
516
- "ord() could not guess the integer representing this character" . to_string ( ) ,
517
- ) ) ,
502
+ fn builtin_ord ( string : Either < PyByteInner , PyStringRef > , vm : & VirtualMachine ) -> PyResult {
503
+ match string {
504
+ Either :: A ( bytes) => {
505
+ let bytes_len = bytes. elements . len ( ) ;
506
+ if bytes_len != 1 {
507
+ return Err ( vm. new_type_error ( format ! (
508
+ "ord() expected a character, but string of length {} found" ,
509
+ bytes_len
510
+ ) ) ) ;
511
+ }
512
+ Ok ( vm. context ( ) . new_int ( bytes. elements [ 0 ] ) )
518
513
}
519
- } else if objtype:: isinstance ( string, & vm. ctx . bytearray_type ( ) )
520
- || objtype:: isinstance ( string, & vm. ctx . bytes_type ( ) )
521
- {
522
- let inner = PyByteInner :: try_from_object ( vm, string. clone ( ) ) . unwrap ( ) ;
523
- let bytes_len = inner. elements . len ( ) ;
524
- if bytes_len != 1 {
525
- return Err ( vm. new_type_error ( format ! (
526
- "ord() expected a character, but string of length {} found" ,
527
- bytes_len
528
- ) ) ) ;
514
+ Either :: B ( string) => {
515
+ let string = string. as_str ( ) ;
516
+ let string_len = string. chars ( ) . count ( ) ;
517
+ if string_len != 1 {
518
+ return Err ( vm. new_type_error ( format ! (
519
+ "ord() expected a character, but string of length {} found" ,
520
+ string_len
521
+ ) ) ) ;
522
+ }
523
+ match string. chars ( ) . next ( ) {
524
+ Some ( character) => Ok ( vm. context ( ) . new_int ( character as i32 ) ) ,
525
+ None => Err ( vm. new_type_error (
526
+ "ord() could not guess the integer representing this character" . to_string ( ) ,
527
+ ) ) ,
528
+ }
529
529
}
530
- Ok ( vm. context ( ) . new_int ( inner. elements [ 0 ] ) )
531
- } else {
532
- Err ( vm. new_type_error ( format ! (
533
- "ord() expected a string, bytes or bytearray, but found {}" ,
534
- string. class( ) . name
535
- ) ) )
536
530
}
537
531
}
538
532
0 commit comments