Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
a88c83cb
Commit
a88c83cb
authored
Sep 07, 2010
by
Antoine Pitrou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #8574: better implementation of test.support.transient_internet().
Original patch by Victor.
parent
f015b3f5
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
11 deletions
+35
-11
Lib/test/support.py
Lib/test/support.py
+31
-7
Lib/test/test_ssl.py
Lib/test/test_ssl.py
+4
-4
No files found.
Lib/test/support.py
View file @
a88c83cb
...
...
@@ -35,7 +35,7 @@ __all__ = [
"check_warnings"
,
"CleanImport"
,
"EnvironmentVarGuard"
,
"TransientResource"
,
"captured_output"
,
"captured_stdout"
,
"time_out"
,
"socket_peer_reset"
,
"ioerror_peer_reset"
,
"run_with_locale"
,
'temp_umask'
,
"run_with_locale"
,
'temp_umask'
,
"transient_internet"
,
"set_memlimit"
,
"bigmemtest"
,
"bigaddrspacetest"
,
"BasicTestRunner"
,
"run_unittest"
,
"run_doctest"
,
"threading_setup"
,
"threading_cleanup"
,
"reap_children"
,
"cpython_only"
,
"check_impl_detail"
,
"get_attribute"
,
...
...
@@ -775,23 +775,47 @@ class TransientResource(object):
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
(
IOError
,
errno
=
errno
.
ETIMEDOUT
)
socket_peer_reset
=
TransientResource
(
socket
.
error
,
errno
=
errno
.
ECONNRESET
)
ioerror_peer_reset
=
TransientResource
(
IOError
,
errno
=
errno
.
ECONNRESET
)
@
contextlib
.
contextmanager
def
transient_internet
():
def
transient_internet
(
resource_name
,
*
,
timeout
=
30.0
,
errnos
=
()
):
"""Return a context manager that raises ResourceDenied when various issues
with the Internet connection manifest themselves as exceptions."""
time_out
=
TransientResource
(
IOError
,
errno
=
errno
.
ETIMEDOUT
)
socket_peer_reset
=
TransientResource
(
socket
.
error
,
errno
=
errno
.
ECONNRESET
)
ioerror_peer_reset
=
TransientResource
(
IOError
,
errno
=
errno
.
ECONNRESET
)
with
time_out
,
socket_peer_reset
,
ioerror_peer_reset
:
denied
=
ResourceDenied
(
"Resource '%s' is not available"
%
resource_name
)
captured_errnos
=
errnos
or
(
errno
.
ETIMEDOUT
,
errno
.
ECONNRESET
)
def
filter_error
(
err
):
if
(
isinstance
(
err
,
socket
.
timeout
)
or
getattr
(
err
,
'errno'
,
None
)
in
captured_errnos
):
if
not
verbose
:
sys
.
stderr
.
write
(
denied
.
args
[
0
]
+
"
\
n
"
)
raise
denied
from
err
old_timeout
=
socket
.
getdefaulttimeout
()
try
:
if
timeout
is
not
None
:
socket
.
setdefaulttimeout
(
timeout
)
yield
except
IOError
as
err
:
# socket.error inherits IOError
filter_error
(
err
)
# urllib.request wraps the original socket.error with IOerror:
#
# except socket.error as msg:
# raise IOError('socket error', msg).with_traceback(sys.exc_info()[2])
if
len
(
err
.
args
)
>=
2
and
isinstance
(
err
.
args
[
1
],
socket
.
error
):
filter_error
(
err
.
args
[
1
])
raise
# XXX should we catch generic exceptions and look for their
# __cause__ or __context__?
finally
:
socket
.
setdefaulttimeout
(
old_timeout
)
@
contextlib
.
contextmanager
...
...
Lib/test/test_ssl.py
View file @
a88c83cb
...
...
@@ -477,10 +477,10 @@ class NetworkedTests(unittest.TestCase):
# NOTE: https://sha256.tbs-internet.com is another possible test host
remote
=
(
"sha2.hboeck.de"
,
443
)
sha256_cert
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
"sha256.pem"
)
s
=
ssl
.
wrap_socket
(
socket
.
socket
(
socket
.
AF_INET
),
cert_reqs
=
ssl
.
CERT_REQUIRED
,
ca_certs
=
sha256_cert
,)
with
support
.
transient_internet
():
with
support
.
transient_internet
(
"sha2.hboeck.de"
):
s
=
ssl
.
wrap_socket
(
socket
.
socket
(
socket
.
AF_INET
)
,
cert_reqs
=
ssl
.
CERT_REQUIRED
,
ca_certs
=
sha256_cert
,)
try
:
s
.
connect
(
remote
)
if
support
.
verbose
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment