By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,050 Members | 1,019 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,050 IT Pros & Developers. It's quick & easy.

Embedding - getting full error string

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.


Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.