Skip to content

Commit d0fd6d2

Browse files
committed
only try to map the filename given to 'compile' to a URI if the file exists and passes a content check
1 parent cb83c51 commit d0fd6d2

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

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

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
import java.io.PrintWriter;
7676
import java.math.BigInteger;
7777
import java.net.URI;
78-
import java.net.URISyntaxException;
7978
import java.nio.CharBuffer;
8079
import java.util.List;
8180
import java.util.function.Supplier;
@@ -158,6 +157,7 @@
158157
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
159158
import com.oracle.graal.python.nodes.util.CastToIntegerFromIndexNode;
160159
import com.oracle.graal.python.nodes.util.CastToStringNode;
160+
import com.oracle.graal.python.runtime.PythonContext;
161161
import com.oracle.graal.python.runtime.PythonCore;
162162
import com.oracle.graal.python.runtime.PythonOptions;
163163
import com.oracle.graal.python.runtime.PythonParser.ParserMode;
@@ -169,6 +169,7 @@
169169
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
170170
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
171171
import com.oracle.truffle.api.Truffle;
172+
import com.oracle.truffle.api.TruffleFile;
172173
import com.oracle.truffle.api.dsl.Cached;
173174
import com.oracle.truffle.api.dsl.Fallback;
174175
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
@@ -548,7 +549,7 @@ private static BigInteger[] divideAndRemainder(PInt a, PInt b) {
548549
@GenerateNodeFactory
549550
public abstract static class EvalNode extends PythonBuiltinNode {
550551
protected final String funcname = "eval";
551-
@Child protected CompileNode compileNode = CompileNode.create();
552+
@Child protected CompileNode compileNode = CompileNode.create(false);
552553
@Child private IndirectCallNode indirectCallNode = IndirectCallNode.create();
553554
@Child private HasInheritedAttributeNode hasGetItemNode;
554555

@@ -702,6 +703,19 @@ public final Object execute(VirtualFrame frame) {
702703
@GenerateNodeFactory
703704
@TypeSystemReference(PythonArithmeticTypes.class)
704705
public abstract static class CompileNode extends PythonBuiltinNode {
706+
/**
707+
* Decides wether this node should attempt to map the filename to a URI for the benefit of
708+
* Truffle tooling
709+
*/
710+
private final boolean mapFilenameToUri;
711+
712+
public CompileNode(boolean mapFilenameToUri) {
713+
this.mapFilenameToUri = mapFilenameToUri;
714+
}
715+
716+
public CompileNode() {
717+
this.mapFilenameToUri = true;
718+
}
705719

706720
public abstract PCode execute(Object source, String filename, String mode, Object kwFlags, Object kwDontInherit, Object kwOptimize);
707721

@@ -722,12 +736,9 @@ PCode compile(OpaqueBytes source, String filename, String mode, Object kwFlags,
722736
@Specialization
723737
@TruffleBoundary
724738
PCode compile(String expression, String filename, String mode, Object kwFlags, Object kwDontInherit, Object kwOptimize) {
725-
URI uri = null;
726-
try {
727-
uri = new URI("file://" + filename);
728-
} catch (URISyntaxException e) {
729-
}
730-
Source source = PythonLanguage.newSource(getContext(), expression, filename, uri);
739+
PythonContext context = getContext();
740+
URI uri = mapToUri(expression, filename, context);
741+
Source source = PythonLanguage.newSource(context, expression, filename, uri);
731742
ParserMode pm;
732743
if (mode.equals("exec")) {
733744
pm = ParserMode.File;
@@ -746,14 +757,35 @@ PCode compile(String expression, String filename, String mode, Object kwFlags, O
746757
}
747758
}
748759

760+
private URI mapToUri(String expression, String filename, PythonContext context) {
761+
if (mapFilenameToUri) {
762+
URI uri = null;
763+
try {
764+
TruffleFile truffleFile = context.getEnv().getTruffleFile(filename);
765+
if (truffleFile.exists()) {
766+
// XXX: (tfel): We don't know if the expression has anything to do with the
767+
// filename that's given. We would really have to compare the entire
768+
// contents, but as a first approximation, we compare the content lengths
769+
if (expression.length() == truffleFile.size()) {
770+
uri = truffleFile.toUri();
771+
}
772+
}
773+
} catch (SecurityException | IOException e) {
774+
}
775+
return uri;
776+
} else {
777+
return null;
778+
}
779+
}
780+
749781
@SuppressWarnings("unused")
750782
@Specialization
751783
PCode compile(PCode code, String filename, String mode, Object flags, Object dontInherit, Object optimize) {
752784
return code;
753785
}
754786

755-
public static CompileNode create() {
756-
return BuiltinFunctionsFactory.CompileNodeFactory.create(new ReadArgumentNode[]{});
787+
public static CompileNode create(boolean mapFilenameToUri) {
788+
return BuiltinFunctionsFactory.CompileNodeFactory.create(mapFilenameToUri, new ReadArgumentNode[]{});
757789
}
758790
}
759791

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/zipimporter/ZipImporterBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public PCode doit(PZipImporter self, String fullname,
280280
}
281281
if (compileNode == null) {
282282
CompilerDirectives.transferToInterpreterAndInvalidate();
283-
compileNode = insert(CompileNode.create());
283+
compileNode = insert(CompileNode.create(false));
284284
}
285285
ModuleCodeData md = self.getModuleCode(fullname);
286286
if (canNotFind.profile(md == null)) {

0 commit comments

Comments
 (0)