Issue 1613479: pydoc info for a package doesn't list all package contents (original) (raw)
When using pydoc to query a package, a "PACKAGE CONTENTS" list is provided to show the modules and packages that are in that package. That list will be incomplete if we are querying a package that has been split across multiple directories.
Suppose you have the following:
/first/path/foo/init.py /first/path/foo/one.py /second/path/foo/init.py /second/path/foo/two.py
and sys.path includes /first/path/ and /second/path/. If both of the foo/init.py files are empty, then "import foo" will only allow you to import modules from one of those two foo/ directories (whichever is found first in sys.path). However, if we add the following to both foo/init.py files, then we can import foo.one and foo.two because "foo" is considered to be a single package split across two directories:
from pkgutil import extend_path path = extend_path(path, name)
Please see http://www.python.org/doc/2.4.2/lib/module-pkgutil.html for some related information.
On line 1052 of pydoc.py, we have the following:
for file in os.listdir(object.path[0]):
and in that loop we only read the contents of the FIRST directory in the package's path. That should be updated to read the contents of ALL directories in the package's path. The following change will do that:
% diff -w pydoc.py pydoc.py.orig 1052,1054c1052,1053 < for objectDir in object.path: < for file in os.listdir(objectDir): < path = os.path.join(objectDir, file)
for file in os.listdir(object.__path__[0]): path = os.path.join(object.__path__[0], file)
I've attached that updated pydoc.py file to this submission. Please consider that as a replacement for the existing pydoc.py module that's currently being distributed.
I’ve just tested that the behavior is now correct. I reproduced the tree structure, set PYTHONPATH, ran “pydoc foo” to get only “one” listed, added the pkgutil call in first/path/foo/init.py, re-ran “pydoc foo”, got “one” and “two” listed. I can also import both submodules.
The pydoc code now uses pkgutil.iter_modules. Someone must have fixed that before 2.6, closing. Thanks for the report nonetheless!