4141package com .oracle .graal .python .nodes .control ;
4242
4343import static com .oracle .graal .python .nodes .SpecialMethodNames .__STR__ ;
44+ import static com .oracle .graal .python .nodes .SpecialMethodNames .__REPR__ ;
4445import static com .oracle .graal .python .runtime .exception .PythonErrorType .SystemExit ;
4546
4647import java .io .IOException ;
48+ import java .io .OutputStream ;
49+ import java .nio .charset .StandardCharsets ;
4750
4851import com .oracle .graal .python .PythonLanguage ;
4952import com .oracle .graal .python .builtins .objects .PNone ;
@@ -81,6 +84,7 @@ public class TopLevelExceptionHandler extends RootNode {
8184
8285 @ Child private CreateArgumentsNode createArgs = CreateArgumentsNode .create ();
8386 @ Child private LookupAndCallUnaryNode callStrNode = LookupAndCallUnaryNode .create (__STR__ );
87+ @ Child private LookupAndCallUnaryNode callReprNode = LookupAndCallUnaryNode .create (__REPR__ );
8488 @ Child private CallNode callNode = CallNode .create ();
8589
8690 public TopLevelExceptionHandler (PythonLanguage language , RootNode child ) {
@@ -106,8 +110,9 @@ public Object execute(VirtualFrame frame) {
106110 return null ;
107111 } else {
108112 assert context .get ().getCurrentException () == null ;
113+ Object result ;
109114 try {
110- return run (frame );
115+ result = run (frame );
111116 } catch (PException e ) {
112117 printExc (e );
113118 return null ;
@@ -120,6 +125,23 @@ public Object execute(VirtualFrame frame) {
120125 }
121126 throw e ;
122127 }
128+ if (getSourceSection ().getSource ().isInteractive ()) {
129+ printResult (result );
130+ }
131+ return result ;
132+ }
133+ }
134+
135+ @ TruffleBoundary
136+ private void printResult (Object result ) {
137+ OutputStream out = getCurrentContext (PythonLanguage .class ).getStandardOut ();
138+ String stringResult = callReprNode .executeObject (result ).toString ();
139+ try {
140+ out .write (stringResult .getBytes (StandardCharsets .UTF_8 ));
141+ out .write (System .getProperty ("line.separator" ).getBytes (StandardCharsets .UTF_8 ));
142+ } catch (IOException ioex ) {
143+ // out stream has problems.
144+ throw new IllegalStateException (ioex );
123145 }
124146 }
125147
0 commit comments