Open nennigb opened 2 years ago
Since you seem to have already investigated it some, care to propose a fix here: https://github.com/pdoc3/pdoc/blob/2cce30a9b55eeeddc1ed826c8a2ada53777c3eea/pdoc/__init__.py#L196-L235 or here: https://github.com/pdoc3/pdoc/blob/2cce30a9b55eeeddc1ed826c8a2ada53777c3eea/pdoc/__init__.py#L719-L768 ?
It looks like something in iter_modules()
. inspect.getmodulename(file)
recognizes those .so files as valid modules, so the function offers them for importing ... :thinking:
yes, module_name = inspect.getmodulename(file)
returns a module name
in this function there is a list of all possible suffix given by importlib.machinery.all_suffixes()
['.py', '.pyc', '.cpython-39-x86_64-linux-gnu.so', '.abi3.so', '.so']
when processing the true extension, the suffix '.cpython-39-x86_64-linux-gnu.so'
is detected and remove, but when we have the wrong python version, the '.so'
is caught and only the '.so'
is removed leading to a wrong module name...
Perhaps we may add a extra check to see if the module_name
is valid. For instance here it contains .
. Not sure if it should be always true at this stage although it is a loop module files...
Another approach is to silent the path import error while keeping invalid python module import error...
I also made a mistake since an arbitrary abcd.so
also raise an error (the file was originality in the wrong place :-( I edit my previous message).
I'd rather something more strict like:
if '.cpython-' in module_name:
continue
PR welcome.
I see with --skip-errors
CLI switch, one can already turn this into a warning.
https://github.com/pdoc3/pdoc/blob/2cce30a9b55eeeddc1ed826c8a2ada53777c3eea/pdoc/__init__.py#L753-L761
Can you show example of an error with abcd.so
file?
if '.cpython-' in module_name: continue
Agree, it will strongly limit the side effect, but I not sure if it will work on other platform. I will have a look.
If abcd.so
is an empty file, the error is
raise ImportError(f'Error importing {module!r}: {e.__class__.__name__}: {e}')
ImportError: Error importing 'pypolsys.abcd': ImportError: /home/xyz/Recherche/CODE/polsysplp/pypolsys/abcd.so: file too short
if abscd.so
is a copy of a valid module extension with bad naming convention, the error is
raise ImportError(f'Error importing {module!r}: {e.__class__.__name__}: {e}')
ImportError: Error importing 'pypolsys.abcd': ImportError: dynamic module does not define module export function (PyInit_abcd)
if the so file is a valid, non extension so file
raise ImportError(f'Error importing {module!r}: {e.__class__.__name__}: {e}')
ImportError: Error importing 'pypolsys.libstdc++': ModuleNotFoundError: No module named 'pypolsys.libstdc++'
I will be happy to contribute.
ps : I miss the --skip-errors
switch !
Expected Behavior
I am documenting a package containing fortran extension build with f2py/gfortran/gcc on linux. Once the package is installed the root folder contains
mypkg.cpython-39-x86_64-linux-gnu.so
which is imported in the__init__.py
file. If I run pdoc, it works fine ;-)I am using several python version (the package are installed in editable mode) and I can have also
mypkg.cpython-38-x86_64-linux-gnu.so
ormypkg.cpython-310-x86_64-linux-gnu.so
in the same place. Then pdoc crashes.It is noteworthy that arbitrary .so file like
abcd.so
is also a problem. We could expect that pdoc ignore such additional .so file (filter by python version, just pick one, ...)Steps to Reproduce
pdoc3 mypkg
Additional info