summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2015-01-03 14:35:52 +0100
committerGiampaolo Rodola <g.rodola@gmail.com>2015-01-03 14:35:52 +0100
commitad2624ae1679e884087f448df8c1aedbaf3d346d (patch)
tree955bcb792d825b591dfc413c94f429267e262e3a
parentb2eccf0a73a15b6f455f578de580c0393133c1cf (diff)
downloadpsutil-ad2624ae1679e884087f448df8c1aedbaf3d346d.tar.gz
#556: all open() statements are now done via 'with' ctx manager; hopefully this should fix all fds leaks
-rw-r--r--psutil/_pslinux.py21
1 files changed, 7 insertions, 14 deletions
diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py
index a367a5be..8ffff23d 100644
--- a/psutil/_pslinux.py
+++ b/psutil/_pslinux.py
@@ -670,11 +670,8 @@ class Process(object):
@wrap_exceptions
def name(self):
fname = "/proc/%s/stat" % self.pid
- if PY3:
- f = open(fname, "rt", encoding=DEFAULT_ENCODING)
- else:
- f = open(fname, "rt")
- with f:
+ kw = dict(encoding=DEFAULT_ENCODING) if PY3 else dict()
+ with open(fname, "rt", **kw) as f:
# XXX - gets changed later and probably needs refactoring
return f.read().split(' ')[1].replace('(', '').replace(')', '')
@@ -708,12 +705,8 @@ class Process(object):
@wrap_exceptions
def cmdline(self):
fname = "/proc/%s/cmdline" % self.pid
- if PY3:
- f = open(fname, "rt", encoding=DEFAULT_ENCODING)
- else:
- f = open(fname, "rt")
- with f:
- # return the args as a list
+ kw = dict(encoding=DEFAULT_ENCODING) if PY3 else dict()
+ with open(fname, "rt", **kw) as f:
return [x for x in f.read().split('\x00') if x]
@wrap_exceptions
@@ -918,8 +911,10 @@ class Process(object):
retlist = []
hit_enoent = False
for thread_id in thread_ids:
+ fname = "/proc/%s/task/%s/stat" % (self.pid, thread_id)
try:
- f = open("/proc/%s/task/%s/stat" % (self.pid, thread_id), 'rb')
+ with open(fname, 'rb') as f:
+ st = f.read().strip()
except EnvironmentError as err:
if err.errno == errno.ENOENT:
# no such file or directory; it means thread
@@ -927,8 +922,6 @@ class Process(object):
hit_enoent = True
continue
raise
- with f:
- st = f.read().strip()
# ignore the first two values ("pid (exe)")
st = st[st.find(b')') + 2:]
values = st.split(b' ')