Skip to content

Commit cae3fec

Browse files
Add set_module_symbol function
To set the correct module name to be imported. Also, set the correct import path
1 parent ebf8b44 commit cae3fec

File tree

3 files changed

+55
-54
lines changed

3 files changed

+55
-54
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

+53-52
Original file line numberDiff line numberDiff line change
@@ -3395,6 +3395,57 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
33953395
}
33963396
}
33973397

3398+
void set_module_symbol(std::string &mod_sym, std::vector<std::string> &paths) {
3399+
if (mod_sym != "ltypes") {
3400+
// Check for the import path and mod_sym not in runtime library
3401+
if (import_path != "" &&
3402+
!path_exists(paths[0] + '/' + mod_sym + ".py")) {
3403+
paths = {import_path};
3404+
if (parent_dir != "") paths[0] += '/' + parent_dir;
3405+
if(path_exists(paths[0])) {
3406+
// Check for the nested modules with "."
3407+
// Example: from x.y import z
3408+
if (mod_sym.find(".") != std::string::npos) {
3409+
mod_sym.replace(mod_sym.find("."), 1, "/");
3410+
if(is_directory(paths[0] + "/" + mod_sym)) {
3411+
// Directory i.e., x/y/__init__.py
3412+
paths[0] += '/' + mod_sym;
3413+
mod_sym = "__init__";
3414+
} else {
3415+
// File i.e., x/y.py
3416+
paths[0] += '/' + mod_sym.substr(0,
3417+
mod_sym.find_last_of('/'));
3418+
mod_sym = mod_sym.substr(mod_sym.find_last_of('/') + 1,
3419+
mod_sym.size() - 1);
3420+
}
3421+
} else if(is_directory(paths[0] + "/" + mod_sym)) {
3422+
// Directory i.e., x/__init__.py
3423+
paths[0] += '/' + mod_sym;
3424+
mod_sym = "__init__";
3425+
}
3426+
}
3427+
} else if (mod_sym.find(".") != std::string::npos) {
3428+
// Check for the nested modules with "."
3429+
// Example: from x.y import z
3430+
mod_sym.replace(mod_sym.find("."), 1, "/");
3431+
if(is_directory(paths[1] + "/" + mod_sym)) {
3432+
if (parent_dir != "") paths[1] += "/";
3433+
paths[1] += mod_sym;
3434+
mod_sym = "__init__";
3435+
}
3436+
} else if (parent_dir != ""
3437+
&& is_directory(paths[1] + '/' + mod_sym)) {
3438+
// Directory i.e., parent_dir/x/__init__.py
3439+
paths[1] += '/' + mod_sym;
3440+
mod_sym = "__init__";
3441+
} else if (is_directory(mod_sym)) {
3442+
// Directory i.e., x/__init__.py
3443+
paths.push_back(mod_sym);
3444+
mod_sym = "__init__";
3445+
}
3446+
}
3447+
}
3448+
33983449
void visit_ImportFrom(const AST::ImportFrom_t &x) {
33993450
if (!x.m_module) {
34003451
throw SemanticError("Not implemented: The import statement must currently specify the module name", x.base.base.loc);
@@ -3415,32 +3466,7 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
34153466
st = st->parent;
34163467
}
34173468
bool ltypes, enum_py;
3418-
if (msym != "ltypes") {
3419-
if (import_path != "" &&
3420-
!path_exits(paths[0] + '/' + msym + ".py")) {
3421-
paths = {import_path};
3422-
if (parent_dir != "") paths[0] += '/' + parent_dir;
3423-
if(is_directory(paths[0])) {
3424-
if (msym.find(".") != std::string::npos) {
3425-
msym.replace(msym.find("."), 1, "/");
3426-
}
3427-
paths[0] += '/' + msym;
3428-
msym = "__init__";
3429-
}
3430-
} else if (msym.find(".") != std::string::npos) {
3431-
msym.replace(msym.find("."), 1, "/");
3432-
if (parent_dir != "") paths[1] += "/";
3433-
paths[1] += msym;
3434-
msym = "__init__";
3435-
} else if (is_directory(msym)) {
3436-
paths = {rl_path, msym};
3437-
msym = "__init__";
3438-
} else if (paths[1] != ""
3439-
&& is_directory(paths[1] + '/' + msym)) {
3440-
paths[1] += '/' + msym;
3441-
msym = "__init__";
3442-
}
3443-
}
3469+
set_module_symbol(msym, paths);
34443470
t = (ASR::symbol_t*)(load_module(al, st,
34453471
msym, x.base.base.loc, false, paths, ltypes, enum_py,
34463472
[&](const std::string &msg, const Location &loc) { throw SemanticError(msg, loc); },
@@ -3486,32 +3512,7 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
34863512
}
34873513
for (auto &mod_sym : mods) {
34883514
bool ltypes, enum_py;
3489-
if (mod_sym != "ltypes") {
3490-
if (import_path != "" &&
3491-
!path_exits(paths[0] + '/' + mod_sym + ".py")) {
3492-
paths = {import_path};
3493-
if (parent_dir != "") paths[0] += '/' + parent_dir;
3494-
if(is_directory(paths[0])) {
3495-
if (mod_sym.find(".") != std::string::npos) {
3496-
mod_sym.replace(mod_sym.find("."), 1, "/");
3497-
}
3498-
paths[0] += '/' + mod_sym;
3499-
mod_sym = "__init__";
3500-
}
3501-
} else if (mod_sym.find(".") != std::string::npos) {
3502-
mod_sym.replace(mod_sym.find("."), 1, "/");
3503-
if (parent_dir != "") paths[1] += "/";
3504-
paths[1] += mod_sym;
3505-
mod_sym = "__init__";
3506-
} else if (is_directory(mod_sym)) {
3507-
paths = {rl_path, mod_sym};
3508-
mod_sym = "__init__";
3509-
} else if (paths[1] != ""
3510-
&& is_directory(paths[1] + '/' + mod_sym)) {
3511-
paths[1] += '/' + mod_sym;
3512-
mod_sym = "__init__";
3513-
}
3514-
}
3515+
set_module_symbol(mod_sym, paths);
35153516
t = (ASR::symbol_t*)(load_module(al, st,
35163517
mod_sym, x.base.base.loc, false, paths, ltypes, enum_py,
35173518
[&](const std::string &msg, const Location &loc) { throw SemanticError(msg, loc); },

src/lpython/utils.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ bool is_directory(std::string path) {
9494
return false;
9595
}
9696

97-
bool path_exits(std::string path) {
97+
bool path_exists(std::string path) {
9898
struct stat buffer;
9999
if (stat(path.c_str(), &buffer) == 0) {
100100
return true;

src/lpython/utils.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ void get_executable_path(std::string &executable_path, int &dirname_length);
1010
std::string get_runtime_library_dir();
1111
std::string get_runtime_library_header_dir();
1212
bool is_directory(std::string path);
13-
bool path_exits(std::string path);
13+
bool path_exists(std::string path);
1414

1515
} // LFortran
1616

0 commit comments

Comments
 (0)