1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* The Universal Permissive License (UPL), Version 1.0
43
43
import static com .oracle .graal .python .nodes .BuiltinNames .DISPLAYHOOK ;
44
44
45
45
import com .oracle .graal .python .PythonLanguage ;
46
+ import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
46
47
import com .oracle .graal .python .builtins .objects .PNone ;
47
48
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 ;
48
51
import com .oracle .graal .python .nodes .attributes .GetAttributeNode ;
49
52
import com .oracle .graal .python .nodes .call .CallNode ;
50
53
import com .oracle .graal .python .nodes .expression .ExpressionNode ;
54
+ import com .oracle .graal .python .nodes .object .IsBuiltinClassProfile ;
51
55
import com .oracle .graal .python .runtime .PythonContext ;
56
+ import com .oracle .graal .python .runtime .exception .PException ;
52
57
import com .oracle .truffle .api .CompilerDirectives ;
53
58
import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
54
59
import com .oracle .truffle .api .TruffleLanguage .ContextReference ;
55
60
import com .oracle .truffle .api .frame .VirtualFrame ;
56
61
57
62
public class PrintExpressionNode extends ExpressionNode {
58
63
@ CompilationFinal private ContextReference <PythonContext > contextRef ;
64
+ @ Child IsBuiltinClassProfile exceptionTypeProfile ;
65
+ @ Child PRaiseNode raiseNode ;
59
66
@ Child GetAttributeNode getAttribute = GetAttributeNode .create (DISPLAYHOOK );
60
67
@ Child CallNode callNode = CallNode .create ();
61
68
@ Child ExpressionNode valueNode ;
@@ -72,12 +79,37 @@ public Object execute(VirtualFrame frame) {
72
79
contextRef = lookupContextReference (PythonLanguage .class );
73
80
}
74
81
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
+ }
76
92
callNode .execute (frame , displayhook , value );
77
93
return PNone .NONE ;
78
94
}
79
95
80
96
public static PrintExpressionNode create (ExpressionNode valueNode ) {
81
97
return new PrintExpressionNode (valueNode );
82
98
}
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
+ }
83
115
}
0 commit comments