Skip to content

Commit f379105

Browse files
committed
Detect submods of native mods more conservatively
- The submodule detection for extension modules (aka native "C" extensions) now runs only if the regular submodule detection didn't run. - Further, the submodule detection for extension modules runs only if submodules are explicitly exposed via the "__all__" attribute. This is is because it would otherwise be difficult or impossible to reliably detect cycles and/or reexports of external modules.
1 parent 0848c42 commit f379105

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

pdoc/__init__.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -708,10 +708,6 @@ def is_from_this_module(obj):
708708
self.doc[name] = Function(name, self, obj)
709709
elif inspect.isclass(obj):
710710
self.doc[name] = Class(name, self, obj)
711-
elif inspect.ismodule(obj):
712-
self.doc[name] = Module(
713-
obj, docfilter=docfilter, supermodule=self,
714-
context=context, skip_errors=skip_errors)
715711
elif name in var_docstrings:
716712
self.doc[name] = Variable(name, self, var_docstrings[name], obj=obj)
717713

@@ -765,6 +761,24 @@ def iter_modules(paths):
765761
if m.is_namespace and not m.doc:
766762
del self.doc[root]
767763
self._context.pop(m.refname)
764+
elif hasattr(self.obj, '__all__'):
765+
# Python extension modules don't get recognized by `is_package` because they have no
766+
# "__path__" attribute. We treat them here separately. We support submodules of
767+
# extension modules only if they are explicitly exposed via the "__all__" attribute
768+
# because otherwise it's hard to distinguish proper submodules from re-exports (i.e.,
769+
# the function `is_from_this_module` doesn't work on submodules).
770+
for name, obj in public_objs:
771+
if inspect.ismodule(obj) and not hasattr(obj, '__file__') and not name in self.doc:
772+
try:
773+
m = Module(
774+
obj, docfilter=docfilter, supermodule=self,
775+
context=self._context, skip_errors=skip_errors)
776+
except Exception as ex:
777+
if skip_errors:
778+
warn(str(ex), Module.ImportWarning)
779+
continue
780+
raise
781+
self.doc[name] = m
768782

769783
# Apply docfilter
770784
if docfilter:

0 commit comments

Comments
 (0)