Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 32 | /* Traceback implementation */ |
| 33 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 35 | |
| 36 | #include "compile.h" |
| 37 | #include "frameobject.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 38 | #include "structmember.h" |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 39 | #include "osdefs.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 40 | |
| 41 | typedef struct _tracebackobject { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 42 | PyObject_HEAD |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 43 | struct _tracebackobject *tb_next; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 44 | PyFrameObject *tb_frame; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 45 | int tb_lasti; |
| 46 | int tb_lineno; |
| 47 | } tracebackobject; |
| 48 | |
| 49 | #define OFF(x) offsetof(tracebackobject, x) |
| 50 | |
| 51 | static struct memberlist tb_memberlist[] = { |
| 52 | {"tb_next", T_OBJECT, OFF(tb_next)}, |
| 53 | {"tb_frame", T_OBJECT, OFF(tb_frame)}, |
| 54 | {"tb_lasti", T_INT, OFF(tb_lasti)}, |
| 55 | {"tb_lineno", T_INT, OFF(tb_lineno)}, |
| 56 | {NULL} /* Sentinel */ |
| 57 | }; |
| 58 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 59 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 60 | tb_getattr(tb, name) |
| 61 | tracebackobject *tb; |
| 62 | char *name; |
| 63 | { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 64 | return PyMember_Get((char *)tb, tb_memberlist, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static void |
| 68 | tb_dealloc(tb) |
| 69 | tracebackobject *tb; |
| 70 | { |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 71 | Py_TRASHCAN_SAFE_BEGIN(tb) |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 72 | Py_XDECREF(tb->tb_next); |
| 73 | Py_XDECREF(tb->tb_frame); |
| 74 | PyMem_DEL(tb); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 75 | Py_TRASHCAN_SAFE_END(tb) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Guido van Rossum | 9d78d8d | 1995-09-18 21:29:36 +0000 | [diff] [blame] | 78 | #define Tracebacktype PyTraceBack_Type |
| 79 | #define is_tracebackobject PyTraceBack_Check |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 81 | PyTypeObject Tracebacktype = { |
| 82 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 83 | 0, |
| 84 | "traceback", |
| 85 | sizeof(tracebackobject), |
| 86 | 0, |
Guido van Rossum | 13836d9 | 1994-08-29 12:09:58 +0000 | [diff] [blame] | 87 | (destructor)tb_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 88 | 0, /*tp_print*/ |
Guido van Rossum | 13836d9 | 1994-08-29 12:09:58 +0000 | [diff] [blame] | 89 | (getattrfunc)tb_getattr, /*tp_getattr*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 90 | 0, /*tp_setattr*/ |
| 91 | 0, /*tp_compare*/ |
| 92 | 0, /*tp_repr*/ |
| 93 | 0, /*tp_as_number*/ |
| 94 | 0, /*tp_as_sequence*/ |
| 95 | 0, /*tp_as_mapping*/ |
| 96 | }; |
| 97 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 98 | static tracebackobject * |
| 99 | newtracebackobject(next, frame, lasti, lineno) |
| 100 | tracebackobject *next; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 101 | PyFrameObject *frame; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 102 | int lasti, lineno; |
| 103 | { |
| 104 | tracebackobject *tb; |
| 105 | if ((next != NULL && !is_tracebackobject(next)) || |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 106 | frame == NULL || !PyFrame_Check(frame)) { |
| 107 | PyErr_BadInternalCall(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 108 | return NULL; |
| 109 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 110 | tb = PyObject_NEW(tracebackobject, &Tracebacktype); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 111 | if (tb != NULL) { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 112 | Py_XINCREF(next); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 113 | tb->tb_next = next; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 114 | Py_XINCREF(frame); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 115 | tb->tb_frame = frame; |
| 116 | tb->tb_lasti = lasti; |
| 117 | tb->tb_lineno = lineno; |
| 118 | } |
| 119 | return tb; |
| 120 | } |
| 121 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 122 | int |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 123 | PyTraceBack_Here(frame) |
| 124 | PyFrameObject *frame; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 125 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 126 | PyThreadState *tstate = frame->f_tstate; |
| 127 | tracebackobject *oldtb = (tracebackobject *) tstate->curexc_traceback; |
| 128 | tracebackobject *tb = newtracebackobject(oldtb, |
| 129 | frame, frame->f_lasti, frame->f_lineno); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 130 | if (tb == NULL) |
| 131 | return -1; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 132 | tstate->curexc_traceback = (PyObject *)tb; |
| 133 | Py_XDECREF(oldtb); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 134 | return 0; |
| 135 | } |
| 136 | |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 137 | static int |
Guido van Rossum | 13836d9 | 1994-08-29 12:09:58 +0000 | [diff] [blame] | 138 | tb_displayline(f, filename, lineno, name) |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 139 | PyObject *f; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 140 | char *filename; |
| 141 | int lineno; |
Guido van Rossum | 13836d9 | 1994-08-29 12:09:58 +0000 | [diff] [blame] | 142 | 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; |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 146 | char linebuf[1000]; |
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 */ |
| 152 | #define FMT " File \"%.900s\"; line %d # in %s\n" |
| 153 | #else |
| 154 | /* This is needed by Emacs' compile command */ |
| 155 | #define FMT " File \"%.900s\", line %d, in %s\n" |
| 156 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 157 | xfp = fopen(filename, "r"); |
| 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 | bfd5d755 | 1994-09-29 09:38:04 +0000 | [diff] [blame] | 169 | int 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 | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 178 | int 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 | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 183 | if ((int)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); |
| 188 | xfp = fopen(namebuf, "r"); |
| 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 | } |
Guido van Rossum | 13836d9 | 1994-08-29 12:09:58 +0000 | [diff] [blame] | 197 | sprintf(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'; |
| 205 | if (fgets(linebuf, sizeof linebuf, xfp) == NULL) |
| 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 |
Guido van Rossum | 67a5fdb | 1993-12-17 12:09:14 +0000 | [diff] [blame] | 230 | tb_printinternal(tb, f, limit) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 231 | tracebackobject *tb; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 232 | PyObject *f; |
Guido van Rossum | 67a5fdb | 1993-12-17 12:09:14 +0000 | [diff] [blame] | 233 | int limit; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 234 | { |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 235 | int err = 0; |
Guido van Rossum | 67a5fdb | 1993-12-17 12:09:14 +0000 | [diff] [blame] | 236 | int depth = 0; |
| 237 | tracebackobject *tb1 = tb; |
| 238 | while (tb1 != NULL) { |
| 239 | depth++; |
| 240 | tb1 = tb1->tb_next; |
| 241 | } |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 242 | while (tb != NULL && err == 0) { |
Guido van Rossum | b3f515a | 1997-01-24 04:02:55 +0000 | [diff] [blame] | 243 | if (depth <= limit) { |
Guido van Rossum | 73237c5 | 1997-05-05 20:53:25 +0000 | [diff] [blame] | 244 | if (Py_OptimizeFlag) |
| 245 | tb->tb_lineno = PyCode_Addr2Line( |
| 246 | tb->tb_frame->f_code, tb->tb_lasti); |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 247 | err = tb_displayline(f, |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 248 | PyString_AsString( |
| 249 | tb->tb_frame->f_code->co_filename), |
Guido van Rossum | 13836d9 | 1994-08-29 12:09:58 +0000 | [diff] [blame] | 250 | tb->tb_lineno, |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 251 | PyString_AsString(tb->tb_frame->f_code->co_name)); |
Guido van Rossum | b3f515a | 1997-01-24 04:02:55 +0000 | [diff] [blame] | 252 | } |
Guido van Rossum | 67a5fdb | 1993-12-17 12:09:14 +0000 | [diff] [blame] | 253 | depth--; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 254 | tb = tb->tb_next; |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 255 | if (err == 0) |
| 256 | err = PyErr_CheckSignals(); |
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 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | int |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 262 | PyTraceBack_Print(v, f) |
| 263 | PyObject *v; |
| 264 | PyObject *f; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 265 | { |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 266 | int err; |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 267 | PyObject *limitv; |
Guido van Rossum | 67a5fdb | 1993-12-17 12:09:14 +0000 | [diff] [blame] | 268 | int limit = 1000; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 269 | if (v == NULL) |
| 270 | return 0; |
| 271 | if (!is_tracebackobject(v)) { |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 272 | PyErr_BadInternalCall(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 273 | return -1; |
| 274 | } |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 275 | limitv = PySys_GetObject("tracebacklimit"); |
| 276 | if (limitv && PyInt_Check(limitv)) { |
| 277 | limit = PyInt_AsLong(limitv); |
Guido van Rossum | 67a5fdb | 1993-12-17 12:09:14 +0000 | [diff] [blame] | 278 | if (limit <= 0) |
| 279 | return 0; |
| 280 | } |
Guido van Rossum | 6d10887 | 2000-03-31 00:39:23 +0000 | [diff] [blame] | 281 | err = PyFile_WriteString("Traceback (most recent call last):\n", f); |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 282 | if (!err) |
| 283 | err = tb_printinternal((tracebackobject *)v, f, limit); |
| 284 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 285 | } |