summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2021-01-05 23:00:23 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2021-01-05 23:37:40 +0900
commita51c8a565280529e25d1cad879349d7addc1414d (patch)
treed27b3866dff7ab6482fdf3b5ed7cc71bd382c485
parent4755557a7df0a335b62c3f6bc062ce1c01c2d45a (diff)
downloadsphinx-git-a51c8a565280529e25d1cad879349d7addc1414d.tar.gz
Fix #8655: autodoc: Crashes when object raises an exception 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. This starts to use `safe_getattr()` to check the object is mocked or not and to prevent an unexpected error.
-rw-r--r--CHANGES3
-rw-r--r--sphinx/ext/autodoc/mock.py5
2 files changed, 7 insertions, 1 deletions
diff --git a/CHANGES b/CHANGES
index 7139f6540..24ebde5aa 100644
--- a/CHANGES
+++ b/CHANGES
@@ -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
--------
diff --git a/sphinx/ext/autodoc/mock.py b/sphinx/ext/autodoc/mock.py
index 381848fc1..3d4f76410 100644
--- a/sphinx/ext/autodoc/mock.py
+++ b/sphinx/ext/autodoc/mock.py
@@ -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