@@ -1326,18 +1326,38 @@ def test_exception_with_note(self):
13261326 e = ValueError (42 )
13271327 vanilla = self .get_report (e )
13281328
1329- e .__note__ = 'My Note'
1329+ e .add_note ( 'My Note' , replace = True )
13301330 self .assertEqual (self .get_report (e ), vanilla + 'My Note\n ' )
13311331
1332- e .__note__ = ''
1332+ e .add_note ( '' , replace = True )
13331333 self .assertEqual (self .get_report (e ), vanilla + '\n ' )
13341334
1335- e .__note__ = 'Your Note'
1335+ e .add_note ( 'Your Note' , replace = True )
13361336 self .assertEqual (self .get_report (e ), vanilla + 'Your Note\n ' )
13371337
1338- e .__note__ = None
1338+ e .add_note ( None , replace = True )
13391339 self .assertEqual (self .get_report (e ), vanilla )
13401340
1341+ def test_exception_with_note_with_multiple_notes (self ):
1342+ e = ValueError (42 )
1343+ vanilla = self .get_report (e )
1344+
1345+ e .add_note ('Note 1' )
1346+ e .add_note ('Note 2' )
1347+ e .add_note ('Note 3' )
1348+
1349+ self .assertEqual (
1350+ self .get_report (e ),
1351+ vanilla + 'Note 1\n ' + 'Note 2\n ' + 'Note 3\n ' )
1352+
1353+ e .add_note ('Note 4' , replace = True )
1354+ e .add_note ('Note 5' , replace = True )
1355+ e .add_note ('Note 6' )
1356+
1357+ self .assertEqual (
1358+ self .get_report (e ),
1359+ vanilla + 'Note 5\n ' + 'Note 6\n ' )
1360+
13411361 def test_exception_qualname (self ):
13421362 class A :
13431363 class B :
@@ -1688,16 +1708,16 @@ def exc():
16881708 try :
16891709 raise ValueError (msg )
16901710 except ValueError as e :
1691- e .__note__ = f'the { msg } '
1711+ e .add_note ( f'the { msg } ' )
16921712 excs .append (e )
16931713 raise ExceptionGroup ("nested" , excs )
16941714 except ExceptionGroup as e :
1695- e .__note__ = ('>> Multi line note\n '
1696- '>> Because I am such\n '
1697- '>> an important exception.\n '
1698- '>> empty lines work too\n '
1699- '\n '
1700- '(that was an empty line)' )
1715+ e .add_note ( ('>> Multi line note\n '
1716+ '>> Because I am such\n '
1717+ '>> an important exception.\n '
1718+ '>> empty lines work too\n '
1719+ '\n '
1720+ '(that was an empty line)' ) )
17011721 raise
17021722
17031723 expected = (f' + Exception Group Traceback (most recent call last):\n '
@@ -1733,6 +1753,64 @@ def exc():
17331753 report = self .get_report (exc )
17341754 self .assertEqual (report , expected )
17351755
1756+ def test_exception_group_with_multiple_notes (self ):
1757+ def exc ():
1758+ try :
1759+ excs = []
1760+ for msg in ['bad value' , 'terrible value' ]:
1761+ try :
1762+ raise ValueError (msg )
1763+ except ValueError as e :
1764+ e .add_note (f'the { msg } ' )
1765+ e .add_note (f'Goodbye { msg } ' )
1766+ excs .append (e )
1767+ raise ExceptionGroup ("nested" , excs )
1768+ except ExceptionGroup as e :
1769+ e .add_note (('>> Multi line note\n '
1770+ '>> Because I am such\n '
1771+ '>> an important exception.\n '
1772+ '>> empty lines work too\n '
1773+ '\n '
1774+ '(that was an empty line)' ))
1775+ e .add_note ('Goodbye!' )
1776+ raise
1777+
1778+ expected = (f' + Exception Group Traceback (most recent call last):\n '
1779+ f' | File "{ __file__ } ", line { self .callable_line } , in get_exception\n '
1780+ f' | exception_or_callable()\n '
1781+ f' | ^^^^^^^^^^^^^^^^^^^^^^^\n '
1782+ f' | File "{ __file__ } ", line { exc .__code__ .co_firstlineno + 10 } , in exc\n '
1783+ f' | raise ExceptionGroup("nested", excs)\n '
1784+ f' | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n '
1785+ f' | ExceptionGroup: nested\n '
1786+ f' | >> Multi line note\n '
1787+ f' | >> Because I am such\n '
1788+ f' | >> an important exception.\n '
1789+ f' | >> empty lines work too\n '
1790+ f' | \n '
1791+ f' | (that was an empty line)\n '
1792+ f' | Goodbye!\n '
1793+ f' +-+---------------- 1 ----------------\n '
1794+ f' | Traceback (most recent call last):\n '
1795+ f' | File "{ __file__ } ", line { exc .__code__ .co_firstlineno + 5 } , in exc\n '
1796+ f' | raise ValueError(msg)\n '
1797+ f' | ^^^^^^^^^^^^^^^^^^^^^\n '
1798+ f' | ValueError: bad value\n '
1799+ f' | the bad value\n '
1800+ f' | Goodbye bad value\n '
1801+ f' +---------------- 2 ----------------\n '
1802+ f' | Traceback (most recent call last):\n '
1803+ f' | File "{ __file__ } ", line { exc .__code__ .co_firstlineno + 5 } , in exc\n '
1804+ f' | raise ValueError(msg)\n '
1805+ f' | ^^^^^^^^^^^^^^^^^^^^^\n '
1806+ f' | ValueError: terrible value\n '
1807+ f' | the terrible value\n '
1808+ f' | Goodbye terrible value\n '
1809+ f' +------------------------------------\n ' )
1810+
1811+ report = self .get_report (exc )
1812+ self .assertEqual (report , expected )
1813+
17361814
17371815class PyExcReportingTests (BaseExceptionReportingTests , unittest .TestCase ):
17381816 #
0 commit comments