Skip to content

Commit a8f56f2

Browse files
committed
GR-26405: ZipImporterBuiltins - add missing find_loader
1 parent 8c69cb3 commit a8f56f2

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/zipimporter/PZipImporter.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,22 @@ protected final ModuleInfo getModuleInfo(String fullname) {
273273
return ModuleInfo.NOT_FOUND;
274274
}
275275

276+
@TruffleBoundary
277+
protected boolean isDir(String path) {
278+
String dirPath = path + separator;
279+
for (Object key : files.keys()) {
280+
if (key.equals(dirPath)) {
281+
return true;
282+
}
283+
}
284+
return false;
285+
}
286+
287+
@TruffleBoundary
288+
protected String getModulePath(String modPath) {
289+
return this.archive + separator + modPath;
290+
}
291+
276292
/**
277293
*
278294
* @param fullname

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/zipimporter/ZipImporterBuiltins.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,41 @@ public Object doit(PZipImporter self, String fullname, @SuppressWarnings("unused
418418

419419
}
420420

421+
@Builtin(name = "find_loader", minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 3)
422+
@TypeSystemReference(PythonArithmeticTypes.class)
423+
@GenerateNodeFactory
424+
public abstract static class FindLoaderNode extends PythonTernaryBuiltinNode {
425+
@Specialization
426+
public Object findLoader(PZipImporter self, String fullname, @SuppressWarnings("unused") Object path) {
427+
PZipImporter.ModuleInfo mi = self.getModuleInfo(fullname);
428+
if (mi != ModuleInfo.NOT_FOUND) {
429+
return makeTuple(self, makeList());
430+
}
431+
432+
String modPath = self.makeFilename(fullname);
433+
if (self.isDir(modPath)) {
434+
return makeTuple(makeList(self.getModulePath(modPath)));
435+
}
436+
return makeTuple(makeList());
437+
}
438+
439+
private PTuple makeTuple(Object second) {
440+
return makeTuple(null, second);
441+
}
442+
443+
private PTuple makeTuple(Object first, Object second) {
444+
return factory().createTuple(new Object[]{first != null ? first : PNone.NONE, second});
445+
}
446+
447+
private PList makeList() {
448+
return factory().createList();
449+
}
450+
451+
private PList makeList(Object first) {
452+
return factory().createList(new Object[]{first});
453+
}
454+
}
455+
421456
@Builtin(name = "get_code", minNumOfPositionalArgs = 2)
422457
@TypeSystemReference(PythonArithmeticTypes.class)
423458
@GenerateNodeFactory

0 commit comments

Comments
 (0)