Skip to content

Commit 635ed20

Browse files
authored
Import issues (#2829)
* Fix: Unused imports no longer give unhandled exception * Test: Added tests for unused imports * Previous commit bug fix moved dependency addition to body visitor
1 parent 22ea159 commit 635ed20

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ RUN(NAME test_import_04 LABELS cpython llvm llvm_jit c)
606606
RUN(NAME test_import_05 LABELS cpython llvm llvm_jit c wasm wasm_x86 wasm_x64)
607607
RUN(NAME test_import_06 LABELS cpython llvm llvm_jit)
608608
RUN(NAME test_import_07 LABELS cpython llvm llvm_jit c)
609+
RUN(NAME test_import_08 LABELS cpython llvm)
609610
RUN(NAME test_math LABELS cpython llvm llvm_jit NOFAST)
610611
RUN(NAME test_membership_01 LABELS cpython llvm)
611612
RUN(NAME test_numpy_01 LABELS cpython llvm llvm_jit c)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import string
2+
3+
import test_import_08_module
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from lpython import i32
2+
3+
a: i32 = 10
4+
b: i32 = a + 10
5+
6+
print("Inside import")

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4910,6 +4910,16 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
49104910
throw SemanticError("The module '" + mod_sym + "' cannot be loaded",
49114911
x.base.base.loc);
49124912
}
4913+
if( mod_sym == "__init__" ) {
4914+
for( auto item: ASRUtils::symbol_symtab(t)->get_scope() ) {
4915+
if( ASR::is_a<ASR::ExternalSymbol_t>(*item.second) ) {
4916+
current_module_dependencies.push_back(al,
4917+
ASR::down_cast<ASR::ExternalSymbol_t>(item.second)->m_module_name);
4918+
}
4919+
}
4920+
} else {
4921+
current_module_dependencies.push_back(al, s2c(al, mod_sym));
4922+
}
49134923
}
49144924
}
49154925

@@ -5114,6 +5124,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
51145124
tmp = nullptr;
51155125
tmp_vec.clear();
51165126
visit_stmt(*x.m_body[i]);
5127+
51175128
for (auto t: global_init) {
51185129
if (t) {
51195130
items.push_back(al, t);

0 commit comments

Comments
 (0)