Correct and simplify URLError exception handling

Julien Palard pointed out that the way URLError exceptions were
being manually cobbled into the stderr stream wasn't quite working
(thanks!), but it was also unnecessarily complicated for reasons I
don't recall now. Rip most of it out and just go with a basic
catch/error/re-raise there instead.
This commit is contained in:
Jeremy Stanley
2021-06-01 15:31:39 +00:00
parent 038e2d65a3
commit 847a98636e

View File

@@ -221,18 +221,10 @@ def get_uri(
data = urlopen(uri).read().decode("utf-8")
except URLError:
if ignore_fail: return ""
else:
import os, sys, traceback
message = "%s error: failed to retrieve\n %s\n %s" % (
os.path.basename( sys.argv[0] ),
uri,
traceback.format_exception_only(
sys.exc_type,
sys.exc_value
)[0]
)
sys.stderr.write(message)
sys.exit(1)
import os, sys
sys.stderr.write("%s error: failed to retrieve\n %s\n\n" % (
os.path.basename( sys.argv[0] ), uri))
raise
# Some data sources are HTML with the plain text wrapped in pre tags
if "<pre>" in data:
data = data[data.find("<pre>")+5:data.find("</pre>")]