|
56 | 56 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.NotImplementedError; |
57 | 57 | import static com.oracle.graal.python.util.PythonUtils.ARRAY_ACCESSOR; |
58 | 58 | import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING; |
| 59 | +import static com.oracle.graal.python.util.PythonUtils.internString; |
59 | 60 | import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached; |
60 | 61 | import static com.oracle.graal.python.util.PythonUtils.tsLiteral; |
61 | 62 |
|
@@ -491,8 +492,7 @@ static Object run(TruffleString name, Object dataObj, |
491 | 492 | info = result.info; |
492 | 493 | raiseFrozenError(inliningTarget, raiseNode, status, name); |
493 | 494 |
|
494 | | - RootCallTarget callTarget = createCallTarget(context, info); |
495 | | - return PFactory.createCode(context.getLanguage(), callTarget); |
| 495 | + return createCode(context, info); |
496 | 496 | } |
497 | 497 | } |
498 | 498 | } |
@@ -625,50 +625,58 @@ public static PythonModule importFrozenModuleObject(Node inliningTarget, PConstr |
625 | 625 | } |
626 | 626 | } |
627 | 627 |
|
628 | | - RootCallTarget callTarget = createCallTarget(core.getContext(), info); |
| 628 | + PCode code = createCode(core.getContext(), info); |
629 | 629 | PythonModule module = globals == null ? PFactory.createPythonModule(name) : globals; |
630 | | - PCode code = PFactory.createCode(core.getLanguage(), callTarget); |
631 | 630 |
|
632 | 631 | if (info.isPackage) { |
633 | 632 | /* Set __path__ to the empty list */ |
634 | 633 | WriteAttributeToPythonObjectNode.getUncached().execute(module, T___PATH__, PFactory.createList(core.getLanguage())); |
635 | 634 | } |
636 | 635 |
|
637 | | - CallDispatchers.SimpleIndirectInvokeNode.executeUncached(callTarget, PArguments.withGlobals(code, module)); |
| 636 | + CallDispatchers.SimpleIndirectInvokeNode.executeUncached(code.getRootCallTarget(), PArguments.withGlobals(code, module)); |
638 | 637 |
|
639 | 638 | Object origName = info.origName == null ? PNone.NONE : info.origName; |
640 | 639 | WriteAttributeToPythonObjectNode.getUncached().execute(module, T___ORIGNAME__, origName); |
641 | 640 |
|
642 | 641 | return module; |
643 | 642 | } |
644 | 643 |
|
645 | | - private static RootCallTarget createCallTarget(PythonContext context, FrozenInfo info) { |
646 | | - return (RootCallTarget) context.getLanguage().cacheCode(new PythonLanguage.CodeCacheKey(info.origName, System.identityHashCode(info.code)), () -> { |
647 | | - String name = PythonLanguage.FROZEN_FILENAME_PREFIX + info.name + PythonLanguage.FROZEN_FILENAME_SUFFIX; |
648 | | - TruffleFile originalFile = null; |
649 | | - try { |
650 | | - String fs = context.getEnv().getFileNameSeparator(); |
651 | | - String basename = context.getStdlibHome() + fs + info.name.toJavaStringUncached().replace(".", fs); |
652 | | - TruffleFile file = context.getEnv().getInternalTruffleFile(basename + J_PY_EXTENSION); |
653 | | - if (!file.isReadable()) { |
654 | | - file = context.getEnv().getInternalTruffleFile(basename + fs + "__init__.py"); |
655 | | - } |
656 | | - if (file.isReadable()) { |
657 | | - originalFile = file; |
658 | | - } |
659 | | - } catch (UnsupportedOperationException | IllegalArgumentException | SecurityException e) { |
660 | | - // Fallthrough |
| 644 | + private static PCode createCode(PythonContext context, FrozenInfo info) { |
| 645 | + String moduleName = info.name.toJavaStringUncached(); |
| 646 | + String codeName = PythonLanguage.FROZEN_FILENAME_PREFIX + moduleName + PythonLanguage.FROZEN_FILENAME_SUFFIX; |
| 647 | + TruffleFile file = null; |
| 648 | + String filename = codeName; |
| 649 | + try { |
| 650 | + String fs = context.getEnv().getFileNameSeparator(); |
| 651 | + String basename = context.getStdlibHome() + fs + moduleName.replace(".", fs); |
| 652 | + String path = info.isPackage ? basename + fs + "__init__.py" : basename + J_PY_EXTENSION; |
| 653 | + file = context.getEnv().getInternalTruffleFile(path); |
| 654 | + if (file.isReadable()) { |
| 655 | + filename = path; |
661 | 656 | } |
662 | | - Source source = Source.newBuilder(PythonLanguage.ID, "", name) // |
| 657 | + } catch (UnsupportedOperationException | IllegalArgumentException | SecurityException e) { |
| 658 | + // Fallthrough |
| 659 | + } |
| 660 | + TruffleFile originalFile = file; |
| 661 | + Source source = context.getLanguage().getOrCreateSource((ignored -> { |
| 662 | + Source newSource = Source.newBuilder(PythonLanguage.ID, "", codeName) // |
663 | 663 | .content(Source.CONTENT_NONE) // |
664 | 664 | .internal(PythonLanguage.shouldMarkSourceInternal(context)) // |
665 | 665 | .mimeType(PythonLanguage.MIME_TYPE).build(); |
666 | 666 | PythonLanguage language = context.getLanguage(); |
667 | 667 | if (originalFile != null) { |
668 | | - language.registerOriginalFile(source, originalFile); |
| 668 | + language.registerOriginalFile(newSource, originalFile); |
669 | 669 | } |
670 | | - return language.callTargetFromBytecode(source, info.code); |
671 | | - }); |
| 670 | + return newSource; |
| 671 | + }), codeName); |
| 672 | + RootCallTarget callTarget = (RootCallTarget) context.getLanguage().cacheCode( |
| 673 | + new PythonLanguage.CodeCacheKey(info.origName, System.identityHashCode(info.code)), |
| 674 | + () -> context.getLanguage().callTargetFromBytecode(source, info.code)); |
| 675 | + /* |
| 676 | + * Setting the original filename as the co_filename is a deviance from CPython, but it's |
| 677 | + * more user friendly and lets us freeze more modules without it being too visible. |
| 678 | + */ |
| 679 | + return PFactory.createCode(context.getLanguage(), callTarget, internString(toTruffleStringUncached(filename))); |
672 | 680 | } |
673 | 681 |
|
674 | 682 | /* |
|
0 commit comments