@@ -546,6 +546,29 @@ def testAttributes(self):
546
546
'pickled "%r", attribute "%s' %
547
547
(e , checkArgName ))
548
548
549
+ def test_setstate (self ):
550
+ e = Exception (42 )
551
+ e .blah = 53
552
+ self .assertEqual (e .args , (42 ,))
553
+ self .assertEqual (e .blah , 53 )
554
+ self .assertRaises (AttributeError , getattr , e , 'a' )
555
+ self .assertRaises (AttributeError , getattr , e , 'b' )
556
+ e .__setstate__ ({'a' : 1 , 'b' : 2 })
557
+ self .assertEqual (e .args , (42 ,))
558
+ self .assertEqual (e .blah , 53 )
559
+ self .assertEqual (e .a , 1 )
560
+ self .assertEqual (e .b , 2 )
561
+ e .__setstate__ ({'a' : 11 , 'args' : (1 ,2 ,3 ), 'blah' : 35 })
562
+ self .assertEqual (e .args , (1 ,2 ,3 ))
563
+ self .assertEqual (e .blah , 35 )
564
+ self .assertEqual (e .a , 11 )
565
+ self .assertEqual (e .b , 2 )
566
+
567
+ def test_invalid_setstate (self ):
568
+ e = Exception (42 )
569
+ with self .assertRaisesRegex (TypeError , "state is not a dictionary" ):
570
+ e .__setstate__ (42 )
571
+
549
572
def test_notes (self ):
550
573
for e in [BaseException (1 ), Exception (2 ), ValueError (3 )]:
551
574
with self .subTest (e = e ):
@@ -602,11 +625,30 @@ def testInvalidTraceback(self):
602
625
else :
603
626
self .fail ("No exception raised" )
604
627
605
- def testInvalidAttrs (self ):
606
- self .assertRaises (TypeError , setattr , Exception (), '__cause__' , 1 )
607
- self .assertRaises (TypeError , delattr , Exception (), '__cause__' )
608
- self .assertRaises (TypeError , setattr , Exception (), '__context__' , 1 )
609
- self .assertRaises (TypeError , delattr , Exception (), '__context__' )
628
+ def test_invalid_setattr (self ):
629
+ TE = TypeError
630
+ exc = Exception ()
631
+ msg = "'int' object is not iterable"
632
+ self .assertRaisesRegex (TE , msg , setattr , exc , 'args' , 1 )
633
+ msg = "__traceback__ must be a traceback or None"
634
+ self .assertRaisesRegex (TE , msg , setattr , exc , '__traceback__' , 1 )
635
+ msg = "exception cause must be None or derive from BaseException"
636
+ self .assertRaisesRegex (TE , msg , setattr , exc , '__cause__' , 1 )
637
+ msg = "exception context must be None or derive from BaseException"
638
+ self .assertRaisesRegex (TE , msg , setattr , exc , '__context__' , 1 )
639
+
640
+ def test_invalid_delattr (self ):
641
+ TE = TypeError
642
+ try :
643
+ raise IndexError (4 )
644
+ except Exception as e :
645
+ exc = e
646
+
647
+ msg = "may not be deleted"
648
+ self .assertRaisesRegex (TE , msg , delattr , exc , 'args' )
649
+ self .assertRaisesRegex (TE , msg , delattr , exc , '__traceback__' )
650
+ self .assertRaisesRegex (TE , msg , delattr , exc , '__cause__' )
651
+ self .assertRaisesRegex (TE , msg , delattr , exc , '__context__' )
610
652
611
653
def testNoneClearsTracebackAttr (self ):
612
654
try :
0 commit comments