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 13, 2024
1 parent 6cc1177 commit eb537d9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ Bugs fixed
* #12425: Use Docutils' SVG processing in the HTML builder
and remove Sphinx's custom logic.
Patch by Tunç Başar Köse.
* #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 @@ -107,7 +107,10 @@ def fetch_inventory_group(
app: Sphinx,
now: int,
) -> bool:
cache_time = now - app.config.intersphinx_cache_limit * 86400
if app.config.intersphinx_cache_limit < 0:
cache_time = now - app.config.intersphinx_cache_limit * 86400
else:
cache_time = 0
failures = []
try:
for inv in invs:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_extensions/test_ext_intersphinx.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Test the intersphinx extension."""

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

import pytest
Expand All @@ -10,6 +12,7 @@
from sphinx.builders.html import INVENTORY_FILENAME
from sphinx.ext.intersphinx import (
fetch_inventory,
fetch_inventory_group,
inspect_main,
load_mappings,
missing_reference,
Expand All @@ -26,6 +29,9 @@
)
from tests.utils import http_server

if TYPE_CHECKING:
from sphinx.ext.intersphinx._shared import InventoryCacheEntry


def fake_node(domain, type, target, content, **attrs):
contnode = nodes.emphasis(content, content)
Expand Down Expand Up @@ -603,3 +609,21 @@ def test_intersphinx_role(app, warning):

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


def test_intersphinx_cache_limit(app):
url = 'https://example.org/'
app.config.intersphinx_mapping = {
'inv': (url, None),
}
# load the inventory and check if it's done correctly
normalize_intersphinx_mapping(app, app.config)
intersphinx_cache: dict[str, InventoryCacheEntry] = {
url: (None, 0, {}), # 0 is a timestamp, make sure the entry is expired
}
now = int(time.time())

app.config.intersphinx_cache_limit = -1
for name, (uri, invs) in app.config.intersphinx_mapping.values():
# no need to read from remote
assert not fetch_inventory_group(name, uri, invs, intersphinx_cache, app, now)

0 comments on commit eb537d9

Please sign in to comment.