Skip to content

Commit

Permalink
Suppress ValueError in apply_source_workaround (#11092)
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Jan 5, 2023
1 parent c80d656 commit 476c115
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Features added
Bugs fixed
----------

* #11091: Fix ``util.nodes.apply_source_workaround`` for ``literal_block`` nodes
with no source information in the node or the node's parents.

Testing
--------

Expand Down
4 changes: 3 additions & 1 deletion sphinx/util/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import contextlib
import re
import unicodedata
from typing import TYPE_CHECKING, Any, Callable, Iterable
Expand Down Expand Up @@ -152,7 +153,8 @@ def apply_source_workaround(node: Element) -> None:

# workaround: literal_block under bullet list (#4913)
if isinstance(node, nodes.literal_block) and node.source is None:
node.source = get_node_source(node)
with contextlib.suppress(ValueError):
node.source = get_node_source(node)

# workaround: recommonmark-0.2.0 doesn't set rawsource attribute
if not node.rawsource:
Expand Down
24 changes: 22 additions & 2 deletions tests/test_util_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from docutils.utils import new_document

from sphinx.transforms import ApplySourceWorkaround
from sphinx.util.nodes import (NodeMatcher, clean_astext, extract_messages, make_id,
split_explicit_title)
from sphinx.util.nodes import (NodeMatcher, apply_source_workaround, clean_astext,
extract_messages, make_id, split_explicit_title)


def _transform(doctree):
Expand Down Expand Up @@ -226,3 +226,23 @@ def test_make_id_sequential(app):
)
def test_split_explicit_target(title, expected):
assert expected == split_explicit_title(title)


def test_apply_source_workaround_literal_block_no_source():
"""Regression test for #11091.
Test that apply_source_workaround doesn't raise.
"""
literal_block = nodes.literal_block('', '')
list_item = nodes.list_item('', literal_block)
bullet_list = nodes.bullet_list('', list_item)

assert literal_block.source is None
assert list_item.source is None
assert bullet_list.source is None

apply_source_workaround(literal_block)

assert literal_block.source is None
assert list_item.source is None
assert bullet_list.source is None

0 comments on commit 476c115

Please sign in to comment.