101101import static com .oracle .graal .python .nodes .BuiltinNames .T___DISPLAYHOOK__ ;
102102import static com .oracle .graal .python .nodes .BuiltinNames .T___EXCEPTHOOK__ ;
103103import static com .oracle .graal .python .nodes .BuiltinNames .T___GRAALPYTHON__ ;
104+ import static com .oracle .graal .python .nodes .BuiltinNames .T___NOTES__ ;
104105import static com .oracle .graal .python .nodes .BuiltinNames .T___STDERR__ ;
105106import static com .oracle .graal .python .nodes .BuiltinNames .T___STDIN__ ;
106107import static com .oracle .graal .python .nodes .BuiltinNames .T___STDOUT__ ;
115116import static com .oracle .graal .python .nodes .SpecialAttributeNames .T___ ;
116117import static com .oracle .graal .python .nodes .SpecialAttributeNames .T___MODULE__ ;
117118import static com .oracle .graal .python .nodes .SpecialMethodNames .T___SIZEOF__ ;
119+ import static com .oracle .graal .python .nodes .StringLiterals .J_NEWLINE ;
118120import static com .oracle .graal .python .nodes .StringLiterals .T_BACKSLASHREPLACE ;
119121import static com .oracle .graal .python .nodes .StringLiterals .T_BASE_PREFIX ;
120122import static com .oracle .graal .python .nodes .StringLiterals .T_BIG ;
180182import com .oracle .graal .python .builtins .objects .function .PArguments ;
181183import com .oracle .graal .python .builtins .objects .function .PKeyword ;
182184import com .oracle .graal .python .builtins .objects .ints .PInt ;
185+ import com .oracle .graal .python .builtins .objects .iterator .IteratorNodes ;
183186import com .oracle .graal .python .builtins .objects .list .PList ;
184187import com .oracle .graal .python .builtins .objects .module .PythonModule ;
185188import com .oracle .graal .python .builtins .objects .namespace .PSimpleNamespace ;
@@ -1518,13 +1521,13 @@ private static void printErrorText(Object out, SyntaxErrData syntaxErrData) {
15181521
15191522 @ TruffleBoundary
15201523 void printExceptionRecursive (Node inliningTarget , TracebackBuiltins .GetTracebackFrameNode getTbFrameNode , TracebackBuiltins .MaterializeTruffleStacktraceNode materializeStNode ,
1521- PythonModule sys , Object out , Object value , Set <Object > seen ) {
1522- printExceptionRecursive (inliningTarget , getTbFrameNode , materializeStNode , sys , out , value , seen , new ExceptionPrintContext ());
1524+ PythonModule sys , Object out , Object value , Set <Object > seen , IteratorNodes . ToArrayNode toArrayNode ) {
1525+ printExceptionRecursive (inliningTarget , getTbFrameNode , materializeStNode , sys , out , value , seen , new ExceptionPrintContext (), toArrayNode );
15231526 }
15241527
15251528 @ TruffleBoundary
15261529 void printExceptionRecursive (Node inliningTarget , TracebackBuiltins .GetTracebackFrameNode getTbFrameNode , TracebackBuiltins .MaterializeTruffleStacktraceNode materializeStNode ,
1527- PythonModule sys , Object out , Object value , Set <Object > seen , ExceptionPrintContext ctx ) {
1530+ PythonModule sys , Object out , Object value , Set <Object > seen , ExceptionPrintContext ctx , IteratorNodes . ToArrayNode toArrayNode ) {
15281531 if (seen != null ) {
15291532 // Exception chaining
15301533 add (seen , value );
@@ -1535,22 +1538,22 @@ void printExceptionRecursive(Node inliningTarget, TracebackBuiltins.GetTraceback
15351538 boolean needsToEnd = ctx .needsToEnd ;
15361539 if (cause != PNone .NONE ) {
15371540 if (notSeen (seen , cause )) {
1538- printExceptionRecursive (inliningTarget , getTbFrameNode , materializeStNode , sys , out , cause , seen , ctx );
1541+ printExceptionRecursive (inliningTarget , getTbFrameNode , materializeStNode , sys , out , cause , seen , ctx , toArrayNode );
15391542 fileWriteString (out , T_CAUSE_MESSAGE );
15401543 }
15411544 } else if (context != PNone .NONE && !ExceptionNodes .GetSuppressContextNode .executeUncached (value )) {
15421545 if (notSeen (seen , context )) {
1543- printExceptionRecursive (inliningTarget , getTbFrameNode , materializeStNode , sys , out , context , seen , ctx );
1546+ printExceptionRecursive (inliningTarget , getTbFrameNode , materializeStNode , sys , out , context , seen , ctx , toArrayNode );
15441547 fileWriteString (out , T_CONTEXT_MESSAGE );
15451548 }
15461549 }
15471550 ctx .needsToEnd = needsToEnd ;
15481551 }
15491552 }
15501553 if (value instanceof PBaseExceptionGroup ) {
1551- printExceptionGroup (inliningTarget , getTbFrameNode , materializeStNode , sys , out , value , seen , ctx );
1554+ printExceptionGroup (inliningTarget , getTbFrameNode , materializeStNode , sys , out , value , seen , ctx , toArrayNode );
15521555 } else {
1553- printException (inliningTarget , getTbFrameNode , materializeStNode , sys , out , value , ctx );
1556+ printException (inliningTarget , getTbFrameNode , materializeStNode , sys , out , value , ctx , toArrayNode );
15541557 }
15551558 }
15561559
@@ -1568,7 +1571,7 @@ protected static void fileWriteIndentedString(Object file, TruffleString string,
15681571 }
15691572
15701573 protected void printException (Node inliningTarget , TracebackBuiltins .GetTracebackFrameNode getTbFrameNode , TracebackBuiltins .MaterializeTruffleStacktraceNode materializeStNode ,
1571- PythonModule sys , Object out , Object excValue , ExceptionPrintContext ctx ) {
1574+ PythonModule sys , Object out , Object excValue , ExceptionPrintContext ctx , IteratorNodes . ToArrayNode toArrayNode ) {
15721575 Object value = excValue ;
15731576 final Object type = getObjectClass (value );
15741577 if (!PyExceptionInstanceCheckNode .executeUncached (value )) {
@@ -1644,10 +1647,35 @@ protected void printException(Node inliningTarget, TracebackBuiltins.GetTracebac
16441647 }
16451648
16461649 fileWriteString (out , T_NEWLINE );
1650+
1651+ if (objectHasAttr (value , T___NOTES__ )) {
1652+ // print notes
1653+ Object notes = objectLookupAttr (value , T___NOTES__ );
1654+ if (notes instanceof PList noteList ) {
1655+ Object [] arr = toArrayNode .execute (null , noteList );
1656+ for (Object oStr : arr ) {
1657+ if (oStr instanceof TruffleString note ) {
1658+ String n = note .toString ();
1659+ if (n .contains (J_NEWLINE )) {
1660+ String [] lines = n .split (J_NEWLINE );
1661+ for (String line : lines ) {
1662+ fileWriteIndentedString (out , ctx .getMargin (), ctx .getIndent ());
1663+ fileWriteString (out , line );
1664+ fileWriteString (out , T_NEWLINE );
1665+ }
1666+ } else {
1667+ fileWriteIndentedString (out , ctx .getMargin (), ctx .getIndent ());
1668+ fileWriteString (out , note );
1669+ fileWriteString (out , T_NEWLINE );
1670+ }
1671+ }
1672+ }
1673+ }
1674+ }
16471675 }
16481676
16491677 protected void printExceptionGroup (Node inliningTarget , TracebackBuiltins .GetTracebackFrameNode getTbFrameNode , TracebackBuiltins .MaterializeTruffleStacktraceNode materializeStNode ,
1650- PythonModule sys , Object out , Object excValue , Set <Object > seen , ExceptionPrintContext ctx ) {
1678+ PythonModule sys , Object out , Object excValue , Set <Object > seen , ExceptionPrintContext ctx , IteratorNodes . ToArrayNode toArrayNode ) {
16511679 Object value = excValue ;
16521680 final Object type = getObjectClass (value );
16531681 if (!PyExceptionGroupInstanceCheckNode .executeUncached (value )) {
@@ -1669,7 +1697,7 @@ protected void printExceptionGroup(Node inliningTarget, TracebackBuiltins.GetTra
16691697 ctx .increaseDepth ();
16701698 }
16711699
1672- printException (inliningTarget , getTbFrameNode , materializeStNode , sys , out , excValue , ctx );
1700+ printException (inliningTarget , getTbFrameNode , materializeStNode , sys , out , excValue , ctx , toArrayNode );
16731701
16741702 PBaseExceptionGroup exceptionGroup = (PBaseExceptionGroup ) excValue ;
16751703 int counter = 1 ;
@@ -1688,7 +1716,7 @@ protected void printExceptionGroup(Node inliningTarget, TracebackBuiltins.GetTra
16881716 fileWriteString (out , String .format ("+---------------- %d ----------------" , counter ));
16891717 fileWriteString (out , T_NEWLINE );
16901718 ctx .increaseDepth ();
1691- printExceptionRecursive (inliningTarget , getTbFrameNode , materializeStNode , sys , out , exception , seen , ctx );
1719+ printExceptionRecursive (inliningTarget , getTbFrameNode , materializeStNode , sys , out , exception , seen , ctx , toArrayNode );
16921720 ctx .decreaseDepth ();
16931721 } else {
16941722 fileWriteString (out , "+---------------- ... ----------------" );
@@ -1757,10 +1785,11 @@ Object doHookWithTb(VirtualFrame frame, PythonModule sys, @SuppressWarnings("unu
17571785 @ Bind Node inliningTarget ,
17581786 @ Shared @ Cached TracebackBuiltins .GetTracebackFrameNode getTbFrameNode ,
17591787 @ Shared @ Cached TracebackBuiltins .MaterializeTruffleStacktraceNode materializeStNode ,
1760- @ Shared @ Cached ("createFor($node)" ) BoundaryCallData boundaryCallData ) {
1788+ @ Shared @ Cached ("createFor($node)" ) BoundaryCallData boundaryCallData ,
1789+ @ Shared @ Cached IteratorNodes .ToArrayNode toArrayNode ) {
17611790 Object saved = BoundaryCallContext .enter (frame , boundaryCallData );
17621791 try {
1763- doHookWithTbImpl (inliningTarget , getTbFrameNode , materializeStNode , sys , value , traceBack );
1792+ doHookWithTbImpl (inliningTarget , getTbFrameNode , materializeStNode , sys , value , traceBack , toArrayNode );
17641793 } finally {
17651794 BoundaryCallContext .exit (frame , boundaryCallData , saved );
17661795 }
@@ -1769,10 +1798,10 @@ Object doHookWithTb(VirtualFrame frame, PythonModule sys, @SuppressWarnings("unu
17691798
17701799 @ TruffleBoundary
17711800 private void doHookWithTbImpl (Node inliningTarget , TracebackBuiltins .GetTracebackFrameNode getTbFrameNode , TracebackBuiltins .MaterializeTruffleStacktraceNode materializeStNode ,
1772- PythonModule sys , Object value , PTraceback traceBack ) {
1801+ PythonModule sys , Object value , PTraceback traceBack , IteratorNodes . ToArrayNode toArrayNode ) {
17731802 setExceptionTraceback (value , traceBack );
17741803 Object stdErr = objectLookupAttr (sys , T_STDERR );
1775- printExceptionRecursive (inliningTarget , getTbFrameNode , materializeStNode , sys , stdErr , value , createSet ());
1804+ printExceptionRecursive (inliningTarget , getTbFrameNode , materializeStNode , sys , stdErr , value , createSet (), toArrayNode );
17761805 fileFlush (stdErr );
17771806 }
17781807
@@ -1781,10 +1810,11 @@ Object doHookWithoutTb(VirtualFrame frame, PythonModule sys, @SuppressWarnings("
17811810 @ Bind Node inliningTarget ,
17821811 @ Shared @ Cached TracebackBuiltins .GetTracebackFrameNode getTbFrameNode ,
17831812 @ Shared @ Cached TracebackBuiltins .MaterializeTruffleStacktraceNode materializeStNode ,
1784- @ Shared @ Cached ("createFor($node)" ) BoundaryCallData boundaryCallData ) {
1813+ @ Shared @ Cached ("createFor($node)" ) BoundaryCallData boundaryCallData ,
1814+ @ Shared @ Cached IteratorNodes .ToArrayNode toArrayNode ) {
17851815 Object saved = BoundaryCallContext .enter (frame , boundaryCallData );
17861816 try {
1787- doHookWithoutTbImpl (inliningTarget , getTbFrameNode , materializeStNode , sys , value );
1817+ doHookWithoutTbImpl (inliningTarget , getTbFrameNode , materializeStNode , sys , value , toArrayNode );
17881818 } finally {
17891819 BoundaryCallContext .exit (frame , boundaryCallData , saved );
17901820 }
@@ -1793,9 +1823,9 @@ Object doHookWithoutTb(VirtualFrame frame, PythonModule sys, @SuppressWarnings("
17931823
17941824 @ TruffleBoundary
17951825 private void doHookWithoutTbImpl (Node inliningTarget , TracebackBuiltins .GetTracebackFrameNode getTbFrameNode , TracebackBuiltins .MaterializeTruffleStacktraceNode materializeStNode ,
1796- PythonModule sys , Object value ) {
1826+ PythonModule sys , Object value , IteratorNodes . ToArrayNode toArrayNode ) {
17971827 Object stdErr = objectLookupAttr (sys , T_STDERR );
1798- printExceptionRecursive (inliningTarget , getTbFrameNode , materializeStNode , sys , stdErr , value , createSet ());
1828+ printExceptionRecursive (inliningTarget , getTbFrameNode , materializeStNode , sys , stdErr , value , createSet (), toArrayNode );
17991829 fileFlush (stdErr );
18001830 }
18011831 }
0 commit comments