25
25
26
26
from sphinx ._cli .util .colour import darkgray , darkgreen , purple , red , turquoise
27
27
from sphinx .builders .dummy import DummyBuilder
28
+ from sphinx .errors import ConfigError
28
29
from sphinx .locale import __
29
30
from sphinx .transforms .post_transforms import SphinxPostTransform
30
31
from sphinx .util import logging , requests
@@ -178,7 +179,7 @@ def process_result(self, result: CheckResult) -> None:
178
179
text = 'with unknown code'
179
180
linkstat ['text' ] = text
180
181
redirection = f'{ text } to { result .message } '
181
- if self .config .linkcheck_allowed_redirects :
182
+ if self .config .linkcheck_allowed_redirects is not None :
182
183
msg = f'redirect { res_uri } - { redirection } '
183
184
logger .warning (msg , location = (result .docname , result .lineno ))
184
185
else :
@@ -386,7 +387,7 @@ def __init__(
386
387
)
387
388
self .check_anchors : bool = config .linkcheck_anchors
388
389
self .allowed_redirects : dict [re .Pattern [str ], re .Pattern [str ]]
389
- self .allowed_redirects = config .linkcheck_allowed_redirects
390
+ self .allowed_redirects = config .linkcheck_allowed_redirects or {}
390
391
self .retries : int = config .linkcheck_retries
391
392
self .rate_limit_timeout = config .linkcheck_rate_limit_timeout
392
393
self ._allow_unauthorized = config .linkcheck_allow_unauthorized
@@ -748,20 +749,26 @@ def rewrite_github_anchor(app: Sphinx, uri: str) -> str | None:
748
749
749
750
750
751
def compile_linkcheck_allowed_redirects (app : Sphinx , config : Config ) -> None :
751
- """Compile patterns in linkcheck_allowed_redirects to the regexp objects."""
752
- linkcheck_allowed_redirects = app .config .linkcheck_allowed_redirects
753
- for url , pattern in list (linkcheck_allowed_redirects .items ()):
752
+ """Compile patterns to the regexp objects."""
753
+ if config .linkcheck_allowed_redirects is _sentinel_lar :
754
+ config .linkcheck_allowed_redirects = None
755
+ return
756
+ if not isinstance (config .linkcheck_allowed_redirects , dict ):
757
+ raise ConfigError
758
+ allowed_redirects = {}
759
+ for url , pattern in config .linkcheck_allowed_redirects .items ():
754
760
try :
755
- linkcheck_allowed_redirects [re .compile (url )] = re .compile (pattern )
761
+ allowed_redirects [re .compile (url )] = re .compile (pattern )
756
762
except re .error as exc :
757
763
logger .warning (
758
764
__ ('Failed to compile regex in linkcheck_allowed_redirects: %r %s' ),
759
765
exc .pattern ,
760
766
exc .msg ,
761
767
)
762
- finally :
763
- # Remove the original regexp-string
764
- linkcheck_allowed_redirects .pop (url )
768
+ config .linkcheck_allowed_redirects = allowed_redirects
769
+
770
+
771
+ _sentinel_lar = object ()
765
772
766
773
767
774
def setup (app : Sphinx ) -> ExtensionMetadata :
@@ -772,7 +779,9 @@ def setup(app: Sphinx) -> ExtensionMetadata:
772
779
app .add_config_value (
773
780
'linkcheck_exclude_documents' , [], '' , types = frozenset ({list , tuple })
774
781
)
775
- app .add_config_value ('linkcheck_allowed_redirects' , {}, '' , types = frozenset ({dict }))
782
+ app .add_config_value (
783
+ 'linkcheck_allowed_redirects' , _sentinel_lar , '' , types = frozenset ({dict })
784
+ )
776
785
app .add_config_value ('linkcheck_auth' , [], '' , types = frozenset ({list , tuple }))
777
786
app .add_config_value ('linkcheck_request_headers' , {}, '' , types = frozenset ({dict }))
778
787
app .add_config_value ('linkcheck_retries' , 1 , '' , types = frozenset ({int }))
@@ -799,7 +808,8 @@ def setup(app: Sphinx) -> ExtensionMetadata:
799
808
800
809
app .add_event ('linkcheck-process-uri' )
801
810
802
- app .connect ('config-inited' , compile_linkcheck_allowed_redirects , priority = 800 )
811
+ # priority 900 to happen after ``check_confval_types()``
812
+ app .connect ('config-inited' , compile_linkcheck_allowed_redirects , priority = 900 )
803
813
804
814
# FIXME: Disable URL rewrite handler for github.com temporarily.
805
815
# See: https://github.com/sphinx-doc/sphinx/issues/9435
0 commit comments