Skip to content

Commit aad73ef

Browse files
mattdc2Matthieu DE CIBEINSAA-Turner
authored
intersphinx: Handle string interpolation failures in _fetch_inventory_group() (#14101)
Co-authored-by: Matthieu DE CIBEINS <matthieu.decibeins@non.se.com> Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com>
1 parent e552f84 commit aad73ef

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ Features added
9898
Note that MathJax v3 is mostly compatible with MathJax v4, so existing
9999
:confval:`mathjax3_config` settings should not need to change.
100100
Patch by Matthias Geier.
101+
* #14029: intersphinx: Fix error in format string interpolation.
102+
Patch by Matthieu de Cibeins.
101103

102104
Bugs fixed
103105
----------

sphinx/ext/intersphinx/_load.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from sphinx.util.inventory import InventoryFile
2020

2121
if TYPE_CHECKING:
22+
from collections.abc import Sequence
2223
from pathlib import Path
2324

2425
from sphinx.application import Sphinx
@@ -226,6 +227,17 @@ def from_config(cls, config: Config) -> _InvConfig:
226227
)
227228

228229

230+
def _display_failures(failures: Sequence[tuple[str, ...]]) -> str:
231+
"""Format a list of failure tuples into a readable multi-line string."""
232+
formatted = []
233+
for failure_args in failures:
234+
try:
235+
formatted.append(failure_args[0] % failure_args[1:])
236+
except TypeError:
237+
formatted.append(' - '.join(failure_args))
238+
return '\n'.join(formatted)
239+
240+
229241
def _fetch_inventory_group(
230242
*,
231243
project: _IntersphinxProject,
@@ -315,11 +327,9 @@ def _fetch_inventory_group(
315327
for fail in failures:
316328
LOGGER.info(*fail)
317329
else:
318-
issues = '\n'.join(f[0] % f[1:] for f in failures)
319330
LOGGER.warning(
320-
'%s\n%s',
321-
__('failed to reach any of the inventories with the following issues:'),
322-
issues,
331+
__('failed to reach any of the inventories with the following issues:\n%s'),
332+
_display_failures(failures),
323333
)
324334
return updated
325335

tests/test_ext_intersphinx/test_ext_intersphinx.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from sphinx.ext.intersphinx import setup as intersphinx_setup
1919
from sphinx.ext.intersphinx._cli import inspect_main
2020
from sphinx.ext.intersphinx._load import (
21+
_display_failures,
2122
_fetch_inventory_data,
2223
_fetch_inventory_group,
2324
_get_safe_url,
@@ -923,3 +924,22 @@ def test_inventory_text_version(tmp_path, app):
923924
assert rn['refuri'] == 'https://docs.python.org/foo.html#module-module1'
924925
assert rn['reftitle'] == '(in foo stable)'
925926
assert rn[0].astext() == 'Long Module desc'
927+
928+
929+
def test_display_failures():
930+
failures_args = [
931+
('Failed to fetch %s from %s', 'inventory', 'http://example.com'),
932+
('Timeout after %d seconds',), # Only one argument
933+
(
934+
'intersphinx inventory has moved:',
935+
'http://example.com',
936+
'http://proxyhost.net',
937+
), # No '%'
938+
]
939+
issues = _display_failures(failures_args)
940+
assert 'Failed to fetch inventory from http://example.com' in issues
941+
assert 'Timeout after %d seconds' in issues
942+
assert (
943+
'intersphinx inventory has moved: - http://example.com - http://proxyhost.net'
944+
in issues
945+
)

0 commit comments

Comments
 (0)