Redirecting STDIO/STDERR in an python embedded application
Donn Cave
donn at u.washington.edu
Mon Apr 2 18:53:37 EDT 2001
Quoth Karl Bellve <Karl.Bellve at umassmed.edu>:
|
| I am trying to redirect Pythons STDIO/STDERR within my application. Here
| is the python code:
|
| PyRun_SimpleString("class my_stdout:\n"
| " def write(self,string):\"
| " image.stdout(string)\n"
| "import sys\n"
| "sys.stdout = my_stdout()\n");
|
| And here is the C++ code:
|
| static struct _object *s_stdout(PyObject *self, PyObject *args)
| {
|
| char *string = NULL;
|
| if (!PyArg_ParseTuple(args, "s" &string))
| return Py_None;
| /* some code
|
| return Py_None
| }
|
| The function s_stdout is being called but PyArg_ParseTuple is failing. I
| believe it doesn't think there is a string in the "args" Object. When I
| look at the "args" object, it claims it is a "string" but I don't
| actually see any text associated with it, other than "string." Any
| ideas?
- I bet you didn't specify METH_VARARGS for s_stdout, like
{"s_stdout", s_stdout, METH_VARARGS},
You don't need it, but if you omit it, then for single-parameter
functions you don't get a tuple, just the parameter.
- Missing a comma before "&string" (I suppose that's a typo.)
- Return 0 after !PyArg_ParseTuple(), that's how you propagate an
exception.
- When you return Py_None, first write Py_INCREF(Py_None).
Donn Cave, donn at u.washington.edu
More information about the Python-list
mailing list