@@ -5,8 +5,8 @@ use crate::obj::objstr::PyStringRef;
5
5
use crate :: obj:: objtype:: PyClassRef ;
6
6
use crate :: obj:: { objbool, objiter} ;
7
7
use crate :: pyobject:: {
8
- BorrowValue , Either , IntoPyObject , PyClassImpl , PyIterable , PyObjectRef , PyRef , PyResult ,
9
- PyValue , TryFromObject ,
8
+ Either , IntoPyObject , PyClassImpl , PyIterable , PyObjectRef , PyRef , PyResult , PyValue ,
9
+ TryFromObject , PyComparisonValue , PyArithmaticValue ,
10
10
} ;
11
11
use crate :: VirtualMachine ;
12
12
@@ -438,113 +438,94 @@ impl PyArray {
438
438
}
439
439
440
440
#[ pymethod( name = "__eq__" ) ]
441
- fn eq ( lhs : PyObjectRef , rhs : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
442
- let lhs = class_or_notimplemented ! ( vm, Self , lhs) ;
443
- let rhs = class_or_notimplemented ! ( vm, Self , rhs) ;
444
- let lhs = lhs. borrow_value ( ) ;
441
+ fn eq ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyResult < PyComparisonValue > {
442
+ let lhs = self . borrow_value ( ) ;
443
+ let rhs = class_or_notimplemented ! ( vm, Self , other) ;
445
444
let rhs = rhs. borrow_value ( ) ;
446
445
if lhs. len ( ) != rhs. len ( ) {
447
- Ok ( vm . ctx . new_bool ( false ) )
446
+ Ok ( PyArithmaticValue :: Implemented ( false ) )
448
447
} else {
449
448
for ( a, b) in lhs. iter ( vm) . zip ( rhs. iter ( vm) ) {
450
449
let ne = objbool:: boolval ( vm, vm. _ne ( a, b) ?) ?;
451
450
if ne {
452
- return Ok ( vm . ctx . new_bool ( false ) ) ;
451
+ return Ok ( PyArithmaticValue :: Implemented ( false ) )
453
452
}
454
453
}
455
- Ok ( vm . ctx . new_bool ( true ) )
454
+ Ok ( PyArithmaticValue :: Implemented ( true ) )
456
455
}
457
456
}
458
457
459
458
#[ pymethod( name = "__ne__" ) ]
460
- fn ne ( lhs : PyObjectRef , rhs : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
461
- let lhs = class_or_notimplemented ! ( vm, Self , lhs) ;
462
- let rhs = class_or_notimplemented ! ( vm, Self , rhs) ;
463
- let lhs = lhs. borrow_value ( ) ;
464
- let rhs = rhs. borrow_value ( ) ;
465
- if lhs. len ( ) != rhs. len ( ) {
466
- Ok ( vm. new_bool ( true ) )
467
- } else {
468
- for ( a, b) in lhs. iter ( vm) . zip ( rhs. iter ( vm) ) {
469
- let ne = objbool:: boolval ( vm, vm. _ne ( a?, b?) ?) ?;
470
- if ne {
471
- return Ok ( vm. new_bool ( true ) ) ;
472
- }
473
- }
474
- Ok ( vm. new_bool ( false ) )
475
- }
459
+ fn ne ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyResult < PyComparisonValue > {
460
+ Ok ( self . eq ( other, vm) ?. map ( |v| !v) )
476
461
}
477
462
478
463
#[ pymethod( name = "__lt__" ) ]
479
- fn lt ( lhs : PyObjectRef , rhs : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
480
- let lhs = class_or_notimplemented ! ( vm, Self , lhs) ;
481
- let rhs = class_or_notimplemented ! ( vm, Self , rhs) ;
482
- let lhs = lhs. borrow_value ( ) ;
464
+ fn lt ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyResult < PyComparisonValue > {
465
+ let lhs = self . borrow_value ( ) ;
466
+ let rhs = class_or_notimplemented ! ( vm, Self , other) ;
483
467
let rhs = rhs. borrow_value ( ) ;
484
468
485
469
for ( a, b) in lhs. iter ( vm) . zip ( rhs. iter ( vm) ) {
486
470
let lt = objbool:: boolval ( vm, vm. _lt ( a, b) ?) ?;
487
471
488
472
if lt {
489
- return Ok ( vm . ctx . new_bool ( true ) ) ;
473
+ return Ok ( PyArithmaticValue :: Implemented ( true ) ) ;
490
474
}
491
475
}
492
476
493
- Ok ( vm . ctx . new_bool ( lhs. len ( ) < rhs. len ( ) ) )
477
+ Ok ( PyArithmaticValue :: Implemented ( lhs. len ( ) < rhs. len ( ) ) )
494
478
}
495
479
496
480
#[ pymethod( name = "__le__" ) ]
497
- fn le ( lhs : PyObjectRef , rhs : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
498
- let lhs = class_or_notimplemented ! ( vm, Self , lhs) ;
499
- let rhs = class_or_notimplemented ! ( vm, Self , rhs) ;
500
- let lhs = lhs. borrow_value ( ) ;
481
+ fn le ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyResult < PyComparisonValue > {
482
+ let lhs = self . borrow_value ( ) ;
483
+ let rhs = class_or_notimplemented ! ( vm, Self , other) ;
501
484
let rhs = rhs. borrow_value ( ) ;
502
485
503
486
for ( a, b) in lhs. iter ( vm) . zip ( rhs. iter ( vm) ) {
504
487
let le = objbool:: boolval ( vm, vm. _le ( a, b) ?) ?;
505
488
506
489
if le {
507
- return Ok ( vm . ctx . new_bool ( true ) ) ;
490
+ return Ok ( PyArithmaticValue :: Implemented ( true ) ) ;
508
491
}
509
492
}
510
493
511
- Ok ( vm . ctx . new_bool ( lhs. len ( ) <= rhs. len ( ) ) )
494
+ Ok ( PyArithmaticValue :: Implemented ( lhs. len ( ) <= rhs. len ( ) ) )
512
495
}
513
496
514
497
#[ pymethod( name = "__gt__" ) ]
515
- fn gt ( lhs : PyObjectRef , rhs : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
516
- let lhs = class_or_notimplemented ! ( vm, Self , lhs) ;
517
- let rhs = class_or_notimplemented ! ( vm, Self , rhs) ;
518
- let lhs = lhs. borrow_value ( ) ;
498
+ fn gt ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyResult < PyComparisonValue > {
499
+ let lhs = self . borrow_value ( ) ;
500
+ let rhs = class_or_notimplemented ! ( vm, Self , other) ;
519
501
let rhs = rhs. borrow_value ( ) ;
520
502
521
503
for ( a, b) in lhs. iter ( vm) . zip ( rhs. iter ( vm) ) {
522
504
let gt = objbool:: boolval ( vm, vm. _gt ( a, b) ?) ?;
523
505
524
506
if gt {
525
- return Ok ( vm . ctx . new_bool ( true ) ) ;
507
+ return Ok ( PyArithmaticValue :: Implemented ( true ) ) ;
526
508
}
527
509
}
528
510
529
- Ok ( vm . ctx . new_bool ( lhs. len ( ) > rhs. len ( ) ) )
511
+ Ok ( PyArithmaticValue :: Implemented ( lhs. len ( ) > rhs. len ( ) ) )
530
512
}
531
513
532
514
#[ pymethod( name = "__ge__" ) ]
533
- fn ge ( lhs : PyObjectRef , rhs : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
534
- let lhs = class_or_notimplemented ! ( vm, Self , lhs) ;
535
- let rhs = class_or_notimplemented ! ( vm, Self , rhs) ;
536
- let lhs = lhs. borrow_value ( ) ;
515
+ fn ge ( & self , other : PyObjectRef , vm : & VirtualMachine ) -> PyResult < PyComparisonValue > {
516
+ let lhs = self . borrow_value ( ) ;
517
+ let rhs = class_or_notimplemented ! ( vm, Self , other) ;
537
518
let rhs = rhs. borrow_value ( ) ;
538
519
539
520
for ( a, b) in lhs. iter ( vm) . zip ( rhs. iter ( vm) ) {
540
521
let ge = objbool:: boolval ( vm, vm. _ge ( a, b) ?) ?;
541
522
542
523
if ge {
543
- return Ok ( vm . ctx . new_bool ( true ) ) ;
524
+ return Ok ( PyArithmaticValue :: Implemented ( true ) ) ;
544
525
}
545
526
}
546
527
547
- Ok ( vm . ctx . new_bool ( lhs. len ( ) >= rhs. len ( ) ) )
528
+ Ok ( PyArithmaticValue :: Implemented ( lhs. len ( ) >= rhs. len ( ) ) )
548
529
}
549
530
550
531
#[ pymethod( name = "__len__" ) ]
0 commit comments