summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2020-02-12 09:28:21 -0800
committerGiampaolo Rodola <g.rodola@gmail.com>2020-02-12 09:28:21 -0800
commitb5cf1f92f01b644067b7daf6011df50fda3be3f6 (patch)
tree6092834e85b85dafd3964e8d3700c6c890764286
parent15a546e0376029d69684875adbb8c9eb2c44af8a (diff)
downloadpsutil-b5cf1f92f01b644067b7daf6011df50fda3be3f6.tar.gz
refactoring
-rw-r--r--psutil/_psutil_common.c84
-rw-r--r--psutil/_psutil_common.h12
2 files changed, 52 insertions, 44 deletions
diff --git a/psutil/_psutil_common.c b/psutil/_psutil_common.c
index b8c6b5e5..028e48e0 100644
--- a/psutil/_psutil_common.c
+++ b/psutil/_psutil_common.c
@@ -17,6 +17,50 @@ int PSUTIL_DEBUG = 0;
int PSUTIL_TESTING = 0;
// PSUTIL_CONN_NONE
+
+// ====================================================================
+// --- Backward compatibility with missing Python.h APIs
+// ====================================================================
+
+// PyPy on Windows
+#if defined(PSUTIL_WINDOWS) && \
+ defined(PYPY_VERSION) && \
+ !defined(PyErr_SetFromWindowsErrWithFilename)
+PyObject *
+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:
+ Py_XDECREF(py_exc);
+ Py_XDECREF(py_winerr);
+ return NULL;
+}
+#endif // PYPY on Windows
+
+
// ====================================================================
// --- Custom exceptions
// ====================================================================
@@ -29,7 +73,7 @@ PyObject *
PyErr_SetFromOSErrnoWithSyscall(const char *syscall) {
char fullmsg[1024];
-#ifdef _WIN32
+#ifdef PSUTIL_WINDOWS
sprintf(fullmsg, "(originated from %s)", syscall);
PyErr_SetFromWindowsErrWithFilename(GetLastError(), fullmsg);
#else
@@ -127,7 +171,6 @@ psutil_setup(void) {
// --- Windows
// ====================================================================
-
#ifdef PSUTIL_WINDOWS
#include <windows.h>
@@ -144,43 +187,6 @@ CRITICAL_SECTION PSUTIL_CRITICAL_SECTION;
#define WIN32_FROM_NTSTATUS(Status) (((ULONG)(Status)) & 0xffff)
-// PyPy on Windows
-#if defined(PYPY_VERSION) && !defined(PyErr_SetFromWindowsErrWithFilename)
-PyObject *
-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:
- Py_XDECREF(py_exc);
- Py_XDECREF(py_winerr);
- return NULL;
-}
-#endif
-
-
// A wrapper around GetModuleHandle and GetProcAddress.
PVOID
psutil_GetProcAddress(LPCSTR libname, LPCSTR procname) {
diff --git a/psutil/_psutil_common.h b/psutil/_psutil_common.h
index b072e357..bb26c922 100644
--- a/psutil/_psutil_common.h
+++ b/psutil/_psutil_common.h
@@ -27,6 +27,13 @@ static const int PSUTIL_CONN_NONE = 128;
#define PyUnicode_DecodeFSDefaultAndSize PyString_FromStringAndSize
#endif
+#if defined(PSUTIL_WINDOWS) && \
+ defined(PYPY_VERSION) && \
+ !defined(PyErr_SetFromWindowsErrWithFilename)
+ PyObject *PyErr_SetFromWindowsErrWithFilename(int ierr,
+ const char *filename);
+#endif
+
// --- _Py_PARSE_PID
// SIZEOF_INT|LONG is missing on Linux + PyPy (only?).
@@ -127,9 +134,4 @@ int psutil_setup(void);
PVOID psutil_GetProcAddress(LPCSTR libname, LPCSTR procname);
PVOID psutil_GetProcAddressFromLib(LPCSTR libname, LPCSTR procname);
PVOID psutil_SetFromNTStatusErr(NTSTATUS Status, const char *syscall);
-
- #if defined(PYPY_VERSION) && !defined(PyErr_SetFromWindowsErrWithFilename)
- PyObject *PyErr_SetFromWindowsErrWithFilename(int ierr,
- const char *filename);
- #endif
#endif