summaryrefslogtreecommitdiff
path: root/psutil/_psutil_common.h
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2020-01-29 00:08:14 +0100
committerGitHub <noreply@github.com>2020-01-29 00:08:14 +0100
commit994c429c010049a6a0556ce3b0d1af1f86f27867 (patch)
tree0042fbc7c863f2cc1ff3cd81c29d6450f0af6a8e /psutil/_psutil_common.h
parent0e8e5a983ac7e346224cfaa45856738628c1bdc5 (diff)
downloadpsutil-994c429c010049a6a0556ce3b0d1af1f86f27867.tar.gz
Properly handle PID type in C (#1672)
Diffstat (limited to 'psutil/_psutil_common.h')
-rw-r--r--psutil/_psutil_common.h39
1 files changed, 38 insertions, 1 deletions
diff --git a/psutil/_psutil_common.h b/psutil/_psutil_common.h
index 92a98b9c..d45cf56b 100644
--- a/psutil/_psutil_common.h
+++ b/psutil/_psutil_common.h
@@ -23,7 +23,43 @@ static const int PSUTIL_CONN_NONE = 128;
PyObject* PyUnicode_DecodeFSDefault(char *s);
PyObject* PyUnicode_DecodeFSDefaultAndSize(char *s, Py_ssize_t size);
#endif
-PyObject* PyErr_SetFromOSErrnoWithSyscall(const char *syscall);
+
+// Python 2: SIZEOF_PID_T not defined but _getpid() returns an int.
+#if defined(PSUTIL_WINDOWS) && !defined(SIZEOF_PID_T)
+ #define SIZEOF_PID_T SIZEOF_INT
+#endif
+
+#if !defined(_Py_PARSE_PID) || PY_MAJOR_VERSION < 3
+ #if !defined(SIZEOF_PID_T) || !defined(SIZEOF_INT) || !defined(SIZEOF_LONG)
+ #error "missing SIZEOF* definition"
+ #endif
+#endif
+
+// _Py_PARSE_PID is Python 3 only, but since it's private make sure it's
+// always present.
+#ifndef _Py_PARSE_PID
+ #if SIZEOF_PID_T == SIZEOF_INT
+ #define _Py_PARSE_PID "i"
+ #elif SIZEOF_PID_T == SIZEOF_LONG
+ #define _Py_PARSE_PID "l"
+ #elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
+ #define _Py_PARSE_PID "L"
+ #else
+ #error "_Py_PARSE_PID: sizeof(pid_t) is neither sizeof(int), "
+ "sizeof(long) or sizeof(long long)"
+ #endif
+#endif
+
+#if PY_MAJOR_VERSION < 3
+ #if ((SIZEOF_PID_T == SIZEOF_INT) || (SIZEOF_PID_T == SIZEOF_LONG))
+ #define PyLong_FromPid PyInt_FromLong
+ #elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
+ #define PyLong_FromPid PyLong_FromLongLong
+ #else
+ #error "PyLong_FromPid: sizeof(pid_t) is neither sizeof(int), "
+ "sizeof(long) or sizeof(long long)"
+ #endif
+#endif
// ====================================================================
// --- Custom exceptions
@@ -31,6 +67,7 @@ PyObject* PyErr_SetFromOSErrnoWithSyscall(const char *syscall);
PyObject* AccessDenied(const char *msg);
PyObject* NoSuchProcess(const char *msg);
+PyObject* PyErr_SetFromOSErrnoWithSyscall(const char *syscall);
// ====================================================================
// --- Global utils