Skip to content

Commit

Permalink
intersphinx: Handle the case where intersphinx_cache_limit is negative
Browse files Browse the repository at this point in the history
The documentation said:

  Set this (intersphinx_cache_limit) to a negative value to cache inventories
  for unlimited time.

In the current implementation, a negative intersphinx_cache_limit causes
inventories always expire, this patch ensures that it behaves as documented.
  • Loading branch information
SilverRainZ committed Jul 26, 2024
1 parent ace5744 commit 4f44c8e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ Features added
Bugs fixed
----------

* #12514: intersphinx: fix the meaning of a negative value for
:confval:`intersphinx_cache_limit`.
Patch by Shengyu Zhang.

Testing
-------

Expand Down
5 changes: 4 additions & 1 deletion sphinx/ext/intersphinx/_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ def _fetch_inventory_group(
config: Config,
srcdir: Path,
) -> bool:
cache_time = now - config.intersphinx_cache_limit * 86400
if config.intersphinx_cache_limit < 0:
cache_time = now - config.intersphinx_cache_limit * 86400
else:
cache_time = 0

updated = False
failures = []
Expand Down
33 changes: 31 additions & 2 deletions tests/test_extensions/test_ext_intersphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import http.server
import time
from typing import TYPE_CHECKING
from unittest import mock

Expand All @@ -19,7 +20,8 @@
validate_intersphinx_mapping,
)
from sphinx.ext.intersphinx import setup as intersphinx_setup
from sphinx.ext.intersphinx._load import _fetch_inventory, _get_safe_url, _strip_basic_auth
from sphinx.ext.intersphinx._load import _fetch_inventory, _get_safe_url, _strip_basic_auth, _fetch_inventory_group
from sphinx.ext.intersphinx._shared import _IntersphinxProject
from sphinx.util.console import strip_colors

from tests.test_util.intersphinx_data import (
Expand All @@ -37,7 +39,6 @@ class FakeList(list):
def __iter__(self) -> NoReturn:
raise NotImplementedError


def fake_node(domain, type, target, content, **attrs):

Check failure on line 42 in tests/test_extensions/test_ext_intersphinx.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E302)

tests/test_extensions/test_ext_intersphinx.py:42:1: E302 Expected 2 blank lines, found 1
contnode = nodes.emphasis(content, content)
node = addnodes.pending_xref('')
Expand Down Expand Up @@ -665,3 +666,31 @@ def test_intersphinx_role(app):

# explicit title
assert html.format('index.html#foons') in content

if TYPE_CHECKING:

Check failure on line 670 in tests/test_extensions/test_ext_intersphinx.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E305)

tests/test_extensions/test_ext_intersphinx.py:670:1: E305 Expected 2 blank lines after class or function definition, found (1)
from sphinx.ext.intersphinx._shared import InventoryCacheEntry

def test_intersphinx_cache_limit(app):

Check failure on line 673 in tests/test_extensions/test_ext_intersphinx.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E302)

tests/test_extensions/test_ext_intersphinx.py:673:1: E302 Expected 2 blank lines, found 1
url = 'https://example.org/'
app.config.intersphinx_cache_limit = -1
app.config.intersphinx_mapping = {
'inv': (url, None),
}
# load the inventory and check if it's done correctly
intersphinx_cache: dict[str, InventoryCacheEntry] = {
url: ('', 0, {}), # 0 is a timestamp, make sure the entry is expired
}
validate_intersphinx_mapping(app, app.config)
load_mappings(app)

now = int(time.time())
for name, (uri, locations) in app.config.intersphinx_mapping.values():
project = _IntersphinxProject(name=name, target_uri=uri, locations=locations)
# no need to read from remote
assert not _fetch_inventory_group(
project=project,
cache=intersphinx_cache,
now=now,
config=app.config,
srcdir=app.srcdir,
)

0 comments on commit 4f44c8e

Please sign in to comment.