Skip to content

Commit 311110a

Browse files
authored
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.
1 parent bcd7dee commit 311110a

File tree

4 files changed

+37
-48
lines changed

4 files changed

+37
-48
lines changed

Doc/library/test.rst

-9
Original file line numberDiff line numberDiff line change
@@ -922,15 +922,6 @@ The :mod:`test.support` module defines the following functions:
922922

923923
The :mod:`test.support` module defines the following classes:
924924

925-
.. class:: TransientResource(exc, **kwargs)
926-
927-
Instances are a context manager that raises :exc:`ResourceDenied` if the
928-
specified exception type is raised. Any keyword arguments are treated as
929-
attribute/value pairs to be compared against any exception raised within the
930-
:keyword:`with` statement. Only if all pairs match properly against
931-
attributes on the exception is :exc:`ResourceDenied` raised.
932-
933-
934925
.. class:: SuppressCrashReport()
935926

936927
A context manager used to try to prevent crash dialog popups on tests that

Lib/test/support/__init__.py

-35
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
raise ImportError('support must be imported from the test package')
55

66
import contextlib
7-
import errno
87
import functools
98
import os
109
import re
@@ -49,7 +48,6 @@
4948
"is_resource_enabled", "requires", "requires_freebsd_version",
5049
"requires_linux_version", "requires_mac_ver",
5150
"check_syntax_error",
52-
"TransientResource", "time_out", "socket_peer_reset", "ioerror_peer_reset",
5351
"BasicTestRunner", "run_unittest", "run_doctest",
5452
"requires_gzip", "requires_bz2", "requires_lzma",
5553
"bigmemtest", "bigaddrspacetest", "cpython_only", "get_attribute",
@@ -551,39 +549,6 @@ def check_valid_file(fn):
551549
raise TestFailed('invalid resource %r' % fn)
552550

553551

554-
class TransientResource(object):
555-
556-
"""Raise ResourceDenied if an exception is raised while the context manager
557-
is in effect that matches the specified exception and attributes."""
558-
559-
def __init__(self, exc, **kwargs):
560-
self.exc = exc
561-
self.attrs = kwargs
562-
563-
def __enter__(self):
564-
return self
565-
566-
def __exit__(self, type_=None, value=None, traceback=None):
567-
"""If type_ is a subclass of self.exc and value has attributes matching
568-
self.attrs, raise ResourceDenied. Otherwise let the exception
569-
propagate (if any)."""
570-
if type_ is not None and issubclass(self.exc, type_):
571-
for attr, attr_value in self.attrs.items():
572-
if not hasattr(value, attr):
573-
break
574-
if getattr(value, attr) != attr_value:
575-
break
576-
else:
577-
raise ResourceDenied("an optional resource is not available")
578-
579-
# Context managers that raise ResourceDenied when various issues
580-
# with the Internet connection manifest themselves as exceptions.
581-
# XXX deprecate these and use transient_internet() instead
582-
time_out = TransientResource(OSError, errno=errno.ETIMEDOUT)
583-
socket_peer_reset = TransientResource(OSError, errno=errno.ECONNRESET)
584-
ioerror_peer_reset = TransientResource(OSError, errno=errno.ECONNRESET)
585-
586-
587552
@contextlib.contextmanager
588553
def captured_output(stream_name):
589554
"""Return a context manager used by captured_stdout/stdin/stderr

Lib/test/test_support.py

-1
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,6 @@ def test_print_warning(self):
660660
# findfile
661661
# check_warnings
662662
# EnvironmentVarGuard
663-
# TransientResource
664663
# transient_internet
665664
# run_with_locale
666665
# set_memlimit

Lib/test/test_urllib2net.py

+37-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import errno
12
import unittest
23
from test import support
34
from test.support import socket_helper
@@ -39,6 +40,39 @@ def wrapped(*args, **kwargs):
3940
urllib.error.URLError)
4041

4142

43+
class TransientResource(object):
44+
45+
"""Raise ResourceDenied if an exception is raised while the context manager
46+
is in effect that matches the specified exception and attributes."""
47+
48+
def __init__(self, exc, **kwargs):
49+
self.exc = exc
50+
self.attrs = kwargs
51+
52+
def __enter__(self):
53+
return self
54+
55+
def __exit__(self, type_=None, value=None, traceback=None):
56+
"""If type_ is a subclass of self.exc and value has attributes matching
57+
self.attrs, raise ResourceDenied. Otherwise let the exception
58+
propagate (if any)."""
59+
if type_ is not None and issubclass(self.exc, type_):
60+
for attr, attr_value in self.attrs.items():
61+
if not hasattr(value, attr):
62+
break
63+
if getattr(value, attr) != attr_value:
64+
break
65+
else:
66+
raise ResourceDenied("an optional resource is not available")
67+
68+
# Context managers that raise ResourceDenied when various issues
69+
# with the Internet connection manifest themselves as exceptions.
70+
# XXX deprecate these and use transient_internet() instead
71+
time_out = TransientResource(OSError, errno=errno.ETIMEDOUT)
72+
socket_peer_reset = TransientResource(OSError, errno=errno.ECONNRESET)
73+
ioerror_peer_reset = TransientResource(OSError, errno=errno.ECONNRESET)
74+
75+
4276
class AuthTests(unittest.TestCase):
4377
"""Tests urllib2 authentication features."""
4478

@@ -237,9 +271,9 @@ def _test_urls(self, urls, handlers, retry=True):
237271
raise
238272
else:
239273
try:
240-
with support.time_out, \
241-
support.socket_peer_reset, \
242-
support.ioerror_peer_reset:
274+
with time_out, \
275+
socket_peer_reset, \
276+
ioerror_peer_reset:
243277
buf = f.read()
244278
debug("read %d bytes" % len(buf))
245279
except socket.timeout:

0 commit comments

Comments
 (0)