| 1 |
/* -*- Mode: C; c-basic-offset: 4 -*- |
| 2 |
* pyglib - Python bindings for GLib toolkit. |
| 3 |
* Copyright (C) 1998-2003 James Henstridge |
| 4 |
* 2004-2008 Johan Dahlin |
| 5 |
* |
| 6 |
* pygspawn.c: wrapper for the glib library. |
| 7 |
* |
| 8 |
* This library is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU Lesser General Public |
| 10 |
* License as published by the Free Software Foundation; either |
| 11 |
* version 2.1 of the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This library is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public |
| 19 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "pygi-basictype.h" |
| 23 |
#include "pygi-error.h" |
| 24 |
#include "pygi-util.h" |
| 25 |
#include "pygspawn.h" |
| 26 |
|
| 27 |
struct _PyGChildSetupData { |
| 28 |
PyObject *func; |
| 29 |
PyObject *data; |
| 30 |
}; |
| 31 |
|
| 32 |
typedef struct { |
| 33 |
PyLongObject obj; |
| 34 |
/* |
| 35 |
* This exists for two reasons: |
| 36 |
* 1. We must only close once (ideally we would not have "close" in python) |
| 37 |
* 2. In PyPy we cannot use PyLong_AsLong during free |
| 38 |
*/ |
| 39 |
GPid unclosed; |
| 40 |
} PyGPid; |
| 41 |
|
| 42 |
PYGI_DEFINE_TYPE ("gi._gi.Pid", PyGPid_Type, PyGPid) |
| 43 |
|
| 44 |
static PyObject * |
| 45 |
pyg_pid_close (PyObject *self, PyObject *args, PyObject *kwargs) |
| 46 |
{ |
| 47 |
PyGPid *gpid = (PyGPid *)self; |
| 48 |
|
| 49 |
if (gpid->unclosed) g_spawn_close_pid (gpid->unclosed); |
| 50 |
#ifdef G_OS_WIN32 |
| 51 |
gpid->unclosed = NULL; |
| 52 |
#else |
| 53 |
gpid->unclosed = 0; |
| 54 |
#endif |
| 55 |
|
| 56 |
Py_RETURN_NONE; |
| 57 |
} |
| 58 |
|
| 59 |
static PyMethodDef pyg_pid_methods[] = { |
| 60 |
{ "close", (PyCFunction)pyg_pid_close, METH_NOARGS }, |
| 61 |
{ NULL, NULL, 0 }, |
| 62 |
}; |
| 63 |
|
| 64 |
static void |
| 65 |
pyg_pid_free (PyObject *self) |
| 66 |
{ |
| 67 |
PyGPid *gpid = (PyGPid *)self; |
| 68 |
|
| 69 |
if (gpid->unclosed) g_spawn_close_pid (gpid->unclosed); |
| 70 |
|
| 71 |
PyLong_Type.tp_free ((void *)self); |
| 72 |
} |
| 73 |
|
| 74 |
static int |
| 75 |
pyg_pid_tp_init (PyObject *self, PyObject *args, PyObject *kwargs) |
| 76 |
{ |
| 77 |
PyErr_SetString (PyExc_TypeError, |
| 78 |
"gi._gi.Pid cannot be manually instantiated"); |
| 79 |
return -1; |
| 80 |
} |
| 81 |
|
| 82 |
static PyObject * |
| 83 |
pyg_pid_new (GPid pid) |
| 84 |
{ |
| 85 |
PyObject *long_val; |
| 86 |
#ifdef G_OS_WIN32 |
| 87 |
long_val = PyLong_FromVoidPtr (pid); |
| 88 |
#else |
| 89 |
long_val = pygi_gint_to_py (pid); |
| 90 |
#endif |
| 91 |
return PyObject_CallMethod ((PyObject *)&PyGPid_Type, "__new__", "ON", |
| 92 |
&PyGPid_Type, long_val); |
| 93 |
} |
| 94 |
|
| 95 |
static void |
| 96 |
_pyg_spawn_async_callback (gpointer user_data) |
| 97 |
{ |
| 98 |
struct _PyGChildSetupData *data; |
| 99 |
PyObject *retval; |
| 100 |
PyGILState_STATE gil; |
| 101 |
|
| 102 |
data = (struct _PyGChildSetupData *)user_data; |
| 103 |
gil = PyGILState_Ensure (); |
| 104 |
if (data->data) |
| 105 |
retval = PyObject_CallFunction (data->func, "O", data->data); |
| 106 |
else |
| 107 |
retval = PyObject_CallFunction (data->func, NULL); |
| 108 |
if (retval) |
| 109 |
Py_DECREF (retval); |
| 110 |
else |
| 111 |
PyErr_Print (); |
| 112 |
Py_DECREF (data->func); |
| 113 |
Py_XDECREF (data->data); |
| 114 |
g_slice_free (struct _PyGChildSetupData, data); |
| 115 |
PyGILState_Release (gil); |
| 116 |
} |
| 117 |
|
| 118 |
PyObject * |
| 119 |
pyglib_spawn_async (PyObject *object, PyObject *args, PyObject *kwargs) |
| 120 |
{ |
| 121 |
static char *kwlist[] = { |
| 122 |
"argv", "envp", "working_directory", "flags", |
| 123 |
"child_setup", "user_data", "standard_input", "standard_output", |
| 124 |
"standard_error", NULL |
| 125 |
}; |
| 126 |
PyObject *pyargv, *pyenvp = NULL; |
| 127 |
char **argv, **envp = NULL; |
| 128 |
PyObject *func = Py_None, *user_data = NULL; |
| 129 |
char *working_directory = NULL; |
| 130 |
int flags = 0, _stdin = -1, _stdout = -1, _stderr = -1; |
| 131 |
PyObject *pystdin = NULL, *pystdout = NULL, *pystderr = NULL; |
| 132 |
gint *standard_input, *standard_output, *standard_error; |
| 133 |
struct _PyGChildSetupData *callback_data = NULL; |
| 134 |
GError *error = NULL; |
| 135 |
GPid child_pid = 0; |
| 136 |
Py_ssize_t len, i; |
| 137 |
|
| 138 |
if (!PyArg_ParseTupleAndKeywords ( |
| 139 |
args, kwargs, "O|OsiOOOOO:gi._gi.spawn_async", kwlist, &pyargv, |
| 140 |
&pyenvp, &working_directory, &flags, &func, &user_data, &pystdin, |
| 141 |
&pystdout, &pystderr)) |
| 142 |
return NULL; |
| 143 |
|
| 144 |
if (pystdin && PyObject_IsTrue (pystdin)) |
| 145 |
standard_input = &_stdin; |
| 146 |
else |
| 147 |
standard_input = NULL; |
| 148 |
|
| 149 |
if (pystdout && PyObject_IsTrue (pystdout)) |
| 150 |
standard_output = &_stdout; |
| 151 |
else |
| 152 |
standard_output = NULL; |
| 153 |
|
| 154 |
if (pystderr && PyObject_IsTrue (pystderr)) |
| 155 |
standard_error = &_stderr; |
| 156 |
else |
| 157 |
standard_error = NULL; |
| 158 |
|
| 159 |
/* parse argv */ |
| 160 |
if (!PySequence_Check (pyargv)) { |
| 161 |
PyErr_SetString (PyExc_TypeError, |
| 162 |
"gi._gi.spawn_async: " |
| 163 |
"first argument must be a sequence of strings"); |
| 164 |
return NULL; |
| 165 |
} |
| 166 |
len = PySequence_Length (pyargv); |
| 167 |
argv = g_new0 (char *, len + 1); |
| 168 |
for (i = 0; i < len; ++i) { |
| 169 |
PyObject *tmp = PySequence_ITEM (pyargv, i); |
| 170 |
if (tmp == NULL || !PyUnicode_Check (tmp)) { |
| 171 |
PyErr_SetString (PyExc_TypeError, |
| 172 |
"gi._gi.spawn_async: " |
| 173 |
"first argument must be a sequence of strings"); |
| 174 |
g_free (argv); |
| 175 |
Py_XDECREF (tmp); |
| 176 |
return NULL; |
| 177 |
} |
| 178 |
argv[i] = PyUnicode_AsUTF8 (tmp); |
| 179 |
Py_DECREF (tmp); |
| 180 |
} |
| 181 |
|
| 182 |
/* parse envp */ |
| 183 |
if (pyenvp) { |
| 184 |
if (!PySequence_Check (pyenvp)) { |
| 185 |
PyErr_SetString (PyExc_TypeError, |
| 186 |
"gi._gi.spawn_async: " |
| 187 |
"second argument must be a sequence of strings"); |
| 188 |
g_free (argv); |
| 189 |
return NULL; |
| 190 |
} |
| 191 |
len = PySequence_Length (pyenvp); |
| 192 |
envp = g_new0 (char *, len + 1); |
| 193 |
for (i = 0; i < len; ++i) { |
| 194 |
PyObject *tmp = PySequence_ITEM (pyenvp, i); |
| 195 |
if (tmp == NULL || !PyUnicode_Check (tmp)) { |
| 196 |
PyErr_SetString ( |
| 197 |
PyExc_TypeError, |
| 198 |
"gi._gi.spawn_async: " |
| 199 |
"second argument must be a sequence of strings"); |
| 200 |
g_free (envp); |
| 201 |
Py_XDECREF (tmp); |
| 202 |
g_free (argv); |
| 203 |
return NULL; |
| 204 |
} |
| 205 |
envp[i] = PyUnicode_AsUTF8 (tmp); |
| 206 |
Py_DECREF (tmp); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if (!Py_IsNone (func)) { |
| 211 |
if (!PyCallable_Check (func)) { |
| 212 |
PyErr_SetString (PyExc_TypeError, |
| 213 |
"child_setup parameter must be callable or None"); |
| 214 |
g_free (argv); |
| 215 |
if (envp) g_free (envp); |
| 216 |
return NULL; |
| 217 |
} |
| 218 |
callback_data = g_slice_new (struct _PyGChildSetupData); |
| 219 |
callback_data->func = func; |
| 220 |
callback_data->data = user_data; |
| 221 |
Py_INCREF (callback_data->func); |
| 222 |
if (callback_data->data) Py_INCREF (callback_data->data); |
| 223 |
} |
| 224 |
|
| 225 |
if (!g_spawn_async_with_pipes ( |
| 226 |
working_directory, argv, envp, flags, |
| 227 |
!Py_IsNone (func) ? _pyg_spawn_async_callback : NULL, |
| 228 |
callback_data, &child_pid, standard_input, standard_output, |
| 229 |
standard_error, &error)) |
| 230 |
|
| 231 |
|
| 232 |
{ |
| 233 |
g_free (argv); |
| 234 |
if (envp) g_free (envp); |
| 235 |
if (callback_data) { |
| 236 |
Py_DECREF (callback_data->func); |
| 237 |
Py_XDECREF (callback_data->data); |
| 238 |
g_slice_free (struct _PyGChildSetupData, callback_data); |
| 239 |
} |
| 240 |
pygi_error_check (&error); |
| 241 |
return NULL; |
| 242 |
} |
| 243 |
g_free (argv); |
| 244 |
if (envp) g_free (envp); |
| 245 |
|
| 246 |
if (standard_input) |
| 247 |
pystdin = pygi_gint_to_py (*standard_input); |
| 248 |
else { |
| 249 |
pystdin = Py_NewRef (Py_None); |
| 250 |
} |
| 251 |
|
| 252 |
if (standard_output) |
| 253 |
pystdout = pygi_gint_to_py (*standard_output); |
| 254 |
else { |
| 255 |
pystdout = Py_NewRef (Py_None); |
| 256 |
} |
| 257 |
|
| 258 |
if (standard_error) |
| 259 |
pystderr = pygi_gint_to_py (*standard_error); |
| 260 |
else { |
| 261 |
pystderr = Py_NewRef (Py_None); |
| 262 |
} |
| 263 |
|
| 264 |
return Py_BuildValue ("NNNN", pyg_pid_new (child_pid), pystdin, pystdout, |
| 265 |
pystderr); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Returns 0 on success, or -1 and sets an exception. |
| 270 |
*/ |
| 271 |
int |
| 272 |
pygi_spawn_register_types (PyObject *d) |
| 273 |
{ |
| 274 |
PyGPid_Type.tp_base = &PyLong_Type; |
| 275 |
PyGPid_Type.tp_flags = Py_TPFLAGS_DEFAULT; |
| 276 |
PyGPid_Type.tp_methods = pyg_pid_methods; |
| 277 |
PyGPid_Type.tp_init = pyg_pid_tp_init; |
| 278 |
PyGPid_Type.tp_free = (freefunc)pyg_pid_free; |
| 279 |
PyGPid_Type.tp_new = PyLong_Type.tp_new; |
| 280 |
PyGPid_Type.tp_alloc = PyType_GenericAlloc; |
| 281 |
|
| 282 |
if (PyType_Ready (&PyGPid_Type)) return -1; |
| 283 |
|
| 284 |
PyDict_SetItemString (d, "Pid", (PyObject *)&PyGPid_Type); |
| 285 |
|
| 286 |
return 0; |
| 287 |
} |