Commit 5e0b10a4 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #221 from toshok/package-imports

load package/__init__.py if it exists, and set its __path__
parents b917c7a6 5874fa9d
...@@ -32,12 +32,78 @@ BoxedModule* createAndRunModule(const std::string& name, const std::string& fn) ...@@ -32,12 +32,78 @@ BoxedModule* createAndRunModule(const std::string& name, const std::string& fn)
return module; return module;
} }
static BoxedModule* createAndRunModule(const std::string& name, const std::string& fn, const std::string& module_path) {
BoxedModule* module = createModule(name, fn);
Box* b_path = boxStringPtr(&module_path);
BoxedList* path_list = new BoxedList();
listAppendInternal(path_list, b_path);
module->setattr("__path__", path_list, NULL);
AST_Module* ast = caching_parse(fn.c_str());
compileAndRunModule(ast, module);
return module;
}
#if LLVMREV < 210072 #if LLVMREV < 210072
#define LLVM_SYS_FS_EXISTS_CODE_OKAY(code) ((code) == 0) #define LLVM_SYS_FS_EXISTS_CODE_OKAY(code) ((code) == 0)
#else #else
#define LLVM_SYS_FS_EXISTS_CODE_OKAY(code) (!(code)) #define LLVM_SYS_FS_EXISTS_CODE_OKAY(code) (!(code))
#endif #endif
static bool pathExists(const std::string& path) {
#if LLVMREV < 217625
bool exists;
llvm_error_code code = llvm::sys::fs::exists(path, exists);
assert(LLVM_SYS_FS_EXISTS_CODE_OKAY(code));
#else
bool exists = llvm::sys::fs::exists(path);
#endif
return exists;
}
static BoxedModule* importPackageFromDirectory(const std::string& name, const std::string& path) {
llvm::SmallString<128> joined_path;
llvm::sys::path::append(joined_path, path, name);
std::string dn(joined_path.str());
llvm::sys::path::append(joined_path, "__init__.py");
std::string fn(joined_path.str());
if (VERBOSITY() >= 2)
printf("Searching for %s at %s...\n", name.c_str(), fn.c_str());
if (!pathExists(fn))
return NULL;
if (VERBOSITY() >= 1)
printf("Importing %s from %s\n", name.c_str(), fn.c_str());
return createAndRunModule(name, fn, dn);
}
static BoxedModule* importFile(const std::string& name, const std::string& path) {
llvm::SmallString<128> joined_path;
llvm::sys::path::append(joined_path, path, name + ".py");
std::string fn(joined_path.str());
if (VERBOSITY() >= 2)
printf("Searching for %s at %s...\n", name.c_str(), fn.c_str());
if (!pathExists(fn))
return NULL;
if (VERBOSITY() >= 1)
printf("Importing %s from %s\n", name.c_str(), fn.c_str());
return createAndRunModule(name, fn);
}
static Box* importSub(const std::string* name, Box* parent_module) { static Box* importSub(const std::string* name, Box* parent_module) {
BoxedList* path_list; BoxedList* path_list;
if (parent_module == NULL) { if (parent_module == NULL) {
...@@ -60,29 +126,21 @@ static Box* importSub(const std::string* name, Box* parent_module) { ...@@ -60,29 +126,21 @@ static Box* importSub(const std::string* name, Box* parent_module) {
BoxedString* p = static_cast<BoxedString*>(_p); BoxedString* p = static_cast<BoxedString*>(_p);
joined_path.clear(); joined_path.clear();
llvm::sys::path::append(joined_path, p->s, *name + ".py"); llvm::sys::path::append(joined_path, p->s);
std::string fn(joined_path.str()); std::string dn(joined_path.str());
if (VERBOSITY() >= 2) BoxedModule* module;
printf("Searching for %s at %s...\n", name->c_str(), fn.c_str());
#if LLVMREV < 217625 module = importPackageFromDirectory(*name, dn);
bool exists; if (!module)
llvm_error_code code = llvm::sys::fs::exists(joined_path.str(), exists); module = importFile(*name, dn);
assert(LLVM_SYS_FS_EXISTS_CODE_OKAY(code));
#else
bool exists = llvm::sys::fs::exists(joined_path.str());
#endif
if (!exists)
continue;
if (VERBOSITY() >= 1) if (module) {
printf("Importing %s from %s\n", name->c_str(), fn.c_str()); if (parent_module)
parent_module->setattr(*name, module, NULL);
BoxedModule* module = createAndRunModule(*name, fn);
return module; return module;
} }
}
if (*name == "basic_test") { if (*name == "basic_test") {
return importTestExtension("basic_test"); return importTestExtension("basic_test");
......
import distutils
import distutils.log
print type(distutils)
print type(distutils.log)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment