Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ASR: Fix Import as #2252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ RUN(NAME test_import_04 IMPORT_PATH ..
LABELS cpython llvm c)
RUN(NAME test_import_05 LABELS cpython llvm c wasm wasm_x86 wasm_x64)
RUN(NAME test_import_06 LABELS cpython llvm)
RUN(NAME test_import_07 LABELS cpython llvm c)
RUN(NAME test_math LABELS cpython llvm NOFAST)
RUN(NAME test_numpy_01 LABELS cpython llvm c)
RUN(NAME test_numpy_02 LABELS cpython llvm c)
Expand Down
8 changes: 8 additions & 0 deletions integration_tests/test_import_07.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# test issue 2153
from test_import_07_module import f as fa

def main0():
assert fa(3) == 6
assert fa(10) == 20

main0()
4 changes: 4 additions & 0 deletions integration_tests/test_import_07_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from lpython import i32

def f(x: i32) -> i32:
return 2 * x
19 changes: 12 additions & 7 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4472,10 +4472,6 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
throw SemanticError("Not implemented: The import statement must currently specify the module name", x.base.base.loc);
}
std::string msym = x.m_module; // Module name
std::vector<std::string> mod_symbols;
for (size_t i=0; i<x.n_names; i++) {
mod_symbols.push_back(x.m_names[i].m_name);
}

// Get the module, for now assuming it is not loaded, so we load it:
ASR::symbol_t *t = nullptr; // current_scope->parent->resolve_symbol(msym);
Expand Down Expand Up @@ -4518,13 +4514,17 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
}

ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(t);
int i=-1;
for (auto &remote_sym : mod_symbols) {
int i = -1;
for (size_t j=0; j<x.n_names; j++) {
std::string remote_sym = x.m_names[j].m_name;
i++;
if( procedures_db.is_function_to_be_ignored(msym, remote_sym) ) {
continue ;
}
std::string new_sym_name = ASRUtils::get_mangled_name(m, remote_sym);
if (x.m_names[j].m_asname) {
new_sym_name = ASRUtils::get_mangled_name(m, x.m_names[j].m_asname);
}
ASR::symbol_t *t = import_from_module(al, m, current_scope, msym,
remote_sym, new_sym_name, x.m_names[i].loc, true);
if (current_scope->get_scope().find(new_sym_name) != current_scope->get_scope().end()) {
Expand Down Expand Up @@ -4914,7 +4914,12 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
// Handled by SymbolTableVisitor already
std::string mod_name = x.m_module;
for (size_t i = 0; i < x.n_names; i++) {
imported_functions[x.m_names[i].m_name] = mod_name;
if (x.m_names[i].m_asname) {
imported_functions[x.m_names[i].m_asname] = mod_name;
}
else {
imported_functions[x.m_names[i].m_name] = mod_name;
}
}
ASR::symbol_t *mod_sym = current_scope->resolve_symbol(mod_name);
if (mod_sym) {
Expand Down