@@ -140,6 +140,34 @@ def test_err_restore(self):
140140 self .assertEqual (1 , v .args [0 ])
141141 self .assertIs (tb , v .__traceback__ .tb_next )
142142
143+ def test_set_object (self ):
144+
145+ # new exception as obj is not an exception
146+ with self .assertRaises (ValueError ) as e :
147+ _testcapi .exc_set_object (ValueError , 42 )
148+ self .assertEqual (e .exception .args , (42 ,))
149+
150+ # wraps the exception because unrelated types
151+ with self .assertRaises (ValueError ) as e :
152+ _testcapi .exc_set_object (ValueError , TypeError (1 ,2 ,3 ))
153+ wrapped = e .exception .args [0 ]
154+ self .assertIsInstance (wrapped , TypeError )
155+ self .assertEqual (wrapped .args , (1 , 2 , 3 ))
156+
157+ # is superclass, so does not wrap
158+ with self .assertRaises (PermissionError ) as e :
159+ _testcapi .exc_set_object (OSError , PermissionError (24 ))
160+ self .assertEqual (e .exception .args , (24 ,))
161+
162+ class Meta (type ):
163+ def __subclasscheck__ (cls , sub ):
164+ 1 / 0
165+
166+ class Broken (Exception , metaclass = Meta ):
167+ pass
168+
169+ with self .assertRaises (ZeroDivisionError ) as e :
170+ _testcapi .exc_set_object (Broken , Broken ())
143171
144172if __name__ == "__main__" :
145173 unittest .main ()
0 commit comments