Commit 4372418d 4372418df0a5872ea27419d6ed8d16400dd9c965 by Wojciech Polak

Capture Python output in maidag.

* include/mailutils/python.h (mu_py_capture_stdout,
mu_py_capture_stderr): New prototypes.
* maidag/python.c (python_check_msg): Redirect output.
1 parent eecafe0f
......@@ -186,6 +186,9 @@ extern void mu_py_script_finish (void);
extern int mu_py_script_run (const char *filename,
mu_py_script_data *data);
extern void mu_py_capture_stdout (mu_debug_t debug);
extern void mu_py_capture_stderr (mu_debug_t debug);
extern int mu_py_script_process_mailbox (int argc, char *argv[],
const char *python_filename,
const char *module_name,
......
......@@ -33,6 +33,14 @@ python_check_msg (mu_message_t msg, struct mu_auth_data *auth,
mu_py_script_init (1, argv);
if (!log_to_stderr)
{
mu_debug_t debug;
mu_diag_get_debug (&debug);
mu_py_capture_stderr (debug);
mu_py_capture_stdout (debug);
}
py_msg = PyMessage_NEW ();
py_msg->msg = msg;
Py_INCREF (py_msg);
......
......@@ -100,6 +100,65 @@ static PyMethodDef methods[] = {
{ NULL, NULL, 0, NULL }
};
static mu_debug_t _mu_prog_debug_stdout;
static mu_debug_t _mu_prog_debug_stderr;
static PyObject *
_capture_stdout (PyObject *self, PyObject *args)
{
char *buf = "";
if (!PyArg_ParseTuple (args, "s", &buf))
return NULL;
if (_mu_prog_debug_stdout)
mu_debug_printf (_mu_prog_debug_stdout, MU_DIAG_INFO, "%.*s",
strlen (buf), buf);
return _ro (Py_None);
}
static PyObject *
_capture_stderr (PyObject *self, PyObject *args)
{
char *buf = "";
if (!PyArg_ParseTuple (args, "s", &buf))
return NULL;
if (_mu_prog_debug_stderr)
mu_debug_printf (_mu_prog_debug_stderr, MU_DIAG_ERROR, "%.*s",
strlen (buf), buf);
return _ro (Py_None);
}
static PyMethodDef capture_stdout_method[] =
{
{ "write", _capture_stdout, 1 },
{ NULL, NULL, 0, NULL }
};
static PyMethodDef capture_stderr_method[] =
{
{ "write", _capture_stderr, 1 },
{ NULL, NULL, 0, NULL }
};
void
mu_py_capture_stdout (mu_debug_t debug)
{
PyObject *py_out;
_mu_prog_debug_stdout = debug;
py_out = Py_InitModule ("stdout", capture_stdout_method);
if (py_out)
PySys_SetObject ("stdout", py_out);
}
void
mu_py_capture_stderr (mu_debug_t debug)
{
PyObject *py_err;
_mu_prog_debug_stderr = debug;
py_err = Py_InitModule ("stderr", capture_stderr_method);
if (py_err)
PySys_SetObject ("stderr", py_err);
}
int
mu_py_init_debug (void)
{
......