|
27 | 27 |
|
28 | 28 | import static com.oracle.graal.python.annotations.PythonOS.PLATFORM_WIN32; |
29 | 29 | import static com.oracle.graal.python.nodes.BuiltinNames.T__SIGNAL; |
30 | | -import static com.oracle.graal.python.nodes.StringLiterals.J_PY_EXTENSION; |
31 | 30 | import static com.oracle.graal.python.nodes.StringLiterals.T_PY_EXTENSION; |
32 | 31 | import static com.oracle.graal.python.nodes.truffle.TruffleStringMigrationHelpers.isJavaString; |
33 | 32 | import static com.oracle.graal.python.util.PythonUtils.ARRAY_ACCESSOR; |
|
110 | 109 | import com.oracle.graal.python.runtime.exception.PException; |
111 | 110 | import com.oracle.graal.python.runtime.object.PFactory; |
112 | 111 | import com.oracle.graal.python.util.Function; |
113 | | -import com.oracle.graal.python.util.LazySource; |
114 | 112 | import com.oracle.graal.python.util.PythonUtils; |
115 | 113 | import com.oracle.graal.python.util.Supplier; |
116 | 114 | import com.oracle.truffle.api.Assumption; |
@@ -549,53 +547,16 @@ protected CallTarget parse(ParsingRequest request) { |
549 | 547 | } |
550 | 548 |
|
551 | 549 | public static RootCallTarget callTargetFromBytecode(PythonContext context, Source source, CodeUnit code) { |
552 | | - boolean internal = shouldMarkSourceInternal(context); |
553 | | - SourceBuilder builder = null; |
554 | | - // The original file path should be passed as the name |
555 | | - String name = source.getName(); |
556 | | - if (name != null && !name.isEmpty()) { |
557 | | - builder = sourceForOriginalFile(context, code, internal, name); |
558 | | - if (builder == null) { |
559 | | - if (name.startsWith(FROZEN_FILENAME_PREFIX) && name.endsWith(FROZEN_FILENAME_SUFFIX)) { |
560 | | - String id = name.substring(FROZEN_FILENAME_PREFIX.length(), name.length() - FROZEN_FILENAME_SUFFIX.length()); |
561 | | - String fs = context.getEnv().getFileNameSeparator(); |
562 | | - String path = context.getStdlibHome() + fs + id.replace(".", fs) + J_PY_EXTENSION; |
563 | | - builder = sourceForOriginalFile(context, code, internal, path); |
564 | | - if (builder == null) { |
565 | | - path = context.getStdlibHome() + fs + id.replace(".", fs) + fs + "__init__.py"; |
566 | | - builder = sourceForOriginalFile(context, code, internal, path); |
567 | | - } |
568 | | - } |
569 | | - } |
570 | | - } |
571 | | - if (builder == null) { |
572 | | - builder = Source.newBuilder(source).internal(internal).content(Source.CONTENT_NONE); |
573 | | - } |
574 | 550 | RootNode rootNode; |
575 | | - LazySource lazySource = new LazySource(builder); |
576 | | - |
577 | 551 | if (PythonOptions.ENABLE_BYTECODE_DSL_INTERPRETER) { |
578 | | - // TODO lazily load source in bytecode DSL interpreter too |
579 | | - rootNode = ((BytecodeDSLCodeUnit) code).createRootNode(context, lazySource.getSource()); |
| 552 | + rootNode = ((BytecodeDSLCodeUnit) code).createRootNode(context, source); |
580 | 553 | } else { |
581 | | - rootNode = PBytecodeRootNode.create(context.getLanguage(), (BytecodeCodeUnit) code, lazySource, internal); |
| 554 | + rootNode = PBytecodeRootNode.create(context.getLanguage(), (BytecodeCodeUnit) code, source, source.isInternal()); |
582 | 555 | } |
583 | 556 |
|
584 | 557 | return PythonUtils.getOrCreateCallTarget(rootNode); |
585 | 558 | } |
586 | 559 |
|
587 | | - private static SourceBuilder sourceForOriginalFile(PythonContext context, CodeUnit code, boolean internal, String path) { |
588 | | - try { |
589 | | - TruffleFile file = context.getEnv().getPublicTruffleFile(path); |
590 | | - if (!file.isReadable()) { |
591 | | - return null; |
592 | | - } |
593 | | - return Source.newBuilder(PythonLanguage.ID, file).name(code.name.toJavaStringUncached()).internal(internal); |
594 | | - } catch (SecurityException | UnsupportedOperationException | InvalidPathException e) { |
595 | | - return null; |
596 | | - } |
597 | | - } |
598 | | - |
599 | 560 | public RootCallTarget parse(PythonContext context, Source source, InputType type, boolean topLevel, int optimize, boolean interactiveTerminal, List<String> argumentNames, |
600 | 561 | EnumSet<FutureFeature> futureFeatures) { |
601 | 562 | return parse(context, source, type, topLevel, optimize, interactiveTerminal, false, argumentNames, futureFeatures); |
@@ -677,7 +638,7 @@ private RootNode compileForBytecodeInterpreter(ModTy mod, Source source, int opt |
677 | 638 | Compiler compiler = new Compiler(parserCallbacks); |
678 | 639 | CompilationUnit cu = compiler.compile(mod, EnumSet.noneOf(Compiler.Flags.class), optimize, futureFeatures); |
679 | 640 | BytecodeCodeUnit co = cu.assemble(); |
680 | | - return PBytecodeRootNode.create(this, co, new LazySource(source), source.isInternal(), parserCallbacks); |
| 641 | + return PBytecodeRootNode.create(this, co, source, source.isInternal(), parserCallbacks); |
681 | 642 | } |
682 | 643 |
|
683 | 644 | private RootNode compileForBytecodeDSLInterpreter(ModTy mod, Source source, int optimize, |
@@ -957,7 +918,7 @@ private static Source newSource(PythonContext context, SourceBuilder srcBuilder) |
957 | 918 | return srcBuilder.build(); |
958 | 919 | } |
959 | 920 |
|
960 | | - private static boolean shouldMarkSourceInternal(PythonContext ctxt) { |
| 921 | + public static boolean shouldMarkSourceInternal(PythonContext ctxt) { |
961 | 922 | return !ctxt.isCoreInitialized() && !ctxt.getLanguage().getEngineOption(PythonOptions.ExposeInternalSources); |
962 | 923 | } |
963 | 924 |
|
@@ -1259,6 +1220,34 @@ public Source getOrCreateSource(Function<Object, Source> rootNodeFunction, Objec |
1259 | 1220 | return sourceCache.computeIfAbsent(key, rootNodeFunction); |
1260 | 1221 | } |
1261 | 1222 |
|
| 1223 | + public Source getOrCreateSourceWithContent(Source sourceWithoutContent) { |
| 1224 | + if (sourceWithoutContent.hasCharacters() || sourceWithoutContent.getPath() == null) { |
| 1225 | + return sourceWithoutContent; |
| 1226 | + } |
| 1227 | + String path = sourceWithoutContent.getPath(); |
| 1228 | + return getOrCreateSource(ignored -> loadSourceWithContent(sourceWithoutContent), path); |
| 1229 | + } |
| 1230 | + |
| 1231 | + private static Source loadSourceWithContent(Source sourceWithoutContent) { |
| 1232 | + String path = sourceWithoutContent.getPath(); |
| 1233 | + if (path == null) { |
| 1234 | + return sourceWithoutContent; |
| 1235 | + } |
| 1236 | + PythonContext context = PythonContext.get(null); |
| 1237 | + if (context == null) { |
| 1238 | + return sourceWithoutContent; |
| 1239 | + } |
| 1240 | + try { |
| 1241 | + TruffleFile file = context.getEnv().getPublicTruffleFile(path); |
| 1242 | + if (!file.isReadable()) { |
| 1243 | + return sourceWithoutContent; |
| 1244 | + } |
| 1245 | + return Source.newBuilder(PythonLanguage.ID, file).name(sourceWithoutContent.getName()).internal(sourceWithoutContent.isInternal()).build(); |
| 1246 | + } catch (IOException | SecurityException | UnsupportedOperationException | InvalidPathException e) { |
| 1247 | + return sourceWithoutContent; |
| 1248 | + } |
| 1249 | + } |
| 1250 | + |
1262 | 1251 | public static PythonOS getPythonOS() { |
1263 | 1252 | if (PythonOS.internalCurrent == PythonOS.PLATFORM_ANY) { |
1264 | 1253 | if (ImageInfo.inImageBuildtimeCode()) { |
|
0 commit comments