Embedding - getting full error string

Discussion in 'Python' started by Elie B., Feb 8, 2004.

  1. Elie B.

    Elie B. Guest

    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
    Elie B., Feb 8, 2004
    #1
    1. Advertising

  2. Elie B.

    Paul Miller Guest


    >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.
    Paul Miller, Feb 8, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nathan Sokalski

    Getting the full URL

    Nathan Sokalski, Jun 28, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    860
    Aaron [SQL Server MVP]
    Jun 28, 2004
  2. Patrick Olurotimi Ige

    Getting Full NAME from Active Directory

    Patrick Olurotimi Ige, Mar 8, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    488
    Patrick Olurotimi Ige
    Mar 8, 2005
  3. Samuel R. Neff
    Replies:
    2
    Views:
    574
    bradley
    Jun 10, 2005
  4. =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    Replies:
    5
    Views:
    1,264
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    May 6, 2006
  5. Replies:
    1
    Views:
    157
    Ken Bloom
    May 28, 2007
Loading...

Share This Page