Skip to content

Commit d5c10b2

Browse files
committed
LazyTraceback: missing truffle boundary
- stylefixes
1 parent a9810f4 commit d5c10b2

File tree

2 files changed

+48
-25
lines changed

2 files changed

+48
-25
lines changed

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

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@
6060
import java.nio.charset.Charset;
6161
import java.util.Arrays;
6262
import java.util.Date;
63+
import java.util.HashSet;
6364
import java.util.List;
6465
import java.util.Map;
66+
import java.util.Set;
6567

66-
import org.graalvm.collections.EconomicSet;
6768
import org.graalvm.nativeimage.ImageInfo;
6869

6970
import com.oracle.graal.python.PythonLanguage;
@@ -833,6 +834,7 @@ abstract static class ExceptHookNode extends PythonBuiltinNode {
833834
static final String ATTR_TEXT = "text";
834835
static final String VALUE_STRING = "<string>";
835836
static final String VALUE_UNKNOWN = "<unknown>";
837+
static final String NL = "\n";
836838

837839
@Child private ReadAttributeFromObjectNode readAttributeFromObjectNode;
838840
@Child private PyLongAsLongAndOverflowNode pyLongAsLongAndOverflowNode;
@@ -999,9 +1001,8 @@ private Object readAttr(Object object, String attribute) {
9991001
return readAttributeFromObjectNode.execute(object, attribute);
10001002
}
10011003

1002-
void printTraceBack(VirtualFrame frame, Object out, PTraceback tb) {
1004+
void printTraceBack(VirtualFrame frame, PythonModule sys, Object out, PTraceback tb) {
10031005
long limit = TRACEBACK_LIMIT;
1004-
final PythonModule sys = getContext().getCore().lookupBuiltinModule("sys");
10051006
final Object limitv = readAttr(sys, ATTR_TB_LIMIT);
10061007
if (checkLong(limitv)) {
10071008
limit = asLongAndOverflow(frame, limitv, MAXSIZE);
@@ -1027,7 +1028,7 @@ void displayLine(VirtualFrame frame, Object out, String fileName, int lineNo, St
10271028
}
10281029

10291030
final StringBuilder sb = PythonUtils.newStringBuilder(" File \"");
1030-
PythonUtils.append(sb, fileName, "\", line ", lineNo, ", in ", name, "\n");
1031+
PythonUtils.append(sb, fileName, "\", line ", lineNo, ", in ", name, NL);
10311032
print(frame, out, PythonUtils.sbToString(sb));
10321033
// ignore errors since we can't report them, can we?
10331034
displaySourceLine(frame, out, fileName, lineNo, 4);
@@ -1059,7 +1060,7 @@ void displaySourceLine(VirtualFrame frame, Object out, String fileName, int line
10591060
if (line != null) {
10601061
print(frame, out, getIndent(indent));
10611062
print(frame, out, PythonUtils.trimLeft(line));
1062-
print(frame, out, "\n");
1063+
print(frame, out, NL);
10631064
}
10641065
}
10651066

@@ -1192,7 +1193,7 @@ void printErrorText(VirtualFrame frame, Object out, SyntaxErrData syntaxErrData)
11921193
print(frame, out, " ");
11931194
print(frame, out, text);
11941195
if (text.charAt(0) == '\0' || text.charAt(text.length() - 1) != '\n') {
1195-
print(frame, out, "\n");
1196+
print(frame, out, NL);
11961197
}
11971198
if (offset == -1) {
11981199
return;
@@ -1209,7 +1210,7 @@ private String classNameNoDot(String name) {
12091210
return (i > 0) ? PythonUtils.substring(name, i + 1) : name;
12101211
}
12111212

1212-
void printException(VirtualFrame frame, Object out, Object excValue) {
1213+
void printException(VirtualFrame frame, PythonModule sys, Object out, Object excValue) {
12131214
Object value = excValue;
12141215
final Object type = getClass(value);
12151216
if (!PGuards.isPBaseException(value)) {
@@ -1222,7 +1223,7 @@ void printException(VirtualFrame frame, Object out, Object excValue) {
12221223
final PBaseException exc = (PBaseException) value;
12231224
final PTraceback tb = getExceptionTraceback(exc);
12241225
if (tb != null) {
1225-
printTraceBack(frame, out, tb);
1226+
printTraceBack(frame, sys, out, tb);
12261227
}
12271228

12281229
if (hasAttr(frame, value, ATTR_PRINT_FILE_AND_LINE)) {
@@ -1277,38 +1278,53 @@ void printException(VirtualFrame frame, Object out, Object excValue) {
12771278
}
12781279
}
12791280

1280-
print(frame, out, "\n");
1281+
print(frame, out, NL);
12811282
}
12821283

1283-
void printExceptionRecursive(VirtualFrame frame, Object out, Object value, EconomicSet<Object> seen) {
1284+
void printExceptionRecursive(VirtualFrame frame, PythonModule sys, Object out, Object value, Set<Object> seen) {
12841285
if (seen != null) {
12851286
// Exception chaining
1286-
seen.add(value);
1287+
add(seen, value);
12871288
if (PGuards.isPBaseException(value)) {
12881289
final PBaseException exc = (PBaseException) value;
12891290
final PBaseException cause = exc.getCause();
12901291
final PBaseException context = exc.getContext();
12911292

12921293
if (cause != null) {
1293-
if (!seen.contains(cause)) {
1294-
printExceptionRecursive(frame, out, cause, seen);
1294+
if (!contains(seen, cause)) {
1295+
printExceptionRecursive(frame, sys, out, cause, seen);
12951296
print(frame, out, CAUSE_MESSAGE);
12961297
}
12971298
} else if (context != null && !exc.getSuppressContext()) {
1298-
if (!seen.contains(context)) {
1299-
printExceptionRecursive(frame, out, context, seen);
1299+
if (!contains(seen, context)) {
1300+
printExceptionRecursive(frame, sys, out, context, seen);
13001301
print(frame, out, CONTEXT_MESSAGE);
13011302
}
13021303
}
13031304
}
13041305
}
1305-
printException(frame, out, value);
1306+
printException(frame, sys, out, value);
1307+
}
1308+
1309+
@TruffleBoundary
1310+
static void add(Set<Object> set, Object value) {
1311+
set.add(value);
1312+
}
1313+
1314+
@TruffleBoundary
1315+
static boolean contains(Set<Object> set, Object value) {
1316+
return set.contains(value);
1317+
}
1318+
1319+
@TruffleBoundary
1320+
static Set<Object> createSet() {
1321+
return new HashSet<>();
13061322
}
13071323

13081324
@Specialization
13091325
Object doWithoutTb(VirtualFrame frame, PythonModule sys, @SuppressWarnings("unused") Object excType, Object value, @SuppressWarnings("unused") PNone traceBack) {
13101326
Object stdErr = lookupAttr(frame, sys, STDERR);
1311-
printExceptionRecursive(frame, stdErr, value, EconomicSet.create());
1327+
printExceptionRecursive(frame, sys, stdErr, value, createSet());
13121328
flush(frame, stdErr);
13131329

13141330
return PNone.NONE;
@@ -1325,7 +1341,7 @@ Object doWithTb(VirtualFrame frame, PythonModule sys, @SuppressWarnings("unused"
13251341
}
13261342

13271343
Object stdErr = lookupAttr(frame, sys, STDERR);
1328-
printExceptionRecursive(frame, stdErr, value, EconomicSet.create());
1344+
printExceptionRecursive(frame, sys, stdErr, value, createSet());
13291345
flush(frame, stdErr);
13301346

13311347
return PNone.NONE;

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.oracle.graal.python.builtins.objects.frame.PFrame;
4646
import com.oracle.graal.python.builtins.objects.function.PArguments;
4747
import com.oracle.graal.python.runtime.exception.PException;
48+
import com.oracle.truffle.api.CompilerDirectives;
4849
import com.oracle.truffle.api.TruffleStackTraceElement;
4950
import com.oracle.truffle.api.frame.Frame;
5051
import com.oracle.truffle.api.nodes.Node;
@@ -114,15 +115,21 @@ public void setTraceback(PTraceback traceback) {
114115
this.materialized = true;
115116
}
116117

118+
@CompilerDirectives.TruffleBoundary
119+
public static int getSourceLineNoOrDefault(PException exception, int defaultValue) {
120+
final Node location = exception.getLocation();
121+
if (location != null) {
122+
final SourceSection sourceSection = location.getSourceSection();
123+
if (sourceSection != null) {
124+
return sourceSection.getStartLine();
125+
}
126+
}
127+
return defaultValue;
128+
}
129+
117130
public int getLineNo() {
118131
if (exception != null) {
119-
final Node location = exception.getLocation();
120-
if (location != null) {
121-
final SourceSection sourceSection = location.getSourceSection();
122-
if (sourceSection != null) {
123-
return sourceSection.getStartLine();
124-
}
125-
}
132+
return getSourceLineNoOrDefault(exception, UNKNOWN_LINE_NUMBER);
126133
}
127134
return UNKNOWN_LINE_NUMBER;
128135
}

0 commit comments

Comments
 (0)