Skip to content

Commit

Permalink
#715: don't crash at import time if cpu_times() fail for some reason.
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Dec 14, 2015
1 parent 82c4a1c commit d832788
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Bug tracker at https://github.com/giampaolo/psutil/issues
**Bug fixes**

- #714: [OpenBSD] virtual_memory().cached value was always set to 0.
- #715: don't crash at import time if cpu_times() fail for some reason.


3.3.0 - 2015-11-25
Expand Down
22 changes: 18 additions & 4 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import subprocess
import sys
import time
import traceback
try:
import pwd
except ImportError:
Expand Down Expand Up @@ -59,12 +60,12 @@
NIC_DUPLEX_UNKNOWN)

if sys.platform.startswith("linux"):
from . import _pslinux as _psplatform

# This is public API and it will be retrieved from _pslinux.py
# via sys.modules.
PROCFS_PATH = "/proc"

from . import _pslinux as _psplatform

from ._pslinux import (IOPRIO_CLASS_NONE, # NOQA
IOPRIO_CLASS_RT,
IOPRIO_CLASS_BE,
Expand Down Expand Up @@ -1422,8 +1423,21 @@ def cpu_times(percpu=False):
return _psplatform.per_cpu_times()


_last_cpu_times = cpu_times()
_last_per_cpu_times = cpu_times(percpu=True)
try:
_last_cpu_times = cpu_times()
except Exception:
# Don't want to crash at import time.
from collections import namedtuple
traceback.print_exc()
_last_cpu_times = namedtuple('scputimes', ['user', 'system', 'idle'])(
0.0, 0.0, 0.0)

try:
_last_per_cpu_times = cpu_times(percpu=True)
except Exception:
# Don't want to crash at import time.
_last_per_cpu_times = []
traceback.print_exc()


def cpu_percent(interval=None, percpu=False):
Expand Down
11 changes: 10 additions & 1 deletion psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import socket
import struct
import sys
import traceback
import warnings
from collections import defaultdict
from collections import namedtuple
Expand All @@ -23,6 +24,7 @@
from . import _psutil_linux as cext
from . import _psutil_posix as cext_posix
from ._common import isfile_strict
from ._common import memoize
from ._common import NIC_DUPLEX_FULL
from ._common import NIC_DUPLEX_HALF
from ._common import NIC_DUPLEX_UNKNOWN
Expand Down Expand Up @@ -149,6 +151,7 @@ def get_procfs_path():

# --- named tuples

@memoize
def set_scputimes_ntuple(procfs_path):
"""Return a namedtuple of variable fields depending on the
CPU times available on this Linux kernel version which may be:
Expand All @@ -173,7 +176,13 @@ def set_scputimes_ntuple(procfs_path):
return scputimes


scputimes = set_scputimes_ntuple('/proc')
try:
scputimes = set_scputimes_ntuple("/proc")
except Exception:
# Don't want to crash at import time.
traceback.print_exc()
scputimes = namedtuple('scputimes', 'user system idle')(0.0, 0.0, 0.0)


svmem = namedtuple(
'svmem', ['total', 'available', 'percent', 'used', 'free',
Expand Down

0 comments on commit d832788

Please sign in to comment.