diff --git a/sphinx/ext/autodoc.py b/sphinx/ext/autodoc.py index 18efb02f9..22a4504f4 100644 --- a/sphinx/ext/autodoc.py +++ b/sphinx/ext/autodoc.py @@ -499,7 +499,14 @@ class RstGenerator(object): if what == 'module': if hasattr(todoc, '__all__'): members_check_module = False - all_members = inspect.getmembers(todoc, lambda x: x in todoc.__all__) + all_members = [] + for mname in todoc.__all__: + try: + all_members.append((mname, getattr(todoc, mname))) + except AttributeError: + self.warn('missing attribute mentioned in __all__: ' + 'module %s, attribute %s' % + (todoc.__name__, mname)) else: # for implicit module members, check __module__ to avoid # documenting imported objects diff --git a/tests/test_autodoc.py b/tests/test_autodoc.py index f1608fc64..71291da2e 100644 --- a/tests/test_autodoc.py +++ b/tests/test_autodoc.py @@ -268,9 +268,9 @@ def test_generate(): def assert_result_contains(item, *args): gen.generate(*args) + print '\n'.join(gen.result) assert len(gen.warnings) == 0, gen.warnings assert item in gen.result - print '\n'.join(gen.result) del gen.result[:] # no module found? @@ -322,6 +322,16 @@ def test_generate(): assert_result_contains(' :deprecated:', 'module', 'test_autodoc', [], None) options.platform = 'Platform' assert_result_contains(' :platform: Platform', 'module', 'test_autodoc', [], None) + # test if __all__ is respected for modules + assert_result_contains('.. class:: Class', 'module', 'test_autodoc', + ['__all__'], None) + try: + assert_result_contains('.. exception:: CustomEx', 'module', 'test_autodoc', + ['__all__'], None) + except AssertionError: + pass + else: + assert False, 'documented CustomEx which is not in __all__' # test noindex flag options.noindex = True @@ -335,6 +345,8 @@ def test_generate(): # --- generate fodder ------------ +__all__ = ['Class'] + class CustomEx(Exception): """My custom exception."""