Skip to content

Commit 3064bad

Browse files
authored
Merge pull request #1306 from Thirumalai-Shaktivel/nested_modules_02
Move nested modules handling into a separate function
2 parents 35a4542 + 9088078 commit 3064bad

File tree

6 files changed

+62
-56
lines changed

6 files changed

+62
-56
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ RUN(NAME modules_01 LABELS cpython llvm wasm wasm_x86)
240240
RUN(NAME modules_02 LABELS cpython llvm wasm wasm_x86)
241241
RUN(NAME test_import_01 LABELS cpython llvm)
242242
RUN(NAME test_import_02 LABELS cpython llvm)
243+
RUN(NAME test_import_03 LABELS cpython llvm)
243244
RUN(NAME test_math LABELS cpython llvm)
244245
RUN(NAME test_numpy_01 LABELS cpython llvm c)
245246
RUN(NAME test_numpy_02 LABELS cpython llvm c)

integration_tests/test_import_01.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import test_import
2-
from test_import import print_a
32

4-
print(print_a())
3+
print(test_import.print_a())
54
print(test_import.print_b())
65
print(test_import.print_c())

integration_tests/test_import_03.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from test_import import print_a
2+
from test_import.test_import_1 import print_b
3+
4+
print(print_a())
5+
print(print_b())

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3397,6 +3397,57 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
33973397
}
33983398
}
33993399

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

src/lpython/utils.cpp

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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)