blob: 1ca4ece4e083d0adaed67918458787a50896db0f [file] [log] [blame]
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001/*
2 * support routines for subprocess module
3 *
4 * Currently, this extension module is only required when using the
5 * subprocess module on Windows, but in the future, stubs for other
Tim Petersf3250b02004-10-12 21:38:22 +00006 * platforms might be added here as well.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00007 *
8 * Copyright (c) 2004 by Fredrik Lundh <fredrik@pythonware.com>
9 * Copyright (c) 2004 by Secret Labs AB, http://www.pythonware.com
10 * Copyright (c) 2004 by Peter Astrand <astrand@lysator.liu.se>
Tim Petersf3250b02004-10-12 21:38:22 +000011 *
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000012 * By obtaining, using, and/or copying this software and/or its
13 * associated documentation, you agree that you have read, understood,
14 * and will comply with the following terms and conditions:
Tim Petersf3250b02004-10-12 21:38:22 +000015 *
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000016 * Permission to use, copy, modify, and distribute this software and
17 * its associated documentation for any purpose and without fee is
18 * hereby granted, provided that the above copyright notice appears in
19 * all copies, and that both that copyright notice and this permission
20 * notice appear in supporting documentation, and that the name of the
21 * authors not be used in advertising or publicity pertaining to
22 * distribution of the software without specific, written prior
23 * permission.
Tim Petersf3250b02004-10-12 21:38:22 +000024 *
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000025 * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
26 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
27 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
28 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
29 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
30 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
31 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Tim Petersf3250b02004-10-12 21:38:22 +000032 *
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000033 */
34
Fredrik Lundh63168a52005-12-14 22:29:34 +000035/* Licensed to PSF under a Contributor Agreement. */
36/* See http://www.python.org/2.4/license for licensing details. */
37
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000038/* TODO: handle unicode command lines? */
39/* TODO: handle unicode environment? */
40
41#include "Python.h"
42
43#define WINDOWS_LEAN_AND_MEAN
44#include "windows.h"
45
46/* -------------------------------------------------------------------- */
47/* handle wrapper. note that this library uses integers when passing
48 handles to a function, and handle wrappers when returning handles.
49 the wrapper is used to provide Detach and Close methods */
50
51typedef struct {
Tim Petersf3250b02004-10-12 21:38:22 +000052 PyObject_HEAD
53 HANDLE handle;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000054} sp_handle_object;
55
Neal Norwitz227b5332006-03-22 09:28:35 +000056static PyTypeObject sp_handle_type;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000057
58static PyObject*
59sp_handle_new(HANDLE handle)
60{
Tim Petersf3250b02004-10-12 21:38:22 +000061 sp_handle_object* self;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000062
Tim Petersf3250b02004-10-12 21:38:22 +000063 self = PyObject_NEW(sp_handle_object, &sp_handle_type);
64 if (self == NULL)
65 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000066
Tim Petersf3250b02004-10-12 21:38:22 +000067 self->handle = handle;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000068
Tim Petersf3250b02004-10-12 21:38:22 +000069 return (PyObject*) self;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000070}
71
72static PyObject*
73sp_handle_detach(sp_handle_object* self, PyObject* args)
74{
Tim Petersf3250b02004-10-12 21:38:22 +000075 HANDLE handle;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000076
Tim Petersf3250b02004-10-12 21:38:22 +000077 if (! PyArg_ParseTuple(args, ":Detach"))
78 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000079
Tim Petersf3250b02004-10-12 21:38:22 +000080 handle = self->handle;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000081
Tim Petersf3250b02004-10-12 21:38:22 +000082 self->handle = NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000083
Tim Petersf3250b02004-10-12 21:38:22 +000084 /* note: return the current handle, as an integer */
85 return PyInt_FromLong((long) handle);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000086}
87
88static PyObject*
89sp_handle_close(sp_handle_object* self, PyObject* args)
90{
Tim Petersf3250b02004-10-12 21:38:22 +000091 if (! PyArg_ParseTuple(args, ":Close"))
92 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000093
Tim Petersf3250b02004-10-12 21:38:22 +000094 if (self->handle != INVALID_HANDLE_VALUE) {
95 CloseHandle(self->handle);
96 self->handle = INVALID_HANDLE_VALUE;
97 }
98 Py_INCREF(Py_None);
99 return Py_None;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000100}
101
102static void
103sp_handle_dealloc(sp_handle_object* self)
104{
Tim Petersf3250b02004-10-12 21:38:22 +0000105 if (self->handle != INVALID_HANDLE_VALUE)
106 CloseHandle(self->handle);
107 PyMem_DEL(self);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000108}
109
110static PyMethodDef sp_handle_methods[] = {
Tim Petersf3250b02004-10-12 21:38:22 +0000111 {"Detach", (PyCFunction) sp_handle_detach, 1},
112 {"Close", (PyCFunction) sp_handle_close, 1},
113 {NULL, NULL}
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000114};
115
Tim Petersf3250b02004-10-12 21:38:22 +0000116static PyObject*
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000117sp_handle_getattr(sp_handle_object* self, char* name)
118{
Tim Petersf3250b02004-10-12 21:38:22 +0000119 return Py_FindMethod(sp_handle_methods, (PyObject*) self, name);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000120}
121
122static PyObject*
123sp_handle_as_int(sp_handle_object* self)
124{
Tim Petersf3250b02004-10-12 21:38:22 +0000125 return PyInt_FromLong((long) self->handle);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000126}
127
128static PyNumberMethods sp_handle_as_number;
129
Neal Norwitz227b5332006-03-22 09:28:35 +0000130static PyTypeObject sp_handle_type = {
Tim Petersf3250b02004-10-12 21:38:22 +0000131 PyObject_HEAD_INIT(NULL)
132 0, /*ob_size*/
133 "_subprocess_handle", sizeof(sp_handle_object), 0,
134 (destructor) sp_handle_dealloc, /*tp_dealloc*/
135 0, /*tp_print*/
136 (getattrfunc) sp_handle_getattr,/*tp_getattr*/
137 0, /*tp_setattr*/
138 0, /*tp_compare*/
139 0, /*tp_repr*/
140 &sp_handle_as_number, /*tp_as_number */
141 0, /*tp_as_sequence */
142 0, /*tp_as_mapping */
143 0 /*tp_hash*/
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000144};
145
146/* -------------------------------------------------------------------- */
147/* windows API functions */
148
149static PyObject *
150sp_GetStdHandle(PyObject* self, PyObject* args)
151{
Tim Petersf3250b02004-10-12 21:38:22 +0000152 HANDLE handle;
153 int std_handle;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000154
Tim Petersf3250b02004-10-12 21:38:22 +0000155 if (! PyArg_ParseTuple(args, "i:GetStdHandle", &std_handle))
156 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000157
Tim Petersf3250b02004-10-12 21:38:22 +0000158 Py_BEGIN_ALLOW_THREADS
159 handle = GetStdHandle((DWORD) std_handle);
160 Py_END_ALLOW_THREADS
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000161
Tim Petersf3250b02004-10-12 21:38:22 +0000162 if (handle == INVALID_HANDLE_VALUE)
163 return PyErr_SetFromWindowsErr(GetLastError());
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000164
Tim Petersf3250b02004-10-12 21:38:22 +0000165 if (! handle) {
166 Py_INCREF(Py_None);
167 return Py_None;
168 }
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000169
Tim Petersf3250b02004-10-12 21:38:22 +0000170 /* note: returns integer, not handle object */
171 return PyInt_FromLong((long) handle);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000172}
173
174static PyObject *
175sp_GetCurrentProcess(PyObject* self, PyObject* args)
176{
Tim Petersf3250b02004-10-12 21:38:22 +0000177 if (! PyArg_ParseTuple(args, ":GetCurrentProcess"))
178 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000179
Tim Petersf3250b02004-10-12 21:38:22 +0000180 return sp_handle_new(GetCurrentProcess());
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000181}
182
183static PyObject *
184sp_DuplicateHandle(PyObject* self, PyObject* args)
185{
Tim Petersf3250b02004-10-12 21:38:22 +0000186 HANDLE target_handle;
187 BOOL result;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000188
Tim Petersf3250b02004-10-12 21:38:22 +0000189 long source_process_handle;
190 long source_handle;
191 long target_process_handle;
192 int desired_access;
193 int inherit_handle;
194 int options = 0;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000195
Tim Petersf3250b02004-10-12 21:38:22 +0000196 if (! PyArg_ParseTuple(args, "lllii|i:DuplicateHandle",
197 &source_process_handle,
198 &source_handle,
199 &target_process_handle,
200 &desired_access,
201 &inherit_handle,
202 &options))
203 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000204
Tim Petersf3250b02004-10-12 21:38:22 +0000205 Py_BEGIN_ALLOW_THREADS
206 result = DuplicateHandle(
207 (HANDLE) source_process_handle,
208 (HANDLE) source_handle,
209 (HANDLE) target_process_handle,
210 &target_handle,
211 desired_access,
212 inherit_handle,
213 options
214 );
215 Py_END_ALLOW_THREADS
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000216
Tim Petersf3250b02004-10-12 21:38:22 +0000217 if (! result)
218 return PyErr_SetFromWindowsErr(GetLastError());
219
220 return sp_handle_new(target_handle);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000221}
222
223static PyObject *
224sp_CreatePipe(PyObject* self, PyObject* args)
225{
Tim Petersf3250b02004-10-12 21:38:22 +0000226 HANDLE read_pipe;
227 HANDLE write_pipe;
228 BOOL result;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000229
Tim Petersf3250b02004-10-12 21:38:22 +0000230 PyObject* pipe_attributes; /* ignored */
231 int size;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000232
Tim Petersf3250b02004-10-12 21:38:22 +0000233 if (! PyArg_ParseTuple(args, "Oi:CreatePipe", &pipe_attributes, &size))
234 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000235
Tim Petersf3250b02004-10-12 21:38:22 +0000236 Py_BEGIN_ALLOW_THREADS
237 result = CreatePipe(&read_pipe, &write_pipe, NULL, size);
238 Py_END_ALLOW_THREADS
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000239
Tim Petersf3250b02004-10-12 21:38:22 +0000240 if (! result)
241 return PyErr_SetFromWindowsErr(GetLastError());
242
243 return Py_BuildValue(
244 "NN", sp_handle_new(read_pipe), sp_handle_new(write_pipe));
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000245}
246
247/* helpers for createprocess */
248
249static int
250getint(PyObject* obj, char* name)
251{
Tim Petersf3250b02004-10-12 21:38:22 +0000252 PyObject* value;
253
254 value = PyObject_GetAttrString(obj, name);
255 if (! value) {
256 PyErr_Clear(); /* FIXME: propagate error? */
257 return 0;
258 }
259 return (int) PyInt_AsLong(value);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000260}
Tim Petersf3250b02004-10-12 21:38:22 +0000261
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000262static HANDLE
263gethandle(PyObject* obj, char* name)
264{
Tim Petersf3250b02004-10-12 21:38:22 +0000265 sp_handle_object* value;
266
267 value = (sp_handle_object*) PyObject_GetAttrString(obj, name);
268 if (! value) {
269 PyErr_Clear(); /* FIXME: propagate error? */
270 return NULL;
271 }
272 if (value->ob_type != &sp_handle_type)
273 return NULL;
274 return value->handle;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000275}
276
277static PyObject*
278getenvironment(PyObject* environment)
279{
Tim Petersf3250b02004-10-12 21:38:22 +0000280 int i, envsize;
281 PyObject* out = NULL;
282 PyObject* keys;
283 PyObject* values;
284 char* p;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000285
Tim Petersf3250b02004-10-12 21:38:22 +0000286 /* convert environment dictionary to windows enviroment string */
287 if (! PyMapping_Check(environment)) {
288 PyErr_SetString(
289 PyExc_TypeError, "environment must be dictionary or None");
290 return NULL;
291 }
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000292
Tim Petersf3250b02004-10-12 21:38:22 +0000293 envsize = PyMapping_Length(environment);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000294
Tim Petersf3250b02004-10-12 21:38:22 +0000295 keys = PyMapping_Keys(environment);
296 values = PyMapping_Values(environment);
297 if (!keys || !values)
298 goto error;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000299
Tim Petersf3250b02004-10-12 21:38:22 +0000300 out = PyString_FromStringAndSize(NULL, 2048);
301 if (! out)
302 goto error;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000303
Tim Petersf3250b02004-10-12 21:38:22 +0000304 p = PyString_AS_STRING(out);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000305
Tim Petersf3250b02004-10-12 21:38:22 +0000306 for (i = 0; i < envsize; i++) {
307 int ksize, vsize, totalsize;
308 PyObject* key = PyList_GET_ITEM(keys, i);
309 PyObject* value = PyList_GET_ITEM(values, i);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000310
Tim Petersf3250b02004-10-12 21:38:22 +0000311 if (! PyString_Check(key) || ! PyString_Check(value)) {
312 PyErr_SetString(PyExc_TypeError,
313 "environment can only contain strings");
314 goto error;
315 }
316 ksize = PyString_GET_SIZE(key);
317 vsize = PyString_GET_SIZE(value);
318 totalsize = (p - PyString_AS_STRING(out)) + ksize + 1 +
319 vsize + 1 + 1;
320 if (totalsize > PyString_GET_SIZE(out)) {
321 int offset = p - PyString_AS_STRING(out);
322 _PyString_Resize(&out, totalsize + 1024);
323 p = PyString_AS_STRING(out) + offset;
324 }
325 memcpy(p, PyString_AS_STRING(key), ksize);
326 p += ksize;
327 *p++ = '=';
328 memcpy(p, PyString_AS_STRING(value), vsize);
329 p += vsize;
330 *p++ = '\0';
331 }
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000332
Tim Petersf3250b02004-10-12 21:38:22 +0000333 /* add trailing null byte */
334 *p++ = '\0';
335 _PyString_Resize(&out, p - PyString_AS_STRING(out));
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000336
Tim Petersf3250b02004-10-12 21:38:22 +0000337 /* PyObject_Print(out, stdout, 0); */
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000338
Fredrik Lundhbb4692b2005-11-12 10:15:03 +0000339 Py_XDECREF(keys);
340 Py_XDECREF(values);
341
Tim Petersf3250b02004-10-12 21:38:22 +0000342 return out;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000343
Tim Petersf3250b02004-10-12 21:38:22 +0000344 error:
345 Py_XDECREF(out);
346 Py_XDECREF(keys);
347 Py_XDECREF(values);
348 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000349}
350
351static PyObject *
352sp_CreateProcess(PyObject* self, PyObject* args)
353{
Tim Petersf3250b02004-10-12 21:38:22 +0000354 BOOL result;
355 PROCESS_INFORMATION pi;
356 STARTUPINFO si;
357 PyObject* environment;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000358
Tim Petersf3250b02004-10-12 21:38:22 +0000359 char* application_name;
360 char* command_line;
361 PyObject* process_attributes; /* ignored */
362 PyObject* thread_attributes; /* ignored */
363 int inherit_handles;
364 int creation_flags;
365 PyObject* env_mapping;
366 char* current_directory;
367 PyObject* startup_info;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000368
Tim Petersf3250b02004-10-12 21:38:22 +0000369 if (! PyArg_ParseTuple(args, "zzOOiiOzO:CreateProcess",
370 &application_name,
371 &command_line,
372 &process_attributes,
373 &thread_attributes,
374 &inherit_handles,
375 &creation_flags,
376 &env_mapping,
377 &current_directory,
378 &startup_info))
379 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000380
Tim Petersf3250b02004-10-12 21:38:22 +0000381 ZeroMemory(&si, sizeof(si));
382 si.cb = sizeof(si);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000383
Tim Petersf3250b02004-10-12 21:38:22 +0000384 /* note: we only support a small subset of all SI attributes */
385 si.dwFlags = getint(startup_info, "dwFlags");
Peter Astrandc1d65362004-11-07 14:30:34 +0000386 si.wShowWindow = getint(startup_info, "wShowWindow");
Tim Petersf3250b02004-10-12 21:38:22 +0000387 si.hStdInput = gethandle(startup_info, "hStdInput");
388 si.hStdOutput = gethandle(startup_info, "hStdOutput");
389 si.hStdError = gethandle(startup_info, "hStdError");
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000390
Fredrik Lundh3a49e922005-11-12 10:15:14 +0000391 if (PyErr_Occurred())
392 return NULL;
393
Tim Petersf3250b02004-10-12 21:38:22 +0000394 if (env_mapping == Py_None)
395 environment = NULL;
396 else {
397 environment = getenvironment(env_mapping);
398 if (! environment)
399 return NULL;
400 }
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000401
Tim Petersf3250b02004-10-12 21:38:22 +0000402 Py_BEGIN_ALLOW_THREADS
403 result = CreateProcess(application_name,
404 command_line,
405 NULL,
406 NULL,
407 inherit_handles,
408 creation_flags,
409 environment ? PyString_AS_STRING(environment) : NULL,
410 current_directory,
411 &si,
412 &pi);
413 Py_END_ALLOW_THREADS
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000414
Tim Petersf3250b02004-10-12 21:38:22 +0000415 Py_XDECREF(environment);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000416
Tim Petersf3250b02004-10-12 21:38:22 +0000417 if (! result)
418 return PyErr_SetFromWindowsErr(GetLastError());
419
420 return Py_BuildValue("NNii",
421 sp_handle_new(pi.hProcess),
422 sp_handle_new(pi.hThread),
423 pi.dwProcessId,
424 pi.dwThreadId);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000425}
426
427static PyObject *
Fredrik Lundhe5152932005-12-18 21:06:46 +0000428sp_TerminateProcess(PyObject* self, PyObject* args)
429{
430 BOOL result;
431
432 long process;
433 int exit_code;
434 if (! PyArg_ParseTuple(args, "li:TerminateProcess", &process,
435 &exit_code))
436 return NULL;
437
438 result = TerminateProcess((HANDLE) process, exit_code);
439
440 if (! result)
441 return PyErr_SetFromWindowsErr(GetLastError());
442
443 Py_INCREF(Py_None);
444 return Py_None;
445}
446
447static PyObject *
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000448sp_GetExitCodeProcess(PyObject* self, PyObject* args)
449{
Tim Petersf3250b02004-10-12 21:38:22 +0000450 DWORD exit_code;
451 BOOL result;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000452
Tim Petersf3250b02004-10-12 21:38:22 +0000453 long process;
454 if (! PyArg_ParseTuple(args, "l:GetExitCodeProcess", &process))
455 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000456
Tim Petersf3250b02004-10-12 21:38:22 +0000457 result = GetExitCodeProcess((HANDLE) process, &exit_code);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000458
Tim Petersf3250b02004-10-12 21:38:22 +0000459 if (! result)
460 return PyErr_SetFromWindowsErr(GetLastError());
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000461
Tim Petersf3250b02004-10-12 21:38:22 +0000462 return PyInt_FromLong(exit_code);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000463}
464
465static PyObject *
466sp_WaitForSingleObject(PyObject* self, PyObject* args)
467{
Tim Petersf3250b02004-10-12 21:38:22 +0000468 DWORD result;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000469
Tim Petersf3250b02004-10-12 21:38:22 +0000470 long handle;
471 int milliseconds;
472 if (! PyArg_ParseTuple(args, "li:WaitForSingleObject",
473 &handle,
474 &milliseconds))
475 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000476
Tim Petersf3250b02004-10-12 21:38:22 +0000477 Py_BEGIN_ALLOW_THREADS
478 result = WaitForSingleObject((HANDLE) handle, (DWORD) milliseconds);
479 Py_END_ALLOW_THREADS
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000480
Tim Petersf3250b02004-10-12 21:38:22 +0000481 if (result == WAIT_FAILED)
482 return PyErr_SetFromWindowsErr(GetLastError());
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000483
Tim Petersf3250b02004-10-12 21:38:22 +0000484 return PyInt_FromLong((int) result);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000485}
486
487static PyObject *
488sp_GetVersion(PyObject* self, PyObject* args)
489{
Tim Petersf3250b02004-10-12 21:38:22 +0000490 if (! PyArg_ParseTuple(args, ":GetVersion"))
491 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000492
Tim Petersf3250b02004-10-12 21:38:22 +0000493 return PyInt_FromLong((int) GetVersion());
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000494}
495
496static PyObject *
497sp_GetModuleFileName(PyObject* self, PyObject* args)
498{
Tim Petersf3250b02004-10-12 21:38:22 +0000499 BOOL result;
500 long module;
501 TCHAR filename[MAX_PATH];
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000502
Tim Petersf3250b02004-10-12 21:38:22 +0000503 if (! PyArg_ParseTuple(args, "l:GetModuleFileName", &module))
504 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000505
Tim Petersf3250b02004-10-12 21:38:22 +0000506 result = GetModuleFileName((HMODULE)module, filename, MAX_PATH);
507 filename[MAX_PATH-1] = '\0';
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000508
Tim Petersf3250b02004-10-12 21:38:22 +0000509 if (! result)
510 return PyErr_SetFromWindowsErr(GetLastError());
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000511
Tim Petersf3250b02004-10-12 21:38:22 +0000512 return PyString_FromString(filename);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000513}
514
515static PyMethodDef sp_functions[] = {
Tim Petersf3250b02004-10-12 21:38:22 +0000516 {"GetStdHandle", sp_GetStdHandle, METH_VARARGS},
517 {"GetCurrentProcess", sp_GetCurrentProcess, METH_VARARGS},
518 {"DuplicateHandle", sp_DuplicateHandle, METH_VARARGS},
519 {"CreatePipe", sp_CreatePipe, METH_VARARGS},
520 {"CreateProcess", sp_CreateProcess, METH_VARARGS},
Fredrik Lundhe5152932005-12-18 21:06:46 +0000521 {"TerminateProcess", sp_TerminateProcess, METH_VARARGS},
Tim Petersf3250b02004-10-12 21:38:22 +0000522 {"GetExitCodeProcess", sp_GetExitCodeProcess, METH_VARARGS},
523 {"WaitForSingleObject", sp_WaitForSingleObject, METH_VARARGS},
524 {"GetVersion", sp_GetVersion, METH_VARARGS},
525 {"GetModuleFileName", sp_GetModuleFileName, METH_VARARGS},
526 {NULL, NULL}
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000527};
528
529/* -------------------------------------------------------------------- */
530
531static void
532defint(PyObject* d, const char* name, int value)
533{
Tim Petersf3250b02004-10-12 21:38:22 +0000534 PyObject* v = PyInt_FromLong((long) value);
535 if (v) {
536 PyDict_SetItemString(d, (char*) name, v);
537 Py_DECREF(v);
538 }
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000539}
540
541#if PY_VERSION_HEX >= 0x02030000
542PyMODINIT_FUNC
543#else
544DL_EXPORT(void)
545#endif
546init_subprocess()
547{
Tim Petersf3250b02004-10-12 21:38:22 +0000548 PyObject *d;
549 PyObject *m;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000550
Tim Petersf3250b02004-10-12 21:38:22 +0000551 /* patch up object descriptors */
552 sp_handle_type.ob_type = &PyType_Type;
553 sp_handle_as_number.nb_int = (unaryfunc) sp_handle_as_int;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000554
Tim Petersf3250b02004-10-12 21:38:22 +0000555 m = Py_InitModule("_subprocess", sp_functions);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000556 if (m == NULL)
557 return;
Tim Petersf3250b02004-10-12 21:38:22 +0000558 d = PyModule_GetDict(m);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000559
Tim Petersf3250b02004-10-12 21:38:22 +0000560 /* constants */
561 defint(d, "STD_INPUT_HANDLE", STD_INPUT_HANDLE);
562 defint(d, "STD_OUTPUT_HANDLE", STD_OUTPUT_HANDLE);
563 defint(d, "STD_ERROR_HANDLE", STD_ERROR_HANDLE);
564 defint(d, "DUPLICATE_SAME_ACCESS", DUPLICATE_SAME_ACCESS);
565 defint(d, "STARTF_USESTDHANDLES", STARTF_USESTDHANDLES);
Peter Astrandc1d65362004-11-07 14:30:34 +0000566 defint(d, "STARTF_USESHOWWINDOW", STARTF_USESHOWWINDOW);
567 defint(d, "SW_HIDE", SW_HIDE);
Tim Petersf3250b02004-10-12 21:38:22 +0000568 defint(d, "INFINITE", INFINITE);
569 defint(d, "WAIT_OBJECT_0", WAIT_OBJECT_0);
570 defint(d, "CREATE_NEW_CONSOLE", CREATE_NEW_CONSOLE);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000571}