@@ -14,6 +14,8 @@ use super::pyobject::{
14
14
PyObjectRef , PyResult , Scope , TypeProtocol ,
15
15
} ;
16
16
use super :: vm:: VirtualMachine ;
17
+ use num_bigint:: ToBigInt ;
18
+ use num_traits:: { Signed , ToPrimitive } ;
17
19
18
20
fn get_locals ( vm : & mut VirtualMachine ) -> PyObjectRef {
19
21
let d = vm. new_dict ( ) ;
@@ -80,9 +82,10 @@ fn builtin_bin(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
80
82
arg_check ! ( vm, args, required = [ ( number, Some ( vm. ctx. int_type( ) ) ) ] ) ;
81
83
82
84
let n = objint:: get_value ( number) ;
83
- let s = match n. signum ( ) {
84
- -1 => format ! ( "-0b{:b}" , n. abs( ) ) ,
85
- _ => format ! ( "0b{:b}" , n) ,
85
+ let s = if n. is_negative ( ) {
86
+ format ! ( "-0b{:b}" , n. abs( ) )
87
+ } else {
88
+ format ! ( "0b{:b}" , n)
86
89
} ;
87
90
88
91
Ok ( vm. new_str ( s) )
@@ -97,15 +100,7 @@ fn builtin_bin(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
97
100
fn builtin_chr ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
98
101
arg_check ! ( vm, args, required = [ ( i, Some ( vm. ctx. int_type( ) ) ) ] ) ;
99
102
100
- let code_point_obj = i. borrow ( ) ;
101
-
102
- let code_point = * match code_point_obj. kind {
103
- PyObjectKind :: Integer { ref value } => value,
104
- ref kind => panic ! (
105
- "argument checking failure: chr not supported for {:?}" ,
106
- kind
107
- ) ,
108
- } as u32 ;
103
+ let code_point = objint:: get_value ( i) . to_u32 ( ) . unwrap ( ) ;
109
104
110
105
let txt = match char:: from_u32 ( code_point) {
111
106
Some ( value) => value. to_string ( ) ,
@@ -261,9 +256,10 @@ fn builtin_hex(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
261
256
arg_check ! ( vm, args, required = [ ( number, Some ( vm. ctx. int_type( ) ) ) ] ) ;
262
257
263
258
let n = objint:: get_value ( number) ;
264
- let s = match n. signum ( ) {
265
- -1 => format ! ( "-0x{:x}" , n. abs( ) ) ,
266
- _ => format ! ( "0x{:x}" , n) ,
259
+ let s = if n. is_negative ( ) {
260
+ format ! ( "-0x{:x}" , n. abs( ) )
261
+ } else {
262
+ format ! ( "0x{:x}" , n)
267
263
} ;
268
264
269
265
Ok ( vm. new_str ( s) )
@@ -272,7 +268,7 @@ fn builtin_hex(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
272
268
fn builtin_id ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
273
269
arg_check ! ( vm, args, required = [ ( obj, None ) ] ) ;
274
270
275
- Ok ( vm. context ( ) . new_int ( obj. get_id ( ) as i32 ) )
271
+ Ok ( vm. context ( ) . new_int ( obj. get_id ( ) . to_bigint ( ) . unwrap ( ) ) )
276
272
}
277
273
278
274
// builtin_input
@@ -304,8 +300,12 @@ fn builtin_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
304
300
fn builtin_len ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
305
301
arg_check ! ( vm, args, required = [ ( obj, None ) ] ) ;
306
302
match obj. borrow ( ) . kind {
307
- PyObjectKind :: Dict { ref elements } => Ok ( vm. context ( ) . new_int ( elements. len ( ) as i32 ) ) ,
308
- PyObjectKind :: Tuple { ref elements } => Ok ( vm. context ( ) . new_int ( elements. len ( ) as i32 ) ) ,
303
+ PyObjectKind :: Dict { ref elements } => {
304
+ Ok ( vm. context ( ) . new_int ( elements. len ( ) . to_bigint ( ) . unwrap ( ) ) )
305
+ }
306
+ PyObjectKind :: Tuple { ref elements } => {
307
+ Ok ( vm. context ( ) . new_int ( elements. len ( ) . to_bigint ( ) . unwrap ( ) ) )
308
+ }
309
309
_ => {
310
310
let len_method_name = "__len__" . to_string ( ) ;
311
311
match vm. get_attribute ( obj. clone ( ) , & len_method_name) {
@@ -427,7 +427,9 @@ fn builtin_ord(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
427
427
) ) ;
428
428
}
429
429
match string. chars ( ) . next ( ) {
430
- Some ( character) => Ok ( vm. context ( ) . new_int ( character as i32 ) ) ,
430
+ Some ( character) => Ok ( vm
431
+ . context ( )
432
+ . new_int ( ( character as i32 ) . to_bigint ( ) . unwrap ( ) ) ) ,
431
433
None => Err ( vm. new_type_error (
432
434
"ord() could not guess the integer representing this character" . to_string ( ) ,
433
435
) ) ,
@@ -486,8 +488,9 @@ fn builtin_range(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
486
488
arg_check ! ( vm, args, required = [ ( range, Some ( vm. ctx. int_type( ) ) ) ] ) ;
487
489
match range. borrow ( ) . kind {
488
490
PyObjectKind :: Integer { ref value } => {
489
- let range_elements: Vec < PyObjectRef > =
490
- ( 0 ..* value) . map ( |num| vm. context ( ) . new_int ( num) ) . collect ( ) ;
491
+ let range_elements: Vec < PyObjectRef > = ( 0 ..value. to_i32 ( ) . unwrap ( ) )
492
+ . map ( |num| vm. context ( ) . new_int ( num. to_bigint ( ) . unwrap ( ) ) )
493
+ . collect ( ) ;
491
494
Ok ( vm. context ( ) . new_list ( range_elements) )
492
495
}
493
496
_ => panic ! ( "argument checking failure: first argument to range must be an integer" ) ,
0 commit comments