1991-02-19 08:39:46 -04:00
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
/* Traceback implementation */
|
|
|
|
|
1997-04-29 15:33:38 -03:00
|
|
|
#include "Python.h"
|
1990-12-20 11:06:42 -04:00
|
|
|
|
2005-10-20 16:59:25 -03:00
|
|
|
#include "code.h"
|
1990-12-20 11:06:42 -04:00
|
|
|
#include "frameobject.h"
|
|
|
|
#include "structmember.h"
|
1992-02-26 11:17:59 -04:00
|
|
|
#include "osdefs.h"
|
2004-03-21 14:37:23 -04:00
|
|
|
#include "traceback.h"
|
1990-12-20 11:06:42 -04:00
|
|
|
|
2004-03-21 14:37:23 -04:00
|
|
|
#define OFF(x) offsetof(PyTracebackObject, x)
|
1990-12-20 11:06:42 -04:00
|
|
|
|
2009-03-18 17:52:15 -03:00
|
|
|
static PyMemberDef tb_memberlist[] = {
|
|
|
|
{"tb_next", T_OBJECT, OFF(tb_next), READONLY},
|
|
|
|
{"tb_frame", T_OBJECT, OFF(tb_frame), READONLY},
|
|
|
|
{"tb_lasti", T_INT, OFF(tb_lasti), READONLY},
|
|
|
|
{"tb_lineno", T_INT, OFF(tb_lineno), READONLY},
|
1990-12-20 11:06:42 -04:00
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2004-03-21 14:37:23 -04:00
|
|
|
tb_dealloc(PyTracebackObject *tb)
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
2002-03-28 23:07:29 -04:00
|
|
|
PyObject_GC_UnTrack(tb);
|
2000-03-13 12:01:29 -04:00
|
|
|
Py_TRASHCAN_SAFE_BEGIN(tb)
|
1997-04-29 15:33:38 -03:00
|
|
|
Py_XDECREF(tb->tb_next);
|
|
|
|
Py_XDECREF(tb->tb_frame);
|
2001-10-22 19:17:41 -03:00
|
|
|
PyObject_GC_Del(tb);
|
2000-03-13 12:01:29 -04:00
|
|
|
Py_TRASHCAN_SAFE_END(tb)
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
2001-10-22 19:17:41 -03:00
|
|
|
static int
|
2004-03-21 14:37:23 -04:00
|
|
|
tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg)
|
2001-10-22 19:17:41 -03:00
|
|
|
{
|
2006-04-15 18:47:09 -03:00
|
|
|
Py_VISIT(tb->tb_next);
|
|
|
|
Py_VISIT(tb->tb_frame);
|
|
|
|
return 0;
|
2001-10-22 19:17:41 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-21 14:37:23 -04:00
|
|
|
tb_clear(PyTracebackObject *tb)
|
2001-10-22 19:17:41 -03:00
|
|
|
{
|
2006-04-15 14:28:34 -03:00
|
|
|
Py_CLEAR(tb->tb_next);
|
|
|
|
Py_CLEAR(tb->tb_frame);
|
2001-10-22 19:17:41 -03:00
|
|
|
}
|
|
|
|
|
2001-10-22 16:34:09 -03:00
|
|
|
PyTypeObject PyTraceBack_Type = {
|
2007-07-21 03:55:02 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
1990-12-20 11:06:42 -04:00
|
|
|
"traceback",
|
2004-03-21 14:37:23 -04:00
|
|
|
sizeof(PyTracebackObject),
|
1990-12-20 11:06:42 -04:00
|
|
|
0,
|
1994-08-29 09:09:58 -03:00
|
|
|
(destructor)tb_dealloc, /*tp_dealloc*/
|
1990-12-20 11:06:42 -04:00
|
|
|
0, /*tp_print*/
|
2009-03-18 17:52:15 -03:00
|
|
|
0, /*tp_getattr*/
|
1990-12-20 11:06:42 -04:00
|
|
|
0, /*tp_setattr*/
|
|
|
|
0, /*tp_compare*/
|
|
|
|
0, /*tp_repr*/
|
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
2001-10-22 19:17:41 -03:00
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
0, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
|
|
|
|
0, /* tp_doc */
|
|
|
|
(traverseproc)tb_traverse, /* tp_traverse */
|
|
|
|
(inquiry)tb_clear, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
0, /* tp_methods */
|
2009-03-18 17:52:15 -03:00
|
|
|
tb_memberlist, /* tp_members */
|
|
|
|
0, /* tp_getset */
|
2001-10-22 19:17:41 -03:00
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
1990-12-20 11:06:42 -04:00
|
|
|
};
|
|
|
|
|
2004-03-21 14:37:23 -04:00
|
|
|
static PyTracebackObject *
|
|
|
|
newtracebackobject(PyTracebackObject *next, PyFrameObject *frame)
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
2004-03-21 14:37:23 -04:00
|
|
|
PyTracebackObject *tb;
|
2001-10-22 16:34:09 -03:00
|
|
|
if ((next != NULL && !PyTraceBack_Check(next)) ||
|
1997-04-29 15:33:38 -03:00
|
|
|
frame == NULL || !PyFrame_Check(frame)) {
|
|
|
|
PyErr_BadInternalCall();
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2004-03-21 14:37:23 -04:00
|
|
|
tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type);
|
1990-12-20 11:06:42 -04:00
|
|
|
if (tb != NULL) {
|
1997-04-29 15:33:38 -03:00
|
|
|
Py_XINCREF(next);
|
1990-12-20 11:06:42 -04:00
|
|
|
tb->tb_next = next;
|
1997-04-29 15:33:38 -03:00
|
|
|
Py_XINCREF(frame);
|
1990-12-20 11:06:42 -04:00
|
|
|
tb->tb_frame = frame;
|
2002-08-15 11:59:02 -03:00
|
|
|
tb->tb_lasti = frame->f_lasti;
|
2009-05-08 19:23:21 -03:00
|
|
|
tb->tb_lineno = PyFrame_GetLineNumber(frame);
|
2002-03-28 23:07:29 -04:00
|
|
|
PyObject_GC_Track(tb);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
return tb;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-07-22 15:47:25 -03:00
|
|
|
PyTraceBack_Here(PyFrameObject *frame)
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
2007-01-23 17:11:47 -04:00
|
|
|
PyThreadState *tstate = PyThreadState_GET();
|
2004-03-21 14:37:23 -04:00
|
|
|
PyTracebackObject *oldtb = (PyTracebackObject *) tstate->curexc_traceback;
|
|
|
|
PyTracebackObject *tb = newtracebackobject(oldtb, frame);
|
1990-12-20 11:06:42 -04:00
|
|
|
if (tb == NULL)
|
|
|
|
return -1;
|
1997-05-05 17:56:21 -03:00
|
|
|
tstate->curexc_traceback = (PyObject *)tb;
|
|
|
|
Py_XDECREF(oldtb);
|
1990-12-20 11:06:42 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-12 20:44:07 -03:00
|
|
|
int
|
2008-07-11 18:45:06 -03:00
|
|
|
_Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno, int indent)
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1997-05-22 19:35:47 -03:00
|
|
|
int err = 0;
|
2008-04-12 20:44:07 -03:00
|
|
|
FILE *xfp = NULL;
|
2001-11-27 16:30:42 -04:00
|
|
|
char linebuf[2000];
|
1990-12-20 11:06:42 -04:00
|
|
|
int i;
|
2008-01-18 04:47:59 -04:00
|
|
|
char namebuf[MAXPATHLEN+1];
|
|
|
|
|
2008-04-12 20:44:07 -03:00
|
|
|
if (filename == NULL)
|
1997-05-22 19:35:47 -03:00
|
|
|
return -1;
|
1994-08-29 09:09:58 -03:00
|
|
|
/* This is needed by Emacs' compile command */
|
2001-11-27 16:30:42 -04:00
|
|
|
#define FMT " File \"%.500s\", line %d, in %.500s\n"
|
2002-04-14 17:12:41 -03:00
|
|
|
xfp = fopen(filename, "r" PY_STDIOTEXTMODE);
|
1990-12-20 11:06:42 -04:00
|
|
|
if (xfp == NULL) {
|
1992-02-26 11:17:59 -04:00
|
|
|
/* Search tail of filename in sys.path before giving up */
|
1997-04-29 15:33:38 -03:00
|
|
|
PyObject *path;
|
2008-04-12 20:44:07 -03:00
|
|
|
const char *tail = strrchr(filename, SEP);
|
1992-02-26 11:17:59 -04:00
|
|
|
if (tail == NULL)
|
|
|
|
tail = filename;
|
|
|
|
else
|
|
|
|
tail++;
|
1997-04-29 15:33:38 -03:00
|
|
|
path = PySys_GetObject("path");
|
|
|
|
if (path != NULL && PyList_Check(path)) {
|
2006-02-16 10:37:16 -04:00
|
|
|
Py_ssize_t _npath = PyList_Size(path);
|
|
|
|
int npath = Py_SAFE_DOWNCAST(_npath, Py_ssize_t, int);
|
2000-06-28 19:07:35 -03:00
|
|
|
size_t taillen = strlen(tail);
|
1992-02-26 11:17:59 -04:00
|
|
|
for (i = 0; i < npath; i++) {
|
1997-04-29 15:33:38 -03:00
|
|
|
PyObject *v = PyList_GetItem(path, i);
|
1997-05-22 19:35:47 -03:00
|
|
|
if (v == NULL) {
|
|
|
|
PyErr_Clear();
|
|
|
|
break;
|
|
|
|
}
|
2008-06-09 01:58:54 -03:00
|
|
|
if (PyString_Check(v)) {
|
2000-06-28 19:07:35 -03:00
|
|
|
size_t len;
|
2008-06-09 01:58:54 -03:00
|
|
|
len = PyString_GET_SIZE(v);
|
1994-09-29 06:38:04 -03:00
|
|
|
if (len + 1 + taillen >= MAXPATHLEN)
|
|
|
|
continue; /* Too long */
|
2008-06-09 01:58:54 -03:00
|
|
|
strcpy(namebuf, PyString_AsString(v));
|
2000-06-28 19:07:35 -03:00
|
|
|
if (strlen(namebuf) != len)
|
1994-09-29 06:38:04 -03:00
|
|
|
continue; /* v contains '\0' */
|
1992-02-26 11:17:59 -04:00
|
|
|
if (len > 0 && namebuf[len-1] != SEP)
|
|
|
|
namebuf[len++] = SEP;
|
|
|
|
strcpy(namebuf+len, tail);
|
2002-04-14 17:12:41 -03:00
|
|
|
xfp = fopen(namebuf, "r" PY_STDIOTEXTMODE);
|
1992-02-26 11:17:59 -04:00
|
|
|
if (xfp != NULL) {
|
|
|
|
filename = namebuf;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
2008-04-12 20:44:07 -03:00
|
|
|
|
|
|
|
if (xfp == NULL)
|
|
|
|
return err;
|
|
|
|
if (err != 0) {
|
|
|
|
fclose(xfp);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
for (i = 0; i < lineno; i++) {
|
1999-09-18 17:49:39 -03:00
|
|
|
char* pLastChar = &linebuf[sizeof(linebuf)-2];
|
|
|
|
do {
|
|
|
|
*pLastChar = '\0';
|
2002-04-14 17:12:41 -03:00
|
|
|
if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, xfp, NULL) == NULL)
|
1999-09-18 17:49:39 -03:00
|
|
|
break;
|
|
|
|
/* fgets read *something*; if it didn't get as
|
|
|
|
far as pLastChar, it must have found a newline
|
|
|
|
or hit the end of the file; if pLastChar is \n,
|
|
|
|
it obviously found a newline; else we haven't
|
|
|
|
yet seen a newline, so must continue */
|
|
|
|
} while (*pLastChar != '\0' && *pLastChar != '\n');
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
if (i == lineno) {
|
2008-07-11 18:45:06 -03:00
|
|
|
char buf[11];
|
1992-02-26 11:17:59 -04:00
|
|
|
char *p = linebuf;
|
1995-07-07 19:45:41 -03:00
|
|
|
while (*p == ' ' || *p == '\t' || *p == '\014')
|
1990-12-20 11:06:42 -04:00
|
|
|
p++;
|
2008-07-11 18:45:06 -03:00
|
|
|
|
|
|
|
/* Write some spaces before the line */
|
|
|
|
strcpy(buf, " ");
|
|
|
|
assert (strlen(buf) == 10);
|
|
|
|
while (indent > 0) {
|
|
|
|
if(indent < 10)
|
|
|
|
buf[indent] = '\0';
|
|
|
|
err = PyFile_WriteString(buf, f);
|
|
|
|
if (err != 0)
|
|
|
|
break;
|
|
|
|
indent -= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err == 0)
|
|
|
|
err = PyFile_WriteString(p, f);
|
|
|
|
if (err == 0 && strchr(p, '\n') == NULL)
|
|
|
|
err = PyFile_WriteString("\n", f);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
fclose(xfp);
|
1997-05-22 19:35:47 -03:00
|
|
|
return err;
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1997-05-22 19:35:47 -03:00
|
|
|
static int
|
2008-04-12 20:44:07 -03:00
|
|
|
tb_displayline(PyObject *f, const char *filename, int lineno, const char *name)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
char linebuf[2000];
|
|
|
|
|
|
|
|
if (filename == NULL || name == NULL)
|
|
|
|
return -1;
|
|
|
|
/* This is needed by Emacs' compile command */
|
|
|
|
#define FMT " File \"%.500s\", line %d, in %.500s\n"
|
|
|
|
PyOS_snprintf(linebuf, sizeof(linebuf), FMT, filename, lineno, name);
|
|
|
|
err = PyFile_WriteString(linebuf, f);
|
|
|
|
if (err != 0)
|
|
|
|
return err;
|
2008-07-11 18:45:06 -03:00
|
|
|
return _Py_DisplaySourceLine(f, filename, lineno, 4);
|
2008-04-12 20:44:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1997-05-22 19:35:47 -03:00
|
|
|
int err = 0;
|
2008-04-12 20:44:07 -03:00
|
|
|
long depth = 0;
|
2004-03-21 14:37:23 -04:00
|
|
|
PyTracebackObject *tb1 = tb;
|
1993-12-17 08:09:14 -04:00
|
|
|
while (tb1 != NULL) {
|
|
|
|
depth++;
|
|
|
|
tb1 = tb1->tb_next;
|
|
|
|
}
|
1997-05-22 19:35:47 -03:00
|
|
|
while (tb != NULL && err == 0) {
|
1997-01-24 00:02:55 -04:00
|
|
|
if (depth <= limit) {
|
1997-05-22 19:35:47 -03:00
|
|
|
err = tb_displayline(f,
|
2008-06-09 01:58:54 -03:00
|
|
|
PyString_AsString(
|
1997-04-29 15:33:38 -03:00
|
|
|
tb->tb_frame->f_code->co_filename),
|
1994-08-29 09:09:58 -03:00
|
|
|
tb->tb_lineno,
|
2008-06-09 01:58:54 -03:00
|
|
|
PyString_AsString(tb->tb_frame->f_code->co_name));
|
1997-01-24 00:02:55 -04:00
|
|
|
}
|
1993-12-17 08:09:14 -04:00
|
|
|
depth--;
|
1990-12-20 11:06:42 -04:00
|
|
|
tb = tb->tb_next;
|
1997-05-22 19:35:47 -03:00
|
|
|
if (err == 0)
|
|
|
|
err = PyErr_CheckSignals();
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
1997-05-22 19:35:47 -03:00
|
|
|
return err;
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-07-22 15:47:25 -03:00
|
|
|
PyTraceBack_Print(PyObject *v, PyObject *f)
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1997-05-22 19:35:47 -03:00
|
|
|
int err;
|
1997-04-29 15:33:38 -03:00
|
|
|
PyObject *limitv;
|
2008-04-12 20:44:07 -03:00
|
|
|
long limit = 1000;
|
1990-12-20 11:06:42 -04:00
|
|
|
if (v == NULL)
|
|
|
|
return 0;
|
2001-10-22 16:34:09 -03:00
|
|
|
if (!PyTraceBack_Check(v)) {
|
1997-04-29 15:33:38 -03:00
|
|
|
PyErr_BadInternalCall();
|
1990-12-20 11:06:42 -04:00
|
|
|
return -1;
|
|
|
|
}
|
1997-04-29 15:33:38 -03:00
|
|
|
limitv = PySys_GetObject("tracebacklimit");
|
|
|
|
if (limitv && PyInt_Check(limitv)) {
|
|
|
|
limit = PyInt_AsLong(limitv);
|
1993-12-17 08:09:14 -04:00
|
|
|
if (limit <= 0)
|
|
|
|
return 0;
|
|
|
|
}
|
2000-03-30 20:39:23 -04:00
|
|
|
err = PyFile_WriteString("Traceback (most recent call last):\n", f);
|
1997-05-22 19:35:47 -03:00
|
|
|
if (!err)
|
2004-03-21 14:37:23 -04:00
|
|
|
err = tb_printinternal((PyTracebackObject *)v, f, limit);
|
1997-05-22 19:35:47 -03:00
|
|
|
return err;
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|