Skip to content

Commit

Permalink
Fix sphinx-doc#8655: autodoc: Crashes when object raises an exception…
Browse files Browse the repository at this point in the history
… on hasattr()

autodoc crashes when the target object raises an exception on
`hasattr()`.  The `hasattr()` function internally calls the
`obj.__getattr__()` or `obj.__getattribute__()` of the target object.
Hence the reaction can be changed on the target object.

Is starts to use `safe_getattr()` to check the object is mocked or not
and to prevent the unexpected errors.
  • Loading branch information
tk0miya committed Jan 5, 2021
1 parent 4755557 commit b4a401c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES
Expand Up @@ -16,6 +16,9 @@ Features added
Bugs fixed
----------

* #8655: autodoc: Failed to generate document if target module contains an
object that raises an exception on ``hasattr()``

Testing
--------

Expand Down
5 changes: 4 additions & 1 deletion sphinx/ext/autodoc/mock.py
Expand Up @@ -153,7 +153,10 @@ def mock(modnames: List[str]) -> Generator[None, None, None]:
def ismock(subject: Any) -> bool:
"""Check if the object is mocked."""
# check the object has '__sphinx_mock__' attribute
if not hasattr(subject, '__sphinx_mock__'):
try:
if safe_getattr(subject, '__sphinx_mock__', None) is None:
return False
except AttributeError:
return False

# check the object is mocked module
Expand Down

0 comments on commit b4a401c

Please sign in to comment.