Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow --pdb option to also debug warnings #12727

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Deprecated
Features added
--------------

* #12727: build: Allow :option:`sphinx-build --pdb` to debug warnings when
:option:`sphinx-build --fail-on-warning` is specified.
Patch by Jeremy Maitin-Shepard.

Bugs fixed
----------

Expand Down
9 changes: 7 additions & 2 deletions sphinx/builders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import codecs
import contextlib
import pickle
import re
import time
Expand Down Expand Up @@ -313,7 +314,9 @@ def build(
logger.info(bold(__('building [%s]: ')) + summary, self.name)

# while reading, collect all warnings from docutils
with logging.pending_warnings():
with contextlib.ExitStack() as exit_stack:
if not self.app.pdb or not self.app.warningiserror:
exit_stack.enter_context(logging.pending_warnings())
updated_docnames = set(self.read())

doccount = len(updated_docnames)
Expand Down Expand Up @@ -613,7 +616,9 @@ def write(
self._write_serial(sorted(docnames))

def _write_serial(self, docnames: Sequence[str]) -> None:
with logging.pending_warnings():
with contextlib.ExitStack() as exit_stack:
if not self.app.pdb or not self.app.warningiserror:
exit_stack.enter_context(logging.pending_warnings())
for docname in status_iterator(docnames, __('writing output... '), "darkgreen",
len(docnames), self.app.verbosity):
self.app.phase = BuildPhase.RESOLVING
Expand Down
15 changes: 15 additions & 0 deletions tests/test_builders/test_build_warnings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import re
import sys
import traceback

import pytest

Expand Down Expand Up @@ -58,6 +59,20 @@ def test_html_warnings(app):
_check_warnings(warnings_exp, app.warning.getvalue())


@pytest.mark.parametrize('pdb', [True, False])
@pytest.mark.sphinx('html', testroot='warnings', freshenv=True)
def test_html_warnings_pdb(app, pdb):
app.pdb = pdb
app.warningiserror = True
try:
app.build(force_all=True)
pytest.fail("Expected an exception to be raised")
except Exception:
tb = traceback.format_exc()
assert ("unindent_warning" in tb) == pdb
assert ("pending_warnings" not in tb) == pdb


@pytest.mark.sphinx('latex', testroot='warnings', freshenv=True)
def test_latex_warnings(app):
app.build(force_all=True)
Expand Down
Loading