Skip to content

Commit 8c98b71

Browse files
committed
[GR-13468] Use a TruffleFile to build sources that we think are from a file
PullRequest: graalpython/388
2 parents f275833 + a0cdb85 commit 8c98b71

File tree

2 files changed

+32
-41
lines changed

2 files changed

+32
-41
lines changed

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

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
package com.oracle.graal.python;
2727

2828
import java.io.IOException;
29-
import java.net.URI;
3029
import java.net.URL;
3130
import java.text.MessageFormat;
3231
import java.util.ArrayList;
@@ -89,7 +88,6 @@
8988
import com.oracle.truffle.api.object.ObjectType;
9089
import com.oracle.truffle.api.object.Shape;
9190
import com.oracle.truffle.api.source.Source;
92-
import com.oracle.truffle.api.source.Source.LiteralBuilder;
9391
import com.oracle.truffle.api.source.Source.SourceBuilder;
9492
import com.oracle.truffle.api.source.SourceSection;
9593

@@ -428,13 +426,31 @@ public static TruffleLogger getLogger() {
428426
return TruffleLogger.getLogger(ID);
429427
}
430428

431-
public static Source newSource(PythonContext ctxt, String src, String name, URI uri) {
429+
public static Source newSource(PythonContext ctxt, String src, String name, boolean mayBeFile) {
432430
try {
433-
LiteralBuilder sourceBuilder = Source.newBuilder(ID, src, name);
434-
if (uri != null) {
435-
sourceBuilder.uri(uri);
431+
SourceBuilder sourceBuilder = null;
432+
if (mayBeFile) {
433+
try {
434+
TruffleFile truffleFile = ctxt.getEnv().getTruffleFile(name);
435+
if (truffleFile.exists()) {
436+
// XXX: (tfel): We don't know if the expression has anything to do with the
437+
// filename that's given. We would really have to compare the entire
438+
// contents, but as a first approximation, we compare the content lengths.
439+
// We override the contents of the source builder with the given source
440+
// regardless.
441+
if (src.length() == truffleFile.size()) {
442+
sourceBuilder = Source.newBuilder(ID, truffleFile);
443+
sourceBuilder.content(src);
444+
}
445+
}
446+
} catch (SecurityException | IOException e) {
447+
sourceBuilder = null;
448+
}
449+
}
450+
if (sourceBuilder == null) {
451+
sourceBuilder = Source.newBuilder(ID, src, name);
436452
}
437-
return newSource(ctxt, sourceBuilder, name);
453+
return newSource(ctxt, sourceBuilder);
438454
} catch (IOException e) {
439455
throw new AssertionError();
440456
}
@@ -446,7 +462,7 @@ public Source newSource(PythonContext ctxt, TruffleFile src, String name) throws
446462
try {
447463
return cachedSources.computeIfAbsent(src, t -> {
448464
try {
449-
return newSource(ctxt, Source.newBuilder(ID, src), name);
465+
return newSource(ctxt, Source.newBuilder(ID, src).name(name));
450466
} catch (IOException e) {
451467
throw new RuntimeException(e);
452468
}
@@ -460,7 +476,7 @@ public Source newSource(PythonContext ctxt, URL url, String name) throws IOExcep
460476
try {
461477
return cachedSources.computeIfAbsent(url, t -> {
462478
try {
463-
return newSource(ctxt, Source.newBuilder(ID, url), name);
479+
return newSource(ctxt, Source.newBuilder(ID, url).name(name));
464480
} catch (IOException e) {
465481
throw new RuntimeException(e);
466482
}
@@ -470,14 +486,13 @@ public Source newSource(PythonContext ctxt, URL url, String name) throws IOExcep
470486
}
471487
}
472488

473-
private static Source newSource(PythonContext ctxt, SourceBuilder srcBuilder, String name) throws IOException {
474-
SourceBuilder newBuilder = srcBuilder.name(name).mimeType(MIME_TYPE);
489+
private static Source newSource(PythonContext ctxt, SourceBuilder srcBuilder) throws IOException {
475490
boolean coreIsInitialized = ctxt.getCore().isInitialized();
476491
boolean internal = !coreIsInitialized && !PythonOptions.getOption(ctxt, PythonOptions.ExposeInternalSources);
477492
if (internal) {
478493
srcBuilder.internal(true);
479494
}
480-
return newBuilder.build();
495+
return srcBuilder.build();
481496
}
482497

483498
@Override

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

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
import java.io.PrintStream;
7575
import java.io.PrintWriter;
7676
import java.math.BigInteger;
77-
import java.net.URI;
7877
import java.nio.CharBuffer;
7978
import java.util.List;
8079
import java.util.function.Supplier;
@@ -167,7 +166,6 @@
167166
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
168167
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
169168
import com.oracle.truffle.api.Truffle;
170-
import com.oracle.truffle.api.TruffleFile;
171169
import com.oracle.truffle.api.dsl.Cached;
172170
import com.oracle.truffle.api.dsl.Fallback;
173171
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
@@ -698,14 +696,14 @@ public abstract static class CompileNode extends PythonBuiltinNode {
698696
* Decides wether this node should attempt to map the filename to a URI for the benefit of
699697
* Truffle tooling
700698
*/
701-
private final boolean mapFilenameToUri;
699+
private final boolean mayBeFromFile;
702700

703-
public CompileNode(boolean mapFilenameToUri) {
704-
this.mapFilenameToUri = mapFilenameToUri;
701+
public CompileNode(boolean mayBeFromFile) {
702+
this.mayBeFromFile = mayBeFromFile;
705703
}
706704

707705
public CompileNode() {
708-
this.mapFilenameToUri = true;
706+
this.mayBeFromFile = true;
709707
}
710708

711709
public abstract PCode execute(Object source, String filename, String mode, Object kwFlags, Object kwDontInherit, Object kwOptimize);
@@ -728,8 +726,7 @@ PCode compile(OpaqueBytes source, String filename, String mode, Object kwFlags,
728726
@TruffleBoundary
729727
PCode compile(String expression, String filename, String mode, Object kwFlags, Object kwDontInherit, Object kwOptimize) {
730728
PythonContext context = getContext();
731-
URI uri = mapToUri(expression, filename, context);
732-
Source source = PythonLanguage.newSource(context, expression, filename, uri);
729+
Source source = PythonLanguage.newSource(context, expression, filename, mayBeFromFile);
733730
ParserMode pm;
734731
if (mode.equals("exec")) {
735732
pm = ParserMode.File;
@@ -748,27 +745,6 @@ PCode compile(String expression, String filename, String mode, Object kwFlags, O
748745
}
749746
}
750747

751-
private URI mapToUri(String expression, String filename, PythonContext context) {
752-
if (mapFilenameToUri) {
753-
URI uri = null;
754-
try {
755-
TruffleFile truffleFile = context.getEnv().getTruffleFile(filename);
756-
if (truffleFile.exists()) {
757-
// XXX: (tfel): We don't know if the expression has anything to do with the
758-
// filename that's given. We would really have to compare the entire
759-
// contents, but as a first approximation, we compare the content lengths
760-
if (expression.length() == truffleFile.size()) {
761-
uri = truffleFile.toUri();
762-
}
763-
}
764-
} catch (SecurityException | IOException e) {
765-
}
766-
return uri;
767-
} else {
768-
return null;
769-
}
770-
}
771-
772748
@SuppressWarnings("unused")
773749
@Specialization
774750
PCode compile(PCode code, String filename, String mode, Object flags, Object dontInherit, Object optimize) {

0 commit comments

Comments
 (0)