Skip to content

Commit

Permalink
#1040: provide an alias for PyUnicode_DecodeFSDefault which is not av…
Browse files Browse the repository at this point in the history
…ailable on Python 2; also start to port the first C functions in FreeBSD
  • Loading branch information
giampaolo committed Apr 30, 2017
1 parent a3e1b41 commit 8646be5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 34 deletions.
13 changes: 2 additions & 11 deletions psutil/_psutil_bsd.c
Expand Up @@ -215,11 +215,7 @@ psutil_proc_oneshot_info(PyObject *self, PyObject *args) {
#elif defined(PSUTIL_OPENBSD) || defined(PSUTIL_NETBSD)
sprintf(str, "%s", kp.p_comm);
#endif
#if PY_MAJOR_VERSION >= 3
py_name = PyUnicode_DecodeFSDefault(str);
#else
py_name = Py_BuildValue("s", str);
#endif
py_name = psutil_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 @@ -372,12 +368,7 @@ psutil_proc_name(PyObject *self, PyObject *args) {
#elif defined(PSUTIL_OPENBSD) || defined(PSUTIL_NETBSD)
sprintf(str, "%s", kp.p_comm);
#endif

#if PY_MAJOR_VERSION >= 3
return PyUnicode_DecodeFSDefault(str);
#else
return Py_BuildValue("s", str);
#endif
return psutil_PyUnicode_DecodeFSDefault(str);
}


Expand Down
16 changes: 16 additions & 0 deletions psutil/_psutil_common.c
Expand Up @@ -34,3 +34,19 @@ AccessDenied(void) {
Py_XDECREF(exc);
return NULL;
}


/*
* Alias for PyUnicode_DecodeFSDefault which is not available
* on Python 2. 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
*/
PyObject *
psutil_PyUnicode_DecodeFSDefault(char *s) {
#if PY_MAJOR_VERSION >= 3
return PyUnicode_DecodeFSDefault(s);
#else
return Py_BuildValue("s", s);
#endif
}
1 change: 1 addition & 0 deletions psutil/_psutil_common.h
Expand Up @@ -8,3 +8,4 @@

PyObject* AccessDenied(void);
PyObject* NoSuchProcess(void);
PyObject* psutil_PyUnicode_DecodeFSDefault(char *s);
23 changes: 5 additions & 18 deletions psutil/arch/bsd/freebsd.c
Expand Up @@ -238,11 +238,7 @@ psutil_get_cmdline(long pid) {
// separator
if (argsize > 0) {
while (pos < argsize) {
#if PY_MAJOR_VERSION >= 3
py_arg = PyUnicode_DecodeFSDefault(&argstr[pos]);
#else
py_arg = Py_BuildValue("s", &argstr[pos]);
#endif
py_arg = psutil_PyUnicode_DecodeFSDefault(&argstr[pos]);
if (!py_arg)
goto error;
if (PyList_Append(py_retlist, py_arg))
Expand Down Expand Up @@ -292,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 Py_BuildValue("s", "");
return psutil_PyUnicode_DecodeFSDefault("");
else
return PyErr_SetFromErrno(PyExc_OSError);
}
Expand All @@ -306,12 +302,7 @@ psutil_proc_exe(PyObject *self, PyObject *args) {
strcpy(pathname, "");
}

#if PY_MAJOR_VERSION >= 3
return PyUnicode_DecodeFSDefault(pathname);
#else
return Py_BuildValue("s", pathname);
#endif

return psutil_PyUnicode_DecodeFSDefault(pathname);
}


Expand Down Expand Up @@ -564,11 +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) {
#if PY_MAJOR_VERSION >= 3
py_path = PyUnicode_DecodeFSDefault(kif->kf_path);
#else
py_path = Py_BuildValue("s", kif->kf_path);
#endif
py_path = psutil_PyUnicode_DecodeFSDefault(kif->kf_path);
if (!py_path)
goto error;
break;
Expand All @@ -580,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 = Py_BuildValue("s", "");
py_path = psutil_PyUnicode_DecodeFSDefault("");
free(freep);
return py_path;

Expand Down
6 changes: 1 addition & 5 deletions psutil/arch/bsd/freebsd_socks.c
Expand Up @@ -596,11 +596,7 @@ psutil_proc_connections(PyObject *self, PyObject *args) {
(int)(sun->sun_len - (sizeof(*sun) - sizeof(sun->sun_path))),
sun->sun_path);

#if PY_MAJOR_VERSION >= 3
py_laddr = PyUnicode_DecodeFSDefault(path);
#else
py_laddr = Py_BuildValue("s", path);
#endif
py_laddr = psutil_PyUnicode_DecodeFSDefault(path);
if (! py_laddr)
goto error;

Expand Down

0 comments on commit 8646be5

Please sign in to comment.