Skip to content

Commit f57b8d8

Browse files
committed
Set frozen module co_filename to original file
1 parent 06c059e commit f57b8d8

1 file changed

Lines changed: 33 additions & 25 deletions

File tree

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ImpModuleBuiltins.java

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import static com.oracle.graal.python.runtime.exception.PythonErrorType.NotImplementedError;
5757
import static com.oracle.graal.python.util.PythonUtils.ARRAY_ACCESSOR;
5858
import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING;
59+
import static com.oracle.graal.python.util.PythonUtils.internString;
5960
import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached;
6061
import static com.oracle.graal.python.util.PythonUtils.tsLiteral;
6162

@@ -491,8 +492,7 @@ static Object run(TruffleString name, Object dataObj,
491492
info = result.info;
492493
raiseFrozenError(inliningTarget, raiseNode, status, name);
493494

494-
RootCallTarget callTarget = createCallTarget(context, info);
495-
return PFactory.createCode(context.getLanguage(), callTarget);
495+
return createCode(context, info);
496496
}
497497
}
498498
}
@@ -625,50 +625,58 @@ public static PythonModule importFrozenModuleObject(Node inliningTarget, PConstr
625625
}
626626
}
627627

628-
RootCallTarget callTarget = createCallTarget(core.getContext(), info);
628+
PCode code = createCode(core.getContext(), info);
629629
PythonModule module = globals == null ? PFactory.createPythonModule(name) : globals;
630-
PCode code = PFactory.createCode(core.getLanguage(), callTarget);
631630

632631
if (info.isPackage) {
633632
/* Set __path__ to the empty list */
634633
WriteAttributeToPythonObjectNode.getUncached().execute(module, T___PATH__, PFactory.createList(core.getLanguage()));
635634
}
636635

637-
CallDispatchers.SimpleIndirectInvokeNode.executeUncached(callTarget, PArguments.withGlobals(code, module));
636+
CallDispatchers.SimpleIndirectInvokeNode.executeUncached(code.getRootCallTarget(), PArguments.withGlobals(code, module));
638637

639638
Object origName = info.origName == null ? PNone.NONE : info.origName;
640639
WriteAttributeToPythonObjectNode.getUncached().execute(module, T___ORIGNAME__, origName);
641640

642641
return module;
643642
}
644643

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;
661656
}
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) //
663663
.content(Source.CONTENT_NONE) //
664664
.internal(PythonLanguage.shouldMarkSourceInternal(context)) //
665665
.mimeType(PythonLanguage.MIME_TYPE).build();
666666
PythonLanguage language = context.getLanguage();
667667
if (originalFile != null) {
668-
language.registerOriginalFile(source, originalFile);
668+
language.registerOriginalFile(newSource, originalFile);
669669
}
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)));
672680
}
673681

674682
/*

0 commit comments

Comments
 (0)