Re: Interactive Console in embedded Python

Discussion in 'Python' started by Thomas Heller, Aug 6, 2003.

  1. "Pablo Yabo" <> writes:

    > Is it possible to get the string of the error messages instead of using
    > PyErr_Print?


    > Hi,
    > I'm runing some python code from a C++ win32 application.
    > I need a tool to debug the code in python that I run.
    > I would like to know if I can run a interactive console to dump objects,
    > etc.


    Yes. Exactly how you would do it in Python itself, only more verbose.

    <code>
    /*
    Modeled after a function from Mark Hammond.

    Obtains a string from a Python traceback. This is the exact same string as
    "traceback.print_exception" would return.

    Result is a string which must be free'd using PyMem_Free()
    */
    #define TRACEBACK_FETCH_ERROR(what) {errMsg = what; goto done;}

    char *PyTraceback_AsString(void)
    {
    char *errMsg = NULL; /* holds a local error message */
    char *result = NULL; /* a valid, allocated result. */
    PyObject *modStringIO = NULL;
    PyObject *modTB = NULL;
    PyObject *obStringIO = NULL;
    PyObject *obResult = NULL;

    PyObject *type, *value, *traceback;

    PyErr_Fetch(&type, &value, &traceback);
    PyErr_NormalizeException(&type, &value, &traceback);

    modStringIO = PyImport_ImportModule("cStringIO");
    if (modStringIO==NULL)
    TRACEBACK_FETCH_ERROR("cant import cStringIO\n");

    obStringIO = PyObject_CallMethod(modStringIO, "StringIO", NULL);

    /* Construct a cStringIO object */
    if (obStringIO==NULL)
    TRACEBACK_FETCH_ERROR("cStringIO.StringIO() failed\n");

    modTB = PyImport_ImportModule("traceback");
    if (modTB==NULL)
    TRACEBACK_FETCH_ERROR("cant import traceback\n");

    obResult = PyObject_CallMethod(modTB, "print_exception",
    "OOOOO",
    type, value ? value : Py_None,
    traceback ? traceback : Py_None,
    Py_None,
    obStringIO);

    if (obResult==NULL)
    TRACEBACK_FETCH_ERROR("traceback.print_exception() failed\n");
    Py_DECREF(obResult);

    obResult = PyObject_CallMethod(obStringIO, "getvalue", NULL);
    if (obResult==NULL)
    TRACEBACK_FETCH_ERROR("getvalue() failed.\n");

    /* And it should be a string all ready to go - duplicate it. */
    if (!PyString_Check(obResult))
    TRACEBACK_FETCH_ERROR("getvalue() did not return a string\n");

    { // a temp scope so I can use temp locals.
    char *tempResult = PyString_AsString(obResult);
    result = (char *)PyMem_Malloc(strlen(tempResult)+1);
    if (result==NULL)
    TRACEBACK_FETCH_ERROR("memory error duplicating the traceback string\n");
    strcpy(result, tempResult);
    } // end of temp scope.
    done:
    /* All finished - first see if we encountered an error */
    if (result==NULL && errMsg != NULL) {
    result = (char *)PyMem_Malloc(strlen(errMsg)+1);
    if (result != NULL)
    /* if it does, not much we can do! */
    strcpy(result, errMsg);
    }
    Py_XDECREF(modStringIO);
    Py_XDECREF(modTB);
    Py_XDECREF(obStringIO);
    Py_XDECREF(obResult);
    Py_XDECREF(value);
    Py_XDECREF(traceback);
    Py_XDECREF(type);
    return result;
    }
    </code>
     
    Thomas Heller, Aug 6, 2003
    #1
    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. WJ
    Replies:
    2
    Views:
    502
  2. Oleg  Konovalov

    Interactive console using Swing

    Oleg Konovalov, May 24, 2004, in forum: Java
    Replies:
    2
    Views:
    1,741
    Hal Rosser
    Jun 1, 2004
  3. Lee Harr
    Replies:
    0
    Views:
    379
    Lee Harr
    Aug 6, 2003
  4. Devrim Erdem
    Replies:
    1
    Views:
    443
    Fredrik Lundh
    Sep 19, 2004
  5. Replies:
    1
    Views:
    438
Loading...

Share This Page