Skip to content

Sphinx<8.1; Can't instantiate sphinx.application.Sphinx twice in the same process with warningiserror #13607

Closed as not planned
@Alxe

Description

@Alxe

Describe the bug

When using warningiserror and calling Sphinx programatically, the second call to Sphinx.__init__ will trigger a warning that gets turned into an error.

from sphinx.application import Sphinx

# Calls `sphinx.utils.docutils.register_node` for, among others, `toctree`.
s1 = Sphinx(".", None, "out", "toctree", "html", warningiserror=True)

# Because `sphinx.utils.docutils.register_node` uses `docutils.nodes.GenericNodeVisitor` as state machine, any new invocation fails.
s2 = Sphinx(".", None, "out", "toctree", "html", warningiserror=True)

This issue doesn't reproduce in 8.1, after #12743, but the logging messages are still emitted.
The latter can be problematic if you're using warning-is-error by yourself, e.g. by installing a io.StringIO as s2.warning and checking there.

How to Reproduce

from sphinx.application import Sphinx

# Calls `sphinx.utils.docutils.register_node` for, among others, `toctree`.
s1 = Sphinx(".", None, "out", "toctree", "html", warningiserror=True)

# Because `sphinx.utils.docutils.register_node` uses `docutils.nodes.GenericNodeVisitor` as state machine, any new invocation fails.
s2 = Sphinx(".", None, "out", "toctree", "html", warningiserror=True)

Environment Information

Platform:              darwin; (macOS-15.5-arm64-arm-64bit-Mach-O)
Python version:        3.13.2 (main, Feb  4 2025, 14:51:09) [Clang 16.0.0 (clang-1600.0.26.6)])
Python implementation: CPython
Sphinx version:        7.4.7
Docutils version:      0.21.2
Jinja2 version:        3.1.6
Pygments version:      2.19.1

Sphinx extensions

Additional context

I am wrapping Sphinx into an internal application that bundles rST generation and using custom Sphinx Directives.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions