Commit 8b63d3af authored by Berker Peksag's avatar Berker Peksag

Issue #22596: support.transient_internet() now also catches

ConnectionRefusedError exceptions wrapped by urllib.error.URLError.

This change should fix sporadic failures in test_urllib2net.
parent 6d1c149a
...@@ -25,6 +25,7 @@ import sysconfig ...@@ -25,6 +25,7 @@ import sysconfig
import tempfile import tempfile
import time import time
import unittest import unittest
import urllib.error
import warnings import warnings
try: try:
...@@ -1307,6 +1308,8 @@ def transient_internet(resource_name, *, timeout=30.0, errnos=()): ...@@ -1307,6 +1308,8 @@ def transient_internet(resource_name, *, timeout=30.0, errnos=()):
n = getattr(err, 'errno', None) n = getattr(err, 'errno', None)
if (isinstance(err, socket.timeout) or if (isinstance(err, socket.timeout) or
(isinstance(err, socket.gaierror) and n in gai_errnos) or (isinstance(err, socket.gaierror) and n in gai_errnos) or
(isinstance(err, urllib.error.URLError) and
"ConnectionRefusedError" in err.reason) or
n in captured_errnos): n in captured_errnos):
if not verbose: if not verbose:
sys.stderr.write(denied.args[0] + "\n") sys.stderr.write(denied.args[0] + "\n")
......
...@@ -229,6 +229,7 @@ class OtherNetworkTests(unittest.TestCase): ...@@ -229,6 +229,7 @@ class OtherNetworkTests(unittest.TestCase):
with support.transient_internet(url): with support.transient_internet(url):
try: try:
f = urlopen(url, req, TIMEOUT) f = urlopen(url, req, TIMEOUT)
# urllib.error.URLError is a subclass of OSError
except OSError as err: except OSError as err:
if expected_err: if expected_err:
msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" % msg = ("Didn't get expected error(s) %s for %s %s, got %s: %s" %
...@@ -236,12 +237,6 @@ class OtherNetworkTests(unittest.TestCase): ...@@ -236,12 +237,6 @@ class OtherNetworkTests(unittest.TestCase):
self.assertIsInstance(err, expected_err, msg) self.assertIsInstance(err, expected_err, msg)
else: else:
raise raise
except urllib.error.URLError as err:
if isinstance(err[0], socket.timeout):
print("<timeout: %s>" % url, file=sys.stderr)
continue
else:
raise
else: else:
try: try:
with support.time_out, \ with support.time_out, \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment