Skip to content

Commit c9d0933

Browse files
linkcheck: Use context managers for HTTP requests (#11318)
This closes HTTP responses when no content reads are required, as when requests are made in streaming mode, ``requests`` doesn't know whether the caller may intend to later read content from a streamed HTTP response object and holds the socket open. Co-authored-by: Adam Turner <[email protected]>
1 parent 2b1c106 commit c9d0933

File tree

13 files changed

+38
-24
lines changed

13 files changed

+38
-24
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ env:
1313
FORCE_COLOR: "1"
1414
PYTHONDEVMODE: "1" # -X dev
1515
PYTHONWARNDEFAULTENCODING: "1" # -X warn_default_encoding
16-
PYTHONWARNINGS: "error,always:unclosed:ResourceWarning::" # default: all warnings as errors, except ResourceWarnings about unclosed items
16+
PYTHONWARNINGS: "error" # default: all warnings as errors
1717

1818
jobs:
1919
ubuntu:

sphinx/builders/linkcheck.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -316,32 +316,30 @@ def check_uri() -> tuple[str, str, int]:
316316
try:
317317
if anchor and self.config.linkcheck_anchors:
318318
# Read the whole document and see if #anchor exists
319-
response = requests.get(req_url, stream=True, config=self.config,
320-
auth=auth_info, **kwargs)
321-
response.raise_for_status()
322-
found = check_anchor(response, unquote(anchor))
319+
with requests.get(req_url, stream=True, config=self.config, auth=auth_info,
320+
**kwargs) as response:
321+
response.raise_for_status()
322+
found = check_anchor(response, unquote(anchor))
323323

324324
if not found:
325325
raise Exception(__("Anchor '%s' not found") % anchor)
326326
else:
327327
try:
328328
# try a HEAD request first, which should be easier on
329329
# the server and the network
330-
response = requests.head(req_url, allow_redirects=True,
331-
config=self.config, auth=auth_info,
332-
**kwargs)
333-
response.raise_for_status()
330+
with requests.head(req_url, allow_redirects=True, config=self.config,
331+
auth=auth_info, **kwargs) as response:
332+
response.raise_for_status()
334333
# Servers drop the connection on HEAD requests, causing
335334
# ConnectionError.
336335
except (ConnectionError, HTTPError, TooManyRedirects) as err:
337336
if isinstance(err, HTTPError) and err.response.status_code == 429:
338337
raise
339338
# retry with GET request if that fails, some servers
340339
# don't like HEAD requests.
341-
response = requests.get(req_url, stream=True,
342-
config=self.config,
343-
auth=auth_info, **kwargs)
344-
response.raise_for_status()
340+
with requests.get(req_url, stream=True, config=self.config,
341+
auth=auth_info, **kwargs) as response:
342+
response.raise_for_status()
345343
except HTTPError as err:
346344
if err.response.status_code == 401:
347345
# We'll take "Unauthorized" as working.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
exclude_patterns = ['_build']
22
linkcheck_anchors = True
3-
linkcheck_timeout = 0.075
3+
linkcheck_timeout = 0.05

tests/roots/test-linkcheck-documents_exclude/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
'^broken_link$',
44
'br[0-9]ken_link',
55
]
6-
linkcheck_timeout = 0.075
6+
linkcheck_timeout = 0.05
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
exclude_patterns = ['_build']
22
linkcheck_anchors = True
3-
linkcheck_timeout = 0.075
3+
linkcheck_timeout = 0.05
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
exclude_patterns = ['_build']
2-
linkcheck_timeout = 0.075
2+
linkcheck_timeout = 0.05
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
exclude_patterns = ['_build']
2-
linkcheck_timeout = 0.075
2+
linkcheck_timeout = 0.05
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
exclude_patterns = ['_build']
2-
linkcheck_timeout = 0.075
2+
linkcheck_timeout = 0.05
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
exclude_patterns = ['_build']
2-
linkcheck_timeout = 0.075
2+
linkcheck_timeout = 0.05
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
exclude_patterns = ['_build']
22
linkcheck_anchors = True
3-
linkcheck_timeout = 0.075
3+
linkcheck_timeout = 0.05

0 commit comments

Comments
 (0)