Merge pull request #5878 from tk0miya/refactor_latex_compat

refactor: Add compat module to avoid recursive import
This commit is contained in:
Takeshi KOMIYA 2019-01-03 11:20:55 +09:00 committed by GitHub
commit 30e51bf3ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 8 deletions

View File

@ -13,6 +13,7 @@ from os import path
from docutils.frontend import OptionParser
import sphinx.builders.latex.nodes # NOQA # Workaround: import this before writer to avoid ImportError
from sphinx import package_dir, addnodes, highlighting
from sphinx.builders import Builder
from sphinx.builders.latex.transforms import (

View File

@ -0,0 +1,22 @@
"""
sphinx.builders.latex.compat
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Compatibility module for LaTeX writer.
This module will be removed after deprecation period.
Don't use components in this modules directly.
:copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from sphinx.builders.latex.transforms import URI_SCHEMES, ShowUrlsTransform
from sphinx.deprecation import RemovedInSphinx30Warning, deprecated_alias
deprecated_alias('sphinx.writers.latex',
{
'ShowUrlsTransform': ShowUrlsTransform,
'URI_SCHEMES': URI_SCHEMES,
},
RemovedInSphinx30Warning)

View File

@ -8,7 +8,9 @@
:license: BSD, see LICENSE for details.
"""
import sys
import warnings
from importlib import import_module
if False:
# For type annotation
@ -26,6 +28,34 @@ class RemovedInSphinx40Warning(PendingDeprecationWarning):
RemovedInNextVersionWarning = RemovedInSphinx30Warning
def deprecated_alias(modname, objects, warning):
# type: (str, Dict, Type[Warning]) -> None
module = sys.modules.get(modname)
if module is None:
module = import_module(modname)
sys.modules[modname] = _ModuleWrapper(module, modname, objects, warning) # type: ignore
class _ModuleWrapper(object):
def __init__(self, module, modname, objects, warning):
# type: (Any, str, Dict, Type[Warning]) -> None
self._module = module
self._modname = modname
self._objects = objects
self._warning = warning
def __getattr__(self, name):
# type: (str) -> Any
if name in self._objects:
warnings.warn("%s.%s is now deprecated. Please refer CHANGES to grasp"
"the changes of Sphinx API." % (self._modname, name),
self._warning, stacklevel=3)
return self._objects[name]
return getattr(self._module, name)
class DeprecatedDict(dict):
"""A deprecated dict which warns on each access."""

View File

@ -23,6 +23,9 @@ from docutils.writers.latex2e import Babel
from sphinx import addnodes
from sphinx import highlighting
from sphinx.builders.latex.nodes import (
HYPERLINK_SUPPORT_NODES, captioned_literal_block, footnotetext
)
from sphinx.deprecation import RemovedInSphinx30Warning, RemovedInSphinx40Warning
from sphinx.domains.std import StandardDomain
from sphinx.errors import SphinxError
@ -2655,11 +2658,4 @@ class LaTeXTranslator(SphinxTranslator):
return visit_admonition
# Import old modules here for compatibility
# They should be imported after `LaTeXTranslator` to avoid recursive import.
#
# refs: https://github.com/sphinx-doc/sphinx/issues/4889
from sphinx.builders.latex.transforms import URI_SCHEMES, ShowUrlsTransform # NOQA
# FIXME: Workaround to avoid circular import
# refs: https://github.com/sphinx-doc/sphinx/issues/5433
from sphinx.builders.latex.nodes import HYPERLINK_SUPPORT_NODES, captioned_literal_block, footnotetext # NOQA
import sphinx.builders.latex.compat # NOQA