Skip to content

Commit

Permalink
#910: [OSX / BSD] in case of error, psutil.pids() raised RuntimeError…
Browse files Browse the repository at this point in the history
… instead of the original OSError exception.
  • Loading branch information
giampaolo committed Oct 18, 2016
1 parent c7ddad2 commit b184c48
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Bug tracker at https://github.com/giampaolo/psutil/issues
instead of OSError and RuntimeError.
- #909: [OSX] Process open_files() and connections() methods may raise
OSError with no exception set if process is gone.
- #910: [OSX / BSD] in case of error, psutil.pids() raised RuntimeError instead
of the original OSError exception.
- #916: [OSX] fix many compilation warnings.
- #918: [NetBSD] all memory metrics were wrong.
- #921: psutil.Popen now defines a __del__ special method which calls the
Expand Down
9 changes: 7 additions & 2 deletions psutil/_psutil_bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,13 @@ psutil_pids(PyObject *self, PyObject *args) {
// TODO: RuntimeError is inappropriate here; we could return the
// original error instead.
if (psutil_get_proc_list(&proclist, &num_processes) != 0) {
PyErr_SetString(PyExc_RuntimeError,
"failed to retrieve process list");
if (errno != 0) {
PyErr_SetFromErrno(PyExc_OSError);
}
else {
PyErr_SetString(PyExc_RuntimeError,
"failed to retrieve process list");
}
goto error;
}

Expand Down
9 changes: 7 additions & 2 deletions psutil/_psutil_osx.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,13 @@ psutil_pids(PyObject *self, PyObject *args) {
return NULL;

if (psutil_get_proc_list(&proclist, &num_processes) != 0) {
PyErr_SetString(PyExc_RuntimeError,
"failed to retrieve process list.");
if (errno != 0) {
PyErr_SetFromErrno(PyExc_OSError);
}
else {
PyErr_SetString(PyExc_RuntimeError,
"failed to retrieve process list");
}
goto error;
}

Expand Down
6 changes: 2 additions & 4 deletions psutil/arch/bsd/freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,8 @@ psutil_get_proc_list(struct kinfo_proc **procList, size_t *procCount) {
int err;
struct kinfo_proc *result;
int done;
static const int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PROC, 0 };
// Declaring name as const requires us to cast it when passing it to
// sysctl because the prototype doesn't include the const modifier.
size_t length;
int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PROC, 0 };
size_t length;

assert( procList != NULL);
assert(*procList == NULL);
Expand Down
1 change: 1 addition & 0 deletions psutil/arch/bsd/netbsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ psutil_proc_num_fds(PyObject *self, PyObject *args) {

PyObject *
psutil_per_cpu_times(PyObject *self, PyObject *args) {
// XXX: why static?
static int maxcpus;
int mib[3];
int ncpu;
Expand Down
11 changes: 5 additions & 6 deletions psutil/arch/osx/process_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@
*/
int
psutil_get_proc_list(kinfo_proc **procList, size_t *procCount) {
// Declaring mib as const requires use of a cast since the
// sysctl prototype doesn't include the const modifier.
static const int mib3[3] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL };
size_t size, size2;
void *ptr;
int err, lim = 8; // some limit
int mib3[3] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL };
size_t size, size2;
void *ptr;
int err;
int lim = 8; // some limit

assert( procList != NULL);
assert(*procList == NULL);
Expand Down

0 comments on commit b184c48

Please sign in to comment.