Skip to content

Commit 2c62e5f

Browse files
remove invalid module
1 parent 3a5d757 commit 2c62e5f

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

Lib/runpy.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,39 @@ def _get_possible_name_list(wrong_name):
110110
d = []
111111
for finder in sys.meta_path:
112112
if discover := getattr(finder, 'discover', None):
113-
d += [spec.name for spec in discover(parent)]
113+
try:
114+
d += [spec.name for spec in discover(parent) if _is_valid_spec(spec)]
115+
except Exception:
116+
continue
114117
return d
115118
except Exception:
116119
return None
117120

118121

122+
def _is_valid_spec(spec):
123+
if spec is None:
124+
return False
125+
mod_name = spec.name
126+
if spec.submodule_search_locations is not None:
127+
if mod_name == "__main__" or mod_name.endswith(".__main__"):
128+
return False
129+
main_module = mod_name + ".__main__"
130+
try:
131+
main_spec = importlib.util.find_spec(main_module)
132+
if main_spec is None:
133+
return False
134+
return main_spec.submodule_search_locations is None and _is_valid_spec(main_spec)
135+
except Exception:
136+
return False
137+
loader = spec.loader
138+
if loader is None:
139+
return False
140+
try:
141+
return loader.get_code(mod_name) is not None
142+
except ImportError:
143+
return False
144+
145+
119146
# Helper to get the full name, spec and code for a module
120147
def _get_module_details(mod_name, error=ImportError):
121148
if mod_name.startswith("."):

0 commit comments

Comments
 (0)