26
26
package com .oracle .graal .python ;
27
27
28
28
import java .io .IOException ;
29
- import java .net .URI ;
30
29
import java .net .URL ;
31
30
import java .text .MessageFormat ;
32
31
import java .util .ArrayList ;
89
88
import com .oracle .truffle .api .object .ObjectType ;
90
89
import com .oracle .truffle .api .object .Shape ;
91
90
import com .oracle .truffle .api .source .Source ;
92
- import com .oracle .truffle .api .source .Source .LiteralBuilder ;
93
91
import com .oracle .truffle .api .source .Source .SourceBuilder ;
94
92
import com .oracle .truffle .api .source .SourceSection ;
95
93
@@ -428,13 +426,31 @@ public static TruffleLogger getLogger() {
428
426
return TruffleLogger .getLogger (ID );
429
427
}
430
428
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 ) {
432
430
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 );
436
452
}
437
- return newSource (ctxt , sourceBuilder , name );
453
+ return newSource (ctxt , sourceBuilder );
438
454
} catch (IOException e ) {
439
455
throw new AssertionError ();
440
456
}
@@ -446,7 +462,7 @@ public Source newSource(PythonContext ctxt, TruffleFile src, String name) throws
446
462
try {
447
463
return cachedSources .computeIfAbsent (src , t -> {
448
464
try {
449
- return newSource (ctxt , Source .newBuilder (ID , src ), name );
465
+ return newSource (ctxt , Source .newBuilder (ID , src ). name ( name ) );
450
466
} catch (IOException e ) {
451
467
throw new RuntimeException (e );
452
468
}
@@ -460,7 +476,7 @@ public Source newSource(PythonContext ctxt, URL url, String name) throws IOExcep
460
476
try {
461
477
return cachedSources .computeIfAbsent (url , t -> {
462
478
try {
463
- return newSource (ctxt , Source .newBuilder (ID , url ), name );
479
+ return newSource (ctxt , Source .newBuilder (ID , url ). name ( name ) );
464
480
} catch (IOException e ) {
465
481
throw new RuntimeException (e );
466
482
}
@@ -470,14 +486,13 @@ public Source newSource(PythonContext ctxt, URL url, String name) throws IOExcep
470
486
}
471
487
}
472
488
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 {
475
490
boolean coreIsInitialized = ctxt .getCore ().isInitialized ();
476
491
boolean internal = !coreIsInitialized && !PythonOptions .getOption (ctxt , PythonOptions .ExposeInternalSources );
477
492
if (internal ) {
478
493
srcBuilder .internal (true );
479
494
}
480
- return newBuilder .build ();
495
+ return srcBuilder .build ();
481
496
}
482
497
483
498
@ Override
0 commit comments