Skip to content

Commit

Permalink
#921: add Popen.__del__ to help the GC freeing resources
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Oct 15, 2016
1 parent 1514fcb commit bdcde7e
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__()
Expand Down
2 changes: 1 addition & 1 deletion psutil/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion psutil/tests/test_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]))

Expand Down

0 comments on commit bdcde7e

Please sign in to comment.