Skip to content

Commit f4ce4ba

Browse files
committed
new tests and changes
1 parent bb5ce9f commit f4ce4ba

3 files changed

Lines changed: 196 additions & 5 deletions

File tree

graalpython/com.oracle.graal.python.test/src/tests/test_except_star.py

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@
4242
import sys
4343
import textwrap
4444

45+
from tests.util import skipIfBytecodeDSL
46+
4547

4648
class 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)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SysModuleBuiltins.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,7 @@ private void writeUnraisableExc(Node inliningTarget, TracebackBuiltins.GetTraceb
12531253
}
12541254

12551255
if (excTb != PNone.NONE) {
1256-
PyTraceBackPrint.print(inliningTarget, getTbFrameNode, materializeStNode, sys, out, excTb, false, 0, null);
1256+
PyTraceBackPrint.print(inliningTarget, getTbFrameNode, materializeStNode, sys, out, excTb, false, false, 0, null);
12571257
}
12581258

12591259
if (excType == PNone.NONE) {
@@ -1580,7 +1580,11 @@ protected void printException(Node inliningTarget, TracebackBuiltins.GetTracebac
15801580

15811581
final Object tb = getExceptionTraceback(value);
15821582
if (tb instanceof PTraceback) {
1583-
PyTraceBackPrint.print(inliningTarget, getTbFrameNode, materializeStNode, sys, out, tb, (value instanceof PBaseExceptionGroup), ctx.getIndent(), ctx.getMargin());
1583+
if (value instanceof PBaseExceptionGroup pbeg) {
1584+
PyTraceBackPrint.print(inliningTarget, getTbFrameNode, materializeStNode, sys, out, tb, true, ctx.depthCurrent > 1, ctx.getIndent(), ctx.getMargin());
1585+
} else {
1586+
PyTraceBackPrint.print(inliningTarget, getTbFrameNode, materializeStNode, sys, out, tb, false, ctx.depthCurrent == 0, ctx.getIndent(), ctx.getMargin());
1587+
}
15841588
}
15851589

15861590
if (objectHasAttr(value, T_ATTR_PRINT_FILE_AND_LINE)) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyTraceBackPrint.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ private static void printInternal(Node inliningTarget, TracebackBuiltins.GetTrac
382382
}
383383

384384
public static void print(Node inliningTarget, TracebackBuiltins.GetTracebackFrameNode getTbFrameNode, TracebackBuiltins.MaterializeTruffleStacktraceNode materializeStNode, PythonModule sys,
385-
Object out, Object tbObj, boolean isExceptionGroup, int indent, TruffleString margin) {
385+
Object out, Object tbObj, boolean isExceptionGroup, boolean printMarginControl, int indent, TruffleString margin) {
386386
// Although we should be behind TB, we need cached nodes, because they may do stack walking
387387
// and for that they must be connected to the currently executing root. In practice, it's
388388
// not strictly necessary, because they will never request the current frame, but in order
@@ -405,9 +405,17 @@ public static void print(Node inliningTarget, TracebackBuiltins.GetTracebackFram
405405
}
406406
}
407407
if (isExceptionGroup) {
408-
printIndentedHeader(out, "Exception Group Traceback (most recent call last):", indent, "+ ");
408+
if (printMarginControl) {
409+
printIndentedHeader(out, "Exception Group Traceback (most recent call last):", indent, margin.toString());
410+
} else {
411+
printIndentedHeader(out, "Exception Group Traceback (most recent call last):", indent, "+ ");
412+
}
409413
} else {
410-
printIndentedHeader(out, "Traceback (most recent call last):", indent, "");
414+
if (printMarginControl) {
415+
printIndentedHeader(out, "Traceback (most recent call last):", indent, "");
416+
} else {
417+
printIndentedHeader(out, "Traceback (most recent call last):", indent, margin.toString());
418+
}
411419
}
412420
printInternal(inliningTarget, getTbFrameNode, materializeStNode, out, tb, limit, indent, margin);
413421
} else {

0 commit comments

Comments
 (0)