Repository: giampaolo/psutil
Pull request #1493 (2019-04-23 20:18:58)

Fix cpu_freq
I have a Linux machine where:
- `/sys/devices/system/cpu/cpufreq/` exists but is empty.
- `/sys/devices/system/cpu/cpu0/` exists but doesn't have `cpufreq` in it.

In this case, use `/proc/cpuinfo`.


Patch:
diff --git a/CREDITS b/CREDITS
index 626a79a8..a2fca99b 100644
--- a/CREDITS
+++ b/CREDITS
@@ -62,7 +62,7 @@ I: 517, 607, 610, 1131, 1123, 1130, 1154, 1164, 1174, 1177, 1210, 1214, 1408,
 N: Alex Manuskin
 W: https://github.com/amanusk
 D: FreeBSD cpu_freq(), OSX temperatures, support for Linux temperatures.
-I: 1284, 1345, 1350, 1352, 1472, 1481.
+I: 1284, 1345, 1350, 1352, 1472, 1481, 1487.
 
 N: Jeff Tang
 W: https://github.com/mrjefftang
@@ -611,4 +611,4 @@ I: 1480
 N: Ammar Askar
 E: ammar@ammaraskar.com
 W: http://ammaraskar.com/
-I: 604, 1484
\ No newline at end of file
+I: 604, 1484
diff --git a/HISTORY.rst b/HISTORY.rst
index b9393d17..9a9df63c 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -20,6 +20,8 @@
 **Bug fixes**
 
 - 1223_: [Windows] boot_time() may return value on Windows XP.
+- 1456_: [Linux] cpu_freq() returns None instead of 0.0 when min/max not
+  available (patch by Alex Manuskin)
 - 1462_: [Linux] (tests) make  tests invariant to LANG setting (patch by
 - 1463_: cpu_distribution.py script was broken.
   Benjamin Drung)
@@ -35,6 +37,8 @@
   APIs. Different process methods were affected by this.
 - 1480_: [Windows] psutil.cpu_count(logical=False) could cause a crash due to
   fixed read violation.  (patch by Samer Masterson)
+- 1486_: [AIX, SunOS] AttributeError when interacting with Process methods
+  involved into oneshot() context.
 
 5.6.1
 =====
diff --git a/psutil/_psaix.py b/psutil/_psaix.py
index b0aefa99..174dac1e 100644
--- a/psutil/_psaix.py
+++ b/psutil/_psaix.py
@@ -7,6 +7,7 @@
 """AIX platform implementation."""
 
 import errno
+import functools
 import glob
 import os
 import re
@@ -322,7 +323,7 @@ def wrap_exceptions(fun):
     """Call callable into a try/except clause and translate ENOENT,
     EACCES and EPERM in NoSuchProcess or AccessDenied exceptions.
     """
-
+    @functools.wraps(fun)
     def wrapper(self, *args, **kwargs):
         try:
             return fun(self, *args, **kwargs)
diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py
index eccfeed2..ac45b54b 100644
--- a/psutil/_pslinux.py
+++ b/psutil/_pslinux.py
@@ -668,7 +668,7 @@ def cpu_stats():
         ctx_switches, interrupts, soft_interrupts, syscalls)
 
 
-if os.path.exists("/sys/devices/system/cpu/cpufreq") or \
+if os.path.exists("/sys/devices/system/cpu/cpufreq/policy0") or \
         os.path.exists("/sys/devices/system/cpu/cpu0/cpufreq"):
     def cpu_freq():
         """Return frequency metrics for all CPUs.
@@ -712,9 +712,15 @@ elif os.path.exists("/proc/cpuinfo"):
             for line in f:
                 if line.lower().startswith(b'cpu mhz'):
                     key, value = line.split(b'\t:', 1)
-                    ret.append(_common.scpufreq(float(value), None, None))
+                    ret.append(_common.scpufreq(float(value), 0.0, 0.0))
         return ret
 
+else:
+    def cpu_freq():
+        """Dummy implementation when none of the above files are present.
+        """
+        return []
+
 
 # =====================================================================
 # --- network
diff --git a/psutil/_pssunos.py b/psutil/_pssunos.py
index 47a18181..6d7fda85 100644
--- a/psutil/_pssunos.py
+++ b/psutil/_pssunos.py
@@ -5,6 +5,7 @@
 """Sun OS Solaris platform implementation."""
 
 import errno
+import functools
 import os
 import socket
 import subprocess
@@ -336,7 +337,7 @@ def wrap_exceptions(fun):
     """Call callable into a try/except clause and translate ENOENT,
     EACCES and EPERM in NoSuchProcess or AccessDenied exceptions.
     """
-
+    @functools.wraps(fun)
     def wrapper(self, *args, **kwargs):
         try:
             return fun(self, *args, **kwargs)
diff --git a/psutil/arch/solaris/v10/ifaddrs.c b/psutil/arch/solaris/v10/ifaddrs.c
index aedba84e..b741a6b9 100644
--- a/psutil/arch/solaris/v10/ifaddrs.c
+++ b/psutil/arch/solaris/v10/ifaddrs.c
@@ -120,5 +120,6 @@ error:
         free(ifc.lifc_buf);
     if (sd != -1)
         close(sd);
+    freeifaddrs(*ifap);
     return (-1);
 }


Test patch:
diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py
index e8745a75..4cdc9cbf 100755
--- a/psutil/tests/test_linux.py
+++ b/psutil/tests/test_linux.py
@@ -739,11 +739,11 @@ class TestSystemCPUFrequency(unittest.TestCase):
                 ret = psutil.cpu_freq()
                 assert ret
                 assert flags
-                self.assertIsNone(ret.min)
-                self.assertIsNone(ret.max)
+                self.assertEqual(ret.max, 0.0)
+                self.assertEqual(ret.min, 0.0)
                 for freq in psutil.cpu_freq(percpu=True):
-                    self.assertIsNone(freq.min)
-                    self.assertIsNone(freq.max)
+                    self.assertEqual(ret.max, 0.0)
+                    self.assertEqual(ret.min, 0.0)
         finally:
             reload_module(psutil._pslinux)
             reload_module(psutil)