@@ -50,8 +50,8 @@ fn dir_object(vm: &mut VirtualMachine, obj: &PyObjectRef) -> PyObjectRef {
50
50
51
51
fn builtin_abs ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
52
52
arg_check ! ( vm, args, required = [ ( x, None ) ] ) ;
53
- match vm. get_attribute ( x . clone ( ) , & "__abs__" ) {
54
- Ok ( attrib ) => vm . invoke ( attrib , PyFuncArgs :: new ( vec ! [ ] , vec ! [ ] ) ) ,
53
+ match vm. call_method ( x , "__abs__" , vec ! [ ] ) {
54
+ Ok ( result ) => Ok ( result ) ,
55
55
Err ( ..) => Err ( vm. new_type_error ( "bad operand for abs" . to_string ( ) ) ) ,
56
56
}
57
57
}
@@ -134,8 +134,8 @@ fn builtin_dir(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
134
134
135
135
fn builtin_divmod ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
136
136
arg_check ! ( vm, args, required = [ ( x, None ) , ( y, None ) ] ) ;
137
- match vm. get_attribute ( x. clone ( ) , & "__divmod__" ) {
138
- Ok ( attrib ) => vm . invoke ( attrib , PyFuncArgs :: new ( vec ! [ y . clone ( ) ] , vec ! [ ] ) ) ,
137
+ match vm. call_method ( & x. clone ( ) , "__divmod__" , vec ! [ y . clone ( ) ] ) {
138
+ Ok ( result ) => Ok ( result ) ,
139
139
Err ( ..) => Err ( vm. new_type_error ( "unsupported operand type(s) for divmod" . to_string ( ) ) ) ,
140
140
}
141
141
}
@@ -218,11 +218,7 @@ fn builtin_getattr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
218
218
args,
219
219
required = [ ( obj, None ) , ( attr, Some ( vm. ctx. str_type( ) ) ) ]
220
220
) ;
221
- if let PyObjectKind :: String { ref value } = attr. borrow ( ) . kind {
222
- vm. get_attribute ( obj. clone ( ) , value)
223
- } else {
224
- panic ! ( "argument checking failure: attr not string" )
225
- }
221
+ vm. call_method ( & obj. clone ( ) , "__getattribute__" , vec ! [ attr. clone( ) ] )
226
222
}
227
223
228
224
// builtin_globals
@@ -233,15 +229,11 @@ fn builtin_hasattr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
233
229
args,
234
230
required = [ ( obj, None ) , ( attr, Some ( vm. ctx. str_type( ) ) ) ]
235
231
) ;
236
- if let PyObjectKind :: String { ref value } = attr. borrow ( ) . kind {
237
- let has_attr = match vm. get_attribute ( obj. clone ( ) , value) {
238
- Ok ( ..) => true ,
239
- Err ( ..) => false ,
240
- } ;
241
- Ok ( vm. context ( ) . new_bool ( has_attr) )
242
- } else {
243
- panic ! ( "argument checking failure: attr not string" )
244
- }
232
+ let has_attr = match vm. call_method ( & obj. clone ( ) , "__getattribute__" , vec ! [ attr. clone( ) ] ) {
233
+ Ok ( ..) => true ,
234
+ Err ( ..) => false ,
235
+ } ;
236
+ Ok ( vm. context ( ) . new_bool ( has_attr) )
245
237
}
246
238
247
239
fn builtin_hash ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
@@ -306,19 +298,7 @@ fn builtin_len(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
306
298
PyObjectKind :: Tuple { ref elements } => {
307
299
Ok ( vm. context ( ) . new_int ( elements. len ( ) . to_bigint ( ) . unwrap ( ) ) )
308
300
}
309
- _ => {
310
- let len_method_name = "__len__" . to_string ( ) ;
311
- match vm. get_attribute ( obj. clone ( ) , & len_method_name) {
312
- Ok ( value) => vm. invoke ( value, PyFuncArgs :: default ( ) ) ,
313
- Err ( ..) => Err ( vm. context ( ) . new_str (
314
- format ! (
315
- "TypeError: object of this {:?} type has no method {:?}" ,
316
- obj, len_method_name
317
- )
318
- . to_string ( ) ,
319
- ) ) ,
320
- }
321
- }
301
+ _ => vm. call_method ( & obj. clone ( ) , "__len__" , vec ! [ ] ) ,
322
302
}
323
303
}
324
304
0 commit comments