41
41
package com .oracle .graal .python .nodes .control ;
42
42
43
43
import static com .oracle .graal .python .nodes .SpecialMethodNames .__STR__ ;
44
+ import static com .oracle .graal .python .nodes .SpecialMethodNames .__REPR__ ;
44
45
import static com .oracle .graal .python .runtime .exception .PythonErrorType .SystemExit ;
45
46
46
47
import java .io .IOException ;
48
+ import java .io .OutputStream ;
49
+ import java .nio .charset .StandardCharsets ;
47
50
48
51
import com .oracle .graal .python .PythonLanguage ;
49
52
import com .oracle .graal .python .builtins .objects .PNone ;
@@ -81,6 +84,7 @@ public class TopLevelExceptionHandler extends RootNode {
81
84
82
85
@ Child private CreateArgumentsNode createArgs = CreateArgumentsNode .create ();
83
86
@ Child private LookupAndCallUnaryNode callStrNode = LookupAndCallUnaryNode .create (__STR__ );
87
+ @ Child private LookupAndCallUnaryNode callReprNode = LookupAndCallUnaryNode .create (__REPR__ );
84
88
@ Child private CallNode callNode = CallNode .create ();
85
89
86
90
public TopLevelExceptionHandler (PythonLanguage language , RootNode child ) {
@@ -106,8 +110,9 @@ public Object execute(VirtualFrame frame) {
106
110
return null ;
107
111
} else {
108
112
assert context .get ().getCurrentException () == null ;
113
+ Object result ;
109
114
try {
110
- return run (frame );
115
+ result = run (frame );
111
116
} catch (PException e ) {
112
117
printExc (e );
113
118
return null ;
@@ -120,6 +125,23 @@ public Object execute(VirtualFrame frame) {
120
125
}
121
126
throw e ;
122
127
}
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 );
123
145
}
124
146
}
125
147
0 commit comments