summaryrefslogtreecommitdiff
path: root/sphinx
diff options
context:
space:
mode:
authorJames Addison <55152140+jayaddison@users.noreply.github.com>2023-05-09 17:09:35 +0100
committerGitHub <noreply@github.com>2023-05-09 17:09:35 +0100
commitc9d0933e5d8e34aa9d2c1d88c5a80b46b575730e (patch)
treebbe414ee449b7288fd6a7df36553c01f8fe689dd /sphinx
parent2b1c106bbff5265e8a6076318db5d083c329d575 (diff)
downloadsphinx-git-c9d0933e5d8e34aa9d2c1d88c5a80b46b575730e.tar.gz
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 <9087854+AA-Turner@users.noreply.github.com>
Diffstat (limited to 'sphinx')
-rw-r--r--sphinx/builders/linkcheck.py22
1 files changed, 10 insertions, 12 deletions
diff --git a/sphinx/builders/linkcheck.py b/sphinx/builders/linkcheck.py
index e828fc209..6c99f96e6 100644
--- a/sphinx/builders/linkcheck.py
+++ b/sphinx/builders/linkcheck.py
@@ -316,10 +316,10 @@ class HyperlinkAvailabilityCheckWorker(Thread):
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 @@ class HyperlinkAvailabilityCheckWorker(Thread):
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 @@ class HyperlinkAvailabilityCheckWorker(Thread):
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.