Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Feb 12, 2020
1 parent 15a546e commit b5cf1f9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 44 deletions.
84 changes: 45 additions & 39 deletions psutil/_psutil_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ====================================================================
Expand All @@ -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
Expand Down Expand Up @@ -127,7 +171,6 @@ psutil_setup(void) {
// --- Windows
// ====================================================================


#ifdef PSUTIL_WINDOWS
#include <windows.h>

Expand All @@ -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) {
Expand Down
12 changes: 7 additions & 5 deletions psutil/_psutil_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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?).
Expand Down Expand Up @@ -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

0 comments on commit b5cf1f9

Please sign in to comment.