--- posixmodule-orig.c Wed Jul 01 10:35:00 2009 +++ posixmodule.c Thu Jul 02 10:00:24 2009 @@ -122,6 +122,7 @@ #else #ifdef _MSC_VER /* Microsoft compiler */ #define HAVE_GETCWD 1 +#define HAVE_GETPPID 1 #define HAVE_SPAWNV 1 #define HAVE_EXECV 1 #define HAVE_PIPE 1 @@ -3889,15 +3890,72 @@ #endif /* HAVE_SETPGRP */ +#ifdef MS_WINDOWS +#include +static PyObject* +win32_getppid() +{ + HANDLE snapshot; + pid_t mypid; + PyObject* result = NULL; + + mypid = getpid(); + snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + if (INVALID_HANDLE_VALUE != snapshot) { + BOOL have_record; + PROCESSENTRY32 pe; + + pe.dwSize = sizeof(pe); + have_record = Process32First(snapshot, &pe); + while (have_record) { + if (mypid == (pid_t)pe.th32ProcessID) { + + /* We could cache the ulong value in a static variable. */ + result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); + break; + + /* The parent process may have already exited. In this + case pe.th32ParentProcessID is the id of a process + that is no longer running. */ + } + + have_record = Process32Next(snapshot, &pe); + } + + /* if our loop exits and our pid was not found + (result will be NULL) then GetLastError will + return ERROR_NO_MORE_FILES. This is an error + anyway, so let's raise it. */ + + if (!result) + result = PyErr_SetFromWindowsErr(GetLastError()); + + CloseHandle(snapshot); + } + else + result = PyErr_SetFromWindowsErr(GetLastError()); + + return result; +} +#endif /*MS_WINDOWS*/ + #ifdef HAVE_GETPPID PyDoc_STRVAR(posix_getppid__doc__, "getppid() -> ppid\n\n\ -Return the parent's process id."); +Return the parent's process id. On Windows systems a\n\ +WindowsError will be raised if the parent process id\n\ +can not be determined. The parent process may have\n\ +already exited. In this case the returned id is that\n\ +of a process that is no longer running."); static PyObject * posix_getppid(PyObject *self, PyObject *noargs) { +#ifdef MS_WINDOWS + return win32_getppid(); +#else return PyLong_FromPid(getppid()); +#endif } #endif --- os-orig.rst Wed Jul 01 09:42:14 2009 +++ os.rst Thu Jul 02 10:07:16 2009 @@ -171,7 +171,10 @@ .. index:: single: process; id of parent - Return the parent's process id. Availability: Unix. + Return the parent's process id. On Windows, raises WindowsError if the + parent's process id can not be determined. The parent process may have + exited, in this case the id returned is that of a process that is no + longer running. Availability: Unix, Windows. .. function:: getuid()