summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2023-03-27 23:44:46 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2023-03-27 23:44:46 +0200
commitf2216559ffa3e48dfccda331f8951a906a32c771 (patch)
treef0063119a8ccdde83e7124434bee4d06955a39e0
parent64b4318bce1a2ba8dcc0b8f7cbaefeb029c12419 (diff)
downloadpsutil-f2216559ffa3e48dfccda331f8951a906a32c771.tar.gz
When raising warning, always use stacklevel=2
B028: No explicit stacklevel argument found. The warn method from the warnings module uses a stacklevel of 1 by default. This will only show a stack trace for the line on which the warn method is called. It is therefore recommended to use a stacklevel of 2 or greater to provide more information to the user. Signed-off-by: Giampaolo Rodola <g.rodola@gmail.com>
-rw-r--r--psutil/_common.py2
-rw-r--r--psutil/_pslinux.py6
-rw-r--r--psutil/tests/__init__.py4
3 files changed, 6 insertions, 6 deletions
diff --git a/psutil/_common.py b/psutil/_common.py
index 90e76c6c..74ae2557 100644
--- a/psutil/_common.py
+++ b/psutil/_common.py
@@ -372,7 +372,7 @@ if PY3:
if isinstance(__builtins__, dict): # cpython
exec_ = __builtins__["exec"]
else: # pypy
- exec_ = getattr(__builtins__, "exec")
+ exec_ = getattr(__builtins__, "exec") # noqa
exec_("""def raise_from(value, from_value):
try:
diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py
index 33ee1b14..11e62944 100644
--- a/psutil/_pslinux.py
+++ b/psutil/_pslinux.py
@@ -510,7 +510,7 @@ def virtual_memory():
msg = "%s memory stats couldn't be determined and %s set to 0" % (
", ".join(missing_fields),
"was" if len(missing_fields) == 1 else "were")
- warnings.warn(msg, RuntimeWarning)
+ warnings.warn(msg, RuntimeWarning, stacklevel=2)
return svmem(total, avail, percent, used, free,
active, inactive, buffers, cached, shared, slab)
@@ -544,7 +544,7 @@ def swap_memory():
# see https://github.com/giampaolo/psutil/issues/722
msg = "'sin' and 'sout' swap memory stats couldn't " \
"be determined and were set to 0 (%s)" % str(err)
- warnings.warn(msg, RuntimeWarning)
+ warnings.warn(msg, RuntimeWarning, stacklevel=2)
sin = sout = 0
else:
with f:
@@ -564,7 +564,7 @@ def swap_memory():
# https://github.com/giampaolo/psutil/issues/313
msg = "'sin' and 'sout' swap memory stats couldn't " \
"be determined and were set to 0"
- warnings.warn(msg, RuntimeWarning)
+ warnings.warn(msg, RuntimeWarning, stacklevel=2)
sin = sout = 0
return _common.sswap(total, used, free, percent, sin, sout)
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py
index e3766eea..cdc8b671 100644
--- a/psutil/tests/__init__.py
+++ b/psutil/tests/__init__.py
@@ -248,7 +248,7 @@ def _get_py_exe():
# Let's use the base python in this case.
base = getattr(sys, "_base_executable", None)
if WINDOWS and sys.version_info >= (3, 7) and base is not None:
- # We need to set __PYVENV_LAUNCHER__ to sys.executable for the
+ # We need to set __PYVENV_LAUNCHER__ to sys.executable for the
# base python executable to know about the environment.
env["__PYVENV_LAUNCHER__"] = sys.executable
return base, env
@@ -1737,7 +1737,7 @@ def import_module_by_path(path):
def warn(msg):
"""Raise a warning msg."""
- warnings.warn(msg, UserWarning)
+ warnings.warn(msg, UserWarning, stacklevel=2)
def is_namedtuple(x):