Skip to content

Commit c7ad6b2

Browse files
committed
fixed up list of stdlib modules and is_stdlib
1 parent 8f593fd commit c7ad6b2

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

pydra/utils/misc.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import ast
44
import inspect
55
import types
6+
import sysconfig
67
import sys
78
import platformdirs
89
import builtins
@@ -148,24 +149,27 @@ def get_builtin_type_names():
148149
return set(name for name, obj in vars(builtins).items() if isinstance(obj, type))
149150

150151

151-
def in_stdlib(obj: types.FunctionType | type) -> bool:
152-
"""Check if a type is in the standard library."""
152+
def in_stdlib(obj: types.FunctionType | type) -> str | bool:
153+
"""Check if a type is in the standard library and return the name of the module if
154+
so."""
153155
module = inspect.getmodule(obj)
154156
if module is None:
155157
return False
156158
if module.__name__.startswith("builtins"):
157-
return True
159+
return "builtins"
158160
if module.__name__ == "types" and obj.__name__ not in dir(types):
159161
return False
160-
return module.__name__.split(".")[-1] in STDLIB_MODULES
162+
toplevel = module.__name__.split(".")[0]
163+
if toplevel in STDLIB_MODULES:
164+
return toplevel
165+
return False
161166

162167

163168
def _stdlib_modules() -> frozenset[str]:
164169
"""List all standard library modules."""
165170
std_lib_modules = set(sys.builtin_module_names)
166-
for _, modname, ispkg in pkgutil.iter_modules():
167-
if not ispkg:
168-
std_lib_modules.add(modname)
171+
std_lib_path = sysconfig.get_path("stdlib")
172+
std_lib_modules.update(m[1] for m in pkgutil.iter_modules([std_lib_path]))
169173
return frozenset(std_lib_modules)
170174

171175

0 commit comments

Comments
 (0)