Skip to content

Commit 3aae83d

Browse files
committed
Pass original files as TruffleFile
1 parent 3d14702 commit 3aae83d

4 files changed

Lines changed: 48 additions & 49 deletions

File tree

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ public boolean isSingleContext() {
365365
*/
366366
private final ConcurrentHashMap<Object, Source> sourceCache = new ConcurrentHashMap<>();
367367

368+
private final ConcurrentHashMap<Source, TruffleFile> originalFiles = new ConcurrentHashMap<>();
369+
368370
@Idempotent
369371
public static PythonLanguage get(Node node) {
370372
return REFERENCE.get(node);
@@ -1221,33 +1223,28 @@ public Source getOrCreateSource(Function<Object, Source> rootNodeFunction, Objec
12211223
}
12221224

12231225
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) {
1226+
if (sourceWithoutContent.hasCharacters()) {
12341227
return sourceWithoutContent;
12351228
}
1236-
PythonContext context = PythonContext.get(null);
1237-
if (context == null) {
1229+
TruffleFile originalFile = originalFiles.get(sourceWithoutContent);
1230+
if (originalFile == null) {
12381231
return sourceWithoutContent;
12391232
}
1233+
return getOrCreateSource(ignored -> loadSourceWithContent(sourceWithoutContent, originalFile), sourceWithoutContent);
1234+
}
1235+
1236+
private Source loadSourceWithContent(Source sourceWithoutContent, TruffleFile originalFile) {
12401237
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) {
1238+
return Source.newBuilder(ID, originalFile).name(sourceWithoutContent.getName()).internal(sourceWithoutContent.isInternal()).mimeType(MIME_TYPE).build();
1239+
} catch (IOException | SecurityException | UnsupportedOperationException | IllegalArgumentException e) {
12471240
return sourceWithoutContent;
12481241
}
12491242
}
12501243

1244+
public void registerOriginalFile(Source sourceWithoutContent, TruffleFile originalFile) {
1245+
originalFiles.put(sourceWithoutContent, originalFile);
1246+
}
1247+
12511248
public static PythonOS getPythonOS() {
12521249
if (PythonOS.internalCurrent == PythonOS.PLATFORM_ANY) {
12531250
if (ImageInfo.inImageBuildtimeCode()) {

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,16 @@ private static Object doLoadBytecodeFile(Object bytecodePath, Object sourcePath,
560560
Object message = PyObjectCallMethodObjArgs.executeUncached(MESSAGE, T_FORMAT, bytecodePath, sourcePath);
561561
CallNode.executeUncached(context.lookupBuiltinModule(T__BOOTSTRAP).getAttribute(T__VERBOSE_MESSAGE), message);
562562
}
563-
return MarshalModuleBuiltins.fromBytecodeFile(context, bytecodeFile, bytes, 16, bytes.length - 16, cacheKey);
563+
TruffleFile sourceFile = null;
564+
if (sourcePath != PNone.NONE) {
565+
try {
566+
TruffleString strSourcePath = PyObjectStrAsTruffleStringNode.executeUncached(sourcePath);
567+
sourceFile = context.getPublicTruffleFileRelaxed(strSourcePath);
568+
} catch (SecurityException | UnsupportedOperationException | IllegalArgumentException ignored) {
569+
// Fall back to Marshal's empty source.
570+
}
571+
}
572+
return MarshalModuleBuiltins.fromBytecodeFile(context, bytecodeFile, sourceFile, bytes, 16, bytes.length - 16, cacheKey);
564573
} catch (MarshalModuleBuiltins.Marshal.MarshalError me) {
565574
throw PRaiseNode.raiseStatic(inliningTarget, me.type, me.message, me.arguments);
566575
} catch (IOException | SecurityException | UnsupportedOperationException | IllegalArgumentException e) {

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -644,28 +644,29 @@ public static PythonModule importFrozenModuleObject(Node inliningTarget, PConstr
644644
private static RootCallTarget createCallTarget(PythonContext context, FrozenInfo info) {
645645
return (RootCallTarget) context.getLanguage().cacheCode(new PythonLanguage.CodeCacheKey(info.origName, System.identityHashCode(info.code)), () -> {
646646
String name = PythonLanguage.FROZEN_FILENAME_PREFIX + info.name + PythonLanguage.FROZEN_FILENAME_SUFFIX;
647-
Source.LiteralBuilder builder = null;
647+
TruffleFile originalFile = null;
648648
try {
649649
String fs = context.getEnv().getFileNameSeparator();
650650
String basename = context.getStdlibHome() + fs + info.name.toJavaStringUncached().replace(".", fs);
651-
String originalPath = basename + J_PY_EXTENSION;
652-
TruffleFile originalFile = context.getEnv().getInternalTruffleFile(originalPath);
653-
if (!originalFile.isReadable()) {
654-
originalPath = basename + fs + "__init__.py";
655-
originalFile = context.getEnv().getInternalTruffleFile(originalPath);
651+
TruffleFile file = context.getEnv().getInternalTruffleFile(basename + J_PY_EXTENSION);
652+
if (!file.isReadable()) {
653+
file = context.getEnv().getInternalTruffleFile(basename + fs + "__init__.py");
656654
}
657-
if (originalFile.isReadable()) {
658-
builder = Source.newBuilder(PythonLanguage.ID, originalFile).content(Source.CONTENT_NONE).name(name);
655+
if (file.isReadable()) {
656+
originalFile = file;
659657
}
660658
} catch (UnsupportedOperationException | IllegalArgumentException | SecurityException e) {
661659
// Fallthrough
662660
}
663-
if (builder == null) {
664-
builder = Source.newBuilder(PythonLanguage.ID, "", name).content(Source.CONTENT_NONE);
661+
Source source = Source.newBuilder(PythonLanguage.ID, "", name) //
662+
.content(Source.CONTENT_NONE) //
663+
.internal(PythonLanguage.shouldMarkSourceInternal(context)) //
664+
.mimeType(PythonLanguage.MIME_TYPE).build();
665+
PythonLanguage language = context.getLanguage();
666+
if (originalFile != null) {
667+
language.registerOriginalFile(source, originalFile);
665668
}
666-
builder.internal(PythonLanguage.shouldMarkSourceInternal(context));
667-
builder.mimeType(PythonLanguage.MIME_TYPE);
668-
return context.getLanguage().callTargetFromBytecode(builder.build(), info.code);
669+
return language.callTargetFromBytecode(source, info.code);
669670
});
670671
}
671672

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

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ public int read(byte[] b, int off, int len) {
476476
int depth = 0;
477477
long cacheKey;
478478
TruffleFile bytecodeFile;
479+
TruffleFile sourceFile;
479480
// Offset of the buffer in parent buffer in nested deserializations
480481
int baseOffset;
481482

@@ -1552,23 +1553,13 @@ private PCode readCode() {
15521553
int firstLineNo = readInt();
15531554
byte[] lnoTab = readBytes();
15541555
com.oracle.graal.python.util.Supplier<CallTarget> supplier = () -> {
1555-
String jFilename = fileName.toJavaStringUncached();
15561556
String jName = code.qualname.toJavaStringUncached();
1557-
Source.LiteralBuilder builder = null;
1558-
try {
1559-
TruffleFile file = context.getEnv().getPublicTruffleFile(jFilename);
1560-
if (file.isReadable()) {
1561-
builder = Source.newBuilder(PythonLanguage.ID, file).content(Source.CONTENT_NONE).name(jName);
1562-
}
1563-
} catch (UnsupportedOperationException | IllegalArgumentException | SecurityException e) {
1564-
// Fallthrough
1565-
}
1566-
if (builder == null) {
1567-
builder = Source.newBuilder(PythonLanguage.ID, "", jName).content(Source.CONTENT_NONE);
1557+
Source source = Source.newBuilder(PythonLanguage.ID, "", jName).content(Source.CONTENT_NONE).build();
1558+
PythonLanguage language = getLanguage();
1559+
if (sourceFile != null) {
1560+
language.registerOriginalFile(source, sourceFile);
15681561
}
1569-
builder.internal(PythonLanguage.shouldMarkSourceInternal(context));
1570-
builder.mimeType(PythonLanguage.MIME_TYPE);
1571-
return PythonLanguage.callTargetFromBytecode(context, builder.build(), code);
1562+
return language.callTargetFromBytecode(source, code);
15721563
};
15731564
CallTarget callTarget;
15741565
if (getLanguage().isSingleContext() || cacheKey == 0) {
@@ -1716,8 +1707,9 @@ public ReparseError(String message) {
17161707
}
17171708

17181709
@TruffleBoundary
1719-
public static Object fromBytecodeFile(PythonContext context, TruffleFile file, byte[] bytes, int offset, int length, long cacheKey) throws IOException {
1720-
MarshalModuleBuiltins.Marshal marshal = new MarshalModuleBuiltins.Marshal(context, bytes, length + offset, cacheKey, file, 0);
1710+
public static Object fromBytecodeFile(PythonContext context, TruffleFile bytecodeFile, TruffleFile sourceFile, byte[] bytes, int offset, int length, long cacheKey) throws IOException {
1711+
MarshalModuleBuiltins.Marshal marshal = new MarshalModuleBuiltins.Marshal(context, bytes, length + offset, cacheKey, bytecodeFile, 0);
1712+
marshal.sourceFile = sourceFile;
17211713
marshal.in.skipBytes(offset);
17221714
return marshal.readObject();
17231715
}

0 commit comments

Comments
 (0)