Skip to content

Commit

Permalink
Merge branch 'master' of github.com:giampaolo/psutil
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Jul 30, 2023
2 parents 1336977 + 962cb9a commit b61e4e8
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ I: 1598

N: Xuehai Pan
W: https://github.com/XuehaiPan
I: 1948
I: 1948, 2264

N: Saeed Rasooli
W: https://github.com/ilius
Expand Down
4 changes: 4 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ XXXX-XX-XX

- 2241_, [NetBSD]: can't compile On NetBSD 10.99.3/amd64. (patch by Thomas
Klausner)
- 2266_: if `Process`_ class is passed a very high PID, raise `NoSuchProcess`_
instead of OverflowError. (patch by Xuehai Pan)
- 2268_: ``bytes2human()`` utility function was unable to properly represent
negative values.

5.9.5
=====
Expand Down
5 changes: 3 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
| |downloads| |stars| |forks| |contributors| |coverage|
| |version| |py-versions| |packages| |license| |twitter| |tidelift|
| |github-actions-wheels| |github-actions-bsd| |appveyor| |doc|
| |version| |py-versions| |packages| |license|
| |github-actions-wheels| |github-actions-bsd| |appveyor| |doc| |twitter| |tidelift|
.. |downloads| image:: https://img.shields.io/pypi/dm/psutil.svg
:target: https://pepy.tech/project/psutil
Expand Down Expand Up @@ -488,6 +488,7 @@ Here's some I find particularly interesting:
- https://github.com/google/grr
- https://github.com/facebook/osquery/
- https://github.com/nicolargo/glances
- https://github.com/aristocratos/bpytop
- https://github.com/Jahaja/psdash
- https://github.com/ajenti/ajenti
- https://github.com/home-assistant/home-assistant/
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2544,7 +2544,7 @@ Bytes conversion
for i, s in enumerate(symbols):
prefix[s] = 1 << (i + 1) * 10
for s in reversed(symbols):
if n >= prefix[s]:
if abs(n) >= prefix[s]:
value = float(n) / prefix[s]
return '%.1f%s' % (value, s)
return "%sB" % n
Expand Down
8 changes: 8 additions & 0 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,14 @@ def _init(self, pid, _ignore_nsp=False):
if pid < 0:
raise ValueError('pid must be a positive integer (got %s)'
% pid)
try:
_psplatform.cext.check_pid_range(pid)
except OverflowError:
raise NoSuchProcess(
pid,
msg='process PID out of range (got %s)' % pid,
)

self._pid = pid
self._name = None
self._exe = None
Expand Down
2 changes: 1 addition & 1 deletion psutil/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ def bytes2human(n, format="%(value).1f%(symbol)s"):
for i, s in enumerate(symbols[1:]):
prefix[s] = 1 << (i + 1) * 10
for symbol in reversed(symbols[1:]):
if n >= prefix[symbol]:
if abs(n) >= prefix[symbol]:
value = float(n) / prefix[symbol]
return format % locals()
return format % dict(symbol=symbols[0], value=n)
Expand Down
1 change: 1 addition & 0 deletions psutil/_psutil_aix.c
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,7 @@ PsutilMethods[] =
{"net_if_stats", psutil_net_if_stats, METH_VARARGS},

// --- others
{"check_pid_range", psutil_check_pid_range, METH_VARARGS},
{"set_debug", psutil_set_debug, METH_VARARGS},

{NULL, NULL, 0, NULL}
Expand Down
1 change: 1 addition & 0 deletions psutil/_psutil_bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ static PyMethodDef mod_methods[] = {
{"sensors_cpu_temperature", psutil_sensors_cpu_temperature, METH_VARARGS},
#endif
// --- others
{"check_pid_range", psutil_check_pid_range, METH_VARARGS},
{"set_debug", psutil_set_debug, METH_VARARGS},

{NULL, NULL, 0, NULL}
Expand Down
21 changes: 21 additions & 0 deletions psutil/_psutil_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,27 @@ AccessDenied(const char *syscall) {
return NULL;
}

/*
* Raise OverflowError if Python int value overflowed when converting to pid_t.
* Raise ValueError if Python int value is negative.
* Otherwise, return None.
*/
PyObject *
psutil_check_pid_range(PyObject *self, PyObject *args) {
#ifdef PSUTIL_WINDOWS
DWORD pid;
#else
pid_t pid;
#endif

if (!PyArg_ParseTuple(args, _Py_PARSE_PID, &pid))
return NULL;
if (pid < 0) {
PyErr_SetString(PyExc_ValueError, "pid must be a positive integer");
return NULL;
}
Py_RETURN_NONE;
}

// Enable or disable PSUTIL_DEBUG messages.
PyObject *
Expand Down
1 change: 1 addition & 0 deletions psutil/_psutil_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ PyObject* PyErr_SetFromOSErrnoWithSyscall(const char *syscall);
// --- Global utils
// ====================================================================

PyObject* psutil_check_pid_range(PyObject *self, PyObject *args);
PyObject* psutil_set_debug(PyObject *self, PyObject *args);
int psutil_setup(void);

Expand Down
1 change: 1 addition & 0 deletions psutil/_psutil_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ static PyMethodDef mod_methods[] = {
// --- linux specific
{"linux_sysinfo", psutil_linux_sysinfo, METH_VARARGS},
// --- others
{"check_pid_range", psutil_check_pid_range, METH_VARARGS},
{"set_debug", psutil_set_debug, METH_VARARGS},

{NULL, NULL, 0, NULL}
Expand Down
1 change: 1 addition & 0 deletions psutil/_psutil_osx.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ static PyMethodDef mod_methods[] = {
{"virtual_mem", psutil_virtual_mem, METH_VARARGS},

// --- others
{"check_pid_range", psutil_check_pid_range, METH_VARARGS},
{"set_debug", psutil_set_debug, METH_VARARGS},

{NULL, NULL, 0, NULL}
Expand Down
1 change: 1 addition & 0 deletions psutil/_psutil_sunos.c
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,7 @@ PsutilMethods[] = {
{"users", psutil_users, METH_VARARGS},

// --- others
{"check_pid_range", psutil_check_pid_range, METH_VARARGS},
{"set_debug", psutil_set_debug, METH_VARARGS},

{NULL, NULL, 0, NULL}
Expand Down
1 change: 1 addition & 0 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ PsutilMethods[] = {
{"QueryDosDevice", psutil_QueryDosDevice, METH_VARARGS},

// --- others
{"check_pid_range", psutil_check_pid_range, METH_VARARGS},
{"set_debug", psutil_set_debug, METH_VARARGS},

{NULL, NULL, 0, NULL}
Expand Down
6 changes: 6 additions & 0 deletions psutil/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@

class TestSpecialMethods(PsutilTestCase):

def test_check_pid_range(self):
with self.assertRaises(OverflowError):
psutil._psplatform.cext.check_pid_range(2 ** 128)
with self.assertRaises(psutil.NoSuchProcess):
psutil.Process(2 ** 128)

def test_process__repr__(self, func=repr):
p = psutil.Process(self.spawn_testproc().pid)
r = func(p)
Expand Down

0 comments on commit b61e4e8

Please sign in to comment.