diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2020-02-11 11:36:45 -0800 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2020-02-11 11:36:45 -0800 |
commit | 6e6ed6b9298279b2c026795d3fa5e592ca6adcf0 (patch) | |
tree | 3193b0f4298b0cbf5a827d7999f7ecf2286ef2d6 | |
parent | 1ef6fc5843bf76c4ee84c1eb20e56cbe124d2d29 (diff) | |
download | psutil-6e6ed6b9298279b2c026795d3fa5e592ca6adcf0.tar.gz |
replace PyErr_SetFromWindowsErrWithFilename missing on PYPY
-rw-r--r-- | psutil/_psutil_common.c | 45 | ||||
-rw-r--r-- | psutil/_psutil_common.h | 7 |
2 files changed, 41 insertions, 11 deletions
diff --git a/psutil/_psutil_common.c b/psutil/_psutil_common.c index b1b8b403..77a4ffc7 100644 --- a/psutil/_psutil_common.c +++ b/psutil/_psutil_common.c @@ -20,17 +20,50 @@ int PSUTIL_TESTING = 0; // ==================================================================== -// --- Python functions and backward compatibility +// --- Backward compatibility with missing Python.h APIs // ==================================================================== +// PyPy on Windows #if defined(PYPY_VERSION) && !defined(PyErr_SetFromWindowsErrWithFilename) PyObject * -PyErr_SetFromWindowsErrWithFilename(int ierr, const char *filename) { - PyErr_SetFromWindowsErr(ierr); +PyErr_SetFromWindowsErrWithFilename(int winerr, const char *filename) { + PyObject *py_exc = NULL; + PyObject *py_winerr = NULL; + + if (winerr == 0) + winerr = GetLastError(); + if (filename == NULL) { + py_exc = PyObject_CallFunction(PyExc_OSError, "(is)", winerr, + strerror(winerr)); + } + else { + py_exc = PyObject_CallFunction(PyExc_OSError, "(iss)", winerr, + strerror(winerr), filename); + } + if (py_exc == NULL) + return NULL; + + py_winerr = Py_BuildValue("i", winerr); + if (py_winerr == NULL) + goto error; + if (PyObject_SetAttrString(py_exc, "winerror", py_winerr) != 0) + goto error; + PyErr_SetObject(PyExc_OSError, py_exc); + Py_XDECREF(py_exc); + return NULL; + +error: + printf("err\n "); + Py_XDECREF(py_exc); + Py_XDECREF(py_winerr); return NULL; } #endif +// ==================================================================== +// --- Custom exceptions +// ==================================================================== + /* * Same as PyErr_SetFromErrno(0) but adds the syscall to the exception * message. @@ -52,11 +85,6 @@ PyErr_SetFromOSErrnoWithSyscall(const char *syscall) { return NULL; } - -// ==================================================================== -// --- Custom exceptions -// ==================================================================== - /* * Set OSError(errno=ESRCH, strerror="No such process (originated from") * Python exception. @@ -141,6 +169,7 @@ psutil_setup(void) { // --- Windows // ==================================================================== + #ifdef PSUTIL_WINDOWS #include <windows.h> diff --git a/psutil/_psutil_common.h b/psutil/_psutil_common.h index 9f02eeb5..e1ad411f 100644 --- a/psutil/_psutil_common.h +++ b/psutil/_psutil_common.h @@ -16,7 +16,7 @@ extern int PSUTIL_DEBUG; static const int PSUTIL_CONN_NONE = 128; // ==================================================================== -// --- Python functions and backward compatibility +// --- Backward compatibility with missing Python.h APIs // ==================================================================== #if PY_MAJOR_VERSION < 3 @@ -28,7 +28,8 @@ static const int PSUTIL_CONN_NONE = 128; #endif #if defined(PYPY_VERSION) && !defined(PyErr_SetFromWindowsErrWithFilename) -PyObject *PyErr_SetFromWindowsErrWithFilename(int ierr, const char *filename); + PyObject *PyErr_SetFromWindowsErrWithFilename(int ierr, + const char *filename); #endif // --- _Py_PARSE_PID @@ -64,7 +65,7 @@ PyObject *PyErr_SetFromWindowsErrWithFilename(int ierr, const char *filename); #endif #endif -// Python 2 or PyPy +// Python 2 or PyPy on Windows #ifndef PyLong_FromPid #if ((SIZEOF_PID_T == SIZEOF_INT) || (SIZEOF_PID_T == SIZEOF_LONG)) #if PY_MAJOR_VERSION >= 3 |