From 8646be534fd5a2bb1653b3c05e05cf443c853249 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Mon, 1 May 2017 00:04:45 +0200 Subject: [PATCH] #1040: provide an alias for PyUnicode_DecodeFSDefault which is not available on Python 2; also start to port the first C functions in FreeBSD --- psutil/_psutil_bsd.c | 13 ++----------- psutil/_psutil_common.c | 16 ++++++++++++++++ psutil/_psutil_common.h | 1 + psutil/arch/bsd/freebsd.c | 23 +++++------------------ psutil/arch/bsd/freebsd_socks.c | 6 +----- 5 files changed, 25 insertions(+), 34 deletions(-) diff --git a/psutil/_psutil_bsd.c b/psutil/_psutil_bsd.c index 75de731de..c158177f4 100644 --- a/psutil/_psutil_bsd.c +++ b/psutil/_psutil_bsd.c @@ -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(). @@ -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); } diff --git a/psutil/_psutil_common.c b/psutil/_psutil_common.c index c8d736e82..88b86202a 100644 --- a/psutil/_psutil_common.c +++ b/psutil/_psutil_common.c @@ -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 +} diff --git a/psutil/_psutil_common.h b/psutil/_psutil_common.h index 43021a72d..31d93fbde 100644 --- a/psutil/_psutil_common.h +++ b/psutil/_psutil_common.h @@ -8,3 +8,4 @@ PyObject* AccessDenied(void); PyObject* NoSuchProcess(void); +PyObject* psutil_PyUnicode_DecodeFSDefault(char *s); diff --git a/psutil/arch/bsd/freebsd.c b/psutil/arch/bsd/freebsd.c index 2188564f5..4e8ebdfac 100644 --- a/psutil/arch/bsd/freebsd.c +++ b/psutil/arch/bsd/freebsd.c @@ -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)) @@ -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); } @@ -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); } @@ -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; @@ -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; diff --git a/psutil/arch/bsd/freebsd_socks.c b/psutil/arch/bsd/freebsd_socks.c index 7d216280d..165acd74a 100644 --- a/psutil/arch/bsd/freebsd_socks.c +++ b/psutil/arch/bsd/freebsd_socks.c @@ -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;