Embedding - getting full error string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Elie B.

    Embedding - getting full error string

    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_SimpleStr ing to execute this code:

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

    than adding in C++:

    static PyMethodDef mymodule_method s[] = {
    {"printit", mymodule_printi t, METH_VARARGS, "prints"},
    {NULL, NULL, 0 ,NULL} /* sentinel */
    };

    static PyObject *mymodule_print it(PyObject *self, PyObject* args)
    {
    char *s;
    PyArg_ParseTupl e(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
  • Paul Miller

    #2
    Re: Embedding - getting full error string

    [color=blue]
    >static PyObject *mymodule_print it(PyObject *self, PyObject* args)
    >{
    > char *s;
    > PyArg_ParseTupl e(args, "s", &s);
    > PrintToWindow(s ); <-- my function
    > return NULL;
    >}[/color]

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

    Py_INCREF(Py_No ne);
    return Py_None;

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




    Comment

    Working...