Skip to content

Commit 9613737

Browse files
committed
[GR-13376] Try to set the filename as URI
PullRequest: graalpython/368
2 parents bbc681a + 8082a7e commit 9613737

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
33
* Copyright (c) 2015, Regents of the University of California
44
*
55
* All rights reserved.
@@ -26,6 +26,7 @@
2626
package com.oracle.graal.python;
2727

2828
import java.io.IOException;
29+
import java.net.URI;
2930
import java.net.URL;
3031
import java.text.MessageFormat;
3132
import java.util.ArrayList;
@@ -88,6 +89,7 @@
8889
import com.oracle.truffle.api.object.ObjectType;
8990
import com.oracle.truffle.api.object.Shape;
9091
import com.oracle.truffle.api.source.Source;
92+
import com.oracle.truffle.api.source.Source.LiteralBuilder;
9193
import com.oracle.truffle.api.source.Source.SourceBuilder;
9294
import com.oracle.truffle.api.source.SourceSection;
9395

@@ -422,9 +424,13 @@ public static TruffleLogger getLogger() {
422424
return TruffleLogger.getLogger(ID);
423425
}
424426

425-
public static Source newSource(PythonContext ctxt, String src, String name) {
427+
public static Source newSource(PythonContext ctxt, String src, String name, URI uri) {
426428
try {
427-
return newSource(ctxt, Source.newBuilder(ID, src, name), name);
429+
LiteralBuilder sourceBuilder = Source.newBuilder(ID, src, name);
430+
if (uri != null) {
431+
sourceBuilder.uri(uri);
432+
}
433+
return newSource(ctxt, sourceBuilder, name);
428434
} catch (IOException e) {
429435
throw new AssertionError();
430436
}

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

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import java.io.PrintStream;
7575
import java.io.PrintWriter;
7676
import java.math.BigInteger;
77+
import java.net.URI;
7778
import java.nio.CharBuffer;
7879
import java.util.List;
7980
import java.util.function.Supplier;
@@ -156,6 +157,7 @@
156157
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
157158
import com.oracle.graal.python.nodes.util.CastToIntegerFromIndexNode;
158159
import com.oracle.graal.python.nodes.util.CastToStringNode;
160+
import com.oracle.graal.python.runtime.PythonContext;
159161
import com.oracle.graal.python.runtime.PythonCore;
160162
import com.oracle.graal.python.runtime.PythonOptions;
161163
import com.oracle.graal.python.runtime.PythonParser.ParserMode;
@@ -167,6 +169,7 @@
167169
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
168170
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
169171
import com.oracle.truffle.api.Truffle;
172+
import com.oracle.truffle.api.TruffleFile;
170173
import com.oracle.truffle.api.dsl.Cached;
171174
import com.oracle.truffle.api.dsl.Fallback;
172175
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
@@ -546,7 +549,7 @@ private static BigInteger[] divideAndRemainder(PInt a, PInt b) {
546549
@GenerateNodeFactory
547550
public abstract static class EvalNode extends PythonBuiltinNode {
548551
protected final String funcname = "eval";
549-
@Child protected CompileNode compileNode = CompileNode.create();
552+
@Child protected CompileNode compileNode = CompileNode.create(false);
550553
@Child private IndirectCallNode indirectCallNode = IndirectCallNode.create();
551554
@Child private HasInheritedAttributeNode hasGetItemNode;
552555

@@ -700,6 +703,19 @@ public final Object execute(VirtualFrame frame) {
700703
@GenerateNodeFactory
701704
@TypeSystemReference(PythonArithmeticTypes.class)
702705
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+
}
703719

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

@@ -720,7 +736,9 @@ PCode compile(OpaqueBytes source, String filename, String mode, Object kwFlags,
720736
@Specialization
721737
@TruffleBoundary
722738
PCode compile(String expression, String filename, String mode, Object kwFlags, Object kwDontInherit, Object kwOptimize) {
723-
Source source = PythonLanguage.newSource(getContext(), expression, filename);
739+
PythonContext context = getContext();
740+
URI uri = mapToUri(expression, filename, context);
741+
Source source = PythonLanguage.newSource(context, expression, filename, uri);
724742
ParserMode pm;
725743
if (mode.equals("exec")) {
726744
pm = ParserMode.File;
@@ -739,14 +757,35 @@ PCode compile(String expression, String filename, String mode, Object kwFlags, O
739757
}
740758
}
741759

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+
742781
@SuppressWarnings("unused")
743782
@Specialization
744783
PCode compile(PCode code, String filename, String mode, Object flags, Object dontInherit, Object optimize) {
745784
return code;
746785
}
747786

748-
public static CompileNode create() {
749-
return BuiltinFunctionsFactory.CompileNodeFactory.create(new ReadArgumentNode[]{});
787+
public static CompileNode create(boolean mapFilenameToUri) {
788+
return BuiltinFunctionsFactory.CompileNodeFactory.create(mapFilenameToUri, new ReadArgumentNode[]{});
750789
}
751790
}
752791

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -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)