From 794b3fc707d8ebc576c6881f85875c8a5a99c75a Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Sat, 23 May 2009 15:47:13 +0000 Subject: [PATCH] Merged revisions 72852 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r72852 | antoine.pitrou | 2009-05-23 17:37:45 +0200 (sam., 23 mai 2009) | 5 lines 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. ........ --- Misc/NEWS | 4 ++++ Modules/posixmodule.c | 51 +++++++++++++++++++++++++++++-------------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/Misc/NEWS b/Misc/NEWS index 2b1630fd2bb..018854b8c38 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -50,6 +50,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 f355cf6e5d0..021b14f36ad 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -310,6 +310,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 PyInt_FromLong +#define PyLong_AsPid PyInt_AsLong +#elif SIZEOF_PID_T == SIZEOF_LONG +#define PARSE_PID "l" +#define PyLong_FromPid PyInt_FromLong +#define PyLong_AsPid PyInt_AsLong +#elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG +#define PARSE_PID "L" +#define PyLong_FromPid PyLong_FromLongLong +#define PyLong_AsPid PyInt_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) @@ -3618,7 +3637,7 @@ posix_fork1(PyObject *self, PyObject *noargs) return posix_error(); if (pid == 0) PyOS_AfterFork(); - return PyInt_FromLong(pid); + return PyLong_FromPid(pid); } #endif @@ -3637,7 +3656,7 @@ posix_fork(PyObject *self, PyObject *noargs) return posix_error(); if (pid == 0) PyOS_AfterFork(); - return PyInt_FromLong(pid); + return PyLong_FromPid(pid); } #endif @@ -3747,7 +3766,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 @@ -3964,7 +3983,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) { @@ -4533,7 +4552,7 @@ _PyPopen(char *cmdstring, int mode, int n, int bufsize) ins_rc[0] = ins_rc[1] = ins_rc[2] = 0; procObj = PyList_New(2); - pidObj = PyInt_FromLong((long) pipe_pid); + pidObj = PyLong_FromPid(pipe_pid); intObj = PyInt_FromLong((long) file_count); if (procObj && pidObj && intObj) @@ -4659,7 +4678,7 @@ static int _PyPclose(FILE *file) (pidObj = PyList_GetItem(procObj,0)) != NULL && (intObj = PyList_GetItem(procObj,1)) != NULL) { - pipe_pid = (int) PyInt_AsLong(pidObj); + pipe_pid = (pid_t) PyLong_AsPid(pidObj); file_count = (int) PyInt_AsLong(intObj); if (file_count > 1) @@ -5776,7 +5795,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 */ @@ -5819,7 +5838,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 @@ -5843,7 +5862,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); @@ -5851,7 +5870,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) @@ -5867,7 +5886,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); @@ -5876,7 +5895,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 */ @@ -5898,7 +5917,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 @@ -6089,7 +6108,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) @@ -6124,7 +6143,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(); @@ -6149,7 +6168,7 @@ posix_tcgetpgrp(PyObject *self, PyObject *args) pgid = tcgetpgrp(fd); if (pgid < 0) return posix_error(); - return PyInt_FromLong((long)pgid); + return PyLong_FromPid(pgid); } #endif /* HAVE_TCGETPGRP */