75
75
import java .io .PrintWriter ;
76
76
import java .math .BigInteger ;
77
77
import java .net .URI ;
78
- import java .net .URISyntaxException ;
79
78
import java .nio .CharBuffer ;
80
79
import java .util .List ;
81
80
import java .util .function .Supplier ;
158
157
import com .oracle .graal .python .nodes .truffle .PythonArithmeticTypes ;
159
158
import com .oracle .graal .python .nodes .util .CastToIntegerFromIndexNode ;
160
159
import com .oracle .graal .python .nodes .util .CastToStringNode ;
160
+ import com .oracle .graal .python .runtime .PythonContext ;
161
161
import com .oracle .graal .python .runtime .PythonCore ;
162
162
import com .oracle .graal .python .runtime .PythonOptions ;
163
163
import com .oracle .graal .python .runtime .PythonParser .ParserMode ;
169
169
import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
170
170
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
171
171
import com .oracle .truffle .api .Truffle ;
172
+ import com .oracle .truffle .api .TruffleFile ;
172
173
import com .oracle .truffle .api .dsl .Cached ;
173
174
import com .oracle .truffle .api .dsl .Fallback ;
174
175
import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
@@ -548,7 +549,7 @@ private static BigInteger[] divideAndRemainder(PInt a, PInt b) {
548
549
@ GenerateNodeFactory
549
550
public abstract static class EvalNode extends PythonBuiltinNode {
550
551
protected final String funcname = "eval" ;
551
- @ Child protected CompileNode compileNode = CompileNode .create ();
552
+ @ Child protected CompileNode compileNode = CompileNode .create (false );
552
553
@ Child private IndirectCallNode indirectCallNode = IndirectCallNode .create ();
553
554
@ Child private HasInheritedAttributeNode hasGetItemNode ;
554
555
@@ -702,6 +703,19 @@ public final Object execute(VirtualFrame frame) {
702
703
@ GenerateNodeFactory
703
704
@ TypeSystemReference (PythonArithmeticTypes .class )
704
705
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
+ }
705
719
706
720
public abstract PCode execute (Object source , String filename , String mode , Object kwFlags , Object kwDontInherit , Object kwOptimize );
707
721
@@ -722,12 +736,9 @@ PCode compile(OpaqueBytes source, String filename, String mode, Object kwFlags,
722
736
@ Specialization
723
737
@ TruffleBoundary
724
738
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 );
731
742
ParserMode pm ;
732
743
if (mode .equals ("exec" )) {
733
744
pm = ParserMode .File ;
@@ -746,14 +757,35 @@ PCode compile(String expression, String filename, String mode, Object kwFlags, O
746
757
}
747
758
}
748
759
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
+
749
781
@ SuppressWarnings ("unused" )
750
782
@ Specialization
751
783
PCode compile (PCode code , String filename , String mode , Object flags , Object dontInherit , Object optimize ) {
752
784
return code ;
753
785
}
754
786
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 []{});
757
789
}
758
790
}
759
791
0 commit comments