Skip to content

Commit

Permalink
#1058: get rid of contextlib.nested (deprecated)
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed May 10, 2017
1 parent f248307 commit 65ea7a8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 52 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
comprehensions.
- 1040_: implemented full unicode support.
- 1051_: disk_usage() on Python 3 is now able to accept bytes.
- 1058_: test suite now enables all warnings by default.

**Bug fixes**

Expand All @@ -41,6 +42,7 @@
- 1046_: [Windows] disk_partitions() on Windows overrides user's SetErrorMode.
- 1047_: [Windows] Process username(): memory leak in case exception is thrown.
- 1048_: [Windows] users()'s host field report an invalid IP address.
- 1058_: fixed Python warnings.

**Porting notes**

Expand Down
50 changes: 1 addition & 49 deletions psutil/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
"""Module which provides compatibility with older Python versions."""

import collections
import contextlib
import functools
import os
import sys

__all__ = ["PY3", "long", "xrange", "unicode", "basestring", "u", "b",
"callable", "lru_cache", "which", "nested"]
"callable", "lru_cache", "which"]

PY3 = sys.version_info[0] == 3

Expand Down Expand Up @@ -248,50 +247,3 @@ def _access_check(fn, mode):
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]
11 changes: 8 additions & 3 deletions psutil/tests/test_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
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
Expand Down Expand Up @@ -207,7 +206,7 @@ def test_tcp(self):
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)):
try:
cons = thisproc.connections(kind='tcp4')
self.assertEqual(len(cons), 2)
self.assertEqual(cons[0].status, psutil.CONN_ESTABLISHED)
Expand All @@ -218,12 +217,15 @@ def test_tcp(self):
# cons = thisproc.connections(kind='all')
# self.assertEqual(len(cons), 1)
# self.assertEqual(cons[0].status, psutil.CONN_CLOSE_WAIT)
finally:
server.close()
client.close()

@unittest.skipIf(not POSIX, 'POSIX only')
def test_unix(self):
with unix_socket_path() as name:
server, client = unix_socketpair(name)
with nested(closing(server), closing(client)):
try:
cons = thisproc.connections(kind='unix')
assert not (cons[0].laddr and cons[0].raddr)
assert not (cons[1].laddr and cons[1].raddr)
Expand All @@ -248,6 +250,9 @@ def test_unix(self):
# of both peers are set.
self.assertEqual(cons[0].laddr or cons[1].laddr, name)
self.assertEqual(cons[0].raddr or cons[1].raddr, name)
finally:
server.close()
client.close()

@skip_on_access_denied(only_if=OSX)
def test_combos(self):
Expand Down

0 comments on commit 65ea7a8

Please sign in to comment.