Skip to content

Commit e20afec

Browse files
committed
use sys.displayhook when running interactively, rather than printing directly
1 parent ce75021 commit e20afec

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

graalpython/com.oracle.graal.python.shell/src/com/oracle/graal/python/shell/GraalPythonMain.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,11 @@ public int readEvalPrint(Context context, ConsoleHandler consoleHandler) {
608608
while (true) { // processing subsequent lines while input is incomplete
609609
lastStatus = 0;
610610
try {
611-
context.eval(Source.newBuilder(getLanguageId(), sb.toString(), "<stdin>").interactive(true).buildLiteral());
611+
Value result = context.eval(Source.newBuilder(getLanguageId(), sb.toString(), "<stdin>").interactive(true).buildLiteral());
612+
Value displayhook = sys.getMember("displayhook");
613+
if (displayhook != null && displayhook.canExecute()) {
614+
displayhook.execute(result);
615+
}
612616
} catch (PolyglotException e) {
613617
if (continuePrompt == null) {
614618
continuePrompt = doEcho ? sys.getMember("ps2").asString() : null;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,13 @@ public static PythonCore getCore() {
343343

344344
@Override
345345
protected boolean isVisible(PythonContext context, Object value) {
346-
return value != PNone.NONE && value != PNone.NO_VALUE;
346+
if (PythonOptions.getOption(context, PythonOptions.TerminalIsInteractive)) {
347+
// if we run through our own launcher, the sys.__displayhook__ would provide the
348+
// printing
349+
return false;
350+
} else {
351+
return value != PNone.NONE && value != PNone.NO_VALUE;
352+
}
347353
}
348354

349355
@Override

graalpython/lib-graalpython/sys.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,27 @@ def breakpointhook(*args, **kws):
204204
@__builtin__
205205
def getrecursionlimit():
206206
return 1000
207+
208+
209+
@__builtin__
210+
def displayhook(value):
211+
if value is None:
212+
return
213+
builtins = modules['builtins']
214+
# Set '_' to None to avoid recursion
215+
builtins._ = None
216+
text = repr(value)
217+
try:
218+
stdout.write(text)
219+
except UnicodeEncodeError:
220+
bytes = text.encode(stdout.encoding, 'backslashreplace')
221+
if hasattr(stdout, 'buffer'):
222+
stdout.buffer.write(bytes)
223+
else:
224+
text = bytes.decode(stdout.encoding, 'strict')
225+
stdout.write(text)
226+
stdout.write("\n")
227+
builtins._ = value
228+
229+
230+
__displayhook__ = displayhook

0 commit comments

Comments
 (0)