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