From bdcde7e714dd098bf9b6241c79eefa3b1cf74d52 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Sat, 15 Oct 2016 16:59:36 +0200 Subject: [PATCH] #921: add Popen.__del__ to help the GC freeing resources --- HISTORY.rst | 2 ++ psutil/__init__.py | 4 ++++ psutil/tests/__init__.py | 2 +- psutil/tests/test_linux.py | 2 +- 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 6de6703f8..59cae0404 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -37,6 +37,8 @@ Bug tracker at https://github.com/giampaolo/psutil/issues OSError with no exception set if process is gone. - #916: [OSX] fix many compilation warnings. - #918: [NetBSD] all memory metrics were wrong. +- #921: psutil.Popen now defines a __del__ special method which calls the + original one, hopefully helping the gc to free resources. 4.3.1 - 2016-09-01 diff --git a/psutil/__init__.py b/psutil/__init__.py index 9c446b53b..a9c76d610 100644 --- a/psutil/__init__.py +++ b/psutil/__init__.py @@ -1264,6 +1264,10 @@ def __init__(self, *args, **kwargs): def __dir__(self): return sorted(set(dir(Popen) + dir(subprocess.Popen))) + def __del__(self, *args, **kwargs): + self.__subproc.__del__(*args, **kwargs) + self.__subproc = None + def __enter__(self): if hasattr(self.__subproc, '__enter__'): self.__subproc.__enter__() diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index c3ba1a435..10ff3be74 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -204,7 +204,7 @@ def get_test_subprocess(cmd=None, **kwds): pyline += "sleep(60)" cmd = [PYTHON, "-c", pyline] sproc = subprocess.Popen(cmd, **kwds) - wait_for_file(TESTFN, empty=True) + wait_for_file(TESTFN, delete_file=True, empty=True) else: sproc = subprocess.Popen(cmd, **kwds) wait_for_pid(sproc.pid) diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py index d3e89b6b4..ae295be61 100755 --- a/psutil/tests/test_linux.py +++ b/psutil/tests/test_linux.py @@ -564,7 +564,7 @@ def test_net_if_stats(self): except RuntimeError: pass else: - self.assertEqual(stats.isup, 'RUNNING' in out) + self.assertEqual(stats.isup, 'RUNNING' in out, msg=out) self.assertEqual(stats.mtu, int(re.findall('MTU:(\d+)', out)[0]))