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