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