Skip to content

Commit

Permalink
change NoSuchProcess and AccessDenied C exceptions signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Nov 14, 2017
1 parent c907d40 commit 99b757c
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 48 deletions.
2 changes: 1 addition & 1 deletion psutil/_psutil_aix.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ psutil_proc_num_ctx_switches(PyObject *self, PyObject *args) {

/* finished iteration without finding requested pid */
free(processes);
return NoSuchProcess();
return NoSuchProcess("");
}


Expand Down
14 changes: 8 additions & 6 deletions psutil/_psutil_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ PyUnicode_DecodeFSDefaultAndSize(char *s, Py_ssize_t size) {

/*
* Set OSError(errno=ESRCH, strerror="No such process") Python exception.
* If msg != "" the exception message will change in accordance.
*/
PyObject *
NoSuchProcess(void) {
NoSuchProcess(char *msg) {
PyObject *exc;
char *msg = strerror(ESRCH);
exc = PyObject_CallFunction(PyExc_OSError, "(is)", ESRCH, msg);
exc = PyObject_CallFunction(
PyExc_OSError, "(is)", ESRCH, strlen(msg) ? msg : strerror(ESRCH));
PyErr_SetObject(PyExc_OSError, exc);
Py_XDECREF(exc);
return NULL;
Expand All @@ -50,12 +51,13 @@ NoSuchProcess(void) {

/*
* Set OSError(errno=EACCES, strerror="Permission denied") Python exception.
* If msg != "" the exception message will change in accordance.
*/
PyObject *
AccessDenied(void) {
AccessDenied(char *msg) {
PyObject *exc;
char *msg = strerror(EACCES);
exc = PyObject_CallFunction(PyExc_OSError, "(is)", EACCES, msg);
exc = PyObject_CallFunction(
PyExc_OSError, "(is)", EACCES, strlen(msg) ? msg : strerror(EACCES));
PyErr_SetObject(PyExc_OSError, exc);
Py_XDECREF(exc);
return NULL;
Expand Down
4 changes: 2 additions & 2 deletions psutil/_psutil_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ PyObject* PyUnicode_DecodeFSDefault(char *s);
PyObject* PyUnicode_DecodeFSDefaultAndSize(char *s, Py_ssize_t size);
#endif

PyObject* AccessDenied(void);
PyObject* NoSuchProcess(void);
PyObject* AccessDenied(char *msg);
PyObject* NoSuchProcess(char *msg);

PyObject* psutil_set_testing(PyObject *self, PyObject *args);
void psutil_setup(void);
12 changes: 6 additions & 6 deletions psutil/_psutil_osx.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ psutil_proc_exe(PyObject *self, PyObject *args) {
ret = proc_pidpath((pid_t)pid, &buf, sizeof(buf));
if (ret == 0) {
if (pid == 0)
AccessDenied();
AccessDenied("");
else
psutil_raise_for_pid(pid, "proc_pidpath() syscall failed");
return NULL;
Expand Down Expand Up @@ -560,9 +560,9 @@ psutil_proc_memory_uss(PyObject *self, PyObject *args) {
err = task_for_pid(mach_task_self(), (pid_t)pid, &task);
if (err != KERN_SUCCESS) {
if (psutil_pid_exists(pid) == 0)
NoSuchProcess();
NoSuchProcess("");
else
AccessDenied();
AccessDenied("");
return NULL;
}

Expand Down Expand Up @@ -1007,9 +1007,9 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
err = task_for_pid(mach_task_self(), (pid_t)pid, &task);
if (err != KERN_SUCCESS) {
if (psutil_pid_exists(pid) == 0)
NoSuchProcess();
NoSuchProcess("");
else
AccessDenied();
AccessDenied("");
goto error;
}

Expand All @@ -1019,7 +1019,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
if (err != KERN_SUCCESS) {
// errcode 4 is "invalid argument" (access denied)
if (err == 4) {
AccessDenied();
AccessDenied("");
}
else {
// otherwise throw a runtime error with appropriate error code
Expand Down
2 changes: 1 addition & 1 deletion psutil/_psutil_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ psutil_raise_for_pid(long pid, char *msg) {
return 0;
}
if (psutil_pid_exists(pid) == 0)
NoSuchProcess();
NoSuchProcess("");
else
PyErr_SetString(PyExc_RuntimeError, msg);
return 0;
Expand Down
2 changes: 1 addition & 1 deletion psutil/_psutil_sunos.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ psutil_proc_environ(PyObject *self, PyObject *args) {
goto error;

if (! info.pr_envp) {
AccessDenied();
AccessDenied("");
goto error;
}

Expand Down
28 changes: 14 additions & 14 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,13 @@ psutil_proc_kill(PyObject *self, PyObject *args) {
if (! PyArg_ParseTuple(args, "l", &pid))
return NULL;
if (pid == 0)
return AccessDenied();
return AccessDenied("");

hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
if (hProcess == NULL) {
if (GetLastError() == ERROR_INVALID_PARAMETER) {
// see https://github.com/giampaolo/psutil/issues/24
NoSuchProcess();
NoSuchProcess("");
}
else {
PyErr_SetFromWindowsErr(0);
Expand Down Expand Up @@ -379,7 +379,7 @@ psutil_proc_wait(PyObject *self, PyObject *args) {
if (! PyArg_ParseTuple(args, "ll", &pid, &timeout))
return NULL;
if (pid == 0)
return AccessDenied();
return AccessDenied("");

hProcess = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,
FALSE, pid);
Expand Down Expand Up @@ -442,7 +442,7 @@ psutil_proc_cpu_times(PyObject *self, PyObject *args) {
if (GetLastError() == ERROR_ACCESS_DENIED) {
// usually means the process has died so we throw a NoSuchProcess
// here
return NoSuchProcess();
return NoSuchProcess("");
}
else {
return PyErr_SetFromWindowsErr(0);
Expand Down Expand Up @@ -496,7 +496,7 @@ psutil_proc_create_time(PyObject *self, PyObject *args) {
if (GetLastError() == ERROR_ACCESS_DENIED) {
// usually means the process has died so we throw a
// NoSuchProcess here
return NoSuchProcess();
return NoSuchProcess("");
}
else {
return PyErr_SetFromWindowsErr(0);
Expand All @@ -515,7 +515,7 @@ psutil_proc_create_time(PyObject *self, PyObject *args) {
CloseHandle(hProcess);
if (ret != 0) {
if (exitCode != STILL_ACTIVE)
return NoSuchProcess();
return NoSuchProcess("");
}
else {
// Ignore access denied as it means the process is still alive.
Expand Down Expand Up @@ -629,7 +629,7 @@ psutil_proc_cmdline(PyObject *self, PyObject *args) {

pid_return = psutil_pid_is_running(pid);
if (pid_return == 0)
return NoSuchProcess();
return NoSuchProcess("");
if (pid_return == -1)
return NULL;

Expand All @@ -652,7 +652,7 @@ psutil_proc_environ(PyObject *self, PyObject *args) {

pid_return = psutil_pid_is_running(pid);
if (pid_return == 0)
return NoSuchProcess();
return NoSuchProcess("");
if (pid_return == -1)
return NULL;

Expand Down Expand Up @@ -717,7 +717,7 @@ psutil_proc_name(PyObject *self, PyObject *args) {
}

CloseHandle(hSnapShot);
NoSuchProcess();
NoSuchProcess("");
return NULL;
}

Expand Down Expand Up @@ -1048,7 +1048,7 @@ psutil_proc_cwd(PyObject *self, PyObject *args) {

pid_return = psutil_pid_is_running(pid);
if (pid_return == 0)
return NoSuchProcess();
return NoSuchProcess("");
if (pid_return == -1)
return NULL;

Expand All @@ -1067,7 +1067,7 @@ psutil_proc_suspend_or_resume(DWORD pid, int suspend) {
THREADENTRY32 te32 = {0};

if (pid == 0) {
AccessDenied();
AccessDenied("");
return FALSE;
}

Expand Down Expand Up @@ -1169,13 +1169,13 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
if (pid == 0) {
// raise AD instead of returning 0 as procexp is able to
// retrieve useful information somehow
AccessDenied();
AccessDenied("");
goto error;
}

pid_return = psutil_pid_is_running(pid);
if (pid_return == 0) {
NoSuchProcess();
NoSuchProcess("");
goto error;
}
if (pid_return == -1)
Expand Down Expand Up @@ -1566,7 +1566,7 @@ psutil_net_connections(PyObject *self, PyObject *args) {
pid_return = psutil_pid_is_running(pid);
if (pid_return == 0) {
_psutil_conn_decref_objs();
return NoSuchProcess();
return NoSuchProcess("");
}
else if (pid_return == -1) {
_psutil_conn_decref_objs();
Expand Down
8 changes: 4 additions & 4 deletions psutil/arch/freebsd/specific.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ psutil_kinfo_proc(const pid_t pid, struct kinfo_proc *proc) {

// sysctl stores 0 in the size if we can't find the process information.
if (size == 0) {
NoSuchProcess();
NoSuchProcess("");
return -1;
}
return 0;
Expand Down Expand Up @@ -297,7 +297,7 @@ psutil_proc_exe(PyObject *self, PyObject *args) {
if (ret == -1)
return NULL;
else if (ret == 0)
return NoSuchProcess();
return NoSuchProcess("");
else
strcpy(pathname, "");
}
Expand Down Expand Up @@ -354,7 +354,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
goto error;
}
if (size == 0) {
NoSuchProcess();
NoSuchProcess("");
goto error;
}

Expand All @@ -370,7 +370,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
goto error;
}
if (size == 0) {
NoSuchProcess();
NoSuchProcess("");
goto error;
}

Expand Down
8 changes: 4 additions & 4 deletions psutil/arch/netbsd/specific.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ psutil_kinfo_proc(pid_t pid, kinfo_proc *proc) {
}
// sysctl stores 0 in the size if we can't find the process information.
if (size == 0) {
NoSuchProcess();
NoSuchProcess("");
return -1;
}
return 0;
Expand Down Expand Up @@ -157,7 +157,7 @@ psutil_proc_exe(PyObject *self, PyObject *args) {
if (ret == -1)
return NULL;
else if (ret == 0)
return NoSuchProcess();
return NoSuchProcess("");
else
strcpy(pathname, "");
}
Expand Down Expand Up @@ -209,7 +209,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
goto error;
}
if (size == 0) {
NoSuchProcess();
NoSuchProcess("");
goto error;
}

Expand All @@ -226,7 +226,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
goto error;
}
if (size == 0) {
NoSuchProcess();
NoSuchProcess("");
goto error;
}

Expand Down
6 changes: 3 additions & 3 deletions psutil/arch/openbsd/specific.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ psutil_kinfo_proc(pid_t pid, struct kinfo_proc *proc) {
}
// sysctl stores 0 in the size if we can't find the process information.
if (size == 0) {
NoSuchProcess();
NoSuchProcess("");
return -1;
}
return 0;
Expand Down Expand Up @@ -242,7 +242,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
kd = kvm_openfiles(0, 0, 0, O_RDONLY, errbuf);
if (! kd) {
if (strstr(errbuf, "Permission denied") != NULL)
AccessDenied();
AccessDenied("");
else
PyErr_Format(PyExc_RuntimeError, "kvm_openfiles() syscall failed");
goto error;
Expand All @@ -253,7 +253,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) {
sizeof(*kp), &nentries);
if (! kp) {
if (strstr(errbuf, "Permission denied") != NULL)
AccessDenied();
AccessDenied("");
else
PyErr_Format(PyExc_RuntimeError, "kvm_getprocs() syscall failed");
goto error;
Expand Down
6 changes: 3 additions & 3 deletions psutil/arch/osx/process_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ psutil_get_cmdline(long pid) {
// In case of zombie process we'll get EINVAL. We translate it
// to NSP and _psosx.py will translate it to ZP.
if ((errno == EINVAL) && (psutil_pid_exists(pid)))
NoSuchProcess();
NoSuchProcess("");
else
PyErr_SetFromErrno(PyExc_OSError);
goto error;
Expand Down Expand Up @@ -238,7 +238,7 @@ psutil_get_environ(long pid) {
// In case of zombie process we'll get EINVAL. We translate it
// to NSP and _psosx.py will translate it to ZP.
if ((errno == EINVAL) && (psutil_pid_exists(pid)))
NoSuchProcess();
NoSuchProcess("");
else
PyErr_SetFromErrno(PyExc_OSError);
goto error;
Expand Down Expand Up @@ -338,7 +338,7 @@ psutil_get_kinfo_proc(long pid, struct kinfo_proc *kp) {

// sysctl succeeds but len is zero, happens when process has gone away
if (len == 0) {
NoSuchProcess();
NoSuchProcess("");
return -1;
}
return 0;
Expand Down
6 changes: 3 additions & 3 deletions psutil/arch/windows/process_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ psutil_check_phandle(HANDLE hProcess, DWORD pid) {
if (ret == 1)
return hProcess;
else if (ret == 0)
return NoSuchProcess();
return NoSuchProcess("");
else if (ret == -1)
return PyErr_SetFromWindowsErr(0);
else // -2
Expand All @@ -277,7 +277,7 @@ psutil_handle_from_pid_waccess(DWORD pid, DWORD dwDesiredAccess) {

if (pid == 0) {
// otherwise we'd get NoSuchProcess
return AccessDenied();
return AccessDenied("");
}

hProcess = OpenProcess(dwDesiredAccess, FALSE, pid);
Expand Down Expand Up @@ -959,7 +959,7 @@ psutil_get_proc_info(DWORD pid, PSYSTEM_PROCESS_INFORMATION *retProcess,
}
} while ( (process = PSUTIL_NEXT_PROCESS(process)) );

NoSuchProcess();
NoSuchProcess("");
goto error;

error:
Expand Down

0 comments on commit 99b757c

Please sign in to comment.