Skip to content

Commit 67251c2

Browse files
authored
Merge pull request #1323 from czgdp1807/import_02
Importing modules containing symbols of same name
2 parents ffd8770 + 9c58510 commit 67251c2

File tree

6 files changed

+58
-13
lines changed

6 files changed

+58
-13
lines changed

integration_tests/CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ macro(RUN)
6161
if (import_path)
6262
add_custom_command(
6363
OUTPUT ${name}.o
64-
COMMAND lpython -c -I ${import_path} ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py -o ${name}.o
64+
COMMAND lpython -c -I ${CMAKE_CURRENT_SOURCE_DIR}/${import_path} ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py -o ${name}.o
6565
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py
6666
VERBATIM)
6767
else ()
@@ -109,7 +109,7 @@ macro(RUN)
109109

110110
add_test(${name} python ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py)
111111
set_tests_properties(${name} PROPERTIES
112-
ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/../src/runtime/ltypes;LPYTHON_PY_MOD_NAME=${PY_MOD};LPYTHON_PY_MOD_PATH=${CMAKE_CURRENT_BINARY_DIR}")
112+
ENVIRONMENT "PYTHONPATH=${CMAKE_SOURCE_DIR}/../src/runtime/ltypes:${CMAKE_SOURCE_DIR}/..;LPYTHON_PY_MOD_NAME=${PY_MOD};LPYTHON_PY_MOD_PATH=${CMAKE_CURRENT_BINARY_DIR}")
113113
if (RUN_LABELS)
114114
set_tests_properties(${name} PROPERTIES LABELS "${RUN_LABELS}")
115115
endif()
@@ -248,9 +248,10 @@ RUN(NAME test_for_loop LABELS cpython llvm c)
248248
RUN(NAME modules_01 LABELS cpython llvm wasm wasm_x86)
249249
RUN(NAME modules_02 LABELS cpython llvm wasm wasm_x86)
250250
RUN(NAME test_import_01 LABELS cpython llvm)
251-
RUN(NAME test_import_02 IMPORT_PATH integration_tests
252-
LABELS cpython llvm)
251+
RUN(NAME test_import_02 LABELS cpython llvm)
253252
RUN(NAME test_import_03 LABELS cpython llvm)
253+
RUN(NAME test_import_04 IMPORT_PATH ..
254+
LABELS cpython llvm)
254255
RUN(NAME test_math LABELS cpython llvm)
255256
RUN(NAME test_numpy_01 LABELS cpython llvm c)
256257
RUN(NAME test_numpy_02 LABELS cpython llvm c)

integration_tests/test_import_04.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import test_modules
2+
3+
def main0():
4+
print(test_modules.sin(0.5))
5+
print(test_modules.cos(0.5))
6+
assert abs(test_modules.sin(0.5) + test_modules.cos(0.5) - 1.0) <= 1e-12
7+
8+
main0()

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,12 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
421421
}
422422

423423
ASR::symbol_t* import_from_module(Allocator &al, ASR::Module_t *m, SymbolTable *current_scope,
424-
std::string mname, std::string cur_sym_name, std::string new_sym_name,
424+
std::string /*mname*/, std::string cur_sym_name, std::string& new_sym_name,
425425
const Location &loc, bool skip_current_scope_check=false) {
426+
new_sym_name = ASRUtils::get_mangled_name(m, new_sym_name);
426427
ASR::symbol_t *t = m->m_symtab->resolve_symbol(cur_sym_name);
427428
if (!t) {
428-
throw SemanticError("The symbol '" + cur_sym_name + "' not found in the module '" + mname + "'",
429+
throw SemanticError("The symbol '" + cur_sym_name + "' not found in the module '" + std::string(m->m_name) + "'",
429430
loc);
430431
}
431432
if (!skip_current_scope_check &&
@@ -481,11 +482,19 @@ ASR::symbol_t* import_from_module(Allocator &al, ASR::Module_t *m, SymbolTable *
481482
} else if (ASR::is_a<ASR::ExternalSymbol_t>(*t)) {
482483
ASR::ExternalSymbol_t *es = ASR::down_cast<ASR::ExternalSymbol_t>(t);
483484
SymbolTable *symtab = current_scope;
484-
while (symtab->parent != nullptr) symtab = symtab->parent;
485-
ASR::symbol_t *sym = symtab->get_symbol(es->m_module_name);
485+
// while (symtab->parent != nullptr) symtab = symtab->parent;
486+
ASR::symbol_t *sym = symtab->resolve_symbol(es->m_module_name);
486487
ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(sym);
487-
return import_from_module(al, m, symtab, es->m_name,
488+
return import_from_module(al, m, symtab, es->m_module_name,
488489
cur_sym_name, new_sym_name, loc);
490+
} else if (ASR::is_a<ASR::Module_t>(*t)) {
491+
ASR::Module_t* mt = ASR::down_cast<ASR::Module_t>(t);
492+
std::string mangled_cur_sym_name = ASRUtils::get_mangled_name(mt, new_sym_name);
493+
if( cur_sym_name == mangled_cur_sym_name ) {
494+
throw SemanticError("Importing modules isn't supported yet.", loc);
495+
}
496+
return import_from_module(al, mt, current_scope, std::string(mt->m_name),
497+
cur_sym_name, new_sym_name, loc);
489498
} else {
490499
throw SemanticError("Only Subroutines, Functions, Variables and "
491500
"ExternalSymbol are currently supported in 'import'", loc);
@@ -1007,6 +1016,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
10071016
std::string mod_name = ASR::down_cast<ASR::ExternalSymbol_t>(stemp)->m_module_name;
10081017
ASR::symbol_t *mt = symtab->resolve_symbol(mod_name);
10091018
ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(mt);
1019+
local_sym = ASRUtils::get_mangled_name(m, local_sym);
10101020
stemp = import_from_module(al, m, symtab, mod_name,
10111021
remote_sym, local_sym, loc);
10121022
LFORTRAN_ASSERT(ASR::is_a<ASR::ExternalSymbol_t>(*stemp));
@@ -3534,7 +3544,18 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
35343544
throw SemanticError("The module '" + msym + "' cannot be loaded",
35353545
x.base.base.loc);
35363546
}
3537-
current_module_dependencies.push_back(al, s2c(al, msym));
3547+
if( msym == "__init__" ) {
3548+
for( auto item: ASRUtils::symbol_symtab(t)->get_scope() ) {
3549+
if( ASR::is_a<ASR::ExternalSymbol_t>(*item.second) &&
3550+
!present(current_module_dependencies,
3551+
ASR::down_cast<ASR::ExternalSymbol_t>(item.second)->m_module_name) ) {
3552+
current_module_dependencies.push_back(al,
3553+
ASR::down_cast<ASR::ExternalSymbol_t>(item.second)->m_module_name);
3554+
}
3555+
}
3556+
} else {
3557+
current_module_dependencies.push_back(al, s2c(al, msym));
3558+
}
35383559
}
35393560

35403561
ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(t);
@@ -3544,7 +3565,7 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
35443565
}
35453566
std::string new_sym_name = ASRUtils::get_mangled_name(m, remote_sym);
35463567
ASR::symbol_t *t = import_from_module(al, m, current_scope, msym,
3547-
remote_sym, new_sym_name, x.base.base.loc, false);
3568+
remote_sym, new_sym_name, x.base.base.loc, true);
35483569
current_scope->add_symbol(new_sym_name, t);
35493570
}
35503571

@@ -4614,7 +4635,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
46144635
ASR::symbol_t *sym = current_scope->resolve_symbol(sym_name);
46154636
if (!sym) {
46164637
sym = import_from_module(al, m, current_scope, value,
4617-
x.m_attr, sym_name, x.base.base.loc, true);
4638+
x.m_attr, sym_name, x.base.base.loc, false);
46184639
LFORTRAN_ASSERT(ASR::is_a<ASR::ExternalSymbol_t>(*sym));
46194640
current_scope->add_symbol(sym_name, sym);
46204641
}
@@ -5898,7 +5919,13 @@ Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al, LocationManager
58985919
} else {
58995920
return res2.error;
59005921
}
5901-
LFORTRAN_ASSERT(asr_verify(*tu, true, diagnostics));
5922+
#if defined(WITH_LFORTRAN_ASSERT)
5923+
diag::Diagnostics diagnostics;
5924+
if (!asr_verify(*tu, true, diagnostics)) {
5925+
std::cerr << diagnostics.render2();
5926+
throw LCompilersException("Verify failed");
5927+
};
5928+
#endif
59025929
}
59035930

59045931
if (main_module) {

test_modules/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .mathfn import sin, cos

test_modules/mathfn/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .sin import sin, cos

test_modules/mathfn/sin.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from ltypes import f64
2+
3+
def sin(x: f64) -> f64:
4+
return x + 1.0
5+
6+
def cos(x: f64) -> f64:
7+
return x - 1.0

0 commit comments

Comments
 (0)