--- posixmodule-orig.c Wed Jul 01 10:35:00 2009 +++ posixmodule.c Wed Jul 01 15:16:06 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,59 @@ #endif /* HAVE_SETPGRP */ +#ifdef MS_WINDOWS +#include +static PyObject* +win32_getppid() +{ + HANDLE snapshot; + pid_t parentpid, mypid; + parentpid = (pid_t)-1; + + 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 this value in a static variable. */ + parentpid = pe.th32ParentProcessID; + break; + } + + have_record = Process32Next(snapshot, &pe); + } + + CloseHandle(snapshot); + } + + if (-1 == parentpid) { + return win32_error("getppid", NULL); + } + + return PyLong_FromPid(parentpid); +} +#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."); 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 Wed Jul 01 15:18:42 2009 @@ -171,7 +171,8 @@ .. 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. Availability: Unix, Windows. .. function:: getuid()