Skip to content

Commit 5afe46d

Browse files
committed
linkcheck: prevent linkcheck_allowed_redirects value of None
1 parent c9d6d6f commit 5afe46d

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

sphinx/builders/linkcheck.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def process_result(self, result: CheckResult) -> None:
179179
text = 'with unknown code'
180180
linkstat['text'] = text
181181
redirection = f'{text} to {result.message}'
182-
if self.config.linkcheck_allowed_redirects is not None:
182+
if self.config.linkcheck_allowed_redirects is not _sentinel_lar:
183183
msg = f'redirect {res_uri} - {redirection}'
184184
logger.warning(msg, location=(result.docname, result.lineno))
185185
else:
@@ -387,7 +387,7 @@ def __init__(
387387
)
388388
self.check_anchors: bool = config.linkcheck_anchors
389389
self.allowed_redirects: dict[re.Pattern[str], re.Pattern[str]]
390-
self.allowed_redirects = config.linkcheck_allowed_redirects or {}
390+
self.allowed_redirects = config.linkcheck_allowed_redirects
391391
self.retries: int = config.linkcheck_retries
392392
self.rate_limit_timeout = config.linkcheck_rate_limit_timeout
393393
self._allow_unauthorized = config.linkcheck_allow_unauthorized
@@ -722,6 +722,8 @@ def handle_starttag(self, tag: Any, attrs: Any) -> None:
722722
def _allowed_redirect(
723723
url: str, new_url: str, allowed_redirects: dict[re.Pattern[str], re.Pattern[str]]
724724
) -> bool:
725+
if allowed_redirects is _sentinel_lar:
726+
return True
725727
return any(
726728
from_url.match(url) and to_url.match(new_url)
727729
for from_url, to_url in allowed_redirects.items()
@@ -751,7 +753,6 @@ def rewrite_github_anchor(app: Sphinx, uri: str) -> str | None:
751753
def compile_linkcheck_allowed_redirects(app: Sphinx, config: Config) -> None:
752754
"""Compile patterns to the regexp objects."""
753755
if config.linkcheck_allowed_redirects is _sentinel_lar:
754-
config.linkcheck_allowed_redirects = None
755756
return
756757
if not isinstance(config.linkcheck_allowed_redirects, dict):
757758
raise ConfigError

tests/test_builders/test_build_linkcheck.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,7 @@ def log_date_time_string(self):
712712
'linkcheck',
713713
testroot='linkcheck-localserver',
714714
freshenv=True,
715+
confoverrides={'linkcheck_allowed_redirects': {}}, # do not follow any redirects
715716
)
716717
def test_follows_redirects_on_HEAD(app, capsys):
717718
with serve_application(app, make_redirect_handler(support_head=True)) as address:
@@ -729,13 +730,17 @@ def test_follows_redirects_on_HEAD(app, capsys):
729730
127.0.0.1 - - [] "HEAD /?redirected=1 HTTP/1.1" 204 -
730731
""",
731732
)
732-
assert app.warning.getvalue() == ''
733+
assert (
734+
f'WARNING: redirect http://{address}/'
735+
f' - with Found to http://{address}/?redirected=1'
736+
) in strip_escape_sequences(app.warning.getvalue())
733737

734738

735739
@pytest.mark.sphinx(
736740
'linkcheck',
737741
testroot='linkcheck-localserver',
738742
freshenv=True,
743+
confoverrides={'linkcheck_allowed_redirects': {}}, # do not follow any redirects
739744
)
740745
def test_follows_redirects_on_GET(app, capsys):
741746
with serve_application(app, make_redirect_handler(support_head=False)) as address:
@@ -754,7 +759,10 @@ def test_follows_redirects_on_GET(app, capsys):
754759
127.0.0.1 - - [] "GET /?redirected=1 HTTP/1.1" 204 -
755760
""",
756761
)
757-
assert app.warning.getvalue() == ''
762+
assert (
763+
f'WARNING: redirect http://{address}/'
764+
f' - with Found to http://{address}/?redirected=1'
765+
) in strip_escape_sequences(app.warning.getvalue())
758766

759767

760768
def test_linkcheck_allowed_redirects_config(

0 commit comments

Comments
 (0)