@@ -270,3 +270,54 @@ def test_local_translator(msg):
270
270
m .throws_local_simple_error ()
271
271
assert not isinstance (excinfo .value , cm .LocalSimpleException )
272
272
assert msg (excinfo .value ) == "this mod"
273
+
274
+
275
+ class FlakyException (Exception ):
276
+ def __init__ (self , failure_point ):
277
+ if failure_point == "failure_point_init" :
278
+ raise ValueError ("triggered_failure_point_init" )
279
+ self .failure_point = failure_point
280
+
281
+ def __str__ (self ):
282
+ if self .failure_point == "failure_point_str" :
283
+ raise ValueError ("triggered_failure_point_str" )
284
+ return "FlakyException.__str__"
285
+
286
+
287
+ @pytest .mark .parametrize (
288
+ "exc_type, exc_value, expected_what" ,
289
+ (
290
+ (ValueError , "plain_str" , "ValueError: plain_str" ),
291
+ (ValueError , ("tuple_elem" ,), "ValueError: ('tuple_elem',)" ),
292
+ (FlakyException , ("happy" ,), "FlakyException: ('happy',)" ),
293
+ ),
294
+ )
295
+ def test_error_already_set_what_with_happy_exceptions (
296
+ exc_type , exc_value , expected_what
297
+ ):
298
+ what , py_err_set_after_what = m .error_already_set_what (exc_type , exc_value )
299
+ assert not py_err_set_after_what
300
+ assert what == expected_what
301
+
302
+
303
+ def test_flaky_exception_failure_point_init ():
304
+ what , py_err_set_after_what = m .error_already_set_what (
305
+ FlakyException , ("failure_point_init" ,)
306
+ )
307
+ assert not py_err_set_after_what
308
+ lines = what .splitlines ()
309
+ # PyErr_NormalizeException replaces the original FlakyException with ValueError:
310
+ assert lines [:3 ] == ["FlakyException: ('failure_point_init',)" , "" , "At:" ]
311
+ # Checking the first two lines of the traceback as formatted in error_string(),
312
+ # which is actually for a different exception (ValueError)!
313
+ assert "test_exceptions.py(" in lines [3 ]
314
+ assert lines [3 ].endswith ("): __init__" )
315
+ assert lines [4 ].endswith ("): test_flaky_exception_failure_point_init" )
316
+
317
+
318
+ def test_flaky_exception_failure_point_str ():
319
+ what , py_err_set_after_what = m .error_already_set_what (
320
+ FlakyException , ("failure_point_str" ,)
321
+ )
322
+ assert not py_err_set_after_what
323
+ assert what == "FlakyException: ('failure_point_str',)"
0 commit comments