diff --git a/Misc/NEWS b/Misc/NEWS index 9d190ff8890..6d576937688 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -32,6 +32,10 @@ Core and Builtins Library ------- +- Issue #1983: Fix functions taking or returning a process identifier to use + the dedicated C type ``pid_t`` instead of a C ``int``. Some platforms have + a process identifier type wider than the standard C integer type. + - Issue #4066: smtplib.SMTP_SSL._get_socket now correctly returns the socket. Patch by Farhan Ahmad, test by Marcin Bachry. diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 2a31d779008..d1951f9dbc7 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -303,6 +303,25 @@ extern int lstat(const char *, struct stat *); #define WAIT_STATUS_INT(s) (s) #endif /* UNION_WAIT */ +/* Issue #1983: pid_t can be longer than a C long on some systems */ +#ifdef SIZEOF_PID_T +#if SIZEOF_PID_T == SIZEOF_INT +#define PARSE_PID "i" +#define PyLong_FromPid PyLong_FromLong +#define PyLong_AsPid PyLong_AsLong +#elif SIZEOF_PID_T == SIZEOF_LONG +#define PARSE_PID "l" +#define PyLong_FromPid PyLong_FromLong +#define PyLong_AsPid PyLong_AsLong +#elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG +#define PARSE_PID "L" +#define PyLong_FromPid PyLong_FromLongLong +#define PyLong_AsPid PyLong_AsLongLong +#else +#error "sizeof(pid_t) is neither sizeof(int), sizeof(long) or sizeof(long long)" +#endif +#endif /* SIZEOF_PID_T */ + /* Don't use the "_r" form if we don't need it (also, won't have a prototype for it, at least on Solaris -- maybe others as well?). */ #if defined(HAVE_CTERMID_R) && defined(WITH_THREAD) @@ -3820,7 +3839,7 @@ posix_fork1(PyObject *self, PyObject *noargs) return posix_error(); if (pid == 0) PyOS_AfterFork(); - return PyLong_FromLong(pid); + return PyLong_FromPid(pid); } #endif @@ -3839,7 +3858,7 @@ posix_fork(PyObject *self, PyObject *noargs) return posix_error(); if (pid == 0) PyOS_AfterFork(); - return PyLong_FromLong(pid); + return PyLong_FromPid(pid); } #endif @@ -3949,7 +3968,7 @@ posix_forkpty(PyObject *self, PyObject *noargs) return posix_error(); if (pid == 0) PyOS_AfterFork(); - return Py_BuildValue("(li)", pid, master_fd); + return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); } #endif @@ -4166,7 +4185,7 @@ posix_kill(PyObject *self, PyObject *args) { pid_t pid; int sig; - if (!PyArg_ParseTuple(args, "ii:kill", &pid, &sig)) + if (!PyArg_ParseTuple(args, PARSE_PID "i:kill", &pid, &sig)) return NULL; #if defined(PYOS_OS2) && !defined(PYCC_GCC) if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) { @@ -4231,9 +4250,6 @@ posix_plock(PyObject *self, PyObject *args) } #endif - - - #ifdef HAVE_SETUID PyDoc_STRVAR(posix_setuid__doc__, "setuid(uid)\n\n\ @@ -4502,7 +4518,7 @@ wait_helper(pid_t pid, int status, struct rusage *ru) return NULL; } - return Py_BuildValue("iiN", pid, status, result); + return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); } #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ @@ -4545,7 +4561,7 @@ posix_wait4(PyObject *self, PyObject *args) WAIT_TYPE status; WAIT_STATUS_INT(status) = 0; - if (!PyArg_ParseTuple(args, "ii:wait4", &pid, &options)) + if (!PyArg_ParseTuple(args, PARSE_PID "i:wait4", &pid, &options)) return NULL; Py_BEGIN_ALLOW_THREADS @@ -4569,7 +4585,7 @@ posix_waitpid(PyObject *self, PyObject *args) WAIT_TYPE status; WAIT_STATUS_INT(status) = 0; - if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) + if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options)) return NULL; Py_BEGIN_ALLOW_THREADS pid = waitpid(pid, &status, options); @@ -4577,7 +4593,7 @@ posix_waitpid(PyObject *self, PyObject *args) if (pid == -1) return posix_error(); - return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); + return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); } #elif defined(HAVE_CWAIT) @@ -4593,7 +4609,7 @@ posix_waitpid(PyObject *self, PyObject *args) Py_intptr_t pid; int status, options; - if (!PyArg_ParseTuple(args, "ii:waitpid", &pid, &options)) + if (!PyArg_ParseTuple(args, PARSE_PID "i:waitpid", &pid, &options)) return NULL; Py_BEGIN_ALLOW_THREADS pid = _cwait(&status, pid, options); @@ -4602,7 +4618,7 @@ posix_waitpid(PyObject *self, PyObject *args) return posix_error(); /* shift the status left a byte so this is more like the POSIX waitpid */ - return Py_BuildValue("ii", pid, status << 8); + return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8); } #endif /* HAVE_WAITPID || HAVE_CWAIT */ @@ -4624,7 +4640,7 @@ posix_wait(PyObject *self, PyObject *noargs) if (pid == -1) return posix_error(); - return Py_BuildValue("ii", pid, WAIT_STATUS_INT(status)); + return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); } #endif @@ -4809,7 +4825,7 @@ posix_getsid(PyObject *self, PyObject *args) { pid_t pid; int sid; - if (!PyArg_ParseTuple(args, "i:getsid", &pid)) + if (!PyArg_ParseTuple(args, PARSE_PID ":getsid", &pid)) return NULL; sid = getsid(pid); if (sid < 0) @@ -4844,7 +4860,7 @@ posix_setpgid(PyObject *self, PyObject *args) { pid_t pid; int pgrp; - if (!PyArg_ParseTuple(args, "ii:setpgid", &pid, &pgrp)) + if (!PyArg_ParseTuple(args, PARSE_PID "i:setpgid", &pid, &pgrp)) return NULL; if (setpgid(pid, pgrp) < 0) return posix_error(); @@ -4869,7 +4885,7 @@ posix_tcgetpgrp(PyObject *self, PyObject *args) pgid = tcgetpgrp(fd); if (pgid < 0) return posix_error(); - return PyLong_FromLong((long)pgid); + return PyLong_FromPid(pgid); } #endif /* HAVE_TCGETPGRP */