From de49b991f635faa0cf2cd07fae29bab3073b0662 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Mon, 17 Dec 2018 22:06:46 +0900 Subject: [PATCH 1/3] refactor: Use simple Input class --- CHANGES | 1 + doc/extdev/index.rst | 5 +++++ sphinx/application.py | 1 - sphinx/io.py | 51 ++++++++++++++++++++++++------------------- sphinx/registry.py | 4 +++- 5 files changed, 37 insertions(+), 25 deletions(-) diff --git a/CHANGES b/CHANGES index cd640cb6b..4fff6e139 100644 --- a/CHANGES +++ b/CHANGES @@ -64,6 +64,7 @@ Deprecated * ``sphinx.ext.autosummary.Autosummary.result`` * ``sphinx.ext.doctest.doctest_encode()`` * ``sphinx.io.SphinxRSTFileInput`` +* ``sphinx.registry.SphinxComponentRegistry.add_source_input()`` * ``sphinx.testing.util.remove_unicode_literal()`` * ``sphinx.util.attrdict`` * ``sphinx.util.force_decode()`` diff --git a/doc/extdev/index.rst b/doc/extdev/index.rst index 67ab1afa6..8c89d2cee 100644 --- a/doc/extdev/index.rst +++ b/doc/extdev/index.rst @@ -273,6 +273,11 @@ The following is a list of deprecated interfaces. - 3.0 - N/A + * - ``sphinx.registry.SphinxComponentRegistry.add_source_input()`` + - 2.0 + - 3.0 + - N/A + * - ``sphinx.writers.latex.LaTeXTranslator._make_visit_admonition()`` - 2.0 - 3.0 diff --git a/sphinx/application.py b/sphinx/application.py index 37f9e600d..0e3bcac3e 100644 --- a/sphinx/application.py +++ b/sphinx/application.py @@ -92,7 +92,6 @@ builtin_extensions = ( 'sphinx.directives.other', 'sphinx.directives.patches', 'sphinx.extension', - 'sphinx.io', 'sphinx.parsers', 'sphinx.registry', 'sphinx.roles', diff --git a/sphinx/io.py b/sphinx/io.py index 68ebe2b3c..2f942dcca 100644 --- a/sphinx/io.py +++ b/sphinx/io.py @@ -197,10 +197,15 @@ class SphinxBaseFileInput(FileInput): return UnicodeDecodeErrorHandler(self.env.docname)(error) -class SphinxFileInput(SphinxBaseFileInput): +class SphinxFileInput(FileInput): """A basic FileInput for Sphinx.""" supported = ('*',) # special source input + def __init__(self, *args, **kwargs): + # type: (Any, Any) -> None + kwargs['error_handler'] = 'sphinx' + super(SphinxFileInput, self).__init__(*args, **kwargs) + class SphinxRSTFileInput(SphinxBaseFileInput): """A reST FileInput for Sphinx. @@ -287,11 +292,8 @@ def read_doc(app, env, filename): error_handler = UnicodeDecodeErrorHandler(env.docname) codecs.register_error('sphinx', error_handler) # type: ignore - filetype = get_filetype(app.config.source_suffix, filename) - input_class = app.registry.get_source_input(filetype) reader = SphinxStandaloneReader(app) - source = input_class(app, env, source=None, source_path=filename, # type: ignore - encoding=env.config.source_encoding) + filetype = get_filetype(app.config.source_suffix, filename) parser = app.registry.create_source_parser(app, filetype) if parser.__class__.__name__ == 'CommonMarkParser' and parser.settings_spec == (): # a workaround for recommonmark @@ -301,23 +303,26 @@ def read_doc(app, env, filename): # CommonMarkParser. parser.settings_spec = RSTParser.settings_spec - pub = Publisher(reader=reader, # type: ignore - parser=parser, - writer=SphinxDummyWriter(), - source_class=SphinxDummySourceClass, - destination=NullOutput()) - pub.process_programmatic_settings(None, env.settings, None) - pub.set_source(source, filename) + input_class = app.registry.get_source_input(filetype) + if input_class: + # Sphinx-1.8 style + source = input_class(app, env, source=None, source_path=filename, # type: ignore + encoding=env.config.source_encoding) + pub = Publisher(reader=reader, # type: ignore + parser=parser, + writer=SphinxDummyWriter(), + source_class=SphinxDummySourceClass, + destination=NullOutput()) + pub.process_programmatic_settings(None, env.settings, None) + pub.set_source(source, filename) + else: + # Sphinx-2.0 style + pub = Publisher(reader=reader, + parser=parser, + writer=SphinxDummyWriter(), + source_class=SphinxFileInput) + pub.process_programmatic_settings(None, env.settings, None) + pub.set_source(source_path=filename) + pub.publish() return pub.document - - -def setup(app): - # type: (Sphinx) -> Dict[str, Any] - app.registry.add_source_input(SphinxFileInput) - - return { - 'version': 'builtin', - 'parallel_read_safe': True, - 'parallel_write_safe': True, - } diff --git a/sphinx/registry.py b/sphinx/registry.py index 0090d39a9..311270ccb 100644 --- a/sphinx/registry.py +++ b/sphinx/registry.py @@ -337,6 +337,8 @@ class SphinxComponentRegistry: def add_source_input(self, input_class, override=False): # type: (Type[SphinxFileInput], bool) -> None + warnings.warn('registry.source_input() is deprecated.', + RemovedInSphinx30Warning, stacklevel=2) for filetype in input_class.supported: if filetype in self.source_inputs and not override: raise ExtensionError(__('source_input for %r is already registered') % @@ -352,7 +354,7 @@ class SphinxComponentRegistry: # use special source_input for unknown filetype return self.source_inputs['*'] except KeyError: - raise SphinxError(__('source_input for %s not registered') % filetype) + return None def add_translator(self, name, translator, override=False): # type: (str, Type[nodes.NodeVisitor], bool) -> None From 636ca67528212772db62771c41d1d76fb3af820e Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Mon, 17 Dec 2018 22:06:51 +0900 Subject: [PATCH 2/3] Deprecate SphinxBaseFileInput --- CHANGES | 1 + doc/extdev/index.rst | 5 +++++ sphinx/io.py | 10 +++------- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/CHANGES b/CHANGES index 4fff6e139..d177e4973 100644 --- a/CHANGES +++ b/CHANGES @@ -63,6 +63,7 @@ Deprecated * ``sphinx.ext.autosummary.Autosummary.warnings`` * ``sphinx.ext.autosummary.Autosummary.result`` * ``sphinx.ext.doctest.doctest_encode()`` +* ``sphinx.io.SphinxBaseFileInput`` * ``sphinx.io.SphinxRSTFileInput`` * ``sphinx.registry.SphinxComponentRegistry.add_source_input()`` * ``sphinx.testing.util.remove_unicode_literal()`` diff --git a/doc/extdev/index.rst b/doc/extdev/index.rst index 8c89d2cee..25703a615 100644 --- a/doc/extdev/index.rst +++ b/doc/extdev/index.rst @@ -268,6 +268,11 @@ The following is a list of deprecated interfaces. - 3.0 - N/A + * - ``sphinx.io.SphinxBaseFileInput`` + - 2.0 + - 3.0 + - N/A + * - ``sphinx.io.SphinxRSTFileInput`` - 2.0 - 3.0 diff --git a/sphinx/io.py b/sphinx/io.py index 2f942dcca..fb3664c89 100644 --- a/sphinx/io.py +++ b/sphinx/io.py @@ -185,15 +185,14 @@ class SphinxBaseFileInput(FileInput): self.app = app self.env = env + warnings.warn('%s is deprecated.' % self.__class__.__name__, + RemovedInSphinx30Warning, stacklevel=2) + kwds['error_handler'] = 'sphinx' # py3: handle error on open. super().__init__(*args, **kwds) def warn_and_replace(self, error): # type: (Any) -> Tuple - warnings.warn('SphinxBaseFileInput.warn_and_replace() is deprecated. ' - 'Use UnicodeDecodeErrorHandler instead.', - RemovedInSphinx30Warning, stacklevel=2) - return UnicodeDecodeErrorHandler(self.env.docname)(error) @@ -246,9 +245,6 @@ class SphinxRSTFileInput(SphinxBaseFileInput): def read(self): # type: ignore # type: () -> StringList - warnings.warn('SphinxRSTFileInput is deprecated.', - RemovedInSphinx30Warning, stacklevel=2) - inputstring = super().read() lines = string2lines(inputstring, convert_whitespace=True) content = StringList() From f3350b8b7d709aada5803865c444b3ba4ccc1bd5 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Mon, 17 Dec 2018 22:06:53 +0900 Subject: [PATCH 3/3] Deprecate SphinxFileInput.supported --- CHANGES | 1 + doc/extdev/index.rst | 5 +++++ sphinx/io.py | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index d177e4973..33f69cc6a 100644 --- a/CHANGES +++ b/CHANGES @@ -64,6 +64,7 @@ Deprecated * ``sphinx.ext.autosummary.Autosummary.result`` * ``sphinx.ext.doctest.doctest_encode()`` * ``sphinx.io.SphinxBaseFileInput`` +* ``sphinx.io.SphinxFileInput.supported`` * ``sphinx.io.SphinxRSTFileInput`` * ``sphinx.registry.SphinxComponentRegistry.add_source_input()`` * ``sphinx.testing.util.remove_unicode_literal()`` diff --git a/doc/extdev/index.rst b/doc/extdev/index.rst index 25703a615..2d6c6559d 100644 --- a/doc/extdev/index.rst +++ b/doc/extdev/index.rst @@ -273,6 +273,11 @@ The following is a list of deprecated interfaces. - 3.0 - N/A + * - ``sphinx.io.SphinxFileInput.supported`` + - 2.0 + - 3.0 + - N/A + * - ``sphinx.io.SphinxRSTFileInput`` - 2.0 - 3.0 diff --git a/sphinx/io.py b/sphinx/io.py index fb3664c89..ee4891a2b 100644 --- a/sphinx/io.py +++ b/sphinx/io.py @@ -198,7 +198,7 @@ class SphinxBaseFileInput(FileInput): class SphinxFileInput(FileInput): """A basic FileInput for Sphinx.""" - supported = ('*',) # special source input + supported = ('*',) # RemovedInSphinx30Warning def __init__(self, *args, **kwargs): # type: (Any, Any) -> None