From 941d03420096197e108596a49ecbe14f5fd8f7fd Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sun, 23 Apr 2023 01:58:47 +0530 Subject: [PATCH 1/5] ASR: Update paths to only include found path (if any) fixes https://github.com/lcompilers/lpython/issues/1737 --- src/lpython/semantics/python_ast_to_asr.cpp | 27 +++++++++++++-------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index bfd2121594..0b0ac26af7 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -3644,19 +3644,16 @@ class SymbolTableVisitor : public CommonVisitor { Search all the paths in order and stop when the desired module is found. */ - bool module_found = false; + std::string path_found = ""; for( auto& path: paths ) { if(is_directory(path + "/" + directory + mod_sym)) { - module_found = true; // Directory i.e., x/y/__init__.py - path += '/' + directory + mod_sym; + path_found = path + '/' + directory + mod_sym; mod_sym = "__init__"; + break; } else if(path_exists(path + "/" + directory + mod_sym + ".py")) { - module_found = true; // File i.e., x/y.py - path += '/' + directory; - } - if( module_found ) { + path_found = path + '/' + directory; break; } } @@ -3666,15 +3663,25 @@ class SymbolTableVisitor : public CommonVisitor { specified and if its a directory then prioritise the directory itself. */ - if( !module_found ) { + if( path_found.empty() ) { if (is_directory(directory + mod_sym)) { // Directory i.e., x/__init__.py - paths.insert(paths.begin(), directory + mod_sym); mod_sym = "__init__"; + path_found = directory + mod_sym; } else if (path_exists(directory + mod_sym + ".py")) { - paths.insert(paths.begin(), directory); + path_found = directory; } } + + // Update paths to contain only the found path (if found) + // so that later load_module() should only use this path + // to read the package/module file + // load_module() need not search through all paths again + if (path_found.empty()) { + paths = {}; + } else { + paths = {path_found}; + } } void visit_ImportFrom(const AST::ImportFrom_t &x) { From 4f94d5591d828ddda6450da7c2673a77ef8aecf2 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sun, 23 Apr 2023 02:05:22 +0530 Subject: [PATCH 2/5] TEST: Add and enable test --- integration_tests/CMakeLists.txt | 1 + integration_tests/test_import/sys.py | 2 ++ integration_tests/test_import_05.py | 3 +++ 3 files changed, 6 insertions(+) create mode 100644 integration_tests/test_import/sys.py create mode 100644 integration_tests/test_import_05.py diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 815b9e052a..65549078ec 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -370,6 +370,7 @@ RUN(NAME test_import_02 LABELS cpython llvm c) RUN(NAME test_import_03 LABELS cpython llvm c) 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_math LABELS cpython llvm) 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/sys.py b/integration_tests/test_import/sys.py new file mode 100644 index 0000000000..6f1802d6b7 --- /dev/null +++ b/integration_tests/test_import/sys.py @@ -0,0 +1,2 @@ +def hi_from_user_sys(): + print("hi from user sys!") diff --git a/integration_tests/test_import_05.py b/integration_tests/test_import_05.py new file mode 100644 index 0000000000..bc11d2acea --- /dev/null +++ b/integration_tests/test_import_05.py @@ -0,0 +1,3 @@ +from test_import.sys import hi_from_user_sys + +hi_from_user_sys() From f3da969d1d791e5aede6f1101052f1def23cfb43 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sun, 23 Apr 2023 02:49:05 +0530 Subject: [PATCH 3/5] ASR: Remove unneeded/duplicate if check --- src/lpython/semantics/python_ast_to_asr.cpp | 43 +++++++++------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 0b0ac26af7..c11c9a9398 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -201,34 +201,29 @@ Result get_full_path(const std::string &filename, if (status) { return file_path; } else { - status = read_file(file_path, input); - if (status) { - return file_path; - } else { - // If this is `lpython`, do a special lookup - if (filename == "lpython.py") { - file_path = runtime_library_dir + "/lpython/" + filename; - status = read_file(file_path, input); - if (status) { - lpython = true; - return file_path; - } else { - return Error(); - } - } else if (startswith(filename, "numpy.py")) { - file_path = runtime_library_dir + "/lpython_intrinsic_" + filename; - status = read_file(file_path, input); - if (status) { - return file_path; - } else { - return Error(); - } - } else if (startswith(filename, "enum.py")) { - enum_py = true; + // If this is `lpython`, do a special lookup + if (filename == "lpython.py") { + file_path = runtime_library_dir + "/lpython/" + filename; + status = read_file(file_path, input); + if (status) { + lpython = true; + return file_path; + } else { return Error(); + } + } else if (startswith(filename, "numpy.py")) { + file_path = runtime_library_dir + "/lpython_intrinsic_" + filename; + status = read_file(file_path, input); + if (status) { + return file_path; } else { return Error(); } + } else if (startswith(filename, "enum.py")) { + enum_py = true; + return Error(); + } else { + return Error(); } } } From 9276d770a4bd2ba8fc1b04d099322b39ed927cef Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sun, 23 Apr 2023 03:12:27 +0530 Subject: [PATCH 4/5] ASR: Include runtime_library_dir in paths --- src/lpython/semantics/python_ast_to_asr.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index c11c9a9398..e7511fa1a6 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -3673,7 +3673,9 @@ class SymbolTableVisitor : public CommonVisitor { // to read the package/module file // load_module() need not search through all paths again if (path_found.empty()) { - paths = {}; + // include the runtime library dir so that later + // runtime library modules could be imported + paths = {get_runtime_library_dir()}; } else { paths = {path_found}; } From cbd49a4e094d02a38abd9e7354bb213d4f1fa6bb Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sun, 23 Apr 2023 12:52:57 +0530 Subject: [PATCH 5/5] TEST: Return integer and add assert() --- integration_tests/test_import/sys.py | 5 ++++- integration_tests/test_import_05.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/integration_tests/test_import/sys.py b/integration_tests/test_import/sys.py index 6f1802d6b7..ac905eb29d 100644 --- a/integration_tests/test_import/sys.py +++ b/integration_tests/test_import/sys.py @@ -1,2 +1,5 @@ -def hi_from_user_sys(): +from lpython import i32 + +def hi_from_user_sys() -> i32: print("hi from user sys!") + return -5 diff --git a/integration_tests/test_import_05.py b/integration_tests/test_import_05.py index bc11d2acea..8ee6c54c7a 100644 --- a/integration_tests/test_import_05.py +++ b/integration_tests/test_import_05.py @@ -1,3 +1,3 @@ from test_import.sys import hi_from_user_sys -hi_from_user_sys() +assert hi_from_user_sys() == -5