Embedding - getting full error string

E

Elie B.

Hi,
I'm new to Python. I'm trying to embbed Python in my Windows
application having some success with redirecting the stdin/out to my
windows application using:

In my C++ code I use PyRun_SimpleString to execute this code:

import sys
import mymodule
class LOGwriter :
def write(self, str):
mymodule.printit(str)
sys.stdout = sys.stderr = LOGwriter()

than adding in C++:

static PyMethodDef mymodule_methods[] = {
{"printit", mymodule_printit, METH_VARARGS, "prints"},
{NULL, NULL, 0 ,NULL} /* sentinel */
};

static PyObject *mymodule_printit(PyObject *self, PyObject* args)
{
char *s;
PyArg_ParseTuple(args, "s", &s);
PrintToWindow(s); <-- my function
return NULL;
}


Well it seems to work ok, until a string with quotes arrives (usualy
when an error occurs). When an error occurs it cuts the error string
after the quote, so I always get
File "
and not the full error string, which should be somthing like File
"<string>", line 3... . I've tried all kind of ways around this, yet
with no sucess. Thanks in advance for any help.

Eli
 
P

Paul Miller

static PyObject *mymodule_printit(PyObject *self, PyObject* args)
{
char *s;
PyArg_ParseTuple(args, "s", &s);
PrintToWindow(s); <-- my function
return NULL;
}

One problem is technically you're returning an error from your print
function. You need to return an increfed PyNone:

Py_INCREF(Py_None);
return Py_None;

Otherwise your code looks pretty much like mine does, and it works fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top