| 1 | n/a | /* |
|---|
| 2 | n/a | * support routines for subprocess module |
|---|
| 3 | n/a | * |
|---|
| 4 | n/a | * Currently, this extension module is only required when using the |
|---|
| 5 | n/a | * subprocess module on Windows, but in the future, stubs for other |
|---|
| 6 | n/a | * platforms might be added here as well. |
|---|
| 7 | n/a | * |
|---|
| 8 | n/a | * Copyright (c) 2004 by Fredrik Lundh <fredrik@pythonware.com> |
|---|
| 9 | n/a | * Copyright (c) 2004 by Secret Labs AB, http://www.pythonware.com |
|---|
| 10 | n/a | * Copyright (c) 2004 by Peter Astrand <astrand@lysator.liu.se> |
|---|
| 11 | n/a | * |
|---|
| 12 | n/a | * By obtaining, using, and/or copying this software and/or its |
|---|
| 13 | n/a | * associated documentation, you agree that you have read, understood, |
|---|
| 14 | n/a | * and will comply with the following terms and conditions: |
|---|
| 15 | n/a | * |
|---|
| 16 | n/a | * Permission to use, copy, modify, and distribute this software and |
|---|
| 17 | n/a | * its associated documentation for any purpose and without fee is |
|---|
| 18 | n/a | * hereby granted, provided that the above copyright notice appears in |
|---|
| 19 | n/a | * all copies, and that both that copyright notice and this permission |
|---|
| 20 | n/a | * notice appear in supporting documentation, and that the name of the |
|---|
| 21 | n/a | * authors not be used in advertising or publicity pertaining to |
|---|
| 22 | n/a | * distribution of the software without specific, written prior |
|---|
| 23 | n/a | * permission. |
|---|
| 24 | n/a | * |
|---|
| 25 | n/a | * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
|---|
| 26 | n/a | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. |
|---|
| 27 | n/a | * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
|---|
| 28 | n/a | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS |
|---|
| 29 | n/a | * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
|---|
| 30 | n/a | * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION |
|---|
| 31 | n/a | * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|---|
| 32 | n/a | * |
|---|
| 33 | n/a | */ |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | /* Licensed to PSF under a Contributor Agreement. */ |
|---|
| 36 | n/a | /* See http://www.python.org/2.4/license for licensing details. */ |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | #include "Python.h" |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | #define WINDOWS_LEAN_AND_MEAN |
|---|
| 41 | n/a | #include "windows.h" |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | /* -------------------------------------------------------------------- */ |
|---|
| 44 | n/a | /* handle wrapper. note that this library uses integers when passing |
|---|
| 45 | n/a | handles to a function, and handle wrappers when returning handles. |
|---|
| 46 | n/a | the wrapper is used to provide Detach and Close methods */ |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | typedef struct { |
|---|
| 49 | n/a | PyObject_HEAD |
|---|
| 50 | n/a | HANDLE handle; |
|---|
| 51 | n/a | } sp_handle_object; |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | static PyTypeObject sp_handle_type; |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | static PyObject* |
|---|
| 56 | n/a | sp_handle_new(HANDLE handle) |
|---|
| 57 | n/a | { |
|---|
| 58 | n/a | sp_handle_object* self; |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | self = PyObject_NEW(sp_handle_object, &sp_handle_type); |
|---|
| 61 | n/a | if (self == NULL) |
|---|
| 62 | n/a | return NULL; |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | self->handle = handle; |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | return (PyObject*) self; |
|---|
| 67 | n/a | } |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | #if defined(MS_WIN32) && !defined(MS_WIN64) |
|---|
| 70 | n/a | #define HANDLE_TO_PYNUM(handle) PyLong_FromLong((long) handle) |
|---|
| 71 | n/a | #define PY_HANDLE_PARAM "l" |
|---|
| 72 | n/a | #else |
|---|
| 73 | n/a | #define HANDLE_TO_PYNUM(handle) PyLong_FromLongLong((long long) handle) |
|---|
| 74 | n/a | #define PY_HANDLE_PARAM "L" |
|---|
| 75 | n/a | #endif |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | static PyObject* |
|---|
| 78 | n/a | sp_handle_detach(sp_handle_object* self, PyObject* args) |
|---|
| 79 | n/a | { |
|---|
| 80 | n/a | HANDLE handle; |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | if (! PyArg_ParseTuple(args, ":Detach")) |
|---|
| 83 | n/a | return NULL; |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | handle = self->handle; |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | self->handle = INVALID_HANDLE_VALUE; |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | /* note: return the current handle, as an integer */ |
|---|
| 90 | n/a | return HANDLE_TO_PYNUM(handle); |
|---|
| 91 | n/a | } |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | static PyObject* |
|---|
| 94 | n/a | sp_handle_close(sp_handle_object* self, PyObject* args) |
|---|
| 95 | n/a | { |
|---|
| 96 | n/a | if (! PyArg_ParseTuple(args, ":Close")) |
|---|
| 97 | n/a | return NULL; |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | if (self->handle != INVALID_HANDLE_VALUE) { |
|---|
| 100 | n/a | CloseHandle(self->handle); |
|---|
| 101 | n/a | self->handle = INVALID_HANDLE_VALUE; |
|---|
| 102 | n/a | } |
|---|
| 103 | n/a | Py_INCREF(Py_None); |
|---|
| 104 | n/a | return Py_None; |
|---|
| 105 | n/a | } |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | static void |
|---|
| 108 | n/a | sp_handle_dealloc(sp_handle_object* self) |
|---|
| 109 | n/a | { |
|---|
| 110 | n/a | if (self->handle != INVALID_HANDLE_VALUE) |
|---|
| 111 | n/a | CloseHandle(self->handle); |
|---|
| 112 | n/a | PyObject_FREE(self); |
|---|
| 113 | n/a | } |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | static PyMethodDef sp_handle_methods[] = { |
|---|
| 116 | n/a | {"Detach", (PyCFunction) sp_handle_detach, METH_VARARGS}, |
|---|
| 117 | n/a | {"Close", (PyCFunction) sp_handle_close, METH_VARARGS}, |
|---|
| 118 | n/a | {NULL, NULL} |
|---|
| 119 | n/a | }; |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | static PyObject* |
|---|
| 122 | n/a | sp_handle_as_int(sp_handle_object* self) |
|---|
| 123 | n/a | { |
|---|
| 124 | n/a | return HANDLE_TO_PYNUM(self->handle); |
|---|
| 125 | n/a | } |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | static PyNumberMethods sp_handle_as_number; |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | static PyTypeObject sp_handle_type = { |
|---|
| 130 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 131 | n/a | "_subprocess_handle", sizeof(sp_handle_object), 0, |
|---|
| 132 | n/a | (destructor) sp_handle_dealloc, /*tp_dealloc*/ |
|---|
| 133 | n/a | 0, /*tp_print*/ |
|---|
| 134 | n/a | 0, /*tp_getattr*/ |
|---|
| 135 | n/a | 0, /*tp_setattr*/ |
|---|
| 136 | n/a | 0, /*tp_reserved*/ |
|---|
| 137 | n/a | 0, /*tp_repr*/ |
|---|
| 138 | n/a | &sp_handle_as_number, /*tp_as_number */ |
|---|
| 139 | n/a | 0, /*tp_as_sequence */ |
|---|
| 140 | n/a | 0, /*tp_as_mapping */ |
|---|
| 141 | n/a | 0, /*tp_hash*/ |
|---|
| 142 | n/a | 0, /*tp_call*/ |
|---|
| 143 | n/a | 0, /*tp_str*/ |
|---|
| 144 | n/a | 0, /*tp_getattro*/ |
|---|
| 145 | n/a | 0, /*tp_setattro*/ |
|---|
| 146 | n/a | 0, /*tp_as_buffer*/ |
|---|
| 147 | n/a | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
|---|
| 148 | n/a | 0, /*tp_doc*/ |
|---|
| 149 | n/a | 0, /*tp_traverse*/ |
|---|
| 150 | n/a | 0, /*tp_clear*/ |
|---|
| 151 | n/a | 0, /*tp_richcompare*/ |
|---|
| 152 | n/a | 0, /*tp_weaklistoffset*/ |
|---|
| 153 | n/a | 0, /*tp_iter*/ |
|---|
| 154 | n/a | 0, /*tp_iternext*/ |
|---|
| 155 | n/a | sp_handle_methods, /*tp_methods*/ |
|---|
| 156 | n/a | }; |
|---|
| 157 | n/a | |
|---|
| 158 | n/a | /* -------------------------------------------------------------------- */ |
|---|
| 159 | n/a | /* windows API functions */ |
|---|
| 160 | n/a | |
|---|
| 161 | n/a | PyDoc_STRVAR(GetStdHandle_doc, |
|---|
| 162 | n/a | "GetStdHandle(handle) -> integer\n\ |
|---|
| 163 | n/a | \n\ |
|---|
| 164 | n/a | Return a handle to the specified standard device\n\ |
|---|
| 165 | n/a | (STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE).\n\ |
|---|
| 166 | n/a | The integer associated with the handle object is returned."); |
|---|
| 167 | n/a | |
|---|
| 168 | n/a | static PyObject * |
|---|
| 169 | n/a | sp_GetStdHandle(PyObject* self, PyObject* args) |
|---|
| 170 | n/a | { |
|---|
| 171 | n/a | HANDLE handle; |
|---|
| 172 | n/a | int std_handle; |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | if (! PyArg_ParseTuple(args, "i:GetStdHandle", &std_handle)) |
|---|
| 175 | n/a | return NULL; |
|---|
| 176 | n/a | |
|---|
| 177 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 178 | n/a | handle = GetStdHandle((DWORD) std_handle); |
|---|
| 179 | n/a | Py_END_ALLOW_THREADS |
|---|
| 180 | n/a | |
|---|
| 181 | n/a | if (handle == INVALID_HANDLE_VALUE) |
|---|
| 182 | n/a | return PyErr_SetFromWindowsErr(GetLastError()); |
|---|
| 183 | n/a | |
|---|
| 184 | n/a | if (! handle) { |
|---|
| 185 | n/a | Py_INCREF(Py_None); |
|---|
| 186 | n/a | return Py_None; |
|---|
| 187 | n/a | } |
|---|
| 188 | n/a | |
|---|
| 189 | n/a | /* note: returns integer, not handle object */ |
|---|
| 190 | n/a | return HANDLE_TO_PYNUM(handle); |
|---|
| 191 | n/a | } |
|---|
| 192 | n/a | |
|---|
| 193 | n/a | PyDoc_STRVAR(GetCurrentProcess_doc, |
|---|
| 194 | n/a | "GetCurrentProcess() -> handle\n\ |
|---|
| 195 | n/a | \n\ |
|---|
| 196 | n/a | Return a handle object for the current process."); |
|---|
| 197 | n/a | |
|---|
| 198 | n/a | static PyObject * |
|---|
| 199 | n/a | sp_GetCurrentProcess(PyObject* self, PyObject* args) |
|---|
| 200 | n/a | { |
|---|
| 201 | n/a | if (! PyArg_ParseTuple(args, ":GetCurrentProcess")) |
|---|
| 202 | n/a | return NULL; |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | return sp_handle_new(GetCurrentProcess()); |
|---|
| 205 | n/a | } |
|---|
| 206 | n/a | |
|---|
| 207 | n/a | PyDoc_STRVAR(DuplicateHandle_doc, |
|---|
| 208 | n/a | "DuplicateHandle(source_proc_handle, source_handle,\n\ |
|---|
| 209 | n/a | target_proc_handle, target_handle, access,\n\ |
|---|
| 210 | n/a | inherit[, options]) -> handle\n\ |
|---|
| 211 | n/a | \n\ |
|---|
| 212 | n/a | Return a duplicate handle object.\n\ |
|---|
| 213 | n/a | \n\ |
|---|
| 214 | n/a | The duplicate handle refers to the same object as the original\n\ |
|---|
| 215 | n/a | handle. Therefore, any changes to the object are reflected\n\ |
|---|
| 216 | n/a | through both handles."); |
|---|
| 217 | n/a | |
|---|
| 218 | n/a | static PyObject * |
|---|
| 219 | n/a | sp_DuplicateHandle(PyObject* self, PyObject* args) |
|---|
| 220 | n/a | { |
|---|
| 221 | n/a | HANDLE target_handle; |
|---|
| 222 | n/a | BOOL result; |
|---|
| 223 | n/a | |
|---|
| 224 | n/a | HANDLE source_process_handle; |
|---|
| 225 | n/a | HANDLE source_handle; |
|---|
| 226 | n/a | HANDLE target_process_handle; |
|---|
| 227 | n/a | int desired_access; |
|---|
| 228 | n/a | int inherit_handle; |
|---|
| 229 | n/a | int options = 0; |
|---|
| 230 | n/a | |
|---|
| 231 | n/a | if (! PyArg_ParseTuple(args, |
|---|
| 232 | n/a | PY_HANDLE_PARAM PY_HANDLE_PARAM PY_HANDLE_PARAM |
|---|
| 233 | n/a | "ii|i:DuplicateHandle", |
|---|
| 234 | n/a | &source_process_handle, |
|---|
| 235 | n/a | &source_handle, |
|---|
| 236 | n/a | &target_process_handle, |
|---|
| 237 | n/a | &desired_access, |
|---|
| 238 | n/a | &inherit_handle, |
|---|
| 239 | n/a | &options)) |
|---|
| 240 | n/a | return NULL; |
|---|
| 241 | n/a | |
|---|
| 242 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 243 | n/a | result = DuplicateHandle( |
|---|
| 244 | n/a | source_process_handle, |
|---|
| 245 | n/a | source_handle, |
|---|
| 246 | n/a | target_process_handle, |
|---|
| 247 | n/a | &target_handle, |
|---|
| 248 | n/a | desired_access, |
|---|
| 249 | n/a | inherit_handle, |
|---|
| 250 | n/a | options |
|---|
| 251 | n/a | ); |
|---|
| 252 | n/a | Py_END_ALLOW_THREADS |
|---|
| 253 | n/a | |
|---|
| 254 | n/a | if (! result) |
|---|
| 255 | n/a | return PyErr_SetFromWindowsErr(GetLastError()); |
|---|
| 256 | n/a | |
|---|
| 257 | n/a | return sp_handle_new(target_handle); |
|---|
| 258 | n/a | } |
|---|
| 259 | n/a | |
|---|
| 260 | n/a | PyDoc_STRVAR(CreatePipe_doc, |
|---|
| 261 | n/a | "CreatePipe(pipe_attrs, size) -> (read_handle, write_handle)\n\ |
|---|
| 262 | n/a | \n\ |
|---|
| 263 | n/a | Create an anonymous pipe, and return handles to the read and\n\ |
|---|
| 264 | n/a | write ends of the pipe.\n\ |
|---|
| 265 | n/a | \n\ |
|---|
| 266 | n/a | pipe_attrs is ignored internally and can be None."); |
|---|
| 267 | n/a | |
|---|
| 268 | n/a | static PyObject * |
|---|
| 269 | n/a | sp_CreatePipe(PyObject* self, PyObject* args) |
|---|
| 270 | n/a | { |
|---|
| 271 | n/a | HANDLE read_pipe; |
|---|
| 272 | n/a | HANDLE write_pipe; |
|---|
| 273 | n/a | BOOL result; |
|---|
| 274 | n/a | |
|---|
| 275 | n/a | PyObject* pipe_attributes; /* ignored */ |
|---|
| 276 | n/a | int size; |
|---|
| 277 | n/a | |
|---|
| 278 | n/a | if (! PyArg_ParseTuple(args, "Oi:CreatePipe", &pipe_attributes, &size)) |
|---|
| 279 | n/a | return NULL; |
|---|
| 280 | n/a | |
|---|
| 281 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 282 | n/a | result = CreatePipe(&read_pipe, &write_pipe, NULL, size); |
|---|
| 283 | n/a | Py_END_ALLOW_THREADS |
|---|
| 284 | n/a | |
|---|
| 285 | n/a | if (! result) |
|---|
| 286 | n/a | return PyErr_SetFromWindowsErr(GetLastError()); |
|---|
| 287 | n/a | |
|---|
| 288 | n/a | return Py_BuildValue( |
|---|
| 289 | n/a | "NN", sp_handle_new(read_pipe), sp_handle_new(write_pipe)); |
|---|
| 290 | n/a | } |
|---|
| 291 | n/a | |
|---|
| 292 | n/a | /* helpers for createprocess */ |
|---|
| 293 | n/a | |
|---|
| 294 | n/a | static int |
|---|
| 295 | n/a | getint(PyObject* obj, char* name) |
|---|
| 296 | n/a | { |
|---|
| 297 | n/a | PyObject* value; |
|---|
| 298 | n/a | int ret; |
|---|
| 299 | n/a | |
|---|
| 300 | n/a | value = PyObject_GetAttrString(obj, name); |
|---|
| 301 | n/a | if (! value) { |
|---|
| 302 | n/a | PyErr_Clear(); /* FIXME: propagate error? */ |
|---|
| 303 | n/a | return 0; |
|---|
| 304 | n/a | } |
|---|
| 305 | n/a | ret = (int) PyLong_AsLong(value); |
|---|
| 306 | n/a | Py_DECREF(value); |
|---|
| 307 | n/a | return ret; |
|---|
| 308 | n/a | } |
|---|
| 309 | n/a | |
|---|
| 310 | n/a | static HANDLE |
|---|
| 311 | n/a | gethandle(PyObject* obj, char* name) |
|---|
| 312 | n/a | { |
|---|
| 313 | n/a | sp_handle_object* value; |
|---|
| 314 | n/a | HANDLE ret; |
|---|
| 315 | n/a | |
|---|
| 316 | n/a | value = (sp_handle_object*) PyObject_GetAttrString(obj, name); |
|---|
| 317 | n/a | if (! value) { |
|---|
| 318 | n/a | PyErr_Clear(); /* FIXME: propagate error? */ |
|---|
| 319 | n/a | return NULL; |
|---|
| 320 | n/a | } |
|---|
| 321 | n/a | if (Py_TYPE(value) != &sp_handle_type) |
|---|
| 322 | n/a | ret = NULL; |
|---|
| 323 | n/a | else |
|---|
| 324 | n/a | ret = value->handle; |
|---|
| 325 | n/a | Py_DECREF(value); |
|---|
| 326 | n/a | return ret; |
|---|
| 327 | n/a | } |
|---|
| 328 | n/a | |
|---|
| 329 | n/a | static PyObject* |
|---|
| 330 | n/a | getenvironment(PyObject* environment) |
|---|
| 331 | n/a | { |
|---|
| 332 | n/a | Py_ssize_t i, envsize, totalsize; |
|---|
| 333 | n/a | Py_UCS4 *buffer = NULL, *p, *end; |
|---|
| 334 | n/a | PyObject *keys, *values, *res; |
|---|
| 335 | n/a | |
|---|
| 336 | n/a | /* convert environment dictionary to windows enviroment string */ |
|---|
| 337 | n/a | if (! PyMapping_Check(environment)) { |
|---|
| 338 | n/a | PyErr_SetString( |
|---|
| 339 | n/a | PyExc_TypeError, "environment must be dictionary or None"); |
|---|
| 340 | n/a | return NULL; |
|---|
| 341 | n/a | } |
|---|
| 342 | n/a | |
|---|
| 343 | n/a | envsize = PyMapping_Length(environment); |
|---|
| 344 | n/a | |
|---|
| 345 | n/a | keys = PyMapping_Keys(environment); |
|---|
| 346 | n/a | values = PyMapping_Values(environment); |
|---|
| 347 | n/a | if (!keys || !values) |
|---|
| 348 | n/a | goto error; |
|---|
| 349 | n/a | |
|---|
| 350 | n/a | totalsize = 1; /* trailing null character */ |
|---|
| 351 | n/a | for (i = 0; i < envsize; i++) { |
|---|
| 352 | n/a | PyObject* key = PyList_GET_ITEM(keys, i); |
|---|
| 353 | n/a | PyObject* value = PyList_GET_ITEM(values, i); |
|---|
| 354 | n/a | |
|---|
| 355 | n/a | if (! PyUnicode_Check(key) || ! PyUnicode_Check(value)) { |
|---|
| 356 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 357 | n/a | "environment can only contain strings"); |
|---|
| 358 | n/a | goto error; |
|---|
| 359 | n/a | } |
|---|
| 360 | n/a | totalsize += PyUnicode_GET_LENGTH(key) + 1; /* +1 for '=' */ |
|---|
| 361 | n/a | totalsize += PyUnicode_GET_LENGTH(value) + 1; /* +1 for '\0' */ |
|---|
| 362 | n/a | } |
|---|
| 363 | n/a | |
|---|
| 364 | n/a | buffer = PyMem_Malloc(totalsize * sizeof(Py_UCS4)); |
|---|
| 365 | n/a | if (! buffer) |
|---|
| 366 | n/a | goto error; |
|---|
| 367 | n/a | p = buffer; |
|---|
| 368 | n/a | end = buffer + totalsize; |
|---|
| 369 | n/a | |
|---|
| 370 | n/a | for (i = 0; i < envsize; i++) { |
|---|
| 371 | n/a | PyObject* key = PyList_GET_ITEM(keys, i); |
|---|
| 372 | n/a | PyObject* value = PyList_GET_ITEM(values, i); |
|---|
| 373 | n/a | if (!PyUnicode_AsUCS4(key, p, end - p, 0)) |
|---|
| 374 | n/a | goto error; |
|---|
| 375 | n/a | p += PyUnicode_GET_LENGTH(key); |
|---|
| 376 | n/a | *p++ = '='; |
|---|
| 377 | n/a | if (!PyUnicode_AsUCS4(value, p, end - p, 0)) |
|---|
| 378 | n/a | goto error; |
|---|
| 379 | n/a | p += PyUnicode_GET_LENGTH(value); |
|---|
| 380 | n/a | *p++ = '\0'; |
|---|
| 381 | n/a | } |
|---|
| 382 | n/a | |
|---|
| 383 | n/a | /* add trailing null byte */ |
|---|
| 384 | n/a | *p++ = '\0'; |
|---|
| 385 | n/a | assert(p == end); |
|---|
| 386 | n/a | |
|---|
| 387 | n/a | Py_XDECREF(keys); |
|---|
| 388 | n/a | Py_XDECREF(values); |
|---|
| 389 | n/a | |
|---|
| 390 | n/a | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, p - buffer); |
|---|
| 391 | n/a | PyMem_Free(buffer); |
|---|
| 392 | n/a | return res; |
|---|
| 393 | n/a | |
|---|
| 394 | n/a | error: |
|---|
| 395 | n/a | PyMem_Free(buffer); |
|---|
| 396 | n/a | Py_XDECREF(keys); |
|---|
| 397 | n/a | Py_XDECREF(values); |
|---|
| 398 | n/a | return NULL; |
|---|
| 399 | n/a | } |
|---|
| 400 | n/a | |
|---|
| 401 | n/a | PyDoc_STRVAR(CreateProcess_doc, |
|---|
| 402 | n/a | "CreateProcess(app_name, cmd_line, proc_attrs, thread_attrs,\n\ |
|---|
| 403 | n/a | inherit, flags, env_mapping, curdir,\n\ |
|---|
| 404 | n/a | startup_info) -> (proc_handle, thread_handle,\n\ |
|---|
| 405 | n/a | pid, tid)\n\ |
|---|
| 406 | n/a | \n\ |
|---|
| 407 | n/a | Create a new process and its primary thread. The return\n\ |
|---|
| 408 | n/a | value is a tuple of the process handle, thread handle,\n\ |
|---|
| 409 | n/a | process ID, and thread ID.\n\ |
|---|
| 410 | n/a | \n\ |
|---|
| 411 | n/a | proc_attrs and thread_attrs are ignored internally and can be None."); |
|---|
| 412 | n/a | |
|---|
| 413 | n/a | static PyObject * |
|---|
| 414 | n/a | sp_CreateProcess(PyObject* self, PyObject* args) |
|---|
| 415 | n/a | { |
|---|
| 416 | n/a | BOOL result; |
|---|
| 417 | n/a | PROCESS_INFORMATION pi; |
|---|
| 418 | n/a | STARTUPINFOW si; |
|---|
| 419 | n/a | PyObject* environment; |
|---|
| 420 | n/a | wchar_t *wenvironment; |
|---|
| 421 | n/a | |
|---|
| 422 | n/a | wchar_t* application_name; |
|---|
| 423 | n/a | wchar_t* command_line; |
|---|
| 424 | n/a | PyObject* process_attributes; /* ignored */ |
|---|
| 425 | n/a | PyObject* thread_attributes; /* ignored */ |
|---|
| 426 | n/a | int inherit_handles; |
|---|
| 427 | n/a | int creation_flags; |
|---|
| 428 | n/a | PyObject* env_mapping; |
|---|
| 429 | n/a | wchar_t* current_directory; |
|---|
| 430 | n/a | PyObject* startup_info; |
|---|
| 431 | n/a | |
|---|
| 432 | n/a | if (! PyArg_ParseTuple(args, "ZZOOiiOZO:CreateProcess", |
|---|
| 433 | n/a | &application_name, |
|---|
| 434 | n/a | &command_line, |
|---|
| 435 | n/a | &process_attributes, |
|---|
| 436 | n/a | &thread_attributes, |
|---|
| 437 | n/a | &inherit_handles, |
|---|
| 438 | n/a | &creation_flags, |
|---|
| 439 | n/a | &env_mapping, |
|---|
| 440 | n/a | ¤t_directory, |
|---|
| 441 | n/a | &startup_info)) |
|---|
| 442 | n/a | return NULL; |
|---|
| 443 | n/a | |
|---|
| 444 | n/a | ZeroMemory(&si, sizeof(si)); |
|---|
| 445 | n/a | si.cb = sizeof(si); |
|---|
| 446 | n/a | |
|---|
| 447 | n/a | /* note: we only support a small subset of all SI attributes */ |
|---|
| 448 | n/a | si.dwFlags = getint(startup_info, "dwFlags"); |
|---|
| 449 | n/a | si.wShowWindow = getint(startup_info, "wShowWindow"); |
|---|
| 450 | n/a | si.hStdInput = gethandle(startup_info, "hStdInput"); |
|---|
| 451 | n/a | si.hStdOutput = gethandle(startup_info, "hStdOutput"); |
|---|
| 452 | n/a | si.hStdError = gethandle(startup_info, "hStdError"); |
|---|
| 453 | n/a | |
|---|
| 454 | n/a | if (PyErr_Occurred()) |
|---|
| 455 | n/a | return NULL; |
|---|
| 456 | n/a | |
|---|
| 457 | n/a | if (env_mapping != Py_None) { |
|---|
| 458 | n/a | environment = getenvironment(env_mapping); |
|---|
| 459 | n/a | if (! environment) |
|---|
| 460 | n/a | return NULL; |
|---|
| 461 | n/a | wenvironment = PyUnicode_AsUnicode(environment); |
|---|
| 462 | n/a | if (wenvironment == NULL) |
|---|
| 463 | n/a | { |
|---|
| 464 | n/a | Py_XDECREF(environment); |
|---|
| 465 | n/a | return NULL; |
|---|
| 466 | n/a | } |
|---|
| 467 | n/a | } |
|---|
| 468 | n/a | else { |
|---|
| 469 | n/a | environment = NULL; |
|---|
| 470 | n/a | wenvironment = NULL; |
|---|
| 471 | n/a | } |
|---|
| 472 | n/a | |
|---|
| 473 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 474 | n/a | result = CreateProcessW(application_name, |
|---|
| 475 | n/a | command_line, |
|---|
| 476 | n/a | NULL, |
|---|
| 477 | n/a | NULL, |
|---|
| 478 | n/a | inherit_handles, |
|---|
| 479 | n/a | creation_flags | CREATE_UNICODE_ENVIRONMENT, |
|---|
| 480 | n/a | wenvironment, |
|---|
| 481 | n/a | current_directory, |
|---|
| 482 | n/a | &si, |
|---|
| 483 | n/a | &pi); |
|---|
| 484 | n/a | Py_END_ALLOW_THREADS |
|---|
| 485 | n/a | |
|---|
| 486 | n/a | Py_XDECREF(environment); |
|---|
| 487 | n/a | |
|---|
| 488 | n/a | if (! result) |
|---|
| 489 | n/a | return PyErr_SetFromWindowsErr(GetLastError()); |
|---|
| 490 | n/a | |
|---|
| 491 | n/a | return Py_BuildValue("NNii", |
|---|
| 492 | n/a | sp_handle_new(pi.hProcess), |
|---|
| 493 | n/a | sp_handle_new(pi.hThread), |
|---|
| 494 | n/a | pi.dwProcessId, |
|---|
| 495 | n/a | pi.dwThreadId); |
|---|
| 496 | n/a | } |
|---|
| 497 | n/a | |
|---|
| 498 | n/a | PyDoc_STRVAR(TerminateProcess_doc, |
|---|
| 499 | n/a | "TerminateProcess(handle, exit_code) -> None\n\ |
|---|
| 500 | n/a | \n\ |
|---|
| 501 | n/a | Terminate the specified process and all of its threads."); |
|---|
| 502 | n/a | |
|---|
| 503 | n/a | static PyObject * |
|---|
| 504 | n/a | sp_TerminateProcess(PyObject* self, PyObject* args) |
|---|
| 505 | n/a | { |
|---|
| 506 | n/a | BOOL result; |
|---|
| 507 | n/a | |
|---|
| 508 | n/a | HANDLE process; |
|---|
| 509 | n/a | int exit_code; |
|---|
| 510 | n/a | if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM "i:TerminateProcess", |
|---|
| 511 | n/a | &process, &exit_code)) |
|---|
| 512 | n/a | return NULL; |
|---|
| 513 | n/a | |
|---|
| 514 | n/a | result = TerminateProcess(process, exit_code); |
|---|
| 515 | n/a | |
|---|
| 516 | n/a | if (! result) |
|---|
| 517 | n/a | return PyErr_SetFromWindowsErr(GetLastError()); |
|---|
| 518 | n/a | |
|---|
| 519 | n/a | Py_INCREF(Py_None); |
|---|
| 520 | n/a | return Py_None; |
|---|
| 521 | n/a | } |
|---|
| 522 | n/a | |
|---|
| 523 | n/a | PyDoc_STRVAR(GetExitCodeProcess_doc, |
|---|
| 524 | n/a | "GetExitCodeProcess(handle) -> Exit code\n\ |
|---|
| 525 | n/a | \n\ |
|---|
| 526 | n/a | Return the termination status of the specified process."); |
|---|
| 527 | n/a | |
|---|
| 528 | n/a | static PyObject * |
|---|
| 529 | n/a | sp_GetExitCodeProcess(PyObject* self, PyObject* args) |
|---|
| 530 | n/a | { |
|---|
| 531 | n/a | DWORD exit_code; |
|---|
| 532 | n/a | BOOL result; |
|---|
| 533 | n/a | |
|---|
| 534 | n/a | HANDLE process; |
|---|
| 535 | n/a | if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM ":GetExitCodeProcess", &process)) |
|---|
| 536 | n/a | return NULL; |
|---|
| 537 | n/a | |
|---|
| 538 | n/a | result = GetExitCodeProcess(process, &exit_code); |
|---|
| 539 | n/a | |
|---|
| 540 | n/a | if (! result) |
|---|
| 541 | n/a | return PyErr_SetFromWindowsErr(GetLastError()); |
|---|
| 542 | n/a | |
|---|
| 543 | n/a | return PyLong_FromLong(exit_code); |
|---|
| 544 | n/a | } |
|---|
| 545 | n/a | |
|---|
| 546 | n/a | PyDoc_STRVAR(WaitForSingleObject_doc, |
|---|
| 547 | n/a | "WaitForSingleObject(handle, timeout) -> result\n\ |
|---|
| 548 | n/a | \n\ |
|---|
| 549 | n/a | Wait until the specified object is in the signaled state or\n\ |
|---|
| 550 | n/a | the time-out interval elapses. The timeout value is specified\n\ |
|---|
| 551 | n/a | in milliseconds."); |
|---|
| 552 | n/a | |
|---|
| 553 | n/a | static PyObject * |
|---|
| 554 | n/a | sp_WaitForSingleObject(PyObject* self, PyObject* args) |
|---|
| 555 | n/a | { |
|---|
| 556 | n/a | DWORD result; |
|---|
| 557 | n/a | |
|---|
| 558 | n/a | HANDLE handle; |
|---|
| 559 | n/a | int milliseconds; |
|---|
| 560 | n/a | if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM "i:WaitForSingleObject", |
|---|
| 561 | n/a | &handle, |
|---|
| 562 | n/a | &milliseconds)) |
|---|
| 563 | n/a | return NULL; |
|---|
| 564 | n/a | |
|---|
| 565 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 566 | n/a | result = WaitForSingleObject(handle, (DWORD) milliseconds); |
|---|
| 567 | n/a | Py_END_ALLOW_THREADS |
|---|
| 568 | n/a | |
|---|
| 569 | n/a | if (result == WAIT_FAILED) |
|---|
| 570 | n/a | return PyErr_SetFromWindowsErr(GetLastError()); |
|---|
| 571 | n/a | |
|---|
| 572 | n/a | return PyLong_FromLong((int) result); |
|---|
| 573 | n/a | } |
|---|
| 574 | n/a | |
|---|
| 575 | n/a | PyDoc_STRVAR(GetVersion_doc, |
|---|
| 576 | n/a | "GetVersion() -> version\n\ |
|---|
| 577 | n/a | \n\ |
|---|
| 578 | n/a | Return the version number of the current operating system."); |
|---|
| 579 | n/a | |
|---|
| 580 | n/a | static PyObject * |
|---|
| 581 | n/a | sp_GetVersion(PyObject* self, PyObject* args) |
|---|
| 582 | n/a | { |
|---|
| 583 | n/a | if (! PyArg_ParseTuple(args, ":GetVersion")) |
|---|
| 584 | n/a | return NULL; |
|---|
| 585 | n/a | |
|---|
| 586 | n/a | return PyLong_FromLong((int) GetVersion()); |
|---|
| 587 | n/a | } |
|---|
| 588 | n/a | |
|---|
| 589 | n/a | PyDoc_STRVAR(GetModuleFileName_doc, |
|---|
| 590 | n/a | "GetModuleFileName(module) -> path\n\ |
|---|
| 591 | n/a | \n\ |
|---|
| 592 | n/a | Return the fully-qualified path for the file that contains\n\ |
|---|
| 593 | n/a | the specified module. The module must have been loaded by the\n\ |
|---|
| 594 | n/a | current process.\n\ |
|---|
| 595 | n/a | \n\ |
|---|
| 596 | n/a | The module parameter should be a handle to the loaded module\n\ |
|---|
| 597 | n/a | whose path is being requested. If this parameter is 0, \n\ |
|---|
| 598 | n/a | GetModuleFileName retrieves the path of the executable file\n\ |
|---|
| 599 | n/a | of the current process."); |
|---|
| 600 | n/a | |
|---|
| 601 | n/a | static PyObject * |
|---|
| 602 | n/a | sp_GetModuleFileName(PyObject* self, PyObject* args) |
|---|
| 603 | n/a | { |
|---|
| 604 | n/a | BOOL result; |
|---|
| 605 | n/a | HMODULE module; |
|---|
| 606 | n/a | WCHAR filename[MAX_PATH]; |
|---|
| 607 | n/a | |
|---|
| 608 | n/a | if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM ":GetModuleFileName", |
|---|
| 609 | n/a | &module)) |
|---|
| 610 | n/a | return NULL; |
|---|
| 611 | n/a | |
|---|
| 612 | n/a | result = GetModuleFileNameW(module, filename, MAX_PATH); |
|---|
| 613 | n/a | filename[MAX_PATH-1] = '\0'; |
|---|
| 614 | n/a | |
|---|
| 615 | n/a | if (! result) |
|---|
| 616 | n/a | return PyErr_SetFromWindowsErr(GetLastError()); |
|---|
| 617 | n/a | |
|---|
| 618 | n/a | return PyUnicode_FromWideChar(filename, wcslen(filename)); |
|---|
| 619 | n/a | } |
|---|
| 620 | n/a | |
|---|
| 621 | n/a | static PyMethodDef sp_functions[] = { |
|---|
| 622 | n/a | {"GetStdHandle", sp_GetStdHandle, METH_VARARGS, GetStdHandle_doc}, |
|---|
| 623 | n/a | {"GetCurrentProcess", sp_GetCurrentProcess, METH_VARARGS, |
|---|
| 624 | n/a | GetCurrentProcess_doc}, |
|---|
| 625 | n/a | {"DuplicateHandle", sp_DuplicateHandle, METH_VARARGS, |
|---|
| 626 | n/a | DuplicateHandle_doc}, |
|---|
| 627 | n/a | {"CreatePipe", sp_CreatePipe, METH_VARARGS, CreatePipe_doc}, |
|---|
| 628 | n/a | {"CreateProcess", sp_CreateProcess, METH_VARARGS, CreateProcess_doc}, |
|---|
| 629 | n/a | {"TerminateProcess", sp_TerminateProcess, METH_VARARGS, |
|---|
| 630 | n/a | TerminateProcess_doc}, |
|---|
| 631 | n/a | {"GetExitCodeProcess", sp_GetExitCodeProcess, METH_VARARGS, |
|---|
| 632 | n/a | GetExitCodeProcess_doc}, |
|---|
| 633 | n/a | {"WaitForSingleObject", sp_WaitForSingleObject, METH_VARARGS, |
|---|
| 634 | n/a | WaitForSingleObject_doc}, |
|---|
| 635 | n/a | {"GetVersion", sp_GetVersion, METH_VARARGS, GetVersion_doc}, |
|---|
| 636 | n/a | {"GetModuleFileName", sp_GetModuleFileName, METH_VARARGS, |
|---|
| 637 | n/a | GetModuleFileName_doc}, |
|---|
| 638 | n/a | {NULL, NULL} |
|---|
| 639 | n/a | }; |
|---|
| 640 | n/a | |
|---|
| 641 | n/a | /* -------------------------------------------------------------------- */ |
|---|
| 642 | n/a | |
|---|
| 643 | n/a | static void |
|---|
| 644 | n/a | defint(PyObject* d, const char* name, int value) |
|---|
| 645 | n/a | { |
|---|
| 646 | n/a | PyObject* v = PyLong_FromLong((long) value); |
|---|
| 647 | n/a | if (v) { |
|---|
| 648 | n/a | PyDict_SetItemString(d, (char*) name, v); |
|---|
| 649 | n/a | Py_DECREF(v); |
|---|
| 650 | n/a | } |
|---|
| 651 | n/a | } |
|---|
| 652 | n/a | |
|---|
| 653 | n/a | static struct PyModuleDef _subprocessmodule = { |
|---|
| 654 | n/a | PyModuleDef_HEAD_INIT, |
|---|
| 655 | n/a | "_subprocess", |
|---|
| 656 | n/a | NULL, |
|---|
| 657 | n/a | -1, |
|---|
| 658 | n/a | sp_functions, |
|---|
| 659 | n/a | NULL, |
|---|
| 660 | n/a | NULL, |
|---|
| 661 | n/a | NULL, |
|---|
| 662 | n/a | NULL |
|---|
| 663 | n/a | }; |
|---|
| 664 | n/a | |
|---|
| 665 | n/a | PyMODINIT_FUNC |
|---|
| 666 | n/a | PyInit__subprocess() |
|---|
| 667 | n/a | { |
|---|
| 668 | n/a | PyObject *d; |
|---|
| 669 | n/a | PyObject *m; |
|---|
| 670 | n/a | |
|---|
| 671 | n/a | /* patch up object descriptors */ |
|---|
| 672 | n/a | sp_handle_as_number.nb_int = (unaryfunc) sp_handle_as_int; |
|---|
| 673 | n/a | if (PyType_Ready(&sp_handle_type) < 0) |
|---|
| 674 | n/a | return NULL; |
|---|
| 675 | n/a | |
|---|
| 676 | n/a | m = PyModule_Create(&_subprocessmodule); |
|---|
| 677 | n/a | if (m == NULL) |
|---|
| 678 | n/a | return NULL; |
|---|
| 679 | n/a | d = PyModule_GetDict(m); |
|---|
| 680 | n/a | |
|---|
| 681 | n/a | /* constants */ |
|---|
| 682 | n/a | defint(d, "STD_INPUT_HANDLE", STD_INPUT_HANDLE); |
|---|
| 683 | n/a | defint(d, "STD_OUTPUT_HANDLE", STD_OUTPUT_HANDLE); |
|---|
| 684 | n/a | defint(d, "STD_ERROR_HANDLE", STD_ERROR_HANDLE); |
|---|
| 685 | n/a | defint(d, "DUPLICATE_SAME_ACCESS", DUPLICATE_SAME_ACCESS); |
|---|
| 686 | n/a | defint(d, "STARTF_USESTDHANDLES", STARTF_USESTDHANDLES); |
|---|
| 687 | n/a | defint(d, "STARTF_USESHOWWINDOW", STARTF_USESHOWWINDOW); |
|---|
| 688 | n/a | defint(d, "SW_HIDE", SW_HIDE); |
|---|
| 689 | n/a | defint(d, "INFINITE", INFINITE); |
|---|
| 690 | n/a | defint(d, "WAIT_OBJECT_0", WAIT_OBJECT_0); |
|---|
| 691 | n/a | defint(d, "WAIT_TIMEOUT", WAIT_TIMEOUT); |
|---|
| 692 | n/a | defint(d, "CREATE_NEW_CONSOLE", CREATE_NEW_CONSOLE); |
|---|
| 693 | n/a | defint(d, "CREATE_NEW_PROCESS_GROUP", CREATE_NEW_PROCESS_GROUP); |
|---|
| 694 | n/a | defint(d, "STILL_ACTIVE", STILL_ACTIVE); |
|---|
| 695 | n/a | |
|---|
| 696 | n/a | return m; |
|---|
| 697 | n/a | } |
|---|