From c6df7762cfad0e472e28f5b8836c88acde79e2de Mon Sep 17 00:00:00 2001 From: Smit-create Date: Fri, 4 Aug 2023 13:11:44 +0530 Subject: [PATCH 1/2] ASR: Use Import as names for external symbols --- src/lpython/semantics/python_ast_to_asr.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index d6cf1ba4d7..5d9816c986 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -4472,10 +4472,6 @@ class SymbolTableVisitor : public CommonVisitor { 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 mod_symbols; - for (size_t i=0; iparent->resolve_symbol(msym); @@ -4518,13 +4514,17 @@ class SymbolTableVisitor : public CommonVisitor { } ASR::Module_t *m = ASR::down_cast(t); - int i=-1; - for (auto &remote_sym : mod_symbols) { + int i = -1; + for (size_t j=0; jget_scope().find(new_sym_name) != current_scope->get_scope().end()) { @@ -4914,7 +4914,12 @@ class BodyVisitor : public CommonVisitor { // 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) { From 36ba561a8c5ee93b3fe2433e67bb07110e278feb Mon Sep 17 00:00:00 2001 From: Smit-create Date: Fri, 4 Aug 2023 13:16:59 +0530 Subject: [PATCH 2/2] Add tests --- integration_tests/CMakeLists.txt | 1 + integration_tests/test_import_07.py | 8 ++++++++ integration_tests/test_import_07_module.py | 4 ++++ 3 files changed, 13 insertions(+) create mode 100644 integration_tests/test_import_07.py create mode 100644 integration_tests/test_import_07_module.py diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index affa4bd57c..df1fefe7a3 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -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) diff --git a/integration_tests/test_import_07.py b/integration_tests/test_import_07.py new file mode 100644 index 0000000000..b799410cbf --- /dev/null +++ b/integration_tests/test_import_07.py @@ -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() diff --git a/integration_tests/test_import_07_module.py b/integration_tests/test_import_07_module.py new file mode 100644 index 0000000000..a0925aa5ae --- /dev/null +++ b/integration_tests/test_import_07_module.py @@ -0,0 +1,4 @@ +from lpython import i32 + +def f(x: i32) -> i32: + return 2 * x