def call(self, function, *args, **kwargs): try: obj = getattr(self.proc, function) if callable(obj): obj(*args, **kwargs) except psutil.Error: pass
def call(p, attr): args = () attr = getattr(p, name, None) if attr is not None and callable(attr): if name == 'rlimit': args = (psutil.RLIMIT_NOFILE,) attr(*args) else: attr
def wrapper(self, *args, **kwargs): try: return callable(self, *args, **kwargs) except OSError: err = sys.exc_info()[1] if err.errno in ACCESS_DENIED_SET: raise psutil.AccessDenied(None, None) if err.errno == errno.ESRCH: raise psutil.NoSuchProcess(None, None) raise
def as_dict(self, attrs=[], ad_value=None): """Utility method returning process information as a hashable dictionary. If 'attrs' is specified it must be a list of strings reflecting available Process class's attribute names (e.g. ['get_cpu_times', 'name']) else all public (read only) attributes are assumed. 'ad_value' is the value which gets assigned to a dict key in case AccessDenied exception is raised when retrieving that particular process information. """ excluded_names = set(['send_signal', 'suspend', 'resume', 'terminate', 'kill', 'wait', 'is_running', 'as_dict', 'parent', 'get_children', 'nice']) retdict = dict() for name in set(attrs or dir(self)): if name.startswith('_'): continue if name.startswith('set_'): continue if name in excluded_names: continue try: attr = getattr(self, name) if callable(attr): if name == 'get_cpu_percent': ret = attr(interval=0) else: ret = attr() else: ret = attr except AccessDenied: ret = ad_value except NotImplementedError: # in case of not implemented functionality (may happen # on old or exotic systems) we want to crash only if # the user explicitly asked for that particular attr if attrs: raise continue if name.startswith('get'): if name[3] == '_': name = name[4:] elif name == 'getcwd': name = 'cwd' retdict[name] = ret return retdict
def call(self, function, *args, **kwargs): if callable(function): if '_exc' in kwargs: exc = kwargs.pop('_exc') self.assertRaises(exc, function, *args, **kwargs) else: try: function(*args, **kwargs) except psutil.Error: pass else: meth = getattr(self.proc, function) if '_exc' in kwargs: exc = kwargs.pop('_exc') self.assertRaises(exc, meth, *args, **kwargs) else: try: meth(*args, **kwargs) except psutil.Error: pass
def call(p, attr): attr = getattr(p, name, None) if attr is not None and callable(attr): attr() else: attr
def call(self, function, *args, **kwargs): obj = getattr(psutil, function) if callable(obj): retvalue = obj(*args, **kwargs)
def test_fetch_all(self): valid_procs = 0 excluded_names = set([ 'send_signal', 'suspend', 'resume', 'terminate', 'kill', 'wait', 'as_dict', 'parent', 'children', 'memory_info_ex', 'oneshot', ]) if LINUX and not HAS_RLIMIT: excluded_names.add('rlimit') attrs = [] for name in dir(psutil.Process): if name.startswith("_"): continue if name in excluded_names: continue attrs.append(name) default = object() failures = [] for p in psutil.process_iter(): with p.oneshot(): for name in attrs: ret = default try: args = () kwargs = {} attr = getattr(p, name, None) if attr is not None and callable(attr): if name == 'rlimit': args = (psutil.RLIMIT_NOFILE, ) elif name == 'memory_maps': kwargs = {'grouped': False} ret = attr(*args, **kwargs) else: ret = attr valid_procs += 1 except NotImplementedError: msg = "%r was skipped because not implemented" % ( self.__class__.__name__ + '.test_' + name) warn(msg) except (psutil.NoSuchProcess, psutil.AccessDenied) as err: self.assertEqual(err.pid, p.pid) if err.name: # make sure exception's name attr is set # with the actual process name self.assertEqual(err.name, p.name()) assert str(err) assert err.msg except Exception as err: s = '\n' + '=' * 70 + '\n' s += "FAIL: test_%s (proc=%s" % (name, p) if ret != default: s += ", ret=%s)" % repr(ret) s += ')\n' s += '-' * 70 s += "\n%s" % traceback.format_exc() s = "\n".join((" " * 4) + i for i in s.splitlines()) s += '\n' failures.append(s) break else: if ret not in (0, 0.0, [], None, '', {}): assert ret, ret meth = getattr(self, name) meth(ret, p) if failures: self.fail(''.join(failures)) # we should always have a non-empty list, not including PID 0 etc. # special cases. assert valid_procs
def call(self, function, *args, **kwargs): fun = function if callable(function) else getattr(psutil, function) fun(*args, **kwargs)
def call(self, function, *args, **kwargs): p = psutil.Process(os.getpid()) obj = getattr(p, function) if callable(obj): obj(*args, **kwargs)
def test_fetch_all(self): valid_procs = 0 excluded_names = set([ 'send_signal', 'suspend', 'resume', 'terminate', 'kill', 'wait', 'as_dict', 'parent', 'children', 'memory_info_ex', 'oneshot', ]) if LINUX and not HAS_RLIMIT: excluded_names.add('rlimit') attrs = [] for name in dir(psutil.Process): if name.startswith("_"): continue if name in excluded_names: continue attrs.append(name) default = object() failures = [] for p in psutil.process_iter(): with p.oneshot(): for name in attrs: ret = default try: args = () kwargs = {} attr = getattr(p, name, None) if attr is not None and callable(attr): if name == 'rlimit': args = (psutil.RLIMIT_NOFILE,) elif name == 'memory_maps': kwargs = {'grouped': False} ret = attr(*args, **kwargs) else: ret = attr valid_procs += 1 except NotImplementedError: msg = "%r was skipped because not implemented" % ( self.__class__.__name__ + '.test_' + name) warn(msg) except (psutil.NoSuchProcess, psutil.AccessDenied) as err: self.assertEqual(err.pid, p.pid) if err.name: # make sure exception's name attr is set # with the actual process name self.assertEqual(err.name, p.name()) assert str(err) assert err.msg except Exception as err: s = '\n' + '=' * 70 + '\n' s += "FAIL: test_%s (proc=%s" % (name, p) if ret != default: s += ", ret=%s)" % repr(ret) s += ')\n' s += '-' * 70 s += "\n%s" % traceback.format_exc() s = "\n".join((" " * 4) + i for i in s.splitlines()) s += '\n' failures.append(s) break else: if ret not in (0, 0.0, [], None, '', {}): assert ret, ret meth = getattr(self, name) meth(ret, p) if failures: self.fail(''.join(failures)) # we should always have a non-empty list, not including PID 0 etc. # special cases. assert valid_procs