7575import java .io .PrintWriter ;
7676import java .math .BigInteger ;
7777import java .net .URI ;
78- import java .net .URISyntaxException ;
7978import java .nio .CharBuffer ;
8079import java .util .List ;
8180import java .util .function .Supplier ;
158157import com .oracle .graal .python .nodes .truffle .PythonArithmeticTypes ;
159158import com .oracle .graal .python .nodes .util .CastToIntegerFromIndexNode ;
160159import com .oracle .graal .python .nodes .util .CastToStringNode ;
160+ import com .oracle .graal .python .runtime .PythonContext ;
161161import com .oracle .graal .python .runtime .PythonCore ;
162162import com .oracle .graal .python .runtime .PythonOptions ;
163163import com .oracle .graal .python .runtime .PythonParser .ParserMode ;
169169import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
170170import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
171171import com .oracle .truffle .api .Truffle ;
172+ import com .oracle .truffle .api .TruffleFile ;
172173import com .oracle .truffle .api .dsl .Cached ;
173174import com .oracle .truffle .api .dsl .Fallback ;
174175import 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
0 commit comments