4141from sphinx .util .tags import Tags
4242
4343if TYPE_CHECKING :
44+ from typing import Final
45+
4446 from docutils import nodes
4547 from docutils .nodes import Element , Node
4648 from docutils .parsers import Parser
@@ -134,7 +136,7 @@ class Sphinx:
134136 :ivar outdir: Directory for storing build documents.
135137 """
136138
137- warningiserror : bool
139+ warningiserror : Final = False
138140 _warncount : int
139141
140142 def __init__ (self , srcdir : str | os .PathLike [str ], confdir : str | os .PathLike [str ] | None ,
@@ -144,7 +146,7 @@ def __init__(self, srcdir: str | os.PathLike[str], confdir: str | os.PathLike[st
144146 freshenv : bool = False , warningiserror : bool = False ,
145147 tags : Sequence [str ] = (),
146148 verbosity : int = 0 , parallel : int = 0 , keep_going : bool = False ,
147- pdb : bool = False ) -> None :
149+ pdb : bool = False , exception_on_warning : bool = False ) -> None :
148150 """Initialize the Sphinx application.
149151
150152 :param srcdir: The path to the source directory.
@@ -163,8 +165,9 @@ def __init__(self, srcdir: str | os.PathLike[str], confdir: str | os.PathLike[st
163165 :param verbosity: The verbosity level.
164166 :param parallel: The maximum number of parallel jobs to use
165167 when reading/writing documents.
166- :param keep_going: If true, continue processing when an error occurs .
168+ :param keep_going: Unused .
167169 :param pdb: If true, enable the Python debugger on an exception.
170+ :param exception_on_warning: If true, raise an exception on warnings.
168171 """
169172 self .phase = BuildPhase .INITIALIZATION
170173 self .verbosity = verbosity
@@ -203,12 +206,10 @@ def __init__(self, srcdir: str | os.PathLike[str], confdir: str | os.PathLike[st
203206 else :
204207 self ._warning = warning
205208 self ._warncount = 0
206- self .keep_going = warningiserror and keep_going
207- if self .keep_going :
208- self .warningiserror = False
209- else :
210- self .warningiserror = warningiserror
209+ self .keep_going = bool (warningiserror ) # Unused
210+ self ._fail_on_warnings = bool (warningiserror )
211211 self .pdb = pdb
212+ self ._exception_on_warning = exception_on_warning
212213 logging .setup (self , self ._status , self ._warning )
213214
214215 self .events = EventManager (self )
@@ -386,26 +387,31 @@ def build(self, force_all: bool = False, filenames: list[str] | None = None) ->
386387 self .events .emit ('build-finished' , err )
387388 raise
388389
389- if self ._warncount and self .keep_going :
390- self .statuscode = 1
391-
392- status = (__ ('succeeded' ) if self .statuscode == 0
393- else __ ('finished with problems' ))
394- if self ._warncount :
395- if self .warningiserror :
396- if self ._warncount == 1 :
397- msg = __ ('build %s, %s warning (with warnings treated as errors).' )
398- else :
399- msg = __ ('build %s, %s warnings (with warnings treated as errors).' )
390+ if self ._warncount == 0 :
391+ if self .statuscode != 0 :
392+ logger .info (bold (__ ('build finished with problems.' )))
400393 else :
401- if self ._warncount == 1 :
402- msg = __ ('build %s, %s warning.' )
403- else :
404- msg = __ ('build %s, %s warnings.' )
405-
406- logger .info (bold (msg ), status , self ._warncount )
394+ logger .info (bold (__ ('build succeeded.' )))
395+ elif self ._warncount == 1 :
396+ if self ._fail_on_warnings :
397+ self .statuscode = 1
398+ msg = __ ('build finished with problems, 1 warning '
399+ '(with warnings treated as errors).' )
400+ elif self .statuscode != 0 :
401+ msg = __ ('build finished with problems, 1 warning.' )
402+ else :
403+ msg = __ ('build succeeded, 1 warning.' )
404+ logger .info (bold (msg ))
407405 else :
408- logger .info (bold (__ ('build %s.' )), status )
406+ if self ._fail_on_warnings :
407+ self .statuscode = 1
408+ msg = __ ('build finished with problems, %s warnings '
409+ '(with warnings treated as errors).' )
410+ elif self .statuscode != 0 :
411+ msg = __ ('build finished with problems, %s warnings.' )
412+ else :
413+ msg = __ ('build succeeded, %s warnings.' )
414+ logger .info (bold (msg ), self ._warncount )
409415
410416 if self .statuscode == 0 and self .builder .epilog :
411417 logger .info ('' )
0 commit comments