@@ -341,11 +341,7 @@ impl VirtualMachine {
341
341
}
342
342
}
343
343
344
- pub fn invoke < T > ( & self , func_ref : PyObjectRef , args : T ) -> PyResult
345
- where
346
- T : Into < PyFuncArgs > ,
347
- {
348
- let args = args. into ( ) ;
344
+ fn _invoke ( & self , func_ref : PyObjectRef , args : PyFuncArgs ) -> PyResult {
349
345
trace ! ( "Invoke: {:?} {:?}" , func_ref, args) ;
350
346
if let Some ( PyFunction {
351
347
ref code,
@@ -354,22 +350,29 @@ impl VirtualMachine {
354
350
ref kw_only_defaults,
355
351
} ) = func_ref. payload ( )
356
352
{
357
- return self . invoke_python_function ( code, scope, defaults, kw_only_defaults, args) ;
358
- }
359
- if let Some ( PyMethod {
353
+ self . invoke_python_function ( code, scope, defaults, kw_only_defaults, args)
354
+ } else if let Some ( PyMethod {
360
355
ref function,
361
356
ref object,
362
357
} ) = func_ref. payload ( )
363
358
{
364
- return self . invoke ( function. clone ( ) , args. insert ( object. clone ( ) ) ) ;
365
- }
366
- if let Some ( PyBuiltinFunction { ref value } ) = func_ref. payload ( ) {
367
- return value ( self , args) ;
359
+ self . invoke ( function. clone ( ) , args. insert ( object. clone ( ) ) )
360
+ } else if let Some ( PyBuiltinFunction { ref value } ) = func_ref. payload ( ) {
361
+ value ( self , args)
362
+ } else {
363
+ // TODO: is it safe to just invoke __call__ otherwise?
364
+ trace ! ( "invoke __call__ for: {:?}" , & func_ref. payload) ;
365
+ self . call_method ( & func_ref, "__call__" , args)
368
366
}
367
+ }
369
368
370
- // TODO: is it safe to just invoke __call__ otherwise?
371
- trace ! ( "invoke __call__ for: {:?}" , & func_ref. payload) ;
372
- self . call_method ( & func_ref, "__call__" , args)
369
+ // TODO: make func_ref an &PyObjectRef
370
+ #[ inline]
371
+ pub fn invoke < T > ( & self , func_ref : PyObjectRef , args : T ) -> PyResult
372
+ where
373
+ T : Into < PyFuncArgs > ,
374
+ {
375
+ self . _invoke ( func_ref, args. into ( ) )
373
376
}
374
377
375
378
fn invoke_python_function (
@@ -666,6 +669,15 @@ impl VirtualMachine {
666
669
crate :: stdlib:: json:: de_pyobject ( self , s)
667
670
}
668
671
672
+ pub fn is_callable ( & self , obj : & PyObjectRef ) -> bool {
673
+ match_class ! ( obj,
674
+ PyFunction => true ,
675
+ PyMethod => true ,
676
+ PyBuiltinFunction => true ,
677
+ obj => objtype:: class_has_attr( & obj. class( ) , "__call__" ) ,
678
+ )
679
+ }
680
+
669
681
pub fn _sub ( & self , a : PyObjectRef , b : PyObjectRef ) -> PyResult {
670
682
self . call_or_reflection ( a, b, "__sub__" , "__rsub__" , |vm, a, b| {
671
683
Err ( vm. new_unsupported_operand_error ( a, b, "-" ) )
0 commit comments