@@ -133,6 +133,34 @@ def test_exc_info(self):
133133 else :
134134 self .assertTrue (False )
135135
136+ def test_set_object (self ):
137+ # new exception as obj is not an exception
138+ with self .assertRaises (ValueError ) as e :
139+ _testcapi .exc_set_object (ValueError , 42 )
140+ self .assertEqual (e .exception .args , (42 ,))
141+
142+ # wraps the exception because unrelated types
143+ with self .assertRaises (ValueError ) as e :
144+ _testcapi .exc_set_object (ValueError , TypeError (1 ,2 ,3 ))
145+ wrapped = e .exception .args [0 ]
146+ self .assertIsInstance (wrapped , TypeError )
147+ self .assertEqual (wrapped .args , (1 , 2 , 3 ))
148+
149+ # is superclass, so does not wrap
150+ with self .assertRaises (PermissionError ) as e :
151+ _testcapi .exc_set_object (OSError , PermissionError (24 ))
152+ self .assertEqual (e .exception .args , (24 ,))
153+
154+ class Meta (type ):
155+ def __subclasscheck__ (cls , sub ):
156+ 1 / 0
157+
158+ class Broken (Exception , metaclass = Meta ):
159+ pass
160+
161+ with self .assertRaises (ZeroDivisionError ) as e :
162+ _testcapi .exc_set_object (Broken , Broken ())
163+
136164 @unittest .skipUnless (_posixsubprocess , '_posixsubprocess required for this test.' )
137165 def test_seq_bytes_to_charp_array (self ):
138166 # Issue #15732: crash in _PySequence_BytesToCharpArray()
0 commit comments