Skip to content

Commit b45a77a

Browse files
committed
SysModuleBuiltins.ExceptHookNode: use uncached versions of the nodes
1 parent 1415350 commit b45a77a

File tree

2 files changed

+60
-123
lines changed

2 files changed

+60
-123
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SysModuleBuiltins.java

Lines changed: 56 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,9 @@
111111
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
112112
import com.oracle.graal.python.builtins.objects.tuple.StructSequence;
113113
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
114-
import com.oracle.graal.python.lib.PyLongAsIntNode;
115114
import com.oracle.graal.python.lib.PyLongAsIntNodeGen;
116-
import com.oracle.graal.python.lib.PyLongAsLongAndOverflowNode;
117115
import com.oracle.graal.python.lib.PyLongAsLongAndOverflowNodeGen;
118-
import com.oracle.graal.python.lib.PyLongCheckNode;
116+
import com.oracle.graal.python.lib.PyLongCheckNodeGen;
119117
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
120118
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
121119
import com.oracle.graal.python.lib.PyObjectLookupAttr;
@@ -841,18 +839,6 @@ abstract static class ExceptHookNode extends PythonBuiltinNode {
841839
static final String VALUE_UNKNOWN = "<unknown>";
842840
static final String NL = "\n";
843841

844-
@Child private ReadAttributeFromObjectNode readAttributeFromObjectNode;
845-
@Child private PyLongAsLongAndOverflowNode pyLongAsLongAndOverflowNode;
846-
@Child private PyLongAsIntNode pyLongAsIntNode;
847-
@Child private PyLongCheckNode pyLongCheckNode;
848-
@Child private GetExceptionTracebackNode getExceptionTracebackNode;
849-
@Child private GetClassNode getClassNode;
850-
@Child private TypeNodes.GetNameNode getNameNode;
851-
@Child private PyObjectLookupAttr objectLookupAttr;
852-
@Child private CastToJavaStringNode castToJavaStringNode;
853-
@Child private PyObjectStrAsObjectNode strAsObjNode;
854-
@Child private TracebackBuiltins.GetTracebackFrameNode getTracebackFrameNode;
855-
@Child private PyObjectCallMethodObjArgs callMethod;
856842
@Child private TracebackBuiltins.MaterializeTruffleStacktraceNode truffleStacktraceNode;
857843

858844
@CompilerDirectives.ValueType
@@ -881,127 +867,78 @@ private PTraceback getNextTb(PTraceback traceback) {
881867
return traceback.getNext();
882868
}
883869

884-
private PyObjectCallMethodObjArgs ensureCallMethodNode() {
885-
if (callMethod == null) {
886-
CompilerDirectives.transferToInterpreterAndInvalidate();
887-
callMethod = insert(PyObjectCallMethodObjArgs.create());
888-
}
889-
return callMethod;
870+
private static void write(VirtualFrame frame, Object file, String data) {
871+
PyObjectCallMethodObjArgs.getUncached().execute(frame, file, WRITE, data);
890872
}
891873

892-
private void print(VirtualFrame frame, Object file, String data) {
893-
ensureCallMethodNode().execute(frame, file, WRITE, data);
874+
private static void flush(VirtualFrame frame, Object file) {
875+
PyObjectCallMethodObjArgs.getUncached().execute(frame, file, FLUSH);
894876
}
895877

896-
private void flush(VirtualFrame frame, Object file) {
897-
ensureCallMethodNode().execute(frame, file, FLUSH);
898-
}
899-
900-
private PFrame getFrame(VirtualFrame frame, PTraceback tb) {
901-
if (getTracebackFrameNode == null) {
902-
CompilerDirectives.transferToInterpreterAndInvalidate();
903-
getTracebackFrameNode = insert(TracebackBuiltins.GetTracebackFrameNode.create());
904-
}
905-
return getTracebackFrameNode.execute(frame, tb);
878+
private static PFrame getFrame(VirtualFrame frame, PTraceback tb) {
879+
return TracebackBuiltins.GetTracebackFrameNode.getUncached().execute(frame, tb);
906880
}
907881

908882
private PCode getCode(VirtualFrame frame, PTraceback tb) {
909883
return factory().createCode(getFrame(frame, tb).getTarget());
910884
}
911885

912-
private Object str(VirtualFrame frame, Object value) {
913-
if (strAsObjNode == null) {
914-
CompilerDirectives.transferToInterpreterAndInvalidate();
915-
strAsObjNode = insert(PyObjectStrAsObjectNode.create());
916-
}
917-
886+
private static Object str(VirtualFrame frame, Object value) {
918887
try {
919-
return strAsObjNode.execute(frame, value);
888+
return PyObjectStrAsObjectNode.getUncached().execute(frame, value);
920889
} catch (PException pe) {
921890
return null;
922891
}
923892
}
924893

925-
private String castToString(Object value) {
926-
if (castToJavaStringNode == null) {
927-
CompilerDirectives.transferToInterpreterAndInvalidate();
928-
castToJavaStringNode = insert(CastToJavaStringNode.create());
929-
}
930-
return castToJavaStringNode.execute(value);
894+
private static String castToString(Object value) {
895+
return CastToJavaStringNode.getUncached().execute(value);
931896
}
932897

933-
private String tryCastToString(Object value) {
898+
private static String tryCastToString(Object value) {
934899
try {
935900
return castToString(value);
936901
} catch (CannotCastException e) {
937902
return null;
938903
}
939904
}
940905

941-
private Object lookupAttr(VirtualFrame frame, Object object, String attr) {
942-
if (objectLookupAttr == null) {
943-
CompilerDirectives.transferToInterpreterAndInvalidate();
944-
objectLookupAttr = insert(PyObjectLookupAttr.create());
945-
}
946-
return objectLookupAttr.execute(frame, object, attr);
906+
private static Object lookupAttr(VirtualFrame frame, Object object, String attr) {
907+
return PyObjectLookupAttr.getUncached().execute(frame, object, attr);
947908
}
948909

949-
private String lookupStrAttr(VirtualFrame frame, Object object, String attr) {
910+
private static String lookupStrAttr(VirtualFrame frame, Object object, String attr) {
950911
final Object value = lookupAttr(frame, object, attr);
951912
return value != PNone.NO_VALUE ? castToString(value) : null;
952913
}
953914

954-
private boolean hasAttr(VirtualFrame frame, Object object, String attr) {
915+
private static boolean hasAttr(VirtualFrame frame, Object object, String attr) {
955916
return lookupAttr(frame, object, attr) != PNone.NO_VALUE;
956917
}
957918

958-
private String getName(Object type) {
959-
if (getNameNode == null) {
960-
CompilerDirectives.transferToInterpreterAndInvalidate();
961-
getNameNode = insert(TypeNodes.GetNameNode.create());
962-
}
963-
return getNameNode.execute(type);
919+
private static String getName(Object type) {
920+
return TypeNodes.GetNameNode.getUncached().execute(type);
964921
}
965922

966-
private Object getClass(Object object) {
967-
if (getClassNode == null) {
968-
CompilerDirectives.transferToInterpreterAndInvalidate();
969-
getClassNode = insert(GetClassNode.create());
970-
}
971-
return getClassNode.execute(object);
923+
private static Object getClass(Object object) {
924+
return GetClassNode.getUncached().execute(object);
972925
}
973926

974-
private PTraceback getExceptionTraceback(PBaseException e) {
975-
if (getExceptionTracebackNode == null) {
976-
CompilerDirectives.transferToInterpreterAndInvalidate();
977-
getExceptionTracebackNode = insert(GetExceptionTracebackNode.create());
978-
}
979-
return getExceptionTracebackNode.execute(e);
927+
private static PTraceback getExceptionTraceback(PBaseException e) {
928+
return GetExceptionTracebackNode.getUncached().execute(e);
980929
}
981930

982931
private boolean checkLong(Object object) {
983-
if (pyLongCheckNode == null) {
984-
CompilerDirectives.transferToInterpreterAndInvalidate();
985-
pyLongCheckNode = insert(PyLongCheckNode.create());
986-
}
987-
return pyLongCheckNode.execute(object);
932+
return PyLongCheckNodeGen.getUncached().execute(object);
988933
}
989934

990-
private int asInt(VirtualFrame frame, Object object) {
991-
if (pyLongAsIntNode == null) {
992-
CompilerDirectives.transferToInterpreterAndInvalidate();
993-
pyLongAsIntNode = insert(PyLongAsIntNodeGen.create());
994-
}
995-
return pyLongAsIntNode.execute(frame, object);
935+
private static int asInt(VirtualFrame frame, Object object) {
936+
return PyLongAsIntNodeGen.getUncached().execute(frame, object);
996937
}
997938

998-
private long asLongAndOverflow(VirtualFrame frame, Object object, long overflowValue) {
999-
if (pyLongAsLongAndOverflowNode == null) {
1000-
CompilerDirectives.transferToInterpreterAndInvalidate();
1001-
pyLongAsLongAndOverflowNode = insert(PyLongAsLongAndOverflowNodeGen.create());
1002-
}
939+
private static long asLongAndOverflow(VirtualFrame frame, Object object, long overflowValue) {
1003940
try {
1004-
return pyLongAsLongAndOverflowNode.execute(frame, object);
941+
return PyLongAsLongAndOverflowNodeGen.getUncached().execute(frame, object);
1005942
} catch (OverflowException e) {
1006943
if (object instanceof PInt) {
1007944
return ((PInt) object).isZeroOrNegative() ? 0 : overflowValue;
@@ -1010,12 +947,8 @@ private long asLongAndOverflow(VirtualFrame frame, Object object, long overflowV
1010947
}
1011948
}
1012949

1013-
private Object readAttr(Object object, String attribute) {
1014-
if (readAttributeFromObjectNode == null) {
1015-
CompilerDirectives.transferToInterpreterAndInvalidate();
1016-
readAttributeFromObjectNode = insert(ReadAttributeFromObjectNode.create());
1017-
}
1018-
return readAttributeFromObjectNode.execute(object, attribute);
950+
private static Object readAttr(Object object, String attribute) {
951+
return ReadAttributeFromObjectNode.getUncached().execute(object, attribute);
1019952
}
1020953

1021954
void printTraceBack(VirtualFrame frame, PythonModule sys, Object out, PTraceback tb) {
@@ -1027,7 +960,7 @@ void printTraceBack(VirtualFrame frame, PythonModule sys, Object out, PTraceback
1027960
return;
1028961
}
1029962
}
1030-
print(frame, out, "Traceback (most recent call last):\n");
963+
write(frame, out, "Traceback (most recent call last):\n");
1031964
printInternal(frame, out, tb, limit);
1032965
}
1033966

@@ -1036,7 +969,7 @@ void printLineRepeated(VirtualFrame frame, Object out, int count) {
1036969
cnt -= TB_RECURSIVE_CUTOFF;
1037970
final StringBuilder sb = PythonUtils.newStringBuilder(" [Previous line repeated ");
1038971
PythonUtils.append(sb, cnt, (cnt > 1) ? " more times]\n" : " more time]\n");
1039-
print(frame, out, PythonUtils.sbToString(sb));
972+
write(frame, out, PythonUtils.sbToString(sb));
1040973
}
1041974

1042975
void displayLine(VirtualFrame frame, Object out, String fileName, int lineNo, String name) {
@@ -1046,7 +979,7 @@ void displayLine(VirtualFrame frame, Object out, String fileName, int lineNo, St
1046979

1047980
final StringBuilder sb = PythonUtils.newStringBuilder(" File \"");
1048981
PythonUtils.append(sb, fileName, "\", line ", lineNo, ", in ", name, NL);
1049-
print(frame, out, PythonUtils.sbToString(sb));
982+
write(frame, out, PythonUtils.sbToString(sb));
1050983
// ignore errors since we can't report them, can we?
1051984
displaySourceLine(frame, out, fileName, lineNo, 4);
1052985
}
@@ -1090,9 +1023,9 @@ CharSequence getSourceLine(String fileName, int lineNo) {
10901023
void displaySourceLine(VirtualFrame frame, Object out, String fileName, int lineNo, int indent) {
10911024
final CharSequence line = getSourceLine(fileName, lineNo);
10921025
if (line != null) {
1093-
print(frame, out, getIndent(indent));
1094-
print(frame, out, PythonUtils.trimLeft(line));
1095-
print(frame, out, NL);
1026+
write(frame, out, getIndent(indent));
1027+
write(frame, out, PythonUtils.trimLeft(line));
1028+
write(frame, out, NL);
10961029
}
10971030
}
10981031

@@ -1222,19 +1155,19 @@ void printErrorText(VirtualFrame frame, Object out, SyntaxErrData syntaxErrData)
12221155
text = PythonUtils.substring(text, idx);
12231156
}
12241157

1225-
print(frame, out, " ");
1226-
print(frame, out, text);
1158+
write(frame, out, " ");
1159+
write(frame, out, text);
12271160
if (text.charAt(0) == '\0' || text.charAt(text.length() - 1) != '\n') {
1228-
print(frame, out, NL);
1161+
write(frame, out, NL);
12291162
}
12301163
if (offset == -1) {
12311164
return;
12321165
}
1233-
print(frame, out, " ");
1166+
write(frame, out, " ");
12341167
while (--offset > 0) {
1235-
print(frame, out, " ");
1168+
write(frame, out, " ");
12361169
}
1237-
print(frame, out, "^\n");
1170+
write(frame, out, "^\n");
12381171
}
12391172

12401173
private String classNameNoDot(String name) {
@@ -1246,9 +1179,9 @@ void printException(VirtualFrame frame, PythonModule sys, Object out, Object exc
12461179
Object value = excValue;
12471180
final Object type = getClass(value);
12481181
if (!PGuards.isPBaseException(value)) {
1249-
print(frame, out, "TypeError: print_exception(): Exception expected for value, ");
1250-
print(frame, out, getName(type));
1251-
print(frame, out, " found\n");
1182+
write(frame, out, "TypeError: print_exception(): Exception expected for value, ");
1183+
write(frame, out, getName(type));
1184+
write(frame, out, " found\n");
12521185
return;
12531186
}
12541187

@@ -1264,7 +1197,7 @@ void printException(VirtualFrame frame, PythonModule sys, Object out, Object exc
12641197
value = syntaxErrData.message;
12651198
StringBuilder sb = PythonUtils.newStringBuilder(" File \"");
12661199
PythonUtils.append(sb, castToString(str(frame, syntaxErrData.fileName)), "\", line ", syntaxErrData.lineNo, "\n");
1267-
print(frame, out, PythonUtils.sbToString(sb));
1200+
write(frame, out, PythonUtils.sbToString(sb));
12681201

12691202
// Can't be bothered to check all those PyFile_WriteString() calls
12701203
if (syntaxErrData.text != null) {
@@ -1282,35 +1215,35 @@ void printException(VirtualFrame frame, PythonModule sys, Object out, Object exc
12821215
String moduleName;
12831216
Object v = lookupAttr(frame, type, __MODULE__);
12841217
if (v == PNone.NO_VALUE || !PGuards.isString(v)) {
1285-
print(frame, out, VALUE_UNKNOWN);
1218+
write(frame, out, VALUE_UNKNOWN);
12861219
} else {
12871220
moduleName = castToString(v);
12881221
if (!moduleName.equals(BUILTINS)) {
1289-
print(frame, out, moduleName);
1290-
print(frame, out, ".");
1222+
write(frame, out, moduleName);
1223+
write(frame, out, ".");
12911224
}
12921225
}
12931226
if (className == null) {
1294-
print(frame, out, VALUE_UNKNOWN);
1227+
write(frame, out, VALUE_UNKNOWN);
12951228
} else {
1296-
print(frame, out, className);
1229+
write(frame, out, className);
12971230
}
12981231

12991232
if (value != PNone.NONE) {
13001233
// only print colon if the str() of the object is not the empty string
13011234
v = str(frame, value);
13021235
String s = tryCastToString(v);
13031236
if (v == null) {
1304-
print(frame, out, ": <exception str() failed>");
1237+
write(frame, out, ": <exception str() failed>");
13051238
} else if (!PGuards.isString(v) || (s != null && !s.isEmpty())) {
1306-
print(frame, out, ": ");
1239+
write(frame, out, ": ");
13071240
}
13081241
if (s != null) {
1309-
print(frame, out, s);
1242+
write(frame, out, s);
13101243
}
13111244
}
13121245

1313-
print(frame, out, NL);
1246+
write(frame, out, NL);
13141247
}
13151248

13161249
@TruffleBoundary
@@ -1326,12 +1259,12 @@ void printExceptionRecursive(MaterializedFrame frame, PythonModule sys, Object o
13261259
if (cause != null) {
13271260
if (!contains(seen, cause)) {
13281261
printExceptionRecursive(frame, sys, out, cause, seen);
1329-
print(frame, out, CAUSE_MESSAGE);
1262+
write(frame, out, CAUSE_MESSAGE);
13301263
}
13311264
} else if (context != null && !exc.getSuppressContext()) {
13321265
if (!contains(seen, context)) {
13331266
printExceptionRecursive(frame, sys, out, context, seen);
1334-
print(frame, out, CONTEXT_MESSAGE);
1267+
write(frame, out, CONTEXT_MESSAGE);
13351268
}
13361269
}
13371270
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/traceback/TracebackBuiltins.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ public static GetTracebackFrameNode create() {
192192
return TracebackBuiltinsFactory.GetTracebackFrameNodeFactory.create(null);
193193
}
194194

195+
public static GetTracebackFrameNode getUncached() {
196+
return TracebackBuiltinsFactory.GetTracebackFrameNodeFactory.getInstance().getUncachedInstance();
197+
}
198+
195199
@Specialization(guards = "hasPFrame(tb)")
196200
PFrame getExisting(PTraceback tb) {
197201
return tb.getFrame();

0 commit comments

Comments
 (0)