@@ -4594,18 +4594,16 @@ def test_special_unbound_method_types(self):
4594
4594
def test_not_implemented (self ):
4595
4595
# Testing NotImplemented...
4596
4596
# all binary methods should be able to return a NotImplemented
4597
- import operator
4598
4597
4599
4598
def specialmethod (self , other ):
4600
4599
return NotImplemented
4601
4600
4602
4601
def check (expr , x , y ):
4603
- try :
4604
- exec (expr , {'x' : x , 'y' : y , 'operator' : operator })
4605
- except TypeError :
4606
- pass
4607
- else :
4608
- self .fail ("no TypeError from %r" % (expr ,))
4602
+ with (
4603
+ self .subTest (expr = expr , x = x , y = y ),
4604
+ self .assertRaises (TypeError ),
4605
+ ):
4606
+ exec (expr , {'x' : x , 'y' : y })
4609
4607
4610
4608
N1 = sys .maxsize + 1 # might trigger OverflowErrors instead of
4611
4609
# TypeErrors
@@ -4626,12 +4624,23 @@ def check(expr, x, y):
4626
4624
('__and__' , 'x & y' , 'x &= y' ),
4627
4625
('__or__' , 'x | y' , 'x |= y' ),
4628
4626
('__xor__' , 'x ^ y' , 'x ^= y' )]:
4629
- rname = '__r' + name [ 2 :]
4627
+ # Defines 'left' magic method:
4630
4628
A = type ('A' , (), {name : specialmethod })
4631
4629
a = A ()
4632
4630
check (expr , a , a )
4633
4631
check (expr , a , N1 )
4634
4632
check (expr , a , N2 )
4633
+ # Defines 'right' magic method:
4634
+ rname = '__r' + name [2 :]
4635
+ B = type ('B' , (), {rname : specialmethod })
4636
+ b = B ()
4637
+ check (expr , b , b )
4638
+ check (expr , a , b )
4639
+ check (expr , b , a )
4640
+ check (expr , b , N1 )
4641
+ check (expr , b , N2 )
4642
+ check (expr , N1 , b )
4643
+ check (expr , N2 , b )
4635
4644
if iexpr :
4636
4645
check (iexpr , a , a )
4637
4646
check (iexpr , a , N1 )
0 commit comments