Skip to content
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

Importing modules does not work inside the function #1317

Closed
Thirumalai-Shaktivel opened this issue Nov 21, 2022 · 5 comments
Closed

Importing modules does not work inside the function #1317

Thirumalai-Shaktivel opened this issue Nov 21, 2022 · 5 comments

Comments

@Thirumalai-Shaktivel
Copy link
Collaborator

I came across another issue that doesn't work yet. Like for this example:

import test_import

def test():
    print(test_import.print_a())
    print(test_import.print_b())
    print(test_import.print_c())

test()

Error:

lpython integration_tests/test_import_01.py
Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
  Binary file "/home/thirumalai/Open_Source/lpython/src/bin/lpython", in _start()
  File "./csu/../csu/libc-start.c", line 392, in __libc_start_main_impl()
  File "./csu/../sysdeps/nptl/libc_start_call_main.h", line 58, in __libc_start_call_main()
  File "/home/thirumalai/Open_Source/lpython/src/bin/lpython.cpp", line 1545, in ??
    err = compile_python_to_object_file(arg_file, tmp_o, runtime_library_dir, lpython_pass_manager, compiler_options, time_report);
  File "/home/thirumalai/Open_Source/lpython/src/bin/lpython.cpp", line 592, in ??
    !(arg_c && compiler_options.disable_main), infile);
AssertFailed: asr_verify(*tu, true, diagnostics)

I will try to work on this later this week. After that, I think we are ready to start making python packages.

Originally posted by @Thirumalai-Shaktivel in #1305 (comment)

@certik
Copy link
Contributor

certik commented Nov 21, 2022

We have two options:

  • Handle it in the frontend, so test_import.print_b() just becomes an ExternalSymbol to the correct print_b() and the information that it was called via test_import. will be lost.
  • Add test_import as a symbol into the SymbolTable.

The second approach is more general, it would allow to treat modules as symbols / variables. Not sure if we need it or not.

We need this in LFortran too to implement j3-fortran/fortran_proposals#1, so we should do a good design.

@czgdp1807 what is your opinion?

@certik
Copy link
Contributor

certik commented Nov 24, 2022

Regarding priorities, this is nice to have, but not critical, as we can simply import the symbols directly for now.

@czgdp1807
Copy link
Collaborator

czgdp1807 commented Nov 24, 2022

Oh yes. I am fixing working on this exact issue. I think we follow second approach right now. But for the case with function defined inside a module has the same name as the module then second approach will be messier because you will have to do name mangling. See mathfn for example, mathfn/sin.py is a module which has def sin defined inside it. So here first approach is an easy to follow option. However where this same name case is not present then second approach is easier.

@certik
Copy link
Contributor

certik commented Nov 24, 2022

I would work on this later, I think first we should implement more examples, such as making mathfn much more complex and see what else is missing. Then we can do this one in a more natural manner.

@Thirumalai-Shaktivel
Copy link
Collaborator Author

Thirumalai-Shaktivel commented Dec 10, 2022

This issue is fixed (#1323), I think.

$ cat integration_tests/test_import_01.py 
import test_import

def test():
    print(test_import.print_a())
    print(test_import.print_b())
    print(test_import.print_c())

test()
$ lpython integration_tests/test_import_01.py 
A
B
C 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants