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 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6 | #include "code.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 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" |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 10 | #include "traceback.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 11 | |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 12 | #define OFF(x) offsetof(PyTracebackObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 13 | |
Benjamin Peterson | 53f098c | 2009-03-18 21:49:29 +0000 | [diff] [blame] | 14 | static PyMemberDef tb_memberlist[] = { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 15 | {"tb_next", T_OBJECT, OFF(tb_next), READONLY}, |
| 16 | {"tb_frame", T_OBJECT, OFF(tb_frame), READONLY}, |
| 17 | {"tb_lasti", T_INT, OFF(tb_lasti), READONLY}, |
| 18 | {"tb_lineno", T_INT, OFF(tb_lineno), READONLY}, |
| 19 | {NULL} /* Sentinel */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 20 | }; |
| 21 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 22 | static void |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 23 | tb_dealloc(PyTracebackObject *tb) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 24 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 25 | PyObject_GC_UnTrack(tb); |
| 26 | Py_TRASHCAN_SAFE_BEGIN(tb) |
| 27 | Py_XDECREF(tb->tb_next); |
| 28 | Py_XDECREF(tb->tb_frame); |
| 29 | PyObject_GC_Del(tb); |
| 30 | Py_TRASHCAN_SAFE_END(tb) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 33 | static int |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 34 | tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg) |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 35 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 36 | Py_VISIT(tb->tb_next); |
| 37 | Py_VISIT(tb->tb_frame); |
| 38 | return 0; |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static void |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 42 | tb_clear(PyTracebackObject *tb) |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 43 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 44 | Py_CLEAR(tb->tb_next); |
| 45 | Py_CLEAR(tb->tb_frame); |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Tim Peters | d7c3652 | 2001-10-22 19:34:09 +0000 | [diff] [blame] | 48 | PyTypeObject PyTraceBack_Type = { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 49 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 50 | "traceback", |
| 51 | sizeof(PyTracebackObject), |
| 52 | 0, |
| 53 | (destructor)tb_dealloc, /*tp_dealloc*/ |
| 54 | 0, /*tp_print*/ |
| 55 | 0, /*tp_getattr*/ |
| 56 | 0, /*tp_setattr*/ |
| 57 | 0, /*tp_compare*/ |
| 58 | 0, /*tp_repr*/ |
| 59 | 0, /*tp_as_number*/ |
| 60 | 0, /*tp_as_sequence*/ |
| 61 | 0, /*tp_as_mapping*/ |
| 62 | 0, /* tp_hash */ |
| 63 | 0, /* tp_call */ |
| 64 | 0, /* tp_str */ |
| 65 | 0, /* tp_getattro */ |
| 66 | 0, /* tp_setattro */ |
| 67 | 0, /* tp_as_buffer */ |
| 68 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 69 | 0, /* tp_doc */ |
| 70 | (traverseproc)tb_traverse, /* tp_traverse */ |
| 71 | (inquiry)tb_clear, /* tp_clear */ |
| 72 | 0, /* tp_richcompare */ |
| 73 | 0, /* tp_weaklistoffset */ |
| 74 | 0, /* tp_iter */ |
| 75 | 0, /* tp_iternext */ |
| 76 | 0, /* tp_methods */ |
| 77 | tb_memberlist, /* tp_members */ |
| 78 | 0, /* tp_getset */ |
| 79 | 0, /* tp_base */ |
| 80 | 0, /* tp_dict */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 83 | static PyTracebackObject * |
| 84 | newtracebackobject(PyTracebackObject *next, PyFrameObject *frame) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 85 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 86 | PyTracebackObject *tb; |
| 87 | if ((next != NULL && !PyTraceBack_Check(next)) || |
| 88 | frame == NULL || !PyFrame_Check(frame)) { |
| 89 | PyErr_BadInternalCall(); |
| 90 | return NULL; |
| 91 | } |
| 92 | tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type); |
| 93 | if (tb != NULL) { |
| 94 | Py_XINCREF(next); |
| 95 | tb->tb_next = next; |
| 96 | Py_XINCREF(frame); |
| 97 | tb->tb_frame = frame; |
| 98 | tb->tb_lasti = frame->f_lasti; |
| 99 | tb->tb_lineno = PyCode_Addr2Line(frame->f_code, |
| 100 | frame->f_lasti); |
| 101 | PyObject_GC_Track(tb); |
| 102 | } |
| 103 | return tb; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 106 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 107 | PyTraceBack_Here(PyFrameObject *frame) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 108 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 109 | PyThreadState *tstate = PyThreadState_GET(); |
| 110 | PyTracebackObject *oldtb = (PyTracebackObject *) tstate->curexc_traceback; |
| 111 | PyTracebackObject *tb = newtracebackobject(oldtb, frame); |
| 112 | if (tb == NULL) |
| 113 | return -1; |
| 114 | tstate->curexc_traceback = (PyObject *)tb; |
| 115 | Py_XDECREF(oldtb); |
| 116 | return 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 119 | int |
Amaury Forgeot d'Arc | 2252d11 | 2008-07-11 21:45:06 +0000 | [diff] [blame] | 120 | _Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno, int indent) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 121 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 122 | int err = 0; |
| 123 | FILE *xfp = NULL; |
| 124 | char linebuf[2000]; |
| 125 | int i; |
| 126 | char namebuf[MAXPATHLEN+1]; |
Christian Heimes | 3e8c897 | 2008-01-18 08:47:59 +0000 | [diff] [blame] | 127 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 128 | if (filename == NULL) |
| 129 | return -1; |
| 130 | /* This is needed by Emacs' compile command */ |
Tim Peters | 6d20b43 | 2001-11-27 20:30:42 +0000 | [diff] [blame] | 131 | #define FMT " File \"%.500s\", line %d, in %.500s\n" |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 132 | xfp = fopen(filename, "r" PY_STDIOTEXTMODE); |
| 133 | if (xfp == NULL) { |
| 134 | /* Search tail of filename in sys.path before giving up */ |
| 135 | PyObject *path; |
| 136 | const char *tail = strrchr(filename, SEP); |
| 137 | if (tail == NULL) |
| 138 | tail = filename; |
| 139 | else |
| 140 | tail++; |
| 141 | path = PySys_GetObject("path"); |
| 142 | if (path != NULL && PyList_Check(path)) { |
| 143 | Py_ssize_t _npath = PyList_Size(path); |
| 144 | int npath = Py_SAFE_DOWNCAST(_npath, Py_ssize_t, int); |
| 145 | size_t taillen = strlen(tail); |
| 146 | for (i = 0; i < npath; i++) { |
| 147 | PyObject *v = PyList_GetItem(path, i); |
| 148 | if (v == NULL) { |
| 149 | PyErr_Clear(); |
| 150 | break; |
| 151 | } |
| 152 | if (PyString_Check(v)) { |
| 153 | size_t len; |
| 154 | len = PyString_GET_SIZE(v); |
| 155 | if (len + 1 + taillen >= MAXPATHLEN) |
| 156 | continue; /* Too long */ |
| 157 | strcpy(namebuf, PyString_AsString(v)); |
| 158 | if (strlen(namebuf) != len) |
| 159 | continue; /* v contains '\0' */ |
| 160 | if (len > 0 && namebuf[len-1] != SEP) |
| 161 | namebuf[len++] = SEP; |
| 162 | strcpy(namebuf+len, tail); |
| 163 | xfp = fopen(namebuf, "r" PY_STDIOTEXTMODE); |
| 164 | if (xfp != NULL) { |
| 165 | filename = namebuf; |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 172 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 173 | if (xfp == NULL) |
| 174 | return err; |
| 175 | if (err != 0) { |
| 176 | fclose(xfp); |
| 177 | return err; |
| 178 | } |
| 179 | |
| 180 | for (i = 0; i < lineno; i++) { |
| 181 | char* pLastChar = &linebuf[sizeof(linebuf)-2]; |
| 182 | do { |
| 183 | *pLastChar = '\0'; |
| 184 | if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, xfp, NULL) == NULL) |
| 185 | break; |
| 186 | /* fgets read *something*; if it didn't get as |
| 187 | far as pLastChar, it must have found a newline |
| 188 | or hit the end of the file; if pLastChar is \n, |
| 189 | it obviously found a newline; else we haven't |
| 190 | yet seen a newline, so must continue */ |
| 191 | } while (*pLastChar != '\0' && *pLastChar != '\n'); |
| 192 | } |
| 193 | if (i == lineno) { |
| 194 | char buf[11]; |
| 195 | char *p = linebuf; |
| 196 | while (*p == ' ' || *p == '\t' || *p == '\014') |
| 197 | p++; |
| 198 | |
| 199 | /* Write some spaces before the line */ |
| 200 | strcpy(buf, " "); |
| 201 | assert (strlen(buf) == 10); |
| 202 | while (indent > 0) { |
| 203 | if(indent < 10) |
| 204 | buf[indent] = '\0'; |
| 205 | err = PyFile_WriteString(buf, f); |
| 206 | if (err != 0) |
| 207 | break; |
| 208 | indent -= 10; |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 211 | if (err == 0) |
| 212 | err = PyFile_WriteString(p, f); |
| 213 | if (err == 0 && strchr(p, '\n') == NULL) |
| 214 | err = PyFile_WriteString("\n", f); |
| 215 | } |
| 216 | fclose(xfp); |
| 217 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 220 | static int |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 221 | tb_displayline(PyObject *f, const char *filename, int lineno, const char *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 222 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 223 | int err = 0; |
| 224 | char linebuf[2000]; |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 225 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 226 | if (filename == NULL || name == NULL) |
| 227 | return -1; |
| 228 | /* This is needed by Emacs' compile command */ |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 229 | #define FMT " File \"%.500s\", line %d, in %.500s\n" |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 230 | PyOS_snprintf(linebuf, sizeof(linebuf), FMT, filename, lineno, name); |
| 231 | err = PyFile_WriteString(linebuf, f); |
| 232 | if (err != 0) |
| 233 | return err; |
| 234 | return _Py_DisplaySourceLine(f, filename, lineno, 4); |
Brett Cannon | e974689 | 2008-04-12 23:44:07 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static int |
| 238 | tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit) |
| 239 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 240 | int err = 0; |
| 241 | long depth = 0; |
| 242 | PyTracebackObject *tb1 = tb; |
| 243 | while (tb1 != NULL) { |
| 244 | depth++; |
| 245 | tb1 = tb1->tb_next; |
| 246 | } |
| 247 | while (tb != NULL && err == 0) { |
| 248 | if (depth <= limit) { |
| 249 | err = tb_displayline(f, |
| 250 | PyString_AsString( |
| 251 | tb->tb_frame->f_code->co_filename), |
| 252 | tb->tb_lineno, |
| 253 | PyString_AsString(tb->tb_frame->f_code->co_name)); |
| 254 | } |
| 255 | depth--; |
| 256 | tb = tb->tb_next; |
| 257 | if (err == 0) |
| 258 | err = PyErr_CheckSignals(); |
| 259 | } |
| 260 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 264 | PyTraceBack_Print(PyObject *v, PyObject *f) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 265 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 266 | int err; |
| 267 | PyObject *limitv; |
| 268 | long limit = 1000; |
| 269 | if (v == NULL) |
| 270 | return 0; |
| 271 | if (!PyTraceBack_Check(v)) { |
| 272 | PyErr_BadInternalCall(); |
| 273 | return -1; |
| 274 | } |
| 275 | limitv = PySys_GetObject("tracebacklimit"); |
| 276 | if (limitv && PyInt_Check(limitv)) { |
| 277 | limit = PyInt_AsLong(limitv); |
| 278 | if (limit <= 0) |
| 279 | return 0; |
| 280 | } |
| 281 | err = PyFile_WriteString("Traceback (most recent call last):\n", f); |
| 282 | if (!err) |
| 283 | err = tb_printinternal((PyTracebackObject *)v, f, limit); |
| 284 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 285 | } |