Skip to content

Commit 476c115

Browse files
authored
Suppress ValueError in apply_source_workaround (#11092)
1 parent c80d656 commit 476c115

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Features added
1616
Bugs fixed
1717
----------
1818

19+
* #11091: Fix ``util.nodes.apply_source_workaround`` for ``literal_block`` nodes
20+
with no source information in the node or the node's parents.
21+
1922
Testing
2023
--------
2124

sphinx/util/nodes.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
import contextlib
56
import re
67
import unicodedata
78
from typing import TYPE_CHECKING, Any, Callable, Iterable
@@ -152,7 +153,8 @@ def apply_source_workaround(node: Element) -> None:
152153

153154
# workaround: literal_block under bullet list (#4913)
154155
if isinstance(node, nodes.literal_block) and node.source is None:
155-
node.source = get_node_source(node)
156+
with contextlib.suppress(ValueError):
157+
node.source = get_node_source(node)
156158

157159
# workaround: recommonmark-0.2.0 doesn't set rawsource attribute
158160
if not node.rawsource:

tests/test_util_nodes.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
from docutils.utils import new_document
1212

1313
from sphinx.transforms import ApplySourceWorkaround
14-
from sphinx.util.nodes import (NodeMatcher, clean_astext, extract_messages, make_id,
15-
split_explicit_title)
14+
from sphinx.util.nodes import (NodeMatcher, apply_source_workaround, clean_astext,
15+
extract_messages, make_id, split_explicit_title)
1616

1717

1818
def _transform(doctree):
@@ -226,3 +226,23 @@ def test_make_id_sequential(app):
226226
)
227227
def test_split_explicit_target(title, expected):
228228
assert expected == split_explicit_title(title)
229+
230+
231+
def test_apply_source_workaround_literal_block_no_source():
232+
"""Regression test for #11091.
233+
234+
Test that apply_source_workaround doesn't raise.
235+
"""
236+
literal_block = nodes.literal_block('', '')
237+
list_item = nodes.list_item('', literal_block)
238+
bullet_list = nodes.bullet_list('', list_item)
239+
240+
assert literal_block.source is None
241+
assert list_item.source is None
242+
assert bullet_list.source is None
243+
244+
apply_source_workaround(literal_block)
245+
246+
assert literal_block.source is None
247+
assert list_item.source is None
248+
assert bullet_list.source is None

0 commit comments

Comments
 (0)