From 11e1a858b9c446de53fce0f5018bcf5ceaf7ee5f Mon Sep 17 00:00:00 2001 From: Jeremy Maitin-Shepard Date: Fri, 2 Aug 2024 10:36:45 -0700 Subject: [PATCH] Allow --pdb option to also debug warnings Previously, warnings emitted during reading and writing were deferred, which made --pdb ineffective for debugging them. With this change, warnings are no longer deferred when --pdb and --fail-on-warning are both specified. --- sphinx/builders/__init__.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/sphinx/builders/__init__.py b/sphinx/builders/__init__.py index 151df6aeb18..4e16ceae513 100644 --- a/sphinx/builders/__init__.py +++ b/sphinx/builders/__init__.py @@ -3,6 +3,7 @@ from __future__ import annotations import codecs +import contextlib import pickle import re import time @@ -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) @@ -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