sphinx-gallery icon indicating copy to clipboard operation
sphinx-gallery copied to clipboard

`AttributeError` in the backreferences module when importing PyTorch

Open antalszava opened this issue 4 years ago • 3 comments

Sphinx gallery seems to be raising an AttributeError in the backreferences module when trying to import PyTorch.

The error seems to arise both with sphinx-gallery 0.10.1 and with 0.9.0.

Full traceback:

# Sphinx version: 1.8.5
# Python version: 3.7.2 (CPython)
# Docutils version: 0.16 release
# Jinja2 version: 2.11.3
# Last messages:

# Loaded extensions:
Traceback (most recent call last):
  File "/home/circleci/project/venv/lib/python3.7/site-packages/sphinx/cmd/build.py", line 303, in build_main
    args.tags, args.verbosity, args.jobs, args.keep_going)
  File "/home/circleci/project/venv/lib/python3.7/site-packages/sphinx/application.py", line 263, in __init__
    self._init_builder()
  File "/home/circleci/project/venv/lib/python3.7/site-packages/sphinx/application.py", line 325, in _init_builder
    self.emit('builder-inited')
  File "/home/circleci/project/venv/lib/python3.7/site-packages/sphinx/application.py", line 510, in emit
    return self.events.emit(event, self, *args)
  File "/home/circleci/project/venv/lib/python3.7/site-packages/sphinx/events.py", line 80, in emit
    results.append(callback(*args))
  File "/home/circleci/project/venv/lib/python3.7/site-packages/sphinx_gallery/gen_gallery.py", line 438, in generate_gallery_rst
    examples_dir, gallery_dir, gallery_conf, seen_backrefs)
  File "/home/circleci/project/venv/lib/python3.7/site-packages/sphinx_gallery/gen_rst.py", line 382, in generate_dir_rst
    fname, target_dir, src_dir, gallery_conf, seen_backrefs)
  File "/home/circleci/project/venv/lib/python3.7/site-packages/sphinx_gallery/gen_rst.py", line 901, in generate_file_rst
    example_code_obj = identify_names(script_blocks, global_variables, node)
  File "/home/circleci/project/venv/lib/python3.7/site-packages/sphinx_gallery/backreferences.py", line 206, in identify_names
    names = list(finder.get_mapping())
  File "/home/circleci/project/venv/lib/python3.7/site-packages/sphinx_gallery/backreferences.py", line 121, in get_mapping
    module = inspect.getmodule(cc)
  File "/usr/local/lib/python3.7/inspect.py", line 725, in getmodule
    file = getabsfile(object, _filename)
  File "/usr/local/lib/python3.7/inspect.py", line 708, in getabsfile
    _filename = getsourcefile(object) or getfile(object)
  File "/usr/local/lib/python3.7/inspect.py", line 684, in getsourcefile
    filename = getfile(object)
  File "/home/circleci/project/venv/lib/python3.7/site-packages/torch/package/package_importer.py", line 618, in patched_getfile
    if object.__module__ in _package_imported_modules:
AttributeError: __module__

Here is a link to the failed run.

antalszava avatar Nov 19 '21 17:11 antalszava

We should probably change this to getattr like getattr(object, '__module__', None), could you see if it fixes your problem and perhaps make a PR to fix it?

larsoner avatar Nov 19 '21 19:11 larsoner

Thank you for the suggestion! Likely that will require the change to be made in Torch. I can get back on whether or not the update solves the issue.

antalszava avatar Nov 23 '21 01:11 antalszava

Oh yes I guess the fix would need to be there.

If you want, you could also make a workaround at our end that wraps this in a try/except:

  File "/home/circleci/project/venv/lib/python3.7/site-packages/sphinx_gallery/backreferences.py", line 121, in get_mapping
    module = inspect.getmodule(cc)

Would that help you at least build your documentation? Some or all PyTorch objects might not get documented properly, but that might be better than having the docs not build at all.

larsoner avatar Nov 23 '21 16:11 larsoner