public NModuleType loadModule(String modname) throws Exception {
if (failedModules.contains(modname)) {
return null;
}
NModuleType cached = getCachedModule(modname); // builtin file-less modules
if (cached != null) {
finer("\nusing cached module " + modname);
return cached;
}
NModuleType mt = getBuiltinModule(modname);
if (mt != null) {
return mt;
}
finer("looking for module " + modname);
if (modname.endsWith(".py")) {
modname = modname.substring(0, modname.length() - 3);
}
String modpath = modname.replace('.', '/');
// A nasty hack to avoid e.g. python2.5 becoming python2/5.
// Should generalize this for directory components containing '.'.
modpath = modpath.replaceFirst("(/python[23])/([0-9]/)", "$1.$2");
List<String> loadPath = getLoadPath();
for (String p : loadPath) {
String dirname = p + modpath;
String pyname = dirname + ".py";
String initname = Util.joinPath(dirname, "__init__.py").getAbsolutePath();
String name;
// foo/bar has priority over foo/bar.py
// http://www.python.org/doc/essays/packages.html
if (Util.isReadableFile(initname)) {
name = initname;
} else if (Util.isReadableFile(pyname)) {
name = pyname;
} else {
continue;
}
name = Util.canonicalize(name);
NModuleType m = loadFile(name);
if (m != null) {
finer("load of module " + modname + "[succeeded]");
return m;
}
}