From 686057b8facb474a4542a2236f6ca2de7aa04cb5 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Thu, 4 Jun 2009 00:11:54 +0000 Subject: Use new form of with-statement instead of contextlib.nested(). --- Lib/filecmp.py | 3 +-- Lib/test/support.py | 12 +++++------- Lib/test/test_signal.py | 4 ++-- Lib/test/test_urllib2net.py | 4 +++- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/Lib/filecmp.py b/Lib/filecmp.py index 1a86e06..e5983cd 100644 --- a/Lib/filecmp.py +++ b/Lib/filecmp.py @@ -11,7 +11,6 @@ Functions: import os import stat -import contextlib from itertools import filterfalse __all__ = ["cmp", "dircmp", "cmpfiles"] @@ -63,7 +62,7 @@ def _sig(st): def _do_cmp(f1, f2): bufsize = BUFSIZE - with contextlib.nested(open(f1, 'rb'), open(f2, 'rb')) as (fp1, fp2): + with open(f1, 'rb') as fp1, open(f2, 'rb') as fp2: while True: b1 = fp1.read(bufsize) b2 = fp2.read(bufsize) diff --git a/Lib/test/support.py b/Lib/test/support.py index bdc6b89..da81422 100644 --- a/Lib/test/support.py +++ b/Lib/test/support.py @@ -591,13 +591,11 @@ class TransientResource(object): raise ResourceDenied("an optional resource is not available") -def transient_internet(): - """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) - return contextlib.nested(time_out, socket_peer_reset, ioerror_peer_reset) +# Context managers that raise 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) @contextlib.contextmanager diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py index 570526c..786bb41 100644 --- a/Lib/test/test_signal.py +++ b/Lib/test/test_signal.py @@ -146,8 +146,8 @@ class InterProcessSignalTests(unittest.TestCase): # re-raises information about any exceptions the child # throws. The real work happens in self.run_test(). os_done_r, os_done_w = os.pipe() - with nested(closing(os.fdopen(os_done_r, 'rb')), - closing(os.fdopen(os_done_w, 'wb'))) as (done_r, done_w): + with closing(os.fdopen(os_done_r, 'rb')) as done_r, \ + closing(os.fdopen(os_done_w, 'wb')) as done_w: child = os.fork() if child == 0: # In the child process; run the test and report results diff --git a/Lib/test/test_urllib2net.py b/Lib/test/test_urllib2net.py index c2da147..dc2d99d 100644 --- a/Lib/test/test_urllib2net.py +++ b/Lib/test/test_urllib2net.py @@ -174,7 +174,9 @@ class OtherNetworkTests(unittest.TestCase): (expected_err, url, req, type(err), err)) self.assert_(isinstance(err, expected_err), msg) else: - with support.transient_internet(): + with support.time_out, \ + support.socket_peer_reset, \ + support.ioerror_peer_reset: buf = f.read() f.close() debug("read %d bytes" % len(buf)) -- cgit v0.12