diff --git a/CHANGES b/CHANGES index b5d984306..929b468fd 100644 --- a/CHANGES +++ b/CHANGES @@ -17,6 +17,7 @@ Bugs fixed ---------- * #5460: html search does not work with some 3rd party themes +* #5614: autodoc: incremental build is broken when builtin modules are imported Testing -------- diff --git a/sphinx/util/__init__.py b/sphinx/util/__init__.py index 109b098ce..9bc88c82f 100644 --- a/sphinx/util/__init__.py +++ b/sphinx/util/__init__.py @@ -301,7 +301,9 @@ def get_module_source(modname): raise PycodeError('error getting filename for %r' % filename, err) if filename is None and loader: try: - return 'string', loader.get_source(modname) + filename = loader.get_source(modname) + if filename: + return 'string', filename except Exception as err: raise PycodeError('error getting source for %r' % modname, err) if filename is None: diff --git a/tests/test_util.py b/tests/test_util.py index 8e4fbd6b0..a3745e7a1 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -12,9 +12,14 @@ import pytest from mock import patch +from six import PY2 + +import sphinx +from sphinx.errors import PycodeError from sphinx.testing.util import strip_escseq from sphinx.util import ( - display_chunk, encode_uri, parselinenos, status_iterator, xmlname_checker + display_chunk, encode_uri, get_module_source, parselinenos, status_iterator, + xmlname_checker ) from sphinx.util import logging @@ -42,6 +47,19 @@ def test_display_chunk(): assert display_chunk(('hello', 'sphinx', 'world')) == 'hello .. world' +def test_get_module_source(): + if PY2: + assert get_module_source('sphinx') == ('file', sphinx.__file__.replace('.pyc', '.py')) + else: + assert get_module_source('sphinx') == ('file', sphinx.__file__) + + # failed to obtain source information from builtin modules + with pytest.raises(PycodeError): + get_module_source('builtins') + with pytest.raises(PycodeError): + get_module_source('itertools') + + @pytest.mark.sphinx('dummy') @patch('sphinx.util.console._tw', 40) # terminal width = 40 def test_status_iterator(app, status, warning):