diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2017-05-06 19:49:47 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2017-05-06 19:49:47 +0200 |
commit | 7ae9ea8cb8315657d9f219a5fe86396de8521107 (patch) | |
tree | 4b562fc61160dec3092ddcce016c6e163d5e1f1b | |
parent | bdd5331ea578e7615efdda8c29fbfc0cb62af094 (diff) | |
download | psutil-7ae9ea8cb8315657d9f219a5fe86396de8521107.tar.gz |
replace contextlib.nested
-rw-r--r-- | psutil/_compat.py | 49 | ||||
-rwxr-xr-x | psutil/tests/test_connections.py | 5 |
2 files changed, 2 insertions, 52 deletions
diff --git a/psutil/_compat.py b/psutil/_compat.py index 9f2182c2..de91638f 100644 --- a/psutil/_compat.py +++ b/psutil/_compat.py @@ -5,7 +5,6 @@ """Module which provides compatibility with older Python versions.""" import collections -import contextlib import functools import os import sys @@ -248,51 +247,3 @@ except ImportError: if _access_check(name, mode): return name return None - - -# A backport of contextlib.nested for Python 3. -nested = getattr(contextlib, "nested", None) -if nested is None: - @contextlib.contextmanager - def nested(*managers): - """Support multiple context managers in a single with-statement. - - Code like this: - - with nested(A, B, C) as (X, Y, Z): - <body> - - is equivalent to this: - - with A as X: - with B as Y: - with C as Z: - <body> - - """ - exits = [] - vars = [] - exc = (None, None, None) - try: - for mgr in managers: - exit = mgr.__exit__ - enter = mgr.__enter__ - vars.append(enter()) - exits.append(exit) - yield vars - except: # NOQA - exc = sys.exc_info() - finally: - while exits: - exit = exits.pop() - try: - if exit(*exc): - exc = (None, None, None) - except: # NOQA - exc = sys.exc_info() - if exc != (None, None, None): - # Don't rely on sys.exc_info() still containing - # the right information. Another exception may - # have been raised and caught by an exit method - # exc[1] already has the __traceback__ attribute populated - raise exc[1] diff --git a/psutil/tests/test_connections.py b/psutil/tests/test_connections.py index d0c5445a..c3421000 100755 --- a/psutil/tests/test_connections.py +++ b/psutil/tests/test_connections.py @@ -26,7 +26,6 @@ from psutil import SUNOS from psutil import WINDOWS from psutil._common import pconn from psutil._common import supports_ipv6 -from psutil._compat import nested from psutil._compat import PY3 from psutil.tests import AF_UNIX from psutil.tests import bind_socket @@ -207,7 +206,7 @@ class TestConnectedSocketPairs(Base, unittest.TestCase): addr = ("127.0.0.1", get_free_port()) assert not thisproc.connections(kind='tcp4') server, client = tcp_socketpair(AF_INET, addr=addr) - with nested(closing(server), closing(client)): + with closing(server), closing(client): cons = thisproc.connections(kind='tcp4') self.assertEqual(len(cons), 2) self.assertEqual(cons[0].status, psutil.CONN_ESTABLISHED) @@ -223,7 +222,7 @@ class TestConnectedSocketPairs(Base, unittest.TestCase): def test_unix(self): with unix_socket_path() as name: server, client = unix_socketpair(name) - with nested(closing(server), closing(client)): + with closing(server), closing(client): cons = thisproc.connections(kind='unix') assert not (cons[0].laddr and cons[0].raddr) assert not (cons[1].laddr and cons[1].raddr) |