File tree 1 file changed +44
-1
lines changed
1 file changed +44
-1
lines changed Original file line number Diff line number Diff line change @@ -531,7 +531,50 @@ impl VirtualMachine {
531
531
}
532
532
533
533
pub fn _and ( & mut self , a : PyObjectRef , b : PyObjectRef ) -> PyResult {
534
- self . call_method ( & a, "__and__" , vec ! [ b] )
534
+ // 1. Try __and__, next __rand__, next, give up
535
+ if let Ok ( method) = self . get_method ( a. clone ( ) , "__and__" ) {
536
+ match self . invoke (
537
+ method,
538
+ PyFuncArgs {
539
+ args : vec ! [ b. clone( ) ] ,
540
+ kwargs : vec ! [ ] ,
541
+ } ,
542
+ ) {
543
+ Ok ( value) => return Ok ( value) ,
544
+ Err ( err) => {
545
+ if !objtype:: isinstance ( & err, & self . ctx . exceptions . not_implemented_error ) {
546
+ return Err ( err) ;
547
+ }
548
+ }
549
+ }
550
+ }
551
+
552
+ // 2. try __rand__
553
+ if let Ok ( method) = self . get_method ( b. clone ( ) , "__rand__" ) {
554
+ match self . invoke (
555
+ method,
556
+ PyFuncArgs {
557
+ args : vec ! [ a. clone( ) ] ,
558
+ kwargs : vec ! [ ] ,
559
+ } ,
560
+ ) {
561
+ Ok ( value) => return Ok ( value) ,
562
+ Err ( err) => {
563
+ if !objtype:: isinstance ( & err, & self . ctx . exceptions . not_implemented_error ) {
564
+ return Err ( err) ;
565
+ }
566
+ }
567
+ }
568
+ }
569
+
570
+ // 3. It all failed :(
571
+ // Cannot and a and b
572
+ let a_type_name = objtype:: get_type_name ( & a. typ ( ) ) ;
573
+ let b_type_name = objtype:: get_type_name ( & b. typ ( ) ) ;
574
+ Err ( self . new_type_error ( format ! (
575
+ "Unsupported operand types for '&': '{}' and '{}'" ,
576
+ a_type_name, b_type_name
577
+ ) ) )
535
578
}
536
579
537
580
pub fn _eq ( & mut self , a : & PyObjectRef , b : PyObjectRef ) -> PyResult {
You can’t perform that action at this time.
0 commit comments