Skip to content

Commit

Permalink
refactor hasattr() checks as global constants
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Oct 16, 2018
1 parent 8b46526 commit 6db8d2e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
15 changes: 10 additions & 5 deletions psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@
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,
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -678,7 +683,7 @@ def create_time(self):

@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:
Expand Down Expand Up @@ -798,7 +803,7 @@ def cwd(self):
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
Expand All @@ -817,7 +822,7 @@ def _not_implemented(self):

# 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."""
Expand All @@ -828,7 +833,7 @@ def open_files(self):

# 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."""
Expand Down
4 changes: 3 additions & 1 deletion psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
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
Expand Down Expand Up @@ -1928,7 +1929,7 @@ def cpu_affinity_set(self, cpus):
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):
Expand Down Expand Up @@ -1971,6 +1972,7 @@ def ionice_set(self, ioclass, value):
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
Expand Down
3 changes: 2 additions & 1 deletion psutil/_pswindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
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:
Expand Down Expand Up @@ -928,7 +929,7 @@ def nice_set(self, value):
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)
Expand Down

0 comments on commit 6db8d2e

Please sign in to comment.