bpo-40275: Move TransientResource to test_urllib2net (GH-20812)
Move TransientResource, time_out, socket_peer_reset and ioerror_peer_reset from test.support to test_urllib2net. Remove "import errno" from test.support.
This commit is contained in:
parent
bcd7deed91
commit
311110abcd
|
@ -922,15 +922,6 @@ The :mod:`test.support` module defines the following functions:
|
||||||
|
|
||||||
The :mod:`test.support` module defines the following classes:
|
The :mod:`test.support` module defines the following classes:
|
||||||
|
|
||||||
.. class:: TransientResource(exc, **kwargs)
|
|
||||||
|
|
||||||
Instances are a context manager that raises :exc:`ResourceDenied` if the
|
|
||||||
specified exception type is raised. Any keyword arguments are treated as
|
|
||||||
attribute/value pairs to be compared against any exception raised within the
|
|
||||||
:keyword:`with` statement. Only if all pairs match properly against
|
|
||||||
attributes on the exception is :exc:`ResourceDenied` raised.
|
|
||||||
|
|
||||||
|
|
||||||
.. class:: SuppressCrashReport()
|
.. class:: SuppressCrashReport()
|
||||||
|
|
||||||
A context manager used to try to prevent crash dialog popups on tests that
|
A context manager used to try to prevent crash dialog popups on tests that
|
||||||
|
|
|
@ -4,7 +4,6 @@ if __name__ != 'test.support':
|
||||||
raise ImportError('support must be imported from the test package')
|
raise ImportError('support must be imported from the test package')
|
||||||
|
|
||||||
import contextlib
|
import contextlib
|
||||||
import errno
|
|
||||||
import functools
|
import functools
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
@ -49,7 +48,6 @@ __all__ = [
|
||||||
"is_resource_enabled", "requires", "requires_freebsd_version",
|
"is_resource_enabled", "requires", "requires_freebsd_version",
|
||||||
"requires_linux_version", "requires_mac_ver",
|
"requires_linux_version", "requires_mac_ver",
|
||||||
"check_syntax_error",
|
"check_syntax_error",
|
||||||
"TransientResource", "time_out", "socket_peer_reset", "ioerror_peer_reset",
|
|
||||||
"BasicTestRunner", "run_unittest", "run_doctest",
|
"BasicTestRunner", "run_unittest", "run_doctest",
|
||||||
"requires_gzip", "requires_bz2", "requires_lzma",
|
"requires_gzip", "requires_bz2", "requires_lzma",
|
||||||
"bigmemtest", "bigaddrspacetest", "cpython_only", "get_attribute",
|
"bigmemtest", "bigaddrspacetest", "cpython_only", "get_attribute",
|
||||||
|
@ -551,39 +549,6 @@ def open_urlresource(url, *args, **kw):
|
||||||
raise TestFailed('invalid resource %r' % fn)
|
raise TestFailed('invalid resource %r' % fn)
|
||||||
|
|
||||||
|
|
||||||
class TransientResource(object):
|
|
||||||
|
|
||||||
"""Raise ResourceDenied if an exception is raised while the context manager
|
|
||||||
is in effect that matches the specified exception and attributes."""
|
|
||||||
|
|
||||||
def __init__(self, exc, **kwargs):
|
|
||||||
self.exc = exc
|
|
||||||
self.attrs = kwargs
|
|
||||||
|
|
||||||
def __enter__(self):
|
|
||||||
return self
|
|
||||||
|
|
||||||
def __exit__(self, type_=None, value=None, traceback=None):
|
|
||||||
"""If type_ is a subclass of self.exc and value has attributes matching
|
|
||||||
self.attrs, raise ResourceDenied. Otherwise let the exception
|
|
||||||
propagate (if any)."""
|
|
||||||
if type_ is not None and issubclass(self.exc, type_):
|
|
||||||
for attr, attr_value in self.attrs.items():
|
|
||||||
if not hasattr(value, attr):
|
|
||||||
break
|
|
||||||
if getattr(value, attr) != attr_value:
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
raise ResourceDenied("an optional resource is not available")
|
|
||||||
|
|
||||||
# Context managers that raise ResourceDenied when various issues
|
|
||||||
# with the Internet connection manifest themselves as exceptions.
|
|
||||||
# XXX deprecate these and use transient_internet() instead
|
|
||||||
time_out = TransientResource(OSError, errno=errno.ETIMEDOUT)
|
|
||||||
socket_peer_reset = TransientResource(OSError, errno=errno.ECONNRESET)
|
|
||||||
ioerror_peer_reset = TransientResource(OSError, errno=errno.ECONNRESET)
|
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def captured_output(stream_name):
|
def captured_output(stream_name):
|
||||||
"""Return a context manager used by captured_stdout/stdin/stderr
|
"""Return a context manager used by captured_stdout/stdin/stderr
|
||||||
|
|
|
@ -660,7 +660,6 @@ class TestSupport(unittest.TestCase):
|
||||||
# findfile
|
# findfile
|
||||||
# check_warnings
|
# check_warnings
|
||||||
# EnvironmentVarGuard
|
# EnvironmentVarGuard
|
||||||
# TransientResource
|
|
||||||
# transient_internet
|
# transient_internet
|
||||||
# run_with_locale
|
# run_with_locale
|
||||||
# set_memlimit
|
# set_memlimit
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import errno
|
||||||
import unittest
|
import unittest
|
||||||
from test import support
|
from test import support
|
||||||
from test.support import socket_helper
|
from test.support import socket_helper
|
||||||
|
@ -39,6 +40,39 @@ _urlopen_with_retry = _wrap_with_retry_thrice(urllib.request.urlopen,
|
||||||
urllib.error.URLError)
|
urllib.error.URLError)
|
||||||
|
|
||||||
|
|
||||||
|
class TransientResource(object):
|
||||||
|
|
||||||
|
"""Raise ResourceDenied if an exception is raised while the context manager
|
||||||
|
is in effect that matches the specified exception and attributes."""
|
||||||
|
|
||||||
|
def __init__(self, exc, **kwargs):
|
||||||
|
self.exc = exc
|
||||||
|
self.attrs = kwargs
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, type_=None, value=None, traceback=None):
|
||||||
|
"""If type_ is a subclass of self.exc and value has attributes matching
|
||||||
|
self.attrs, raise ResourceDenied. Otherwise let the exception
|
||||||
|
propagate (if any)."""
|
||||||
|
if type_ is not None and issubclass(self.exc, type_):
|
||||||
|
for attr, attr_value in self.attrs.items():
|
||||||
|
if not hasattr(value, attr):
|
||||||
|
break
|
||||||
|
if getattr(value, attr) != attr_value:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
raise ResourceDenied("an optional resource is not available")
|
||||||
|
|
||||||
|
# Context managers that raise ResourceDenied when various issues
|
||||||
|
# with the Internet connection manifest themselves as exceptions.
|
||||||
|
# XXX deprecate these and use transient_internet() instead
|
||||||
|
time_out = TransientResource(OSError, errno=errno.ETIMEDOUT)
|
||||||
|
socket_peer_reset = TransientResource(OSError, errno=errno.ECONNRESET)
|
||||||
|
ioerror_peer_reset = TransientResource(OSError, errno=errno.ECONNRESET)
|
||||||
|
|
||||||
|
|
||||||
class AuthTests(unittest.TestCase):
|
class AuthTests(unittest.TestCase):
|
||||||
"""Tests urllib2 authentication features."""
|
"""Tests urllib2 authentication features."""
|
||||||
|
|
||||||
|
@ -237,9 +271,9 @@ class OtherNetworkTests(unittest.TestCase):
|
||||||
raise
|
raise
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
with support.time_out, \
|
with time_out, \
|
||||||
support.socket_peer_reset, \
|
socket_peer_reset, \
|
||||||
support.ioerror_peer_reset:
|
ioerror_peer_reset:
|
||||||
buf = f.read()
|
buf = f.read()
|
||||||
debug("read %d bytes" % len(buf))
|
debug("read %d bytes" % len(buf))
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
|
|
Loading…
Reference in New Issue