4242import sys
4343import textwrap
4444
45+ from tests .util import skipIfBytecodeDSL
46+
4547
4648class ExceptStarPrintTest (unittest .TestCase ):
49+ @skipIfBytecodeDSL ("a" )
4750 def test_01_eg_simple (self ):
4851 script = textwrap .dedent ("""
4952 raise ExceptionGroup("eg", [
@@ -62,6 +65,7 @@ def test_01_eg_simple(self):
6265 b' +------------------------------------' ]
6366 self .assertEqual (p .stderr .splitlines (), expected )
6467
68+ @skipIfBytecodeDSL ("a" )
6569 def test_02_eg_nested (self ):
6670 script = textwrap .dedent ("""
6771 raise ExceptionGroup("EG", [
@@ -166,6 +170,7 @@ def test_02_eg_nested(self):
166170 self .maxDiff = None
167171 self .assertEqual (p .stderr .splitlines (), expected )
168172
173+ @skipIfBytecodeDSL ("a" )
169174 def test_03_eg_nested_truncated (self ):
170175 script = textwrap .dedent ("""
171176 raise ExceptionGroup("EG", [
@@ -379,3 +384,177 @@ def test_03_eg_nested_truncated(self):
379384 b' +------------------------------------' ]
380385 self .maxDiff = None
381386 self .assertEqual (p .stderr .splitlines (), expected )
387+
388+ @skipIfBytecodeDSL ("a" )
389+ def test_04_eg_cause (self ):
390+ script = textwrap .dedent ("""
391+ EG = ExceptionGroup
392+ try:
393+ raise EG("eg1", [ValueError(1), TypeError(2)])
394+ except Exception as e:
395+ raise EG("eg2", [ValueError(3), TypeError(4)]) from e
396+ """ )
397+
398+ p = subprocess .run ([sys .executable , "-c" , script ], capture_output = True )
399+
400+ expected = [b' + Exception Group Traceback (most recent call last):' ,
401+ b' | File "<string>", line 4, in <module>' ,
402+ b' | ExceptionGroup: eg1 (2 sub-exceptions)' ,
403+ b' +-+---------------- 1 ----------------' ,
404+ b' | ValueError: 1' ,
405+ b' +---------------- 2 ----------------' ,
406+ b' | TypeError: 2' ,
407+ b' +------------------------------------' ,
408+ b'' ,
409+ b'The above exception was the direct cause of the following exception:' ,
410+ b'' ,
411+ b' + Exception Group Traceback (most recent call last):' ,
412+ b' | File "<string>", line 6, in <module>' ,
413+ b' | ExceptionGroup: eg2 (2 sub-exceptions)' ,
414+ b' +-+---------------- 1 ----------------' ,
415+ b' | ValueError: 3' ,
416+ b' +---------------- 2 ----------------' ,
417+ b' | TypeError: 4' ,
418+ b' +------------------------------------' ]
419+
420+ self .maxDiff = None
421+ self .assertEqual (p .stderr .splitlines (), expected )
422+
423+ @skipIfBytecodeDSL ("a" )
424+ def test_05_eg_context_with_context (self ):
425+ script = textwrap .dedent ("""
426+ EG = ExceptionGroup
427+ try:
428+ try:
429+ raise EG("eg1", [ValueError(1), TypeError(2)])
430+ except EG:
431+ raise EG("eg2", [ValueError(3), TypeError(4)])
432+ except EG:
433+ raise ImportError(5)
434+ """ )
435+
436+ p = subprocess .run ([sys .executable , "-c" , script ], capture_output = True )
437+
438+ expected = [b' + Exception Group Traceback (most recent call last):' ,
439+ b' | File "<string>", line 5, in <module>' ,
440+ b' | ExceptionGroup: eg1 (2 sub-exceptions)' ,
441+ b' +-+---------------- 1 ----------------' ,
442+ b' | ValueError: 1' ,
443+ b' +---------------- 2 ----------------' ,
444+ b' | TypeError: 2' ,
445+ b' +------------------------------------' ,
446+ b'' ,
447+ b'During handling of the above exception, another exception occurred:' ,
448+ b'' ,
449+ b' + Exception Group Traceback (most recent call last):' ,
450+ b' | File "<string>", line 7, in <module>' ,
451+ b' | ExceptionGroup: eg2 (2 sub-exceptions)' ,
452+ b' +-+---------------- 1 ----------------' ,
453+ b' | ValueError: 3' ,
454+ b' +---------------- 2 ----------------' ,
455+ b' | TypeError: 4' ,
456+ b' +------------------------------------' ,
457+ b'' ,
458+ b'During handling of the above exception, another exception occurred:' ,
459+ b'' ,
460+ b'Traceback (most recent call last):' ,
461+ b' File "<string>", line 9, in <module>' ,
462+ b'ImportError: 5' ,]
463+
464+ self .maxDiff = None
465+ self .assertEqual (p .stderr .splitlines (), expected )
466+
467+ @skipIfBytecodeDSL ("a" )
468+ def test_06_eg_nested_with_context (self ):
469+ script = textwrap .dedent ("""
470+ EG = ExceptionGroup
471+ VE = ValueError
472+ TE = TypeError
473+ try:
474+ try:
475+ raise EG("nested", [TE(2), TE(3)])
476+ except Exception as e:
477+ exc = e
478+ raise EG("eg", [VE(1), exc, VE(4)])
479+ except EG:
480+ raise EG("top", [VE(5)])
481+ """ )
482+
483+ p = subprocess .run ([sys .executable , "-c" , script ], capture_output = True )
484+
485+ expected = [b' + Exception Group Traceback (most recent call last):' ,
486+ b' | File "<string>", line 10, in <module>' ,
487+ b' | ExceptionGroup: eg (3 sub-exceptions)' ,
488+ b' +-+---------------- 1 ----------------' ,
489+ b' | ValueError: 1' ,
490+ b' +---------------- 2 ----------------' ,
491+ b' | Exception Group Traceback (most recent call last):' ,
492+ b' | File "<string>", line 7, in <module>' ,
493+ b' | ExceptionGroup: nested (2 sub-exceptions)' ,
494+ b' +-+---------------- 1 ----------------' ,
495+ b' | TypeError: 2' ,
496+ b' +---------------- 2 ----------------' ,
497+ b' | TypeError: 3' ,
498+ b' +------------------------------------' ,
499+ b' +---------------- 3 ----------------' ,
500+ b' | ValueError: 4' ,
501+ b' +------------------------------------' ,
502+ b'' ,
503+ b'During handling of the above exception, another exception occurred:' ,
504+ b'' ,
505+ b' + Exception Group Traceback (most recent call last):' ,
506+ b' | File "<string>", line 12, in <module>' ,
507+ b' | ExceptionGroup: top (1 sub-exception)' ,
508+ b' +-+---------------- 1 ----------------' ,
509+ b' | ValueError: 5' ,
510+ b' +------------------------------------' ,]
511+
512+ self .maxDiff = None
513+ self .assertEqual (p .stderr .splitlines (), expected )
514+
515+ def test_07_eg_with_notes (self ):
516+ script = textwrap .dedent ("""
517+ try:
518+ excs = []
519+ for msg in ['bad value', 'terrible value']:
520+ try:
521+ raise ValueError(msg)
522+ except ValueError as e:
523+ e.add_note(f'the {msg}')
524+ excs.append(e)
525+ raise ExceptionGroup("nested", excs)
526+ except ExceptionGroup as e:
527+ e.add_note(('>> Multi line note\\ n'
528+ '>> Because I am such\\ n'
529+ '>> an important exception.\\ n'
530+ '>> empty lines work too\\ n'
531+ '\\ n'
532+ '(that was an empty line)'))
533+ raise
534+ """ )
535+
536+ p = subprocess .run ([sys .executable , "-c" , script ], capture_output = True )
537+
538+ expected = [b' + Exception Group Traceback (most recent call last):' ,
539+ b' | File "<string>", line 10, in <module>' ,
540+ b' | ExceptionGroup: nested (2 sub-exceptions)' ,
541+ b' | >> Multi line note' ,
542+ b' | >> Because I am such' ,
543+ b' | >> an important exception.' ,
544+ b' | >> empty lines work too' ,
545+ b' | ' ,
546+ b' | (that was an empty line)' ,
547+ b' +-+---------------- 1 ----------------' ,
548+ b' | Traceback (most recent call last):' ,
549+ b' | File "<string>", line 6, in <module>' ,
550+ b' | ValueError: bad value' ,
551+ b' | the bad value' ,
552+ b' +---------------- 2 ----------------' ,
553+ b' | Traceback (most recent call last):' ,
554+ b' | File "<string>", line 6, in <module>' ,
555+ b' | ValueError: terrible value' ,
556+ b' | the terrible value' ,
557+ b' +------------------------------------' ,]
558+
559+ self .maxDiff = None
560+ self .assertEqual (p .stderr .splitlines (), expected )
0 commit comments