From: arcege at shore.net (Michael P. Reilly)
Date: Sat, 03 Apr 1999 16:24:16 GMT
Subject: Put exception information into a string?
References: <OkdL2.2102$h3.263316@homer.alpha.net> <GROL2.232$eJ.55045@news.shore.net> <gkvM2.257$Px6.35388@homer.alpha.net> <SnwM2.545$eJ.101107@news.shore.net> <8J6N2.457$Px6.74582@homer.alpha.net>
Message-ID: <QgrN2.943$eJ.155254@news.shore.net>
Content-Length: 2540
X-UID: 291                                                  

Fabian <micronet at globaldialog.com> wrote:
: I've been having some odd problems..
: It works in certain circumstances.  For example if I have an plainly invalid
: object, it reports it right, such as "blah\n", or "import bad-name\n", but
: if I have an error
: such as "if a ==10\n  b = 2\n" (missing ':'), it does not.  what happens in
: this circumstance is:
:>:>    lines = PyObject_CallFunction(
:>:>      traceback_format_exception, "OOO", exc, val, tb)
: returns NULL into lines, and from then on the function does not want to
: return -anything- but NULL.

: Thanks for all the help so far..  This is atleast useful in a way, but any
: ideas how to get it to work with these other situations?

Without seeing your code, I can't say more.  I tried this same string and
get the following on stderr.
'  File "<string>", line 1\n    if a == 10\n            ^\nSyntaxError: invalid
 syntax\n'

My little test code is below, maybe it can help.  It compiled and ran with
gcc 2.7.2.2 on Solaris for Intel 2.6 with Python 1.5.1.

And sorry for all the typos in the program, as I said, I wasn't able to
test it. :/

  -Arcege

#include <stdio.h>
#include <strings.h>
#include <Python.h>

int main(argc, argv)
  int argc;
  char *argv[];
  { char cmdstr[256];
    PyObject *traceback_format_exception;
    PyObject *string_joinfields;
    PyObject *mod, *moddict;
    PyObject *exc, *val, *tb;
    PyObject *lines, *exception_string;
    int rc;

    /* setup */
    Py_Initialize();
    mod = PyImport_ImportModule("traceback");
    moddict = PyModule_GetDict(mod);
    traceback_format_exception =
      PyDict_GetItemString(moddict, "format_exception");
    Py_DECREF(mod);
    Py_DECREF(moddict);
    mod = PyImport_ImportModule("string");
    moddict = PyModule_GetDict(mod);
    string_joinfields =
      PyDict_GetItemString(moddict, "joinfields");
    Py_DECREF(mod);
    Py_DECREF(moddict);

    /* strcpy(cmdstr, "import foobar"); */
    strcpy(cmdstr, "if a == 10\n  b = 2\n");
    rc = PyRun_SimpleString(cmdstr);

    /* print the traceback to stderr */
    if (PyErr_Occurred())
      { PyErr_Fetch(&exc, &val, &tb);
        lines = PyObject_CallFunction(
          traceback_format_exception, "OOO", exc, val, tb
        );
        exception_string = PyObject_CallFunction(
          string_joinfields, "Os", lines, ""
        );
        Py_DECREF(lines);
        fprintf(stderr, PyString_AsString(exception_string));
        Py_DECREF(exception_string);

        Py_DECREF(exc); Py_DECREF(val); Py_DECREF(tb);
      }
  }





