Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2 | /* Traceback implementation */ |
| 3 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5 | |
| 6 | #include "compile.h" |
| 7 | #include "frameobject.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 8 | #include "structmember.h" |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 9 | #include "osdefs.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 10 | |
| 11 | typedef struct _tracebackobject { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 12 | PyObject_HEAD |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 13 | struct _tracebackobject *tb_next; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 14 | PyFrameObject *tb_frame; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 15 | int tb_lasti; |
| 16 | int tb_lineno; |
| 17 | } tracebackobject; |
| 18 | |
| 19 | #define OFF(x) offsetof(tracebackobject, x) |
| 20 | |
| 21 | static struct memberlist tb_memberlist[] = { |
| 22 | {"tb_next", T_OBJECT, OFF(tb_next)}, |
| 23 | {"tb_frame", T_OBJECT, OFF(tb_frame)}, |
| 24 | {"tb_lasti", T_INT, OFF(tb_lasti)}, |
| 25 | {"tb_lineno", T_INT, OFF(tb_lineno)}, |
| 26 | {NULL} /* Sentinel */ |
| 27 | }; |
| 28 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 29 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 30 | tb_getattr(tracebackobject *tb, char *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 31 | { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 32 | return PyMember_Get((char *)tb, tb_memberlist, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 36 | tb_dealloc(tracebackobject *tb) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 37 | { |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 38 | Py_TRASHCAN_SAFE_BEGIN(tb) |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 39 | Py_XDECREF(tb->tb_next); |
| 40 | Py_XDECREF(tb->tb_frame); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 41 | PyObject_DEL(tb); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 42 | Py_TRASHCAN_SAFE_END(tb) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Guido van Rossum | 9d78d8d | 1995-09-18 21:29:36 +0000 | [diff] [blame] | 45 | #define Tracebacktype PyTraceBack_Type |
| 46 | #define is_tracebackobject PyTraceBack_Check |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 48 | PyTypeObject Tracebacktype = { |
| 49 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 50 | 0, |
| 51 | "traceback", |
| 52 | sizeof(tracebackobject), |
| 53 | 0, |
Guido van Rossum | 13836d9 | 1994-08-29 12:09:58 +0000 | [diff] [blame] | 54 | (destructor)tb_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 55 | 0, /*tp_print*/ |
Guido van Rossum | 13836d9 | 1994-08-29 12:09:58 +0000 | [diff] [blame] | 56 | (getattrfunc)tb_getattr, /*tp_getattr*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 57 | 0, /*tp_setattr*/ |
| 58 | 0, /*tp_compare*/ |
| 59 | 0, /*tp_repr*/ |
| 60 | 0, /*tp_as_number*/ |
| 61 | 0, /*tp_as_sequence*/ |
| 62 | 0, /*tp_as_mapping*/ |
| 63 | }; |
| 64 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 65 | static tracebackobject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 66 | newtracebackobject(tracebackobject *next, PyFrameObject *frame, int lasti, |
| 67 | int lineno) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 68 | { |
| 69 | tracebackobject *tb; |
| 70 | if ((next != NULL && !is_tracebackobject(next)) || |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 71 | frame == NULL || !PyFrame_Check(frame)) { |
| 72 | PyErr_BadInternalCall(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 73 | return NULL; |
| 74 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 75 | tb = PyObject_NEW(tracebackobject, &Tracebacktype); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 76 | if (tb != NULL) { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 77 | Py_XINCREF(next); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 78 | tb->tb_next = next; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 79 | Py_XINCREF(frame); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 80 | tb->tb_frame = frame; |
| 81 | tb->tb_lasti = lasti; |
| 82 | tb->tb_lineno = lineno; |
| 83 | } |
| 84 | return tb; |
| 85 | } |
| 86 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 87 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 88 | PyTraceBack_Here(PyFrameObject *frame) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 89 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 90 | PyThreadState *tstate = frame->f_tstate; |
| 91 | tracebackobject *oldtb = (tracebackobject *) tstate->curexc_traceback; |
| 92 | tracebackobject *tb = newtracebackobject(oldtb, |
| 93 | frame, frame->f_lasti, frame->f_lineno); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 94 | if (tb == NULL) |
| 95 | return -1; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 96 | tstate->curexc_traceback = (PyObject *)tb; |
| 97 | Py_XDECREF(oldtb); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 101 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 102 | tb_displayline(PyObject *f, char *filename, int lineno, char *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 103 | { |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 104 | int err = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 105 | FILE *xfp; |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 106 | char linebuf[1000]; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 107 | int i; |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 108 | if (filename == NULL || name == NULL) |
| 109 | return -1; |
Guido van Rossum | 13836d9 | 1994-08-29 12:09:58 +0000 | [diff] [blame] | 110 | #ifdef MPW |
| 111 | /* This is needed by MPW's File and Line commands */ |
| 112 | #define FMT " File \"%.900s\"; line %d # in %s\n" |
| 113 | #else |
| 114 | /* This is needed by Emacs' compile command */ |
| 115 | #define FMT " File \"%.900s\", line %d, in %s\n" |
| 116 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 117 | xfp = fopen(filename, "r"); |
| 118 | if (xfp == NULL) { |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 119 | /* Search tail of filename in sys.path before giving up */ |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 120 | PyObject *path; |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 121 | char *tail = strrchr(filename, SEP); |
| 122 | if (tail == NULL) |
| 123 | tail = filename; |
| 124 | else |
| 125 | tail++; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 126 | path = PySys_GetObject("path"); |
| 127 | if (path != NULL && PyList_Check(path)) { |
| 128 | int npath = PyList_Size(path); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 129 | size_t taillen = strlen(tail); |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 130 | char namebuf[MAXPATHLEN+1]; |
| 131 | for (i = 0; i < npath; i++) { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 132 | PyObject *v = PyList_GetItem(path, i); |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 133 | if (v == NULL) { |
| 134 | PyErr_Clear(); |
| 135 | break; |
| 136 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 137 | if (PyString_Check(v)) { |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 138 | size_t len; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 139 | len = PyString_Size(v); |
Guido van Rossum | bfd5d755 | 1994-09-29 09:38:04 +0000 | [diff] [blame] | 140 | if (len + 1 + taillen >= MAXPATHLEN) |
| 141 | continue; /* Too long */ |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 142 | strcpy(namebuf, PyString_AsString(v)); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 143 | if (strlen(namebuf) != len) |
Guido van Rossum | bfd5d755 | 1994-09-29 09:38:04 +0000 | [diff] [blame] | 144 | continue; /* v contains '\0' */ |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 145 | if (len > 0 && namebuf[len-1] != SEP) |
| 146 | namebuf[len++] = SEP; |
| 147 | strcpy(namebuf+len, tail); |
| 148 | xfp = fopen(namebuf, "r"); |
| 149 | if (xfp != NULL) { |
| 150 | filename = namebuf; |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 156 | } |
Guido van Rossum | 13836d9 | 1994-08-29 12:09:58 +0000 | [diff] [blame] | 157 | sprintf(linebuf, FMT, filename, lineno, name); |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 158 | err = PyFile_WriteString(linebuf, f); |
| 159 | if (xfp == NULL || err != 0) |
| 160 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 161 | for (i = 0; i < lineno; i++) { |
Guido van Rossum | 3aca653 | 1999-09-18 20:49:39 +0000 | [diff] [blame] | 162 | char* pLastChar = &linebuf[sizeof(linebuf)-2]; |
| 163 | do { |
| 164 | *pLastChar = '\0'; |
| 165 | if (fgets(linebuf, sizeof linebuf, xfp) == NULL) |
| 166 | break; |
| 167 | /* fgets read *something*; if it didn't get as |
| 168 | far as pLastChar, it must have found a newline |
| 169 | or hit the end of the file; if pLastChar is \n, |
| 170 | it obviously found a newline; else we haven't |
| 171 | yet seen a newline, so must continue */ |
| 172 | } while (*pLastChar != '\0' && *pLastChar != '\n'); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 173 | } |
| 174 | if (i == lineno) { |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 175 | char *p = linebuf; |
Guido van Rossum | e78c5d0 | 1995-07-07 22:45:41 +0000 | [diff] [blame] | 176 | while (*p == ' ' || *p == '\t' || *p == '\014') |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 177 | p++; |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 178 | err = PyFile_WriteString(" ", f); |
| 179 | if (err == 0) { |
| 180 | err = PyFile_WriteString(p, f); |
| 181 | if (err == 0 && strchr(p, '\n') == NULL) |
| 182 | err = PyFile_WriteString("\n", f); |
| 183 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 184 | } |
| 185 | fclose(xfp); |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 186 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 189 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 190 | tb_printinternal(tracebackobject *tb, PyObject *f, int limit) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 191 | { |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 192 | int err = 0; |
Guido van Rossum | 67a5fdb | 1993-12-17 12:09:14 +0000 | [diff] [blame] | 193 | int depth = 0; |
| 194 | tracebackobject *tb1 = tb; |
| 195 | while (tb1 != NULL) { |
| 196 | depth++; |
| 197 | tb1 = tb1->tb_next; |
| 198 | } |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 199 | while (tb != NULL && err == 0) { |
Guido van Rossum | b3f515a | 1997-01-24 04:02:55 +0000 | [diff] [blame] | 200 | if (depth <= limit) { |
Guido van Rossum | 73237c5 | 1997-05-05 20:53:25 +0000 | [diff] [blame] | 201 | if (Py_OptimizeFlag) |
| 202 | tb->tb_lineno = PyCode_Addr2Line( |
| 203 | tb->tb_frame->f_code, tb->tb_lasti); |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 204 | err = tb_displayline(f, |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 205 | PyString_AsString( |
| 206 | tb->tb_frame->f_code->co_filename), |
Guido van Rossum | 13836d9 | 1994-08-29 12:09:58 +0000 | [diff] [blame] | 207 | tb->tb_lineno, |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 208 | PyString_AsString(tb->tb_frame->f_code->co_name)); |
Guido van Rossum | b3f515a | 1997-01-24 04:02:55 +0000 | [diff] [blame] | 209 | } |
Guido van Rossum | 67a5fdb | 1993-12-17 12:09:14 +0000 | [diff] [blame] | 210 | depth--; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 211 | tb = tb->tb_next; |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 212 | if (err == 0) |
| 213 | err = PyErr_CheckSignals(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 214 | } |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 215 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 219 | PyTraceBack_Print(PyObject *v, PyObject *f) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 220 | { |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 221 | int err; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 222 | PyObject *limitv; |
Guido van Rossum | 67a5fdb | 1993-12-17 12:09:14 +0000 | [diff] [blame] | 223 | int limit = 1000; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 224 | if (v == NULL) |
| 225 | return 0; |
| 226 | if (!is_tracebackobject(v)) { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 227 | PyErr_BadInternalCall(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 228 | return -1; |
| 229 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 230 | limitv = PySys_GetObject("tracebacklimit"); |
| 231 | if (limitv && PyInt_Check(limitv)) { |
| 232 | limit = PyInt_AsLong(limitv); |
Guido van Rossum | 67a5fdb | 1993-12-17 12:09:14 +0000 | [diff] [blame] | 233 | if (limit <= 0) |
| 234 | return 0; |
| 235 | } |
Guido van Rossum | 6d10887 | 2000-03-31 00:39:23 +0000 | [diff] [blame] | 236 | err = PyFile_WriteString("Traceback (most recent call last):\n", f); |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 237 | if (!err) |
| 238 | err = tb_printinternal((tracebackobject *)v, f, limit); |
| 239 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 240 | } |