@@ -196,7 +196,7 @@ def visit_literal_block(self, node: nodes.literal_block):
196196 """Visit a generic literal block."""
197197 return self .parse_source (node , node .get ("language" , self .highlight_lang ))
198198
199- def parse_source ( # noqa: C901
199+ def parse_source ( # noqa: C901,PLR0912
200200 self , node : nodes .literal_block | nodes .doctest_block , language : str | None
201201 ) -> None :
202202 """Analyse Python code blocks."""
@@ -258,6 +258,12 @@ def parse_source( # noqa: C901
258258 msg = self ._parsing_error_msg (e , language , show_source )
259259 logger .warning (msg , type = warn_type , subtype = "parse_block" , location = node )
260260 return
261+ except Exception as e :
262+ show_source = self ._format_source_for_error (
263+ self .global_preface , self .concat_sources , prefaces , transform .source
264+ )
265+ msg = self ._parsing_error_msg (e , language , show_source )
266+ raise type (e )(msg ) from e
261267
262268 if prefaces or self .concat_sources or self .global_preface :
263269 concat_lens = [s .count ("\n " ) + 1 for s in self .concat_sources ]
@@ -302,6 +308,7 @@ def _parsing_error_msg(self, error: Exception, language: str, source: str) -> st
302308 str (error ) + f" in document { self .current_document !r} " ,
303309 f"Parsed source in `{ language } ` block:" ,
304310 source ,
311+ "" ,
305312 ]
306313 )
307314
0 commit comments