Skip to content

Commit 2a7bbc9

Browse files
committed
Raise correct exception when sys.displayhook is lost
1 parent dd8af16 commit 2a7bbc9

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_sys.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@
3535
*graalpython.lib-python.3.test.test_sys.UnraisableHookTest.test_original_unraisablehook_err
3636
*graalpython.lib-python.3.test.test_sys.UnraisableHookTest.test_original_unraisablehook_wrong_type
3737
*graalpython.lib-python.3.test.test_sys.SysModuleTest.test_sys_flags
38+
*graalpython.lib-python.3.test.test_sys.DisplayHookTest.test_lost_displayhook

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ErrorMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ public abstract class ErrorMessages {
335335
public static final String LOCAL_VAR_REFERENCED_BEFORE_ASSIGMENT = "local variable '%s' referenced before assignment";
336336
public static final String LOCALS_MUST_BE_MAPPING = "%s() locals must be a mapping or None, not %p";
337337
public static final String LOST_SYSBREAKPOINTHOOK = "lost sys.breakpointhook";
338+
public static final String LOST_SYSDISPLAYHOOK = "lost sys.displayhook";
338339
public static final String LENGTH_SHOULD_NOT_BE_NEG = "length should not be negative";
339340
public static final String MATH_DOMAIN_ERROR = "math domain error";
340341
public static final String MATH_RANGE_ERROR = "math range error";

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement/PrintExpressionNode.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -43,19 +43,26 @@
4343
import static com.oracle.graal.python.nodes.BuiltinNames.DISPLAYHOOK;
4444

4545
import com.oracle.graal.python.PythonLanguage;
46+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4647
import com.oracle.graal.python.builtins.objects.PNone;
4748
import com.oracle.graal.python.builtins.objects.module.PythonModule;
49+
import com.oracle.graal.python.nodes.ErrorMessages;
50+
import com.oracle.graal.python.nodes.PRaiseNode;
4851
import com.oracle.graal.python.nodes.attributes.GetAttributeNode;
4952
import com.oracle.graal.python.nodes.call.CallNode;
5053
import com.oracle.graal.python.nodes.expression.ExpressionNode;
54+
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
5155
import com.oracle.graal.python.runtime.PythonContext;
56+
import com.oracle.graal.python.runtime.exception.PException;
5257
import com.oracle.truffle.api.CompilerDirectives;
5358
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
5459
import com.oracle.truffle.api.TruffleLanguage.ContextReference;
5560
import com.oracle.truffle.api.frame.VirtualFrame;
5661

5762
public class PrintExpressionNode extends ExpressionNode {
5863
@CompilationFinal private ContextReference<PythonContext> contextRef;
64+
@Child IsBuiltinClassProfile exceptionTypeProfile;
65+
@Child PRaiseNode raiseNode;
5966
@Child GetAttributeNode getAttribute = GetAttributeNode.create(DISPLAYHOOK);
6067
@Child CallNode callNode = CallNode.create();
6168
@Child ExpressionNode valueNode;
@@ -72,12 +79,37 @@ public Object execute(VirtualFrame frame) {
7279
contextRef = lookupContextReference(PythonLanguage.class);
7380
}
7481
PythonModule sysModule = contextRef.get().getCore().lookupBuiltinModule("sys");
75-
Object displayhook = getAttribute.executeObject(frame, sysModule);
82+
Object displayhook;
83+
try {
84+
displayhook = getAttribute.executeObject(frame, sysModule);
85+
} catch (PException ex) {
86+
if (ensureExceptionTypeProfile().profileException(ex, PythonBuiltinClassType.AttributeError)) {
87+
throw ensureRaiseNode().raise(PythonBuiltinClassType.RuntimeError, ErrorMessages.LOST_SYSDISPLAYHOOK);
88+
} else {
89+
throw ex;
90+
}
91+
}
7692
callNode.execute(frame, displayhook, value);
7793
return PNone.NONE;
7894
}
7995

8096
public static PrintExpressionNode create(ExpressionNode valueNode) {
8197
return new PrintExpressionNode(valueNode);
8298
}
99+
100+
public IsBuiltinClassProfile ensureExceptionTypeProfile() {
101+
if (exceptionTypeProfile == null) {
102+
CompilerDirectives.transferToInterpreterAndInvalidate();
103+
exceptionTypeProfile = insert(IsBuiltinClassProfile.create());
104+
}
105+
return exceptionTypeProfile;
106+
}
107+
108+
public PRaiseNode ensureRaiseNode() {
109+
if (raiseNode == null) {
110+
CompilerDirectives.transferToInterpreterAndInvalidate();
111+
raiseNode = insert(PRaiseNode.create());
112+
}
113+
return raiseNode;
114+
}
83115
}

0 commit comments

Comments
 (0)