Skip to content

Commit f5b49a9

Browse files
msimacektimfel
authored andcommitted
TruffleString fixes in PythonLanguage
1 parent 34a5a20 commit f5b49a9

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import java.util.concurrent.Semaphore;
3939
import java.util.logging.Level;
4040

41-
import com.oracle.graal.python.pegparser.tokenizer.SourceRange;
4241
import org.graalvm.options.OptionDescriptors;
4342
import org.graalvm.options.OptionKey;
4443
import org.graalvm.options.OptionValues;
@@ -64,21 +63,22 @@
6463
import com.oracle.graal.python.compiler.CompilationUnit;
6564
import com.oracle.graal.python.compiler.Compiler;
6665
import com.oracle.graal.python.nodes.HiddenAttributes;
67-
import com.oracle.graal.python.nodes.bytecode.PBytecodeRootNode;
6866
import com.oracle.graal.python.nodes.PRootNode;
6967
import com.oracle.graal.python.nodes.RootNodeFactory;
68+
import com.oracle.graal.python.nodes.bytecode.PBytecodeRootNode;
7069
import com.oracle.graal.python.nodes.control.TopLevelExceptionHandler;
7170
import com.oracle.graal.python.nodes.expression.ExpressionNode;
7271
import com.oracle.graal.python.nodes.util.BadOPCodeNode;
7372
import com.oracle.graal.python.parser.PythonParserImpl;
73+
import com.oracle.graal.python.pegparser.ErrorCallback;
7474
import com.oracle.graal.python.pegparser.FExprParser;
7575
import com.oracle.graal.python.pegparser.InputType;
7676
import com.oracle.graal.python.pegparser.NodeFactoryImp;
7777
import com.oracle.graal.python.pegparser.Parser;
78-
import com.oracle.graal.python.pegparser.ErrorCallback;
7978
import com.oracle.graal.python.pegparser.ParserTokenizer;
8079
import com.oracle.graal.python.pegparser.sst.ExprTy;
8180
import com.oracle.graal.python.pegparser.sst.ModTy;
81+
import com.oracle.graal.python.pegparser.tokenizer.SourceRange;
8282
import com.oracle.graal.python.runtime.GilNode;
8383
import com.oracle.graal.python.runtime.PythonContext;
8484
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
@@ -166,6 +166,8 @@ public final class PythonLanguage extends TruffleLanguage<PythonContext> {
166166
public static final int RELEASE_LEVEL_FINAL = 0xF;
167167
public static final int RELEASE_LEVEL = RELEASE_LEVEL_ALPHA;
168168
public static final TruffleString RELEASE_LEVEL_STRING;
169+
public static final TruffleString DEFAULT_FILENAME = tsLiteral("<string>");
170+
169171
static {
170172
switch (RELEASE_LEVEL) {
171173
case RELEASE_LEVEL_ALPHA:
@@ -581,13 +583,22 @@ public SourceSection getSourceSection() {
581583
instance = PythonObjectFactory.getUncached().createBaseException(cls, message, PythonUtils.EMPTY_OBJECT_ARRAY);
582584
final Object[] excAttrs = SyntaxErrorBuiltins.SYNTAX_ERROR_ATTR_FACTORY.create();
583585
SourceSection section = location.getSourceSection();
584-
TruffleString path = toTruffleStringUncached(source.getPath());
585-
excAttrs[SyntaxErrorBuiltins.IDX_FILENAME] = (path != null) ? path : source.getName() != null ? toTruffleStringUncached(source.getName()) : tsLiteral("<string>");
586+
TruffleString filename = toTruffleStringUncached(source.getPath());
587+
if (filename == null) {
588+
filename = toTruffleStringUncached(source.getName());
589+
if (filename == null) {
590+
filename = DEFAULT_FILENAME;
591+
}
592+
}
593+
excAttrs[SyntaxErrorBuiltins.IDX_FILENAME] = filename;
586594
excAttrs[SyntaxErrorBuiltins.IDX_LINENO] = section.getStartLine();
587595
excAttrs[SyntaxErrorBuiltins.IDX_OFFSET] = section.getStartColumn();
588596
// Not very nice. This counts on the implementation in traceback.py where if the value of
589597
// text attribute is NONE, then the line is not printed
590-
final TruffleString text = section.isAvailable() ? toTruffleStringUncached(source.getCharacters(section.getStartLine()).toString()) : null;
598+
Object text = PNone.NONE;
599+
if (section.isAvailable()) {
600+
text = toTruffleStringUncached(source.getCharacters(section.getStartLine()).toString());
601+
}
591602
excAttrs[SyntaxErrorBuiltins.IDX_MSG] = message;
592603
excAttrs[SyntaxErrorBuiltins.IDX_TEXT] = text;
593604
instance.setExceptionAttributes(excAttrs);

0 commit comments

Comments
 (0)