diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d96481d6ba9..2efc1c1844c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -13,7 +13,7 @@ env: FORCE_COLOR: "1" PYTHONDEVMODE: "1" # -X dev PYTHONWARNDEFAULTENCODING: "1" # -X warn_default_encoding - PYTHONWARNINGS: "error,always:unclosed:ResourceWarning::" # default: all warnings as errors, except ResourceWarnings about unclosed items + PYTHONWARNINGS: "error" # default: all warnings as errors jobs: ubuntu: diff --git a/sphinx/builders/linkcheck.py b/sphinx/builders/linkcheck.py index e828fc20988..6c99f96e685 100644 --- a/sphinx/builders/linkcheck.py +++ b/sphinx/builders/linkcheck.py @@ -316,10 +316,10 @@ def check_uri() -> tuple[str, str, int]: try: if anchor and self.config.linkcheck_anchors: # Read the whole document and see if #anchor exists - response = requests.get(req_url, stream=True, config=self.config, - auth=auth_info, **kwargs) - response.raise_for_status() - found = check_anchor(response, unquote(anchor)) + with requests.get(req_url, stream=True, config=self.config, auth=auth_info, + **kwargs) as response: + response.raise_for_status() + found = check_anchor(response, unquote(anchor)) if not found: raise Exception(__("Anchor '%s' not found") % anchor) @@ -327,10 +327,9 @@ def check_uri() -> tuple[str, str, int]: try: # try a HEAD request first, which should be easier on # the server and the network - response = requests.head(req_url, allow_redirects=True, - config=self.config, auth=auth_info, - **kwargs) - response.raise_for_status() + with requests.head(req_url, allow_redirects=True, config=self.config, + auth=auth_info, **kwargs) as response: + response.raise_for_status() # Servers drop the connection on HEAD requests, causing # ConnectionError. except (ConnectionError, HTTPError, TooManyRedirects) as err: @@ -338,10 +337,9 @@ def check_uri() -> tuple[str, str, int]: raise # retry with GET request if that fails, some servers # don't like HEAD requests. - response = requests.get(req_url, stream=True, - config=self.config, - auth=auth_info, **kwargs) - response.raise_for_status() + with requests.get(req_url, stream=True, config=self.config, + auth=auth_info, **kwargs) as response: + response.raise_for_status() except HTTPError as err: if err.response.status_code == 401: # We'll take "Unauthorized" as working. diff --git a/tests/roots/test-linkcheck-anchors-ignore/conf.py b/tests/roots/test-linkcheck-anchors-ignore/conf.py index e3a1918fe9e..0005bfadafe 100644 --- a/tests/roots/test-linkcheck-anchors-ignore/conf.py +++ b/tests/roots/test-linkcheck-anchors-ignore/conf.py @@ -1,3 +1,3 @@ exclude_patterns = ['_build'] linkcheck_anchors = True -linkcheck_timeout = 0.075 +linkcheck_timeout = 0.05 diff --git a/tests/roots/test-linkcheck-documents_exclude/conf.py b/tests/roots/test-linkcheck-documents_exclude/conf.py index b6dfb26a428..52388f93af0 100644 --- a/tests/roots/test-linkcheck-documents_exclude/conf.py +++ b/tests/roots/test-linkcheck-documents_exclude/conf.py @@ -3,4 +3,4 @@ '^broken_link$', 'br[0-9]ken_link', ] -linkcheck_timeout = 0.075 +linkcheck_timeout = 0.05 diff --git a/tests/roots/test-linkcheck-localserver-anchor/conf.py b/tests/roots/test-linkcheck-localserver-anchor/conf.py index e3a1918fe9e..0005bfadafe 100644 --- a/tests/roots/test-linkcheck-localserver-anchor/conf.py +++ b/tests/roots/test-linkcheck-localserver-anchor/conf.py @@ -1,3 +1,3 @@ exclude_patterns = ['_build'] linkcheck_anchors = True -linkcheck_timeout = 0.075 +linkcheck_timeout = 0.05 diff --git a/tests/roots/test-linkcheck-localserver-https/conf.py b/tests/roots/test-linkcheck-localserver-https/conf.py index 25747bbc016..a2ce01e6547 100644 --- a/tests/roots/test-linkcheck-localserver-https/conf.py +++ b/tests/roots/test-linkcheck-localserver-https/conf.py @@ -1,2 +1,2 @@ exclude_patterns = ['_build'] -linkcheck_timeout = 0.075 +linkcheck_timeout = 0.05 diff --git a/tests/roots/test-linkcheck-localserver-warn-redirects/conf.py b/tests/roots/test-linkcheck-localserver-warn-redirects/conf.py index 25747bbc016..a2ce01e6547 100644 --- a/tests/roots/test-linkcheck-localserver-warn-redirects/conf.py +++ b/tests/roots/test-linkcheck-localserver-warn-redirects/conf.py @@ -1,2 +1,2 @@ exclude_patterns = ['_build'] -linkcheck_timeout = 0.075 +linkcheck_timeout = 0.05 diff --git a/tests/roots/test-linkcheck-localserver/conf.py b/tests/roots/test-linkcheck-localserver/conf.py index 25747bbc016..a2ce01e6547 100644 --- a/tests/roots/test-linkcheck-localserver/conf.py +++ b/tests/roots/test-linkcheck-localserver/conf.py @@ -1,2 +1,2 @@ exclude_patterns = ['_build'] -linkcheck_timeout = 0.075 +linkcheck_timeout = 0.05 diff --git a/tests/roots/test-linkcheck-raw-node/conf.py b/tests/roots/test-linkcheck-raw-node/conf.py index 25747bbc016..a2ce01e6547 100644 --- a/tests/roots/test-linkcheck-raw-node/conf.py +++ b/tests/roots/test-linkcheck-raw-node/conf.py @@ -1,2 +1,2 @@ exclude_patterns = ['_build'] -linkcheck_timeout = 0.075 +linkcheck_timeout = 0.05 diff --git a/tests/roots/test-linkcheck-too-many-retries/conf.py b/tests/roots/test-linkcheck-too-many-retries/conf.py index e3a1918fe9e..0005bfadafe 100644 --- a/tests/roots/test-linkcheck-too-many-retries/conf.py +++ b/tests/roots/test-linkcheck-too-many-retries/conf.py @@ -1,3 +1,3 @@ exclude_patterns = ['_build'] linkcheck_anchors = True -linkcheck_timeout = 0.075 +linkcheck_timeout = 0.05 diff --git a/tests/roots/test-linkcheck/conf.py b/tests/roots/test-linkcheck/conf.py index 0c197b6f03b..6ddb41a5d01 100644 --- a/tests/roots/test-linkcheck/conf.py +++ b/tests/roots/test-linkcheck/conf.py @@ -1,4 +1,4 @@ root_doc = 'links' exclude_patterns = ['_build'] linkcheck_anchors = True -linkcheck_timeout = 0.075 +linkcheck_timeout = 0.05 diff --git a/tests/roots/test-linkcheck/links.rst b/tests/roots/test-linkcheck/links.rst index 49afba2b39a..88c757e03d9 100644 --- a/tests/roots/test-linkcheck/links.rst +++ b/tests/roots/test-linkcheck/links.rst @@ -11,3 +11,4 @@ Some additional anchors to exercise ignore code .. image:: http://localhost:7777/image.png .. figure:: http://localhost:7777/image2.png +* `Valid anchored url `_ diff --git a/tests/test_build_linkcheck.py b/tests/test_build_linkcheck.py index acfebd64999..260cf2c4214 100644 --- a/tests/test_build_linkcheck.py +++ b/tests/test_build_linkcheck.py @@ -31,6 +31,9 @@ def do_HEAD(self): if self.path[1:].rstrip() == "": self.send_response(200, "OK") self.end_headers() + elif self.path[1:].rstrip() == "anchor.html": + self.send_response(200, "OK") + self.end_headers() else: self.send_response(404, "Not Found") self.end_headers() @@ -39,6 +42,9 @@ def do_GET(self): self.do_HEAD() if self.path[1:].rstrip() == "": self.wfile.write(b"ok\n\n") + elif self.path[1:].rstrip() == "anchor.html": + doc = '' + self.wfile.write(doc.encode('utf-8')) @pytest.mark.sphinx('linkcheck', testroot='linkcheck', freshenv=True) @@ -69,8 +75,8 @@ def test_defaults(app): for attr in ("filename", "lineno", "status", "code", "uri", "info"): assert attr in row - assert len(content.splitlines()) == 9 - assert len(rows) == 9 + assert len(content.splitlines()) == 10 + assert len(rows) == 10 # the output order of the rows is not stable # due to possible variance in network latency rowsby = {row["uri"]: row for row in rows} @@ -95,6 +101,15 @@ def test_defaults(app): assert rowsby["http://localhost:7777#does-not-exist"]["info"] == "Anchor 'does-not-exist' not found" # images should fail assert "Not Found for url: http://localhost:7777/image.png" in rowsby["http://localhost:7777/image.png"]["info"] + # anchor should be found + assert rowsby['http://localhost:7777/anchor.html#found'] == { + 'filename': 'links.rst', + 'lineno': 14, + 'status': 'working', + 'code': 0, + 'uri': 'http://localhost:7777/anchor.html#found', + 'info': '', + } @pytest.mark.sphinx('linkcheck', testroot='linkcheck-too-many-retries', freshenv=True)