@@ -3644,19 +3644,16 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
3644
3644
Search all the paths in order and stop
3645
3645
when the desired module is found.
3646
3646
*/
3647
- bool module_found = false ;
3647
+ std::string path_found = " " ;
3648
3648
for ( auto & path: paths ) {
3649
3649
if (is_directory (path + " /" + directory + mod_sym)) {
3650
- module_found = true ;
3651
3650
// Directory i.e., x/y/__init__.py
3652
- path += ' /' + directory + mod_sym;
3651
+ path_found = path + ' /' + directory + mod_sym;
3653
3652
mod_sym = " __init__" ;
3653
+ break ;
3654
3654
} else if (path_exists (path + " /" + directory + mod_sym + " .py" )) {
3655
- module_found = true ;
3656
3655
// File i.e., x/y.py
3657
- path += ' /' + directory;
3658
- }
3659
- if ( module_found ) {
3656
+ path_found = path + ' /' + directory;
3660
3657
break ;
3661
3658
}
3662
3659
}
@@ -3666,15 +3663,25 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
3666
3663
specified and if its a directory
3667
3664
then prioritise the directory itself.
3668
3665
*/
3669
- if ( !module_found ) {
3666
+ if ( path_found. empty () ) {
3670
3667
if (is_directory (directory + mod_sym)) {
3671
3668
// Directory i.e., x/__init__.py
3672
- paths.insert (paths.begin (), directory + mod_sym);
3673
3669
mod_sym = " __init__" ;
3670
+ path_found = directory + mod_sym;
3674
3671
} else if (path_exists (directory + mod_sym + " .py" )) {
3675
- paths. insert (paths. begin (), directory) ;
3672
+ path_found = directory;
3676
3673
}
3677
3674
}
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
+ }
3678
3685
}
3679
3686
3680
3687
void visit_ImportFrom (const AST::ImportFrom_t &x) {
0 commit comments