mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Merge pull request #4299 from tk0miya/refactor_MockImporter
autodoc: refactor MockImporter
This commit is contained in:
commit
9d44cb5952
@ -24,7 +24,8 @@ from docutils.parsers.rst import Directive
|
||||
from docutils.statemachine import ViewList
|
||||
|
||||
import sphinx
|
||||
from sphinx.ext.autodoc.importer import _MockImporter, import_module
|
||||
from sphinx.ext.autodoc.importer import mock, import_module
|
||||
from sphinx.ext.autodoc.importer import _MockImporter # to keep compatibility # NOQA
|
||||
from sphinx.ext.autodoc.inspector import format_annotation, formatargspec # to keep compatibility # NOQA
|
||||
from sphinx.util import rpartition, force_decode
|
||||
from sphinx.locale import _
|
||||
@ -388,7 +389,7 @@ class Documenter(object):
|
||||
self.modname, '.'.join(self.objpath))
|
||||
# always enable mock import hook
|
||||
# it will do nothing if autodoc_mock_imports is empty
|
||||
import_hook = _MockImporter(self.env.config.autodoc_mock_imports)
|
||||
with mock(self.env.config.autodoc_mock_imports):
|
||||
try:
|
||||
logger.debug('[autodoc] import %s', self.modname)
|
||||
obj = import_module(self.modname, self.env.config.autodoc_warningiserror)
|
||||
@ -427,8 +428,6 @@ class Documenter(object):
|
||||
self.directive.warn(errmsg)
|
||||
self.env.note_reread()
|
||||
return False
|
||||
finally:
|
||||
import_hook.disable()
|
||||
|
||||
def get_real_modname(self):
|
||||
# type: () -> str
|
||||
|
@ -10,15 +10,16 @@
|
||||
"""
|
||||
|
||||
import sys
|
||||
import traceback
|
||||
import warnings
|
||||
import traceback
|
||||
import contextlib
|
||||
from types import FunctionType, MethodType, ModuleType
|
||||
|
||||
from sphinx.util import logging
|
||||
|
||||
if False:
|
||||
# For type annotation
|
||||
from typing import Any, List, Set # NOQA
|
||||
from typing import Any, Generator, List, Set # NOQA
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -77,7 +78,6 @@ class _MockModule(ModuleType):
|
||||
|
||||
|
||||
class _MockImporter(object):
|
||||
|
||||
def __init__(self, names):
|
||||
# type: (List[str]) -> None
|
||||
self.base_packages = set() # type: Set[str]
|
||||
@ -120,6 +120,16 @@ class _MockImporter(object):
|
||||
return module
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def mock(names):
|
||||
# type: (List[str]) -> Generator
|
||||
try:
|
||||
importer = _MockImporter(names)
|
||||
yield
|
||||
finally:
|
||||
importer.disable()
|
||||
|
||||
|
||||
def import_module(modname, warningiserror=False):
|
||||
"""
|
||||
Call __import__(modname), convert exceptions to ImportError
|
||||
|
Loading…
Reference in New Issue
Block a user