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