public
Created

  • Download Gist
linkcheck_retries.diff
Diff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
diff --git a/doc/config.rst b/doc/config.rst
--- a/doc/config.rst
+++ b/doc/config.rst
@@ -1336,6 +1336,13 @@
.. versionadded:: 1.1
+.. confval:: linkcheck_retries
+
+ The number of checking attempts before a link will be considered broken.
+ Default is 1 attempt.
+
+ .. versionadded:: 1.2
+
.. confval:: linkcheck_workers
The number of worker threads to use when checking links. Default is 5
diff --git a/sphinx/builders/linkcheck.py b/sphinx/builders/linkcheck.py
--- a/sphinx/builders/linkcheck.py
+++ b/sphinx/builders/linkcheck.py
@@ -124,22 +124,30 @@
hash = None
# need to actually check the URI
- try:
- if hash and self.app.config.linkcheck_anchors:
- # Read the whole document and see if #hash exists
- f = opener.open(Request(req_url), **kwargs)
- found = check_anchor(f, unquote(hash))
- f.close()
+ attempt_nr = 1
+ while True:
+ try:
+ if hash and self.app.config.linkcheck_anchors:
+ # Read the whole document and see if #hash exists
+ f = opener.open(Request(req_url), **kwargs)
+ found = check_anchor(f, unquote(hash))
+ f.close()
- if not found:
- raise Exception("Anchor '%s' not found" % hash)
- else:
- f = opener.open(HeadRequest(req_url), **kwargs)
- f.close()
+ if not found:
+ raise Exception("Anchor '%s' not found" % hash)
+ else:
+ f = opener.open(HeadRequest(req_url), **kwargs)
+ f.close()
+ # Link is not broken
+ break
+ except Exception, err:
+ if attempt_nr < self.app.config.linkcheck_retries:
+ attempt_nr += 1
+ pass
+ else:
+ self.broken[uri] = str(err)
+ return 'broken', str(err)
- except Exception, err:
- self.broken[uri] = str(err)
- return 'broken', str(err)
if f.url.rstrip('/') == req_url.rstrip('/'):
self.good.add(uri)
return 'working', 'new'
diff --git a/sphinx/config.py b/sphinx/config.py
--- a/sphinx/config.py
+++ b/sphinx/config.py
@@ -195,6 +195,7 @@
# linkcheck options
linkcheck_ignore = ([], None),
linkcheck_timeout = (None, None),
+ linkcheck_retries = (1, None),
linkcheck_workers = (5, None),
linkcheck_anchors = (True, None),

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.