Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1 | /* |
| 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 Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 6 | * platforms might be added here as well. |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 7 | * |
| 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 Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 11 | * |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 12 | * 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 Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 15 | * |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 16 | * 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 Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 24 | * |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 25 | * 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 Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 32 | * |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 33 | */ |
| 34 | |
| 35 | /* TODO: handle unicode command lines? */ |
| 36 | /* TODO: handle unicode environment? */ |
| 37 | |
| 38 | #include "Python.h" |
| 39 | |
| 40 | #define WINDOWS_LEAN_AND_MEAN |
| 41 | #include "windows.h" |
| 42 | |
| 43 | /* -------------------------------------------------------------------- */ |
| 44 | /* handle wrapper. note that this library uses integers when passing |
| 45 | handles to a function, and handle wrappers when returning handles. |
| 46 | the wrapper is used to provide Detach and Close methods */ |
| 47 | |
| 48 | typedef struct { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 49 | PyObject_HEAD |
| 50 | HANDLE handle; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 51 | } sp_handle_object; |
| 52 | |
| 53 | staticforward PyTypeObject sp_handle_type; |
| 54 | |
| 55 | static PyObject* |
| 56 | sp_handle_new(HANDLE handle) |
| 57 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 58 | sp_handle_object* self; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 59 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 60 | self = PyObject_NEW(sp_handle_object, &sp_handle_type); |
| 61 | if (self == NULL) |
| 62 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 63 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 64 | self->handle = handle; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 65 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 66 | return (PyObject*) self; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static PyObject* |
| 70 | sp_handle_detach(sp_handle_object* self, PyObject* args) |
| 71 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 72 | HANDLE handle; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 73 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 74 | if (! PyArg_ParseTuple(args, ":Detach")) |
| 75 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 76 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 77 | handle = self->handle; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 78 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 79 | self->handle = NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 80 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 81 | /* note: return the current handle, as an integer */ |
| 82 | return PyInt_FromLong((long) handle); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static PyObject* |
| 86 | sp_handle_close(sp_handle_object* self, PyObject* args) |
| 87 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 88 | if (! PyArg_ParseTuple(args, ":Close")) |
| 89 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 90 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 91 | if (self->handle != INVALID_HANDLE_VALUE) { |
| 92 | CloseHandle(self->handle); |
| 93 | self->handle = INVALID_HANDLE_VALUE; |
| 94 | } |
| 95 | Py_INCREF(Py_None); |
| 96 | return Py_None; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static void |
| 100 | sp_handle_dealloc(sp_handle_object* self) |
| 101 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 102 | if (self->handle != INVALID_HANDLE_VALUE) |
| 103 | CloseHandle(self->handle); |
| 104 | PyMem_DEL(self); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static PyMethodDef sp_handle_methods[] = { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 108 | {"Detach", (PyCFunction) sp_handle_detach, 1}, |
| 109 | {"Close", (PyCFunction) sp_handle_close, 1}, |
| 110 | {NULL, NULL} |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 113 | static PyObject* |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 114 | sp_handle_getattr(sp_handle_object* self, char* name) |
| 115 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 116 | return Py_FindMethod(sp_handle_methods, (PyObject*) self, name); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static PyObject* |
| 120 | sp_handle_as_int(sp_handle_object* self) |
| 121 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 122 | return PyInt_FromLong((long) self->handle); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static PyNumberMethods sp_handle_as_number; |
| 126 | |
| 127 | statichere PyTypeObject sp_handle_type = { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 128 | PyObject_HEAD_INIT(NULL) |
| 129 | 0, /*ob_size*/ |
| 130 | "_subprocess_handle", sizeof(sp_handle_object), 0, |
| 131 | (destructor) sp_handle_dealloc, /*tp_dealloc*/ |
| 132 | 0, /*tp_print*/ |
| 133 | (getattrfunc) sp_handle_getattr,/*tp_getattr*/ |
| 134 | 0, /*tp_setattr*/ |
| 135 | 0, /*tp_compare*/ |
| 136 | 0, /*tp_repr*/ |
| 137 | &sp_handle_as_number, /*tp_as_number */ |
| 138 | 0, /*tp_as_sequence */ |
| 139 | 0, /*tp_as_mapping */ |
| 140 | 0 /*tp_hash*/ |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | /* -------------------------------------------------------------------- */ |
| 144 | /* windows API functions */ |
| 145 | |
| 146 | static PyObject * |
| 147 | sp_GetStdHandle(PyObject* self, PyObject* args) |
| 148 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 149 | HANDLE handle; |
| 150 | int std_handle; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 151 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 152 | if (! PyArg_ParseTuple(args, "i:GetStdHandle", &std_handle)) |
| 153 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 154 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 155 | Py_BEGIN_ALLOW_THREADS |
| 156 | handle = GetStdHandle((DWORD) std_handle); |
| 157 | Py_END_ALLOW_THREADS |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 158 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 159 | if (handle == INVALID_HANDLE_VALUE) |
| 160 | return PyErr_SetFromWindowsErr(GetLastError()); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 161 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 162 | if (! handle) { |
| 163 | Py_INCREF(Py_None); |
| 164 | return Py_None; |
| 165 | } |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 166 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 167 | /* note: returns integer, not handle object */ |
| 168 | return PyInt_FromLong((long) handle); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static PyObject * |
| 172 | sp_GetCurrentProcess(PyObject* self, PyObject* args) |
| 173 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 174 | if (! PyArg_ParseTuple(args, ":GetCurrentProcess")) |
| 175 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 176 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 177 | return sp_handle_new(GetCurrentProcess()); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static PyObject * |
| 181 | sp_DuplicateHandle(PyObject* self, PyObject* args) |
| 182 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 183 | HANDLE target_handle; |
| 184 | BOOL result; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 185 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 186 | long source_process_handle; |
| 187 | long source_handle; |
| 188 | long target_process_handle; |
| 189 | int desired_access; |
| 190 | int inherit_handle; |
| 191 | int options = 0; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 192 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 193 | if (! PyArg_ParseTuple(args, "lllii|i:DuplicateHandle", |
| 194 | &source_process_handle, |
| 195 | &source_handle, |
| 196 | &target_process_handle, |
| 197 | &desired_access, |
| 198 | &inherit_handle, |
| 199 | &options)) |
| 200 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 201 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 202 | Py_BEGIN_ALLOW_THREADS |
| 203 | result = DuplicateHandle( |
| 204 | (HANDLE) source_process_handle, |
| 205 | (HANDLE) source_handle, |
| 206 | (HANDLE) target_process_handle, |
| 207 | &target_handle, |
| 208 | desired_access, |
| 209 | inherit_handle, |
| 210 | options |
| 211 | ); |
| 212 | Py_END_ALLOW_THREADS |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 213 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 214 | if (! result) |
| 215 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 216 | |
| 217 | return sp_handle_new(target_handle); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static PyObject * |
| 221 | sp_CreatePipe(PyObject* self, PyObject* args) |
| 222 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 223 | HANDLE read_pipe; |
| 224 | HANDLE write_pipe; |
| 225 | BOOL result; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 226 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 227 | PyObject* pipe_attributes; /* ignored */ |
| 228 | int size; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 229 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 230 | if (! PyArg_ParseTuple(args, "Oi:CreatePipe", &pipe_attributes, &size)) |
| 231 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 232 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 233 | Py_BEGIN_ALLOW_THREADS |
| 234 | result = CreatePipe(&read_pipe, &write_pipe, NULL, size); |
| 235 | Py_END_ALLOW_THREADS |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 236 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 237 | if (! result) |
| 238 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 239 | |
| 240 | return Py_BuildValue( |
| 241 | "NN", sp_handle_new(read_pipe), sp_handle_new(write_pipe)); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /* helpers for createprocess */ |
| 245 | |
| 246 | static int |
| 247 | getint(PyObject* obj, char* name) |
| 248 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 249 | PyObject* value; |
| 250 | |
| 251 | value = PyObject_GetAttrString(obj, name); |
| 252 | if (! value) { |
| 253 | PyErr_Clear(); /* FIXME: propagate error? */ |
| 254 | return 0; |
| 255 | } |
| 256 | return (int) PyInt_AsLong(value); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 257 | } |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 258 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 259 | static HANDLE |
| 260 | gethandle(PyObject* obj, char* name) |
| 261 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 262 | sp_handle_object* value; |
| 263 | |
| 264 | value = (sp_handle_object*) PyObject_GetAttrString(obj, name); |
| 265 | if (! value) { |
| 266 | PyErr_Clear(); /* FIXME: propagate error? */ |
| 267 | return NULL; |
| 268 | } |
| 269 | if (value->ob_type != &sp_handle_type) |
| 270 | return NULL; |
| 271 | return value->handle; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | static PyObject* |
| 275 | getenvironment(PyObject* environment) |
| 276 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 277 | int i, envsize; |
| 278 | PyObject* out = NULL; |
| 279 | PyObject* keys; |
| 280 | PyObject* values; |
| 281 | char* p; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 282 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 283 | /* convert environment dictionary to windows enviroment string */ |
| 284 | if (! PyMapping_Check(environment)) { |
| 285 | PyErr_SetString( |
| 286 | PyExc_TypeError, "environment must be dictionary or None"); |
| 287 | return NULL; |
| 288 | } |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 289 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 290 | envsize = PyMapping_Length(environment); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 291 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 292 | keys = PyMapping_Keys(environment); |
| 293 | values = PyMapping_Values(environment); |
| 294 | if (!keys || !values) |
| 295 | goto error; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 296 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 297 | out = PyString_FromStringAndSize(NULL, 2048); |
| 298 | if (! out) |
| 299 | goto error; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 300 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 301 | p = PyString_AS_STRING(out); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 302 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 303 | for (i = 0; i < envsize; i++) { |
| 304 | int ksize, vsize, totalsize; |
| 305 | PyObject* key = PyList_GET_ITEM(keys, i); |
| 306 | PyObject* value = PyList_GET_ITEM(values, i); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 307 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 308 | if (! PyString_Check(key) || ! PyString_Check(value)) { |
| 309 | PyErr_SetString(PyExc_TypeError, |
| 310 | "environment can only contain strings"); |
| 311 | goto error; |
| 312 | } |
| 313 | ksize = PyString_GET_SIZE(key); |
| 314 | vsize = PyString_GET_SIZE(value); |
| 315 | totalsize = (p - PyString_AS_STRING(out)) + ksize + 1 + |
| 316 | vsize + 1 + 1; |
| 317 | if (totalsize > PyString_GET_SIZE(out)) { |
| 318 | int offset = p - PyString_AS_STRING(out); |
| 319 | _PyString_Resize(&out, totalsize + 1024); |
| 320 | p = PyString_AS_STRING(out) + offset; |
| 321 | } |
| 322 | memcpy(p, PyString_AS_STRING(key), ksize); |
| 323 | p += ksize; |
| 324 | *p++ = '='; |
| 325 | memcpy(p, PyString_AS_STRING(value), vsize); |
| 326 | p += vsize; |
| 327 | *p++ = '\0'; |
| 328 | } |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 329 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 330 | /* add trailing null byte */ |
| 331 | *p++ = '\0'; |
| 332 | _PyString_Resize(&out, p - PyString_AS_STRING(out)); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 333 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 334 | /* PyObject_Print(out, stdout, 0); */ |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 335 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 336 | return out; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 337 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 338 | error: |
| 339 | Py_XDECREF(out); |
| 340 | Py_XDECREF(keys); |
| 341 | Py_XDECREF(values); |
| 342 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | static PyObject * |
| 346 | sp_CreateProcess(PyObject* self, PyObject* args) |
| 347 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 348 | BOOL result; |
| 349 | PROCESS_INFORMATION pi; |
| 350 | STARTUPINFO si; |
| 351 | PyObject* environment; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 352 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 353 | char* application_name; |
| 354 | char* command_line; |
| 355 | PyObject* process_attributes; /* ignored */ |
| 356 | PyObject* thread_attributes; /* ignored */ |
| 357 | int inherit_handles; |
| 358 | int creation_flags; |
| 359 | PyObject* env_mapping; |
| 360 | char* current_directory; |
| 361 | PyObject* startup_info; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 362 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 363 | if (! PyArg_ParseTuple(args, "zzOOiiOzO:CreateProcess", |
| 364 | &application_name, |
| 365 | &command_line, |
| 366 | &process_attributes, |
| 367 | &thread_attributes, |
| 368 | &inherit_handles, |
| 369 | &creation_flags, |
| 370 | &env_mapping, |
| 371 | ¤t_directory, |
| 372 | &startup_info)) |
| 373 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 374 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 375 | ZeroMemory(&si, sizeof(si)); |
| 376 | si.cb = sizeof(si); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 377 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 378 | /* note: we only support a small subset of all SI attributes */ |
| 379 | si.dwFlags = getint(startup_info, "dwFlags"); |
Peter Astrand | c1d6536 | 2004-11-07 14:30:34 +0000 | [diff] [blame] | 380 | si.wShowWindow = getint(startup_info, "wShowWindow"); |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 381 | si.hStdInput = gethandle(startup_info, "hStdInput"); |
| 382 | si.hStdOutput = gethandle(startup_info, "hStdOutput"); |
| 383 | si.hStdError = gethandle(startup_info, "hStdError"); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 384 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 385 | if (env_mapping == Py_None) |
| 386 | environment = NULL; |
| 387 | else { |
| 388 | environment = getenvironment(env_mapping); |
| 389 | if (! environment) |
| 390 | return NULL; |
| 391 | } |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 392 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 393 | Py_BEGIN_ALLOW_THREADS |
| 394 | result = CreateProcess(application_name, |
| 395 | command_line, |
| 396 | NULL, |
| 397 | NULL, |
| 398 | inherit_handles, |
| 399 | creation_flags, |
| 400 | environment ? PyString_AS_STRING(environment) : NULL, |
| 401 | current_directory, |
| 402 | &si, |
| 403 | &pi); |
| 404 | Py_END_ALLOW_THREADS |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 405 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 406 | Py_XDECREF(environment); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 407 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 408 | if (! result) |
| 409 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 410 | |
| 411 | return Py_BuildValue("NNii", |
| 412 | sp_handle_new(pi.hProcess), |
| 413 | sp_handle_new(pi.hThread), |
| 414 | pi.dwProcessId, |
| 415 | pi.dwThreadId); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static PyObject * |
| 419 | sp_GetExitCodeProcess(PyObject* self, PyObject* args) |
| 420 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 421 | DWORD exit_code; |
| 422 | BOOL result; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 423 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 424 | long process; |
| 425 | if (! PyArg_ParseTuple(args, "l:GetExitCodeProcess", &process)) |
| 426 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 427 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 428 | result = GetExitCodeProcess((HANDLE) process, &exit_code); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 429 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 430 | if (! result) |
| 431 | return PyErr_SetFromWindowsErr(GetLastError()); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 432 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 433 | return PyInt_FromLong(exit_code); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | static PyObject * |
| 437 | sp_WaitForSingleObject(PyObject* self, PyObject* args) |
| 438 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 439 | DWORD result; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 440 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 441 | long handle; |
| 442 | int milliseconds; |
| 443 | if (! PyArg_ParseTuple(args, "li:WaitForSingleObject", |
| 444 | &handle, |
| 445 | &milliseconds)) |
| 446 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 447 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 448 | Py_BEGIN_ALLOW_THREADS |
| 449 | result = WaitForSingleObject((HANDLE) handle, (DWORD) milliseconds); |
| 450 | Py_END_ALLOW_THREADS |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 451 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 452 | if (result == WAIT_FAILED) |
| 453 | return PyErr_SetFromWindowsErr(GetLastError()); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 454 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 455 | return PyInt_FromLong((int) result); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | static PyObject * |
| 459 | sp_GetVersion(PyObject* self, PyObject* args) |
| 460 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 461 | if (! PyArg_ParseTuple(args, ":GetVersion")) |
| 462 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 463 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 464 | return PyInt_FromLong((int) GetVersion()); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | static PyObject * |
| 468 | sp_GetModuleFileName(PyObject* self, PyObject* args) |
| 469 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 470 | BOOL result; |
| 471 | long module; |
| 472 | TCHAR filename[MAX_PATH]; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 473 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 474 | if (! PyArg_ParseTuple(args, "l:GetModuleFileName", &module)) |
| 475 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 476 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 477 | result = GetModuleFileName((HMODULE)module, filename, MAX_PATH); |
| 478 | filename[MAX_PATH-1] = '\0'; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 479 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 480 | if (! result) |
| 481 | return PyErr_SetFromWindowsErr(GetLastError()); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 482 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 483 | return PyString_FromString(filename); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | static PyMethodDef sp_functions[] = { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 487 | {"GetStdHandle", sp_GetStdHandle, METH_VARARGS}, |
| 488 | {"GetCurrentProcess", sp_GetCurrentProcess, METH_VARARGS}, |
| 489 | {"DuplicateHandle", sp_DuplicateHandle, METH_VARARGS}, |
| 490 | {"CreatePipe", sp_CreatePipe, METH_VARARGS}, |
| 491 | {"CreateProcess", sp_CreateProcess, METH_VARARGS}, |
| 492 | {"GetExitCodeProcess", sp_GetExitCodeProcess, METH_VARARGS}, |
| 493 | {"WaitForSingleObject", sp_WaitForSingleObject, METH_VARARGS}, |
| 494 | {"GetVersion", sp_GetVersion, METH_VARARGS}, |
| 495 | {"GetModuleFileName", sp_GetModuleFileName, METH_VARARGS}, |
| 496 | {NULL, NULL} |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 497 | }; |
| 498 | |
| 499 | /* -------------------------------------------------------------------- */ |
| 500 | |
| 501 | static void |
| 502 | defint(PyObject* d, const char* name, int value) |
| 503 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 504 | PyObject* v = PyInt_FromLong((long) value); |
| 505 | if (v) { |
| 506 | PyDict_SetItemString(d, (char*) name, v); |
| 507 | Py_DECREF(v); |
| 508 | } |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | #if PY_VERSION_HEX >= 0x02030000 |
| 512 | PyMODINIT_FUNC |
| 513 | #else |
| 514 | DL_EXPORT(void) |
| 515 | #endif |
| 516 | init_subprocess() |
| 517 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 518 | PyObject *d; |
| 519 | PyObject *m; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 520 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 521 | /* patch up object descriptors */ |
| 522 | sp_handle_type.ob_type = &PyType_Type; |
| 523 | sp_handle_as_number.nb_int = (unaryfunc) sp_handle_as_int; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 524 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 525 | m = Py_InitModule("_subprocess", sp_functions); |
| 526 | d = PyModule_GetDict(m); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 527 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 528 | /* constants */ |
| 529 | defint(d, "STD_INPUT_HANDLE", STD_INPUT_HANDLE); |
| 530 | defint(d, "STD_OUTPUT_HANDLE", STD_OUTPUT_HANDLE); |
| 531 | defint(d, "STD_ERROR_HANDLE", STD_ERROR_HANDLE); |
| 532 | defint(d, "DUPLICATE_SAME_ACCESS", DUPLICATE_SAME_ACCESS); |
| 533 | defint(d, "STARTF_USESTDHANDLES", STARTF_USESTDHANDLES); |
Peter Astrand | c1d6536 | 2004-11-07 14:30:34 +0000 | [diff] [blame] | 534 | defint(d, "STARTF_USESHOWWINDOW", STARTF_USESHOWWINDOW); |
| 535 | defint(d, "SW_HIDE", SW_HIDE); |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 536 | defint(d, "INFINITE", INFINITE); |
| 537 | defint(d, "WAIT_OBJECT_0", WAIT_OBJECT_0); |
| 538 | defint(d, "CREATE_NEW_CONSOLE", CREATE_NEW_CONSOLE); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 539 | } |