summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2020-05-12 21:15:38 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2020-05-12 21:15:38 +0200
commit12353e940fa5d8f75fe30a36a1dc9f11e082bbe3 (patch)
treee5a6e88166d75cef8ce41297878c3e5f03657eb4
parent5484498eb68225760e8d4dcce13d5078019ab5f2 (diff)
downloadpsutil-12353e940fa5d8f75fe30a36a1dc9f11e082bbe3.tar.gz
get rid of redirect_stderr
-rw-r--r--psutil/_compat.py17
-rw-r--r--psutil/tests/__init__.py7
-rwxr-xr-xpsutil/tests/test_testutils.py12
3 files changed, 13 insertions, 23 deletions
diff --git a/psutil/_compat.py b/psutil/_compat.py
index 145fb71d..17f38485 100644
--- a/psutil/_compat.py
+++ b/psutil/_compat.py
@@ -8,7 +8,6 @@ Python 3 way of doing things).
"""
import collections
-import contextlib
import errno
import functools
import os
@@ -23,7 +22,7 @@ __all__ = [
# literals
"u", "b",
# collections module
- "lru_cache", "redirect_stderr",
+ "lru_cache",
# shutil module
"which", "get_terminal_size",
# python 3 exceptions
@@ -423,17 +422,3 @@ except ImportError:
return (res[1], res[0])
except Exception:
return fallback
-
-
-# python 3.4
-try:
- from contextlib import redirect_stderr
-except ImportError:
- @contextlib.contextmanager
- def redirect_stderr(target):
- original = sys.stderr
- try:
- sys.stderr = target
- yield
- finally:
- sys.stderr = original
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py
index 65c79a01..fd0eaf16 100644
--- a/psutil/tests/__init__.py
+++ b/psutil/tests/__init__.py
@@ -974,21 +974,20 @@ class TestMemoryLeak(PsutilTestCase):
raise ValueError("times must be > 0")
if warmup_times < 0:
raise ValueError("warmup_times must be >= 0")
+ if retries < 1:
+ raise ValueError("retries must be >= 1")
if tolerance is not None and tolerance < 0:
raise ValueError("tolerance must be >= 0")
# warm up
self._call_ntimes(fun, warmup_times)
-
- b2h = bytes2human
messages = []
prev_mem = 0
increase = times
-
for idx in range(1, retries + 1):
mem = self._call_ntimes(fun, times)
msg = "Run #%s: extra-mem=%s, per-call=%s, calls=%s" % (
- idx, b2h(mem), b2h(mem / times), times)
+ idx, bytes2human(mem), bytes2human(mem / times), times)
messages.append(msg)
success = mem <= tolerance or mem < prev_mem
if success:
diff --git a/psutil/tests/test_testutils.py b/psutil/tests/test_testutils.py
index a3e55699..274de8ae 100755
--- a/psutil/tests/test_testutils.py
+++ b/psutil/tests/test_testutils.py
@@ -363,10 +363,15 @@ class TestMemLeakClass(TestMemoryLeak):
self.assertRaises(ValueError, self.execute, lambda: 0, tolerance=-1)
def test_leak(self):
- def fun():
- ls.append("x" * 24 * 1024)
ls = []
- self.assertRaises(AssertionError, self.execute, fun)
+
+ def fun(ls=ls):
+ ls.append("x" * 24 * 1024)
+
+ try:
+ self.assertRaises(AssertionError, self.execute, fun)
+ finally:
+ del ls
def test_tolerance(self):
def fun():
@@ -392,6 +397,7 @@ class TestMemLeakClass(TestMemoryLeak):
self.execute_w_exc(ZeroDivisionError, fun)
+@serialrun
class TestFdsLeakClass(TestFdsLeak):
def test_unclosed_files(self):