Skip to content

Commit

Permalink
#1040: add replacement for PyUnicode_DecodeFSDefaultAndSize on Python…
Browse files Browse the repository at this point in the history
… 2; also get rid of the pstuil_ prefix and use the original Python names
  • Loading branch information
giampaolo committed May 1, 2017
1 parent 043d573 commit 0cab2d1
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 49 deletions.
22 changes: 11 additions & 11 deletions psutil/_psutil_bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ psutil_proc_oneshot_info(PyObject *self, PyObject *args) {
#elif defined(PSUTIL_OPENBSD) || defined(PSUTIL_NETBSD)
sprintf(str, "%s", kp.p_comm);
#endif
py_name = psutil_PyUnicode_DecodeFSDefault(str);
py_name = PyUnicode_DecodeFSDefault(str);
if (! py_name) {
// Likely a decoding error. We don't want to fail the whole
// operation. The python module may retry with proc_name().
Expand Down Expand Up @@ -368,7 +368,7 @@ psutil_proc_name(PyObject *self, PyObject *args) {
#elif defined(PSUTIL_OPENBSD) || defined(PSUTIL_NETBSD)
sprintf(str, "%s", kp.p_comm);
#endif
return psutil_PyUnicode_DecodeFSDefault(str);
return PyUnicode_DecodeFSDefault(str);
}


Expand Down Expand Up @@ -499,7 +499,7 @@ psutil_proc_open_files(PyObject *self, PyObject *args) {
// XXX - it appears path is not exposed in the kinfo_file struct.
path = "";
#endif
py_path = psutil_PyUnicode_DecodeFSDefault(path);
py_path = PyUnicode_DecodeFSDefault(path);
if (! py_path)
goto error;
if (regular == 1) {
Expand Down Expand Up @@ -656,10 +656,10 @@ psutil_disk_partitions(PyObject *self, PyObject *args) {
if (flags & MNT_NODEVMTIME)
strlcat(opts, ",nodevmtime", sizeof(opts));
#endif
py_dev = psutil_PyUnicode_DecodeFSDefault(fs[i].f_mntfromname);
py_dev = PyUnicode_DecodeFSDefault(fs[i].f_mntfromname);
if (! py_dev)
goto error;
py_mountp = psutil_PyUnicode_DecodeFSDefault(fs[i].f_mntonname);
py_mountp = PyUnicode_DecodeFSDefault(fs[i].f_mntonname);
if (! py_mountp)
goto error;
py_tuple = Py_BuildValue("(OOss)",
Expand Down Expand Up @@ -812,13 +812,13 @@ psutil_users(PyObject *self, PyObject *args) {
while (fread(&ut, sizeof(ut), 1, fp) == 1) {
if (*ut.ut_name == '\0')
continue;
py_username = psutil_PyUnicode_DecodeFSDefault(ut.ut_name);
py_username = PyUnicode_DecodeFSDefault(ut.ut_name);
if (! py_username)
goto error;
py_tty = psutil_PyUnicode_DecodeFSDefault(ut.ut_line);
py_tty = PyUnicode_DecodeFSDefault(ut.ut_line);
if (! py_tty)
goto error;
py_hostname = psutil_PyUnicode_DecodeFSDefault(ut.ut_host);
py_hostname = PyUnicode_DecodeFSDefault(ut.ut_host);
if (! py_hostname)
goto error;
py_tuple = Py_BuildValue(
Expand Down Expand Up @@ -850,13 +850,13 @@ psutil_users(PyObject *self, PyObject *args) {
while ((utx = getutxent()) != NULL) {
if (utx->ut_type != USER_PROCESS)
continue;
py_username = psutil_PyUnicode_DecodeFSDefault(utx->ut_user);
py_username = PyUnicode_DecodeFSDefault(utx->ut_user);
if (! py_username)
goto error;
py_tty = psutil_PyUnicode_DecodeFSDefault(utx->ut_line);
py_tty = PyUnicode_DecodeFSDefault(utx->ut_line);
if (! py_tty)
goto error;
py_hostname = psutil_PyUnicode_DecodeFSDefault(utx->ut_host);
py_hostname = PyUnicode_DecodeFSDefault(utx->ut_host);
if (! py_hostname)
goto error;
py_tuple = Py_BuildValue(
Expand Down
23 changes: 8 additions & 15 deletions psutil/_psutil_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,19 @@ AccessDenied(void) {


/*
* Alias for PyUnicode_DecodeFSDefault which is not available
* on Python 2. On Python 2 we just return a plain byte string
* Backport of unicode FS APIs from Python 3.
* On Python 2 we just return a plain byte string
* which is never supposed to raise decoding errors.
* See: https://github.com/giampaolo/psutil/issues/1040
*/
#if PY_MAJOR_VERSION < 3
PyObject *
psutil_PyUnicode_DecodeFSDefault(char *s) {
#if PY_MAJOR_VERSION >= 3
return PyUnicode_DecodeFSDefault(s);
#else
return Py_BuildValue("s", s);
#endif
PyUnicode_DecodeFSDefault(char *s) {
return PyString_FromString(s);
}


PyObject*
psutil_PyUnicode_DecodeFSDefaultAndSize(char *s, Py_ssize_t size) {
#if PY_MAJOR_VERSION >= 3
return PyUnicode_DecodeFSDefaultAndSize(s, size);
#else
PyObject *
PyUnicode_DecodeFSDefaultAndSize(char *s, Py_ssize_t size) {
return PyString_FromStringAndSize(s, size);
#endif
}
#endif
6 changes: 4 additions & 2 deletions psutil/_psutil_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@

PyObject* AccessDenied(void);
PyObject* NoSuchProcess(void);
PyObject* psutil_PyUnicode_DecodeFSDefault(char *s);
PyObject* psutil_PyUnicode_DecodeFSDefaultAndSize(char *s, Py_ssize_t size);
#if PY_MAJOR_VERSION < 3
PyObject* PyUnicode_DecodeFSDefault(char *s);
PyObject* PyUnicode_DecodeFSDefaultAndSize(char *s, Py_ssize_t size);
#endif
16 changes: 8 additions & 8 deletions psutil/_psutil_osx.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ psutil_proc_kinfo_oneshot(PyObject *self, PyObject *args) {
if (psutil_get_kinfo_proc(pid, &kp) == -1)
return NULL;

py_name = psutil_PyUnicode_DecodeFSDefault(kp.kp_proc.p_comm);
py_name = PyUnicode_DecodeFSDefault(kp.kp_proc.p_comm);
if (! py_name) {
// Likely a decoding error. We don't want to fail the whole
// operation. The python module may retry with proc_name().
Expand Down Expand Up @@ -220,7 +220,7 @@ psutil_proc_name(PyObject *self, PyObject *args) {
return NULL;
if (psutil_get_kinfo_proc(pid, &kp) == -1)
return NULL;
return psutil_PyUnicode_DecodeFSDefault(kp.kp_proc.p_comm);
return PyUnicode_DecodeFSDefault(kp.kp_proc.p_comm);
}


Expand All @@ -241,7 +241,7 @@ psutil_proc_cwd(PyObject *self, PyObject *args) {
return NULL;
}

return psutil_PyUnicode_DecodeFSDefault(pathinfo.pvi_cdir.vip_path);
return PyUnicode_DecodeFSDefault(pathinfo.pvi_cdir.vip_path);
}


Expand All @@ -265,7 +265,7 @@ psutil_proc_exe(PyObject *self, PyObject *args) {
psutil_raise_for_pid(pid, "proc_pidpath() syscall failed");
return NULL;
}
return psutil_PyUnicode_DecodeFSDefault(buf);
return PyUnicode_DecodeFSDefault(buf);
}


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

py_path = psutil_PyUnicode_DecodeFSDefault(buf);
py_path = PyUnicode_DecodeFSDefault(buf);
if (! py_path)
goto error;
py_tuple = Py_BuildValue(
Expand Down Expand Up @@ -1141,7 +1141,7 @@ psutil_proc_open_files(PyObject *self, PyObject *args) {
// --- /errors checking

// --- construct python list
py_path = psutil_PyUnicode_DecodeFSDefault(vi.pvip.vip_path);
py_path = PyUnicode_DecodeFSDefault(vi.pvip.vip_path);
if (! py_path)
goto error;
py_tuple = Py_BuildValue(
Expand Down Expand Up @@ -1337,11 +1337,11 @@ psutil_proc_connections(PyObject *self, PyObject *args) {
Py_DECREF(py_tuple);
}
else if (family == AF_UNIX) {
py_laddr = psutil_PyUnicode_DecodeFSDefault(
py_laddr = PyUnicode_DecodeFSDefault(
si.psi.soi_proto.pri_un.unsi_addr.ua_sun.sun_path);
if (!py_laddr)
goto error;
py_raddr = psutil_PyUnicode_DecodeFSDefault(
py_raddr = PyUnicode_DecodeFSDefault(
si.psi.soi_proto.pri_un.unsi_caddr.ua_sun.sun_path);
if (!py_raddr)
goto error;
Expand Down
12 changes: 6 additions & 6 deletions psutil/arch/bsd/freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ psutil_get_cmdline(long pid) {
// separator
if (argsize > 0) {
while (pos < argsize) {
py_arg = psutil_PyUnicode_DecodeFSDefault(&argstr[pos]);
py_arg = PyUnicode_DecodeFSDefault(&argstr[pos]);
if (!py_arg)
goto error;
if (PyList_Append(py_retlist, py_arg))
Expand Down Expand Up @@ -288,7 +288,7 @@ psutil_proc_exe(PyObject *self, PyObject *args) {
if (error == -1) {
// see: https://github.com/giampaolo/psutil/issues/907
if (errno == ENOENT)
return psutil_PyUnicode_DecodeFSDefault("");
return PyUnicode_DecodeFSDefault("");
else
return PyErr_SetFromErrno(PyExc_OSError);
}
Expand All @@ -302,7 +302,7 @@ psutil_proc_exe(PyObject *self, PyObject *args) {
strcpy(pathname, "");
}

return psutil_PyUnicode_DecodeFSDefault(pathname);
return PyUnicode_DecodeFSDefault(pathname);
}


Expand Down Expand Up @@ -555,7 +555,7 @@ psutil_proc_cwd(PyObject *self, PyObject *args) {
for (i = 0; i < cnt; i++) {
kif = &freep[i];
if (kif->kf_fd == KF_FD_TYPE_CWD) {
py_path = psutil_PyUnicode_DecodeFSDefault(kif->kf_path);
py_path = PyUnicode_DecodeFSDefault(kif->kf_path);
if (!py_path)
goto error;
break;
Expand All @@ -567,7 +567,7 @@ psutil_proc_cwd(PyObject *self, PyObject *args) {
* as root we return an empty string instead of AccessDenied.
*/
if (py_path == NULL)
py_path = psutil_PyUnicode_DecodeFSDefault("");
py_path = PyUnicode_DecodeFSDefault("");
free(freep);
return py_path;

Expand Down Expand Up @@ -821,7 +821,7 @@ psutil_proc_memory_maps(PyObject *self, PyObject *args) {
path = kve->kve_path;
}

py_path = psutil_PyUnicode_DecodeFSDefault(path);
py_path = PyUnicode_DecodeFSDefault(path);
if (! py_path)
goto error;
py_tuple = Py_BuildValue("ssOiiii",
Expand Down
4 changes: 2 additions & 2 deletions psutil/arch/bsd/freebsd_socks.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ int psutil_gather_unix(int proto, PyObject *py_retlist) {
snprintf(path, sizeof(path), "%.*s",
(int)(sun->sun_len - (sizeof(*sun) - sizeof(sun->sun_path))),
sun->sun_path);
py_lpath = psutil_PyUnicode_DecodeFSDefault(path);
py_lpath = PyUnicode_DecodeFSDefault(path);
if (! py_lpath)
goto error;

Expand Down Expand Up @@ -604,7 +604,7 @@ psutil_proc_connections(PyObject *self, PyObject *args) {
(int)(sun->sun_len - (sizeof(*sun) - sizeof(sun->sun_path))),
sun->sun_path);

py_laddr = psutil_PyUnicode_DecodeFSDefault(path);
py_laddr = PyUnicode_DecodeFSDefault(path);
if (! py_laddr)
goto error;

Expand Down
2 changes: 1 addition & 1 deletion psutil/arch/bsd/netbsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ psutil_get_cmdline(pid_t pid) {
// separator
if (argsize > 0) {
while (pos < argsize) {
py_arg = psutil_PyUnicode_DecodeFSDefault(&argstr[pos]);
py_arg = PyUnicode_DecodeFSDefault(&argstr[pos]);
if (!py_arg)
goto error;
if (PyList_Append(py_retlist, py_arg))
Expand Down
4 changes: 2 additions & 2 deletions psutil/arch/bsd/netbsd_socks.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,10 @@ psutil_net_connections(PyObject *self, PyObject *args) {
strcpy(laddr, sun_src->sun_path);
strcpy(raddr, sun_dst->sun_path);
status = PSUTIL_CONN_NONE;
py_laddr = psutil_PyUnicode_DecodeFSDefault(laddr);
py_laddr = PyUnicode_DecodeFSDefault(laddr);
if (! py_laddr)
goto error;
py_raddr = psutil_PyUnicode_DecodeFSDefault(raddr);
py_raddr = PyUnicode_DecodeFSDefault(raddr);
if (! py_raddr)
goto error;
}
Expand Down
4 changes: 2 additions & 2 deletions psutil/arch/osx/process_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ psutil_get_cmdline(long pid) {
goto error;
while (arg_ptr < arg_end && nargs > 0) {
if (*arg_ptr++ == '\0') {
py_arg = psutil_PyUnicode_DecodeFSDefault(curr_arg);
py_arg = PyUnicode_DecodeFSDefault(curr_arg);
if (! py_arg)
goto error;
if (PyList_Append(py_retlist, py_arg))
Expand Down Expand Up @@ -289,7 +289,7 @@ psutil_get_environ(long pid) {
arg_ptr = s + 1;
}

py_ret = psutil_PyUnicode_DecodeFSDefaultAndSize(
py_ret = PyUnicode_DecodeFSDefaultAndSize(
procenv, arg_ptr - env_start + 1);
if (!py_ret) {
// XXX: don't want to free() this as per:
Expand Down

0 comments on commit 0cab2d1

Please sign in to comment.