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 | |
Fredrik Lundh | 63168a5 | 2005-12-14 22:29:34 +0000 | [diff] [blame] | 35 | /* Licensed to PSF under a Contributor Agreement. */ |
| 36 | /* See http://www.python.org/2.4/license for licensing details. */ |
| 37 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 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 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 53 | static PyTypeObject sp_handle_type; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 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 */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame^] | 82 | return PyLong_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); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 104 | PyObject_FREE(self); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static PyMethodDef sp_handle_methods[] = { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 108 | {"Detach", (PyCFunction) sp_handle_detach, METH_VARARGS}, |
| 109 | {"Close", (PyCFunction) sp_handle_close, METH_VARARGS}, |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 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 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame^] | 122 | return PyLong_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 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 127 | static PyTypeObject sp_handle_type = { |
Martin v. Löwis | 95c95ce | 2007-07-22 14:41:55 +0000 | [diff] [blame] | 128 | PyVarObject_HEAD_INIT(NULL, 0) |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 129 | "_subprocess_handle", sizeof(sp_handle_object), 0, |
| 130 | (destructor) sp_handle_dealloc, /*tp_dealloc*/ |
| 131 | 0, /*tp_print*/ |
| 132 | (getattrfunc) sp_handle_getattr,/*tp_getattr*/ |
| 133 | 0, /*tp_setattr*/ |
| 134 | 0, /*tp_compare*/ |
| 135 | 0, /*tp_repr*/ |
| 136 | &sp_handle_as_number, /*tp_as_number */ |
| 137 | 0, /*tp_as_sequence */ |
| 138 | 0, /*tp_as_mapping */ |
| 139 | 0 /*tp_hash*/ |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | /* -------------------------------------------------------------------- */ |
| 143 | /* windows API functions */ |
| 144 | |
| 145 | static PyObject * |
| 146 | sp_GetStdHandle(PyObject* self, PyObject* args) |
| 147 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 148 | HANDLE handle; |
| 149 | int std_handle; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 150 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 151 | if (! PyArg_ParseTuple(args, "i:GetStdHandle", &std_handle)) |
| 152 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 153 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 154 | Py_BEGIN_ALLOW_THREADS |
| 155 | handle = GetStdHandle((DWORD) std_handle); |
| 156 | Py_END_ALLOW_THREADS |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 157 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 158 | if (handle == INVALID_HANDLE_VALUE) |
| 159 | return PyErr_SetFromWindowsErr(GetLastError()); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 160 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 161 | if (! handle) { |
| 162 | Py_INCREF(Py_None); |
| 163 | return Py_None; |
| 164 | } |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 165 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 166 | /* note: returns integer, not handle object */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame^] | 167 | return PyLong_FromLong((long) handle); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static PyObject * |
| 171 | sp_GetCurrentProcess(PyObject* self, PyObject* args) |
| 172 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 173 | if (! PyArg_ParseTuple(args, ":GetCurrentProcess")) |
| 174 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 175 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 176 | return sp_handle_new(GetCurrentProcess()); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static PyObject * |
| 180 | sp_DuplicateHandle(PyObject* self, PyObject* args) |
| 181 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 182 | HANDLE target_handle; |
| 183 | BOOL result; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 184 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 185 | long source_process_handle; |
| 186 | long source_handle; |
| 187 | long target_process_handle; |
| 188 | int desired_access; |
| 189 | int inherit_handle; |
| 190 | int options = 0; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 191 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 192 | if (! PyArg_ParseTuple(args, "lllii|i:DuplicateHandle", |
| 193 | &source_process_handle, |
| 194 | &source_handle, |
| 195 | &target_process_handle, |
| 196 | &desired_access, |
| 197 | &inherit_handle, |
| 198 | &options)) |
| 199 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 200 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 201 | Py_BEGIN_ALLOW_THREADS |
| 202 | result = DuplicateHandle( |
| 203 | (HANDLE) source_process_handle, |
| 204 | (HANDLE) source_handle, |
| 205 | (HANDLE) target_process_handle, |
| 206 | &target_handle, |
| 207 | desired_access, |
| 208 | inherit_handle, |
| 209 | options |
| 210 | ); |
| 211 | Py_END_ALLOW_THREADS |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 212 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 213 | if (! result) |
| 214 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 215 | |
| 216 | return sp_handle_new(target_handle); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static PyObject * |
| 220 | sp_CreatePipe(PyObject* self, PyObject* args) |
| 221 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 222 | HANDLE read_pipe; |
| 223 | HANDLE write_pipe; |
| 224 | BOOL result; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 225 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 226 | PyObject* pipe_attributes; /* ignored */ |
| 227 | int size; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 228 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 229 | if (! PyArg_ParseTuple(args, "Oi:CreatePipe", &pipe_attributes, &size)) |
| 230 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 231 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 232 | Py_BEGIN_ALLOW_THREADS |
| 233 | result = CreatePipe(&read_pipe, &write_pipe, NULL, size); |
| 234 | Py_END_ALLOW_THREADS |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 235 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 236 | if (! result) |
| 237 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 238 | |
| 239 | return Py_BuildValue( |
| 240 | "NN", sp_handle_new(read_pipe), sp_handle_new(write_pipe)); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* helpers for createprocess */ |
| 244 | |
| 245 | static int |
| 246 | getint(PyObject* obj, char* name) |
| 247 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 248 | PyObject* value; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 249 | int ret; |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 250 | |
| 251 | value = PyObject_GetAttrString(obj, name); |
| 252 | if (! value) { |
| 253 | PyErr_Clear(); /* FIXME: propagate error? */ |
| 254 | return 0; |
| 255 | } |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame^] | 256 | ret = (int) PyLong_AsLong(value); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 257 | Py_DECREF(value); |
| 258 | return ret; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 259 | } |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 260 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 261 | static HANDLE |
| 262 | gethandle(PyObject* obj, char* name) |
| 263 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 264 | sp_handle_object* value; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 265 | HANDLE ret; |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 266 | |
| 267 | value = (sp_handle_object*) PyObject_GetAttrString(obj, name); |
| 268 | if (! value) { |
| 269 | PyErr_Clear(); /* FIXME: propagate error? */ |
| 270 | return NULL; |
| 271 | } |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 272 | if (Py_Type(value) != &sp_handle_type) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 273 | ret = NULL; |
| 274 | else |
| 275 | ret = value->handle; |
| 276 | Py_DECREF(value); |
| 277 | return ret; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static PyObject* |
| 281 | getenvironment(PyObject* environment) |
| 282 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 283 | int i, envsize; |
| 284 | PyObject* out = NULL; |
| 285 | PyObject* keys; |
| 286 | PyObject* values; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 287 | Py_UNICODE* p; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 288 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 289 | /* convert environment dictionary to windows enviroment string */ |
| 290 | if (! PyMapping_Check(environment)) { |
| 291 | PyErr_SetString( |
| 292 | PyExc_TypeError, "environment must be dictionary or None"); |
| 293 | return NULL; |
| 294 | } |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 295 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 296 | envsize = PyMapping_Length(environment); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 297 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 298 | keys = PyMapping_Keys(environment); |
| 299 | values = PyMapping_Values(environment); |
| 300 | if (!keys || !values) |
| 301 | goto error; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 302 | |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 303 | out = PyUnicode_FromUnicode(NULL, 2048); |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 304 | if (! out) |
| 305 | goto error; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 306 | |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 307 | p = PyUnicode_AS_UNICODE(out); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 308 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 309 | for (i = 0; i < envsize; i++) { |
| 310 | int ksize, vsize, totalsize; |
| 311 | PyObject* key = PyList_GET_ITEM(keys, i); |
| 312 | PyObject* value = PyList_GET_ITEM(values, i); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 313 | |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 314 | if (! PyUnicode_Check(key) || ! PyUnicode_Check(value)) { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 315 | PyErr_SetString(PyExc_TypeError, |
| 316 | "environment can only contain strings"); |
| 317 | goto error; |
| 318 | } |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 319 | ksize = PyUnicode_GET_SIZE(key); |
| 320 | vsize = PyUnicode_GET_SIZE(value); |
| 321 | totalsize = (p - PyUnicode_AS_UNICODE(out)) + ksize + 1 + |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 322 | vsize + 1 + 1; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 323 | if (totalsize > PyUnicode_GET_SIZE(out)) { |
| 324 | int offset = p - PyUnicode_AS_UNICODE(out); |
| 325 | PyUnicode_Resize(&out, totalsize + 1024); |
| 326 | p = PyUnicode_AS_UNICODE(out) + offset; |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 327 | } |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 328 | Py_UNICODE_COPY(p, PyUnicode_AS_UNICODE(key), ksize); |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 329 | p += ksize; |
| 330 | *p++ = '='; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 331 | Py_UNICODE_COPY(p, PyUnicode_AS_UNICODE(value), vsize); |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 332 | p += vsize; |
| 333 | *p++ = '\0'; |
| 334 | } |
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 | /* add trailing null byte */ |
| 337 | *p++ = '\0'; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 338 | PyUnicode_Resize(&out, p - PyUnicode_AS_UNICODE(out)); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 339 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 340 | /* PyObject_Print(out, stdout, 0); */ |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 341 | |
Fredrik Lundh | bb4692b | 2005-11-12 10:15:03 +0000 | [diff] [blame] | 342 | Py_XDECREF(keys); |
| 343 | Py_XDECREF(values); |
| 344 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 345 | return out; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 346 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 347 | error: |
| 348 | Py_XDECREF(out); |
| 349 | Py_XDECREF(keys); |
| 350 | Py_XDECREF(values); |
| 351 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | static PyObject * |
| 355 | sp_CreateProcess(PyObject* self, PyObject* args) |
| 356 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 357 | BOOL result; |
| 358 | PROCESS_INFORMATION pi; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 359 | STARTUPINFOW si; |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 360 | PyObject* environment; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 361 | |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 362 | Py_UNICODE* application_name; |
| 363 | Py_UNICODE* command_line; |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 364 | PyObject* process_attributes; /* ignored */ |
| 365 | PyObject* thread_attributes; /* ignored */ |
| 366 | int inherit_handles; |
| 367 | int creation_flags; |
| 368 | PyObject* env_mapping; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 369 | Py_UNICODE* current_directory; |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 370 | PyObject* startup_info; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 371 | |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 372 | if (! PyArg_ParseTuple(args, "ZZOOiiOZO:CreateProcess", |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 373 | &application_name, |
| 374 | &command_line, |
| 375 | &process_attributes, |
| 376 | &thread_attributes, |
| 377 | &inherit_handles, |
| 378 | &creation_flags, |
| 379 | &env_mapping, |
| 380 | ¤t_directory, |
| 381 | &startup_info)) |
| 382 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 383 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 384 | ZeroMemory(&si, sizeof(si)); |
| 385 | si.cb = sizeof(si); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 386 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 387 | /* note: we only support a small subset of all SI attributes */ |
| 388 | si.dwFlags = getint(startup_info, "dwFlags"); |
Peter Astrand | c1d6536 | 2004-11-07 14:30:34 +0000 | [diff] [blame] | 389 | si.wShowWindow = getint(startup_info, "wShowWindow"); |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 390 | si.hStdInput = gethandle(startup_info, "hStdInput"); |
| 391 | si.hStdOutput = gethandle(startup_info, "hStdOutput"); |
| 392 | si.hStdError = gethandle(startup_info, "hStdError"); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 393 | |
Fredrik Lundh | 3a49e92 | 2005-11-12 10:15:14 +0000 | [diff] [blame] | 394 | if (PyErr_Occurred()) |
| 395 | return NULL; |
| 396 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 397 | if (env_mapping == Py_None) |
| 398 | environment = NULL; |
| 399 | else { |
| 400 | environment = getenvironment(env_mapping); |
| 401 | if (! environment) |
| 402 | return NULL; |
| 403 | } |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 404 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 405 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 406 | result = CreateProcessW(application_name, |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 407 | command_line, |
| 408 | NULL, |
| 409 | NULL, |
| 410 | inherit_handles, |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 411 | creation_flags | CREATE_UNICODE_ENVIRONMENT, |
| 412 | environment ? PyUnicode_AS_UNICODE(environment) : NULL, |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 413 | current_directory, |
| 414 | &si, |
| 415 | &pi); |
| 416 | Py_END_ALLOW_THREADS |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 417 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 418 | Py_XDECREF(environment); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 419 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 420 | if (! result) |
| 421 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 422 | |
| 423 | return Py_BuildValue("NNii", |
| 424 | sp_handle_new(pi.hProcess), |
| 425 | sp_handle_new(pi.hThread), |
| 426 | pi.dwProcessId, |
| 427 | pi.dwThreadId); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | static PyObject * |
Fredrik Lundh | e515293 | 2005-12-18 21:06:46 +0000 | [diff] [blame] | 431 | sp_TerminateProcess(PyObject* self, PyObject* args) |
| 432 | { |
| 433 | BOOL result; |
| 434 | |
| 435 | long process; |
| 436 | int exit_code; |
| 437 | if (! PyArg_ParseTuple(args, "li:TerminateProcess", &process, |
| 438 | &exit_code)) |
| 439 | return NULL; |
| 440 | |
| 441 | result = TerminateProcess((HANDLE) process, exit_code); |
| 442 | |
| 443 | if (! result) |
| 444 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 445 | |
| 446 | Py_INCREF(Py_None); |
| 447 | return Py_None; |
| 448 | } |
| 449 | |
| 450 | static PyObject * |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 451 | sp_GetExitCodeProcess(PyObject* self, PyObject* args) |
| 452 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 453 | DWORD exit_code; |
| 454 | BOOL result; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 455 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 456 | long process; |
| 457 | if (! PyArg_ParseTuple(args, "l:GetExitCodeProcess", &process)) |
| 458 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 459 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 460 | result = GetExitCodeProcess((HANDLE) process, &exit_code); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 461 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 462 | if (! result) |
| 463 | return PyErr_SetFromWindowsErr(GetLastError()); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 464 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame^] | 465 | return PyLong_FromLong(exit_code); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | static PyObject * |
| 469 | sp_WaitForSingleObject(PyObject* self, PyObject* args) |
| 470 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 471 | DWORD result; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 472 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 473 | long handle; |
| 474 | int milliseconds; |
| 475 | if (! PyArg_ParseTuple(args, "li:WaitForSingleObject", |
| 476 | &handle, |
| 477 | &milliseconds)) |
| 478 | return NULL; |
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 | Py_BEGIN_ALLOW_THREADS |
| 481 | result = WaitForSingleObject((HANDLE) handle, (DWORD) milliseconds); |
| 482 | Py_END_ALLOW_THREADS |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 483 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 484 | if (result == WAIT_FAILED) |
| 485 | return PyErr_SetFromWindowsErr(GetLastError()); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 486 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame^] | 487 | return PyLong_FromLong((int) result); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | static PyObject * |
| 491 | sp_GetVersion(PyObject* self, PyObject* args) |
| 492 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 493 | if (! PyArg_ParseTuple(args, ":GetVersion")) |
| 494 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 495 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame^] | 496 | return PyLong_FromLong((int) GetVersion()); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | static PyObject * |
| 500 | sp_GetModuleFileName(PyObject* self, PyObject* args) |
| 501 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 502 | BOOL result; |
| 503 | long module; |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 504 | WCHAR filename[MAX_PATH]; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 505 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 506 | if (! PyArg_ParseTuple(args, "l:GetModuleFileName", &module)) |
| 507 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 508 | |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 509 | result = GetModuleFileNameW((HMODULE)module, filename, MAX_PATH); |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 510 | filename[MAX_PATH-1] = '\0'; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 511 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 512 | if (! result) |
| 513 | return PyErr_SetFromWindowsErr(GetLastError()); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 514 | |
Guido van Rossum | fb67be2 | 2007-08-29 18:38:11 +0000 | [diff] [blame] | 515 | return PyUnicode_FromUnicode(filename, Py_UNICODE_strlen(filename)); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | static PyMethodDef sp_functions[] = { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 519 | {"GetStdHandle", sp_GetStdHandle, METH_VARARGS}, |
| 520 | {"GetCurrentProcess", sp_GetCurrentProcess, METH_VARARGS}, |
| 521 | {"DuplicateHandle", sp_DuplicateHandle, METH_VARARGS}, |
| 522 | {"CreatePipe", sp_CreatePipe, METH_VARARGS}, |
| 523 | {"CreateProcess", sp_CreateProcess, METH_VARARGS}, |
Fredrik Lundh | e515293 | 2005-12-18 21:06:46 +0000 | [diff] [blame] | 524 | {"TerminateProcess", sp_TerminateProcess, METH_VARARGS}, |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 525 | {"GetExitCodeProcess", sp_GetExitCodeProcess, METH_VARARGS}, |
| 526 | {"WaitForSingleObject", sp_WaitForSingleObject, METH_VARARGS}, |
| 527 | {"GetVersion", sp_GetVersion, METH_VARARGS}, |
| 528 | {"GetModuleFileName", sp_GetModuleFileName, METH_VARARGS}, |
| 529 | {NULL, NULL} |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 530 | }; |
| 531 | |
| 532 | /* -------------------------------------------------------------------- */ |
| 533 | |
| 534 | static void |
| 535 | defint(PyObject* d, const char* name, int value) |
| 536 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame^] | 537 | PyObject* v = PyLong_FromLong((long) value); |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 538 | if (v) { |
| 539 | PyDict_SetItemString(d, (char*) name, v); |
| 540 | Py_DECREF(v); |
| 541 | } |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | #if PY_VERSION_HEX >= 0x02030000 |
| 545 | PyMODINIT_FUNC |
| 546 | #else |
| 547 | DL_EXPORT(void) |
| 548 | #endif |
| 549 | init_subprocess() |
| 550 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 551 | PyObject *d; |
| 552 | PyObject *m; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 553 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 554 | /* patch up object descriptors */ |
Martin v. Löwis | 95c95ce | 2007-07-22 14:41:55 +0000 | [diff] [blame] | 555 | Py_Type(&sp_handle_type) = &PyType_Type; |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 556 | sp_handle_as_number.nb_int = (unaryfunc) sp_handle_as_int; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 557 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 558 | m = Py_InitModule("_subprocess", sp_functions); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 559 | if (m == NULL) |
| 560 | return; |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 561 | d = PyModule_GetDict(m); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 562 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 563 | /* constants */ |
| 564 | defint(d, "STD_INPUT_HANDLE", STD_INPUT_HANDLE); |
| 565 | defint(d, "STD_OUTPUT_HANDLE", STD_OUTPUT_HANDLE); |
| 566 | defint(d, "STD_ERROR_HANDLE", STD_ERROR_HANDLE); |
| 567 | defint(d, "DUPLICATE_SAME_ACCESS", DUPLICATE_SAME_ACCESS); |
| 568 | defint(d, "STARTF_USESTDHANDLES", STARTF_USESTDHANDLES); |
Peter Astrand | c1d6536 | 2004-11-07 14:30:34 +0000 | [diff] [blame] | 569 | defint(d, "STARTF_USESHOWWINDOW", STARTF_USESHOWWINDOW); |
| 570 | defint(d, "SW_HIDE", SW_HIDE); |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 571 | defint(d, "INFINITE", INFINITE); |
| 572 | defint(d, "WAIT_OBJECT_0", WAIT_OBJECT_0); |
| 573 | defint(d, "CREATE_NEW_CONSOLE", CREATE_NEW_CONSOLE); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 574 | } |