← 输入框|github.com|刷新
Skip to content

Imports Fails When Import From Files: Links To #1611 Issue #1612

Open

Description

@ronnuriel

Imports from import file fails

Lets create bar.py (bar.py is in the same directory as expr7)
bar.py:

from expr7 import test_pow

def main():
	test_pow()
 
main()

expr7.py:

from ltypes import i32

def test_pow():
    a: i32
    a = i32(pow(2, 2))

def test_pow_1(a: i32, b: i32) -> i32:
    res: i32
    res = i32(pow(a, b))
    return res

def main0():
    test_pow()
    c: i32
    c = test_pow_1(1, 2)

main0()

*** Please notice main0() call

Run:
lpython --show-c -I . bar.py

Getting the following error

ASR verify pass error: ASR verify: SubroutineCall::m_name 'main0' cannot point outside of its symbol table
Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
  File "/Users/ronnuriel/git/lpython/src/bin/lpython.cpp", line 1609
    return emit_c(arg_file, runtime_library_dir, compiler_options);
  File "/Users/ronnuriel/git/lpython/src/bin/lpython.cpp", line 288
    r1 = LCompilers::LPython::python_ast_to_asr(al, lm, *ast, diagnostics, compiler_options, true, infile);
  File "/Users/ronnuriel/git/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 6235
    auto res = symbol_table_visitor(al, lm, *ast_m, diagnostics, main_module,
  File "/Users/ronnuriel/git/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 3814
    v.visit_Module(ast);
  File "/Users/ronnuriel/git/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 3302
    visit_stmt(*x.m_body[i]);
  File "/Users/ronnuriel/git/lpython/src/lpython/python_ast.h", line 1882
    void visit_stmt(const stmt_t &b) { visit_stmt_t(b, self()); }
  File "/Users/ronnuriel/git/lpython/src/lpython/python_ast.h", line 1768
    case stmtType::Import: { v.visit_Import((const Import_t &)x); return; }
  File "/Users/ronnuriel/git/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 3635
    t = (ASR::symbol_t*)(load_module(al, st,
  File "/Users/ronnuriel/git/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 365
    mod1 = compile_module_till_asr(al, rl_path, infile, loc, diagnostics,
  File "/Users/ronnuriel/git/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 272
    Result<ASR::TranslationUnit_t*> r2 = python_ast_to_asr(al, lm, *ast,
  File "/Users/ronnuriel/git/lpython/src/lpython/semantics/python_ast_to_asr.cpp", line 6262
    throw LCompilersException("Verify failed");
LCompilersException: Verify failed

Activity

  1. added
    c
    asr
    on Mar 23, 2023
  2. changed the title [-]Imports fails when import from files: links to #1611 issue[/-] [+]Imports Fails When Import From Files: Links To #1611 Issue[/+] on Mar 23, 2023
  3. certik commented on Mar 23, 2023

    @certik
    Contributor

    I think this issue is due to us not supporting global statements in a module that is being imported. A workaround until we fix it is to put all such statements into a function, say, init_expr7() and then manually call this function every time you import the module.

  4. Smit-create commented on Apr 10, 2023

    @Smit-create
    Collaborator

    This is fixed in the master.

    See, the generated code. I have added print statements to verify the execution.

    C code

    #include <complex.h>
    #include <inttypes.h>
    #include <math.h>
    
    #include <stdlib.h>
    #include <stdbool.h>
    #include <stdio.h>
    #include <string.h>
    #include <lfortran_intrinsics.h>
    
    #define ASSERT(cond)                                                           \
        {                                                                          \
            if (!(cond)) {                                                         \
                printf("%s%s", "ASSERT failed: ", __FILE__);                       \
                printf("%s%s", "\nfunction ", __func__);                           \
                printf("%s%d%s", "(), line number ", __LINE__, " at \n");          \
                printf("%s%s", #cond, "\n");                                       \
                exit(1);                                                           \
            }                                                                      \
        }
    #define ASSERT_MSG(cond, msg)                                                  \
        {                                                                          \
            if (!(cond)) {                                                         \
                printf("%s%s", "ASSERT failed: ", __FILE__);                       \
                printf("%s%s", "\nfunction ", __func__);                           \
                printf("%s%d%s", "(), line number ", __LINE__, " at \n");          \
                printf("%s%s", #cond, "\n");                                       \
                printf("%s", "ERROR MESSAGE:\n");                                  \
                printf("%s%s", msg, "\n");                                         \
                exit(1);                                                           \
            }                                                                      \
        }
    
    
    struct dimension_descriptor
    {
        int32_t lower_bound, length;
    };
    
    // Implementations
    double __lpython_overloaded_0__pow(int32_t x, int32_t y)
    {
        double _lpython_return_variable;
        _lpython_return_variable = (double)(pow(x, y));
        return _lpython_return_variable;
    }
    
    float _lfortran_caimag(float complex x);
    
    double _lfortran_zaimag(double complex x);
    
    void test_pow()
    {
        int32_t a;
        a = (int32_t)(__lpython_overloaded_0__pow(2, 2));
    }
    
    int32_t test_pow_1(int32_t a, int32_t b)
    {
        int32_t _lpython_return_variable;
        int32_t res;
        res = (int32_t)(__lpython_overloaded_0__pow(a, b));
        _lpython_return_variable = res;
        return _lpython_return_variable;
    }
    
    void main0()
    {
        int32_t c;
        test_pow();
        printf("%s\n", "in module expr7");
        c = test_pow_1(1, 2);
    }
    
    void global_statements()
    {
        main0();
    }
    
    void _xx_lcompilers_changed_main_xx()
    {
        printf("%s\n", "in module bar");
        test_pow();
    }
    
    void _lpython_main_program()
    {
        global_statements();
        _xx_lcompilers_changed_main_xx();
    }
    
    int main(int argc, char* argv[])
    {
        _lpython_main_program();
        return 0;
    }

    Can you please verify @ronnuriel? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    asrc

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions