diff options
| author | Giampaolo Rodola <g.rodola@gmail.com> | 2018-10-16 22:18:33 +0200 |
|---|---|---|
| committer | Giampaolo Rodola <g.rodola@gmail.com> | 2018-10-16 22:18:33 +0200 |
| commit | 6db8d2e41dc67621eae9d8eeab3c39fef7e2ddf4 (patch) | |
| tree | db98aaae072a2b73a5c2d4ac76858d8346beaefc /psutil | |
| parent | 8b465260638cde5370808e23eb9ace608141393a (diff) | |
| download | psutil-6db8d2e41dc67621eae9d8eeab3c39fef7e2ddf4.tar.gz | |
refactor hasattr() checks as global constants
Diffstat (limited to 'psutil')
| -rw-r--r-- | psutil/_psbsd.py | 15 | ||||
| -rw-r--r-- | psutil/_pslinux.py | 4 | ||||
| -rw-r--r-- | psutil/_pswindows.py | 3 |
3 files changed, 15 insertions, 7 deletions
diff --git a/psutil/_psbsd.py b/psutil/_psbsd.py index 7f4bcb6d..c2896cb7 100644 --- a/psutil/_psbsd.py +++ b/psutil/_psbsd.py @@ -103,6 +103,11 @@ else: PAGESIZE = os.sysconf("SC_PAGE_SIZE") AF_LINK = cext_posix.AF_LINK +HAS_PER_CPU_TIMES = hasattr(cext, "per_cpu_times") +HAS_PROC_NUM_THREADS = hasattr(cext, "proc_num_threads") +HAS_PROC_OPEN_FILES = hasattr(cext, 'proc_open_files') +HAS_PROC_NUM_FDS = hasattr(cext, 'proc_num_fds') + kinfo_proc_map = dict( ppid=0, status=1, @@ -211,7 +216,7 @@ def cpu_times(): return scputimes(user, nice, system, idle, irq) -if hasattr(cext, "per_cpu_times"): +if HAS_PER_CPU_TIMES: def per_cpu_times(): """Return system CPU times as a namedtuple""" ret = [] @@ -678,7 +683,7 @@ class Process(object): @wrap_exceptions def num_threads(self): - if hasattr(cext, "proc_num_threads"): + if HAS_PROC_NUM_THREADS: # FreeBSD return cext.proc_num_threads(self.pid) else: @@ -798,7 +803,7 @@ class Process(object): elif NETBSD: with wrap_exceptions_procfs(self): return os.readlink("/proc/%s/cwd" % self.pid) - elif hasattr(cext, 'proc_open_files'): + elif HAS_PROC_OPEN_FILES: # FreeBSD < 8 does not support functions based on # kinfo_getfile() and kinfo_getvmmap() return cext.proc_cwd(self.pid) or None @@ -817,7 +822,7 @@ class Process(object): # FreeBSD < 8 does not support functions based on kinfo_getfile() # and kinfo_getvmmap() - if hasattr(cext, 'proc_open_files'): + if HAS_PROC_OPEN_FILES: @wrap_exceptions def open_files(self): """Return files opened by process as a list of namedtuples.""" @@ -828,7 +833,7 @@ class Process(object): # FreeBSD < 8 does not support functions based on kinfo_getfile() # and kinfo_getvmmap() - if hasattr(cext, 'proc_num_fds'): + if HAS_PROC_NUM_FDS: @wrap_exceptions def num_fds(self): """Return the number of file descriptors opened by this process.""" diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index 1520261c..236934fc 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -73,6 +73,7 @@ __extra__all__ = [ POWER_SUPPLY_PATH = "/sys/class/power_supply" HAS_SMAPS = os.path.exists('/proc/%s/smaps' % os.getpid()) HAS_PRLIMIT = hasattr(cext, "linux_prlimit") +HAS_PROC_IO_PRIORITY = hasattr(cext, "proc_ioprio_get") _DEFAULT = object() # RLIMIT_* constants, not guaranteed to be present on all kernels @@ -1928,7 +1929,7 @@ class Process(object): raise # only starting from kernel 2.6.13 - if hasattr(cext, "proc_ioprio_get"): + if HAS_PROC_IO_PRIORITY: @wrap_exceptions def ionice_get(self): @@ -1971,6 +1972,7 @@ class Process(object): return cext.proc_ioprio_set(self.pid, ioclass, value) if HAS_PRLIMIT: + @wrap_exceptions def rlimit(self, resource, limits=None): # If pid is 0 prlimit() applies to the calling process and diff --git a/psutil/_pswindows.py b/psutil/_pswindows.py index 18651d6c..b938d42f 100644 --- a/psutil/_pswindows.py +++ b/psutil/_pswindows.py @@ -83,6 +83,7 @@ ACCESS_DENIED_ERRSET = frozenset([errno.EPERM, errno.EACCES, cext.ERROR_ACCESS_DENIED]) NO_SUCH_SERVICE_ERRSET = frozenset([cext.ERROR_INVALID_NAME, cext.ERROR_SERVICE_DOES_NOT_EXIST]) +HAS_PROC_IO_PRIORITY = hasattr(cext, "proc_io_priority_get") if enum is None: @@ -928,7 +929,7 @@ class Process(object): return cext.proc_priority_set(self.pid, value) # available on Windows >= Vista - if hasattr(cext, "proc_io_priority_get"): + if HAS_PROC_IO_PRIORITY: @wrap_exceptions def ionice_get(self): return cext.proc_io_priority_get(self.pid) |
