Skip to content

Commit 941d034

Browse files
committed
ASR: Update paths to only include found path (if any)
fixes lcompilers#1737
1 parent fd6ee57 commit 941d034

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3644,19 +3644,16 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
36443644
Search all the paths in order and stop
36453645
when the desired module is found.
36463646
*/
3647-
bool module_found = false;
3647+
std::string path_found = "";
36483648
for( auto& path: paths ) {
36493649
if(is_directory(path + "/" + directory + mod_sym)) {
3650-
module_found = true;
36513650
// Directory i.e., x/y/__init__.py
3652-
path += '/' + directory + mod_sym;
3651+
path_found = path + '/' + directory + mod_sym;
36533652
mod_sym = "__init__";
3653+
break;
36543654
} else if(path_exists(path + "/" + directory + mod_sym + ".py")) {
3655-
module_found = true;
36563655
// File i.e., x/y.py
3657-
path += '/' + directory;
3658-
}
3659-
if( module_found ) {
3656+
path_found = path + '/' + directory;
36603657
break;
36613658
}
36623659
}
@@ -3666,15 +3663,25 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
36663663
specified and if its a directory
36673664
then prioritise the directory itself.
36683665
*/
3669-
if( !module_found ) {
3666+
if( path_found.empty() ) {
36703667
if (is_directory(directory + mod_sym)) {
36713668
// Directory i.e., x/__init__.py
3672-
paths.insert(paths.begin(), directory + mod_sym);
36733669
mod_sym = "__init__";
3670+
path_found = directory + mod_sym;
36743671
} else if (path_exists(directory + mod_sym + ".py")) {
3675-
paths.insert(paths.begin(), directory);
3672+
path_found = directory;
36763673
}
36773674
}
3675+
3676+
// Update paths to contain only the found path (if found)
3677+
// so that later load_module() should only use this path
3678+
// to read the package/module file
3679+
// load_module() need not search through all paths again
3680+
if (path_found.empty()) {
3681+
paths = {};
3682+
} else {
3683+
paths = {path_found};
3684+
}
36783685
}
36793686

36803687
void visit_ImportFrom(const AST::ImportFrom_t &x) {

0 commit comments

Comments
 (0)