@@ -6,96 +6,76 @@ use std::char;
6
6
use std:: io:: { self , Write } ;
7
7
use std:: path:: PathBuf ;
8
8
9
- use num_traits:: { Signed , ToPrimitive } ;
9
+ use num_traits:: Signed ;
10
10
11
11
use crate :: compile;
12
12
use crate :: import:: import_module;
13
13
use crate :: obj:: objbool;
14
14
use crate :: obj:: objdict:: PyDictRef ;
15
- use crate :: obj:: objint;
15
+ use crate :: obj:: objint:: { self , PyIntRef } ;
16
16
use crate :: obj:: objiter;
17
- use crate :: obj:: objstr:: { self , PyStringRef } ;
17
+ use crate :: obj:: objstr:: { self , PyString , PyStringRef } ;
18
18
use crate :: obj:: objtype:: { self , PyClassRef } ;
19
19
20
20
use crate :: frame:: Scope ;
21
21
use crate :: function:: { Args , OptionalArg , PyFuncArgs } ;
22
22
use crate :: pyobject:: {
23
- DictProtocol , IdProtocol , PyContext , PyObjectRef , PyResult , TryFromObject , TypeProtocol ,
23
+ DictProtocol , IdProtocol , PyContext , PyIterable , PyObjectRef , PyResult , PyValue , TryFromObject ,
24
+ TypeProtocol ,
24
25
} ;
25
26
use crate :: vm:: VirtualMachine ;
26
27
27
28
use crate :: obj:: objcode:: PyCodeRef ;
28
29
#[ cfg( not( target_arch = "wasm32" ) ) ]
29
30
use crate :: stdlib:: io:: io_open;
30
31
31
- fn builtin_abs ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
32
- arg_check ! ( vm, args, required = [ ( x, None ) ] ) ;
32
+ fn builtin_abs ( x : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
33
33
match vm. get_method ( x. clone ( ) , "__abs__" ) {
34
34
Ok ( attrib) => vm. invoke ( attrib, PyFuncArgs :: new ( vec ! [ ] , vec ! [ ] ) ) ,
35
35
Err ( ..) => Err ( vm. new_type_error ( "bad operand for abs" . to_string ( ) ) ) ,
36
36
}
37
37
}
38
38
39
- fn builtin_all ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
40
- arg_check ! ( vm, args, required = [ ( iterable, None ) ] ) ;
41
- let items = vm. extract_elements ( iterable) ?;
42
- for item in items {
43
- let result = objbool:: boolval ( vm, item) ?;
44
- if !result {
45
- return Ok ( vm. new_bool ( false ) ) ;
39
+ fn builtin_all ( iterable : PyIterable < bool > , vm : & VirtualMachine ) -> PyResult < bool > {
40
+ for item in iterable. iter ( vm) ? {
41
+ if !item? {
42
+ return Ok ( false ) ;
46
43
}
47
44
}
48
- Ok ( vm . new_bool ( true ) )
45
+ Ok ( true )
49
46
}
50
47
51
- fn builtin_any ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
52
- arg_check ! ( vm, args, required = [ ( iterable, None ) ] ) ;
53
- let iterator = objiter:: get_iter ( vm, iterable) ?;
54
-
55
- while let Some ( item) = objiter:: get_next_object ( vm, & iterator) ? {
56
- let result = objbool:: boolval ( vm, item) ?;
57
- if result {
58
- return Ok ( vm. new_bool ( true ) ) ;
48
+ fn builtin_any ( iterable : PyIterable < bool > , vm : & VirtualMachine ) -> PyResult < bool > {
49
+ for item in iterable. iter ( vm) ? {
50
+ if item? {
51
+ return Ok ( true ) ;
59
52
}
60
53
}
61
-
62
- Ok ( vm. new_bool ( false ) )
54
+ Ok ( false )
63
55
}
64
56
65
57
// builtin_ascii
66
58
67
- fn builtin_bin ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
68
- arg_check ! ( vm, args, required = [ ( number, Some ( vm. ctx. int_type( ) ) ) ] ) ;
69
-
70
- let n = objint:: get_value ( number) ;
71
- let s = if n. is_negative ( ) {
72
- format ! ( "-0b{:b}" , n. abs( ) )
59
+ fn builtin_bin ( x : PyIntRef , _vm : & VirtualMachine ) -> String {
60
+ let x = x. as_bigint ( ) ;
61
+ if x. is_negative ( ) {
62
+ format ! ( "-0b{:b}" , x. abs( ) )
73
63
} else {
74
- format ! ( "0b{:b}" , n)
75
- } ;
76
-
77
- Ok ( vm. new_str ( s) )
64
+ format ! ( "0b{:b}" , x)
65
+ }
78
66
}
79
67
80
68
// builtin_breakpoint
81
69
82
- fn builtin_callable ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
83
- arg_check ! ( vm, args, required = [ ( obj, None ) ] ) ;
84
- let is_callable = objtype:: class_has_attr ( & obj. class ( ) , "__call__" ) ;
85
- Ok ( vm. new_bool ( is_callable) )
70
+ fn builtin_callable ( obj : PyObjectRef , _vm : & VirtualMachine ) -> bool {
71
+ objtype:: class_has_attr ( & obj. class ( ) , "__call__" )
86
72
}
87
73
88
- fn builtin_chr ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
89
- arg_check ! ( vm, args, required = [ ( i, Some ( vm. ctx. int_type( ) ) ) ] ) ;
90
-
91
- let code_point = objint:: get_value ( i) . to_u32 ( ) . unwrap ( ) ;
92
-
93
- let txt = match char:: from_u32 ( code_point) {
74
+ fn builtin_chr ( i : u32 , _vm : & VirtualMachine ) -> String {
75
+ match char:: from_u32 ( i) {
94
76
Some ( value) => value. to_string ( ) ,
95
77
None => '_' . to_string ( ) ,
96
- } ;
97
-
98
- Ok ( vm. new_str ( txt) )
78
+ }
99
79
}
100
80
101
81
fn builtin_compile (
@@ -128,13 +108,8 @@ fn builtin_compile(
128
108
} )
129
109
}
130
110
131
- fn builtin_delattr ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
132
- arg_check ! (
133
- vm,
134
- args,
135
- required = [ ( obj, None ) , ( attr, Some ( vm. ctx. str_type( ) ) ) ]
136
- ) ;
137
- vm. del_attr ( obj, attr. clone ( ) )
111
+ fn builtin_delattr ( obj : PyObjectRef , attr : PyStringRef , vm : & VirtualMachine ) -> PyResult < ( ) > {
112
+ vm. del_attr ( & obj, attr. into_object ( ) )
138
113
}
139
114
140
115
fn builtin_dir ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
@@ -258,17 +233,26 @@ fn make_scope(
258
233
Ok ( Scope :: new ( locals, globals) )
259
234
}
260
235
261
- fn builtin_format ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
262
- arg_check ! (
263
- vm,
264
- args,
265
- required = [ ( obj, None ) ] ,
266
- optional = [ ( format_spec, Some ( vm. ctx. str_type( ) ) ) ]
267
- ) ;
268
- let format_spec = format_spec
269
- . cloned ( )
270
- . unwrap_or_else ( || vm. new_str ( "" . to_string ( ) ) ) ;
271
- vm. call_method ( obj, "__format__" , vec ! [ format_spec] )
236
+ fn builtin_format (
237
+ value : PyObjectRef ,
238
+ format_spec : OptionalArg < PyStringRef > ,
239
+ vm : & VirtualMachine ,
240
+ ) -> PyResult < PyStringRef > {
241
+ let format_spec = format_spec. into_option ( ) . unwrap_or_else ( || {
242
+ PyString {
243
+ value : "" . to_string ( ) ,
244
+ }
245
+ . into_ref ( vm)
246
+ } ) ;
247
+
248
+ vm. call_method ( & value, "__format__" , vec ! [ format_spec. into_object( ) ] ) ?
249
+ . downcast ( )
250
+ . map_err ( |obj| {
251
+ vm. new_type_error ( format ! (
252
+ "__format__ must return a str, not {}" ,
253
+ obj. class( ) . name
254
+ ) )
255
+ } )
272
256
}
273
257
274
258
fn catch_attr_exception < T > ( ex : PyObjectRef , default : T , vm : & VirtualMachine ) -> PyResult < T > {
@@ -644,14 +628,11 @@ fn builtin_sorted(vm: &VirtualMachine, mut args: PyFuncArgs) -> PyResult {
644
628
Ok ( lst)
645
629
}
646
630
647
- fn builtin_sum ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
648
- arg_check ! ( vm, args, required = [ ( iterable, None ) ] ) ;
649
- let items = vm. extract_elements ( iterable) ?;
650
-
631
+ fn builtin_sum ( iterable : PyIterable , start : OptionalArg , vm : & VirtualMachine ) -> PyResult {
651
632
// Start with zero and add at will:
652
- let mut sum = vm. ctx . new_int ( 0 ) ;
653
- for item in items {
654
- sum = vm. _add ( sum, item) ?;
633
+ let mut sum = start . into_option ( ) . unwrap_or_else ( || vm. ctx . new_int ( 0 ) ) ;
634
+ for item in iterable . iter ( vm ) ? {
635
+ sum = vm. _add ( sum, item? ) ?;
655
636
}
656
637
Ok ( sum)
657
638
}
0 commit comments