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" |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 11 | #ifdef HAVE_FCNTL_H |
| 12 | #include <fcntl.h> |
| 13 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 14 | |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 15 | #define OFF(x) offsetof(PyTracebackObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 16 | |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 17 | /* Method from Parser/tokenizer.c */ |
| 18 | extern char * PyTokenizer_FindEncoding(int); |
| 19 | |
Collin Winter | 3eed765 | 2007-08-14 17:53:54 +0000 | [diff] [blame] | 20 | static PyObject * |
| 21 | tb_dir(PyTracebackObject *self) |
| 22 | { |
| 23 | return Py_BuildValue("[ssss]", "tb_frame", "tb_next", |
| 24 | "tb_lasti", "tb_lineno"); |
| 25 | } |
| 26 | |
| 27 | static PyMethodDef tb_methods[] = { |
| 28 | {"__dir__", (PyCFunction)tb_dir, METH_NOARGS}, |
| 29 | {NULL, NULL, 0, NULL}, |
| 30 | }; |
| 31 | |
Neal Norwitz | 8dfc4a9 | 2007-08-11 06:39:53 +0000 | [diff] [blame] | 32 | static PyMemberDef tb_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 33 | {"tb_next", T_OBJECT, OFF(tb_next), READONLY}, |
| 34 | {"tb_frame", T_OBJECT, OFF(tb_frame), READONLY}, |
| 35 | {"tb_lasti", T_INT, OFF(tb_lasti), READONLY}, |
| 36 | {"tb_lineno", T_INT, OFF(tb_lineno), READONLY}, |
| 37 | {NULL} /* Sentinel */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 40 | static void |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 41 | tb_dealloc(PyTracebackObject *tb) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 42 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 43 | PyObject_GC_UnTrack(tb); |
| 44 | Py_TRASHCAN_SAFE_BEGIN(tb) |
| 45 | Py_XDECREF(tb->tb_next); |
| 46 | Py_XDECREF(tb->tb_frame); |
| 47 | PyObject_GC_Del(tb); |
| 48 | Py_TRASHCAN_SAFE_END(tb) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 51 | static int |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 52 | tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg) |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 53 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 | Py_VISIT(tb->tb_next); |
| 55 | Py_VISIT(tb->tb_frame); |
| 56 | return 0; |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static void |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 60 | tb_clear(PyTracebackObject *tb) |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 61 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | Py_CLEAR(tb->tb_next); |
| 63 | Py_CLEAR(tb->tb_frame); |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Tim Peters | d7c3652 | 2001-10-22 19:34:09 +0000 | [diff] [blame] | 66 | PyTypeObject PyTraceBack_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 68 | "traceback", |
| 69 | sizeof(PyTracebackObject), |
| 70 | 0, |
| 71 | (destructor)tb_dealloc, /*tp_dealloc*/ |
| 72 | 0, /*tp_print*/ |
| 73 | 0, /*tp_getattr*/ |
| 74 | 0, /*tp_setattr*/ |
| 75 | 0, /*tp_reserved*/ |
| 76 | 0, /*tp_repr*/ |
| 77 | 0, /*tp_as_number*/ |
| 78 | 0, /*tp_as_sequence*/ |
| 79 | 0, /*tp_as_mapping*/ |
| 80 | 0, /* tp_hash */ |
| 81 | 0, /* tp_call */ |
| 82 | 0, /* tp_str */ |
| 83 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 84 | 0, /* tp_setattro */ |
| 85 | 0, /* tp_as_buffer */ |
| 86 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 87 | 0, /* tp_doc */ |
| 88 | (traverseproc)tb_traverse, /* tp_traverse */ |
| 89 | (inquiry)tb_clear, /* tp_clear */ |
| 90 | 0, /* tp_richcompare */ |
| 91 | 0, /* tp_weaklistoffset */ |
| 92 | 0, /* tp_iter */ |
| 93 | 0, /* tp_iternext */ |
| 94 | tb_methods, /* tp_methods */ |
| 95 | tb_memberlist, /* tp_members */ |
| 96 | 0, /* tp_getset */ |
| 97 | 0, /* tp_base */ |
| 98 | 0, /* tp_dict */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 101 | static PyTracebackObject * |
| 102 | newtracebackobject(PyTracebackObject *next, PyFrameObject *frame) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 103 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | PyTracebackObject *tb; |
| 105 | if ((next != NULL && !PyTraceBack_Check(next)) || |
| 106 | frame == NULL || !PyFrame_Check(frame)) { |
| 107 | PyErr_BadInternalCall(); |
| 108 | return NULL; |
| 109 | } |
| 110 | tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type); |
| 111 | if (tb != NULL) { |
| 112 | Py_XINCREF(next); |
| 113 | tb->tb_next = next; |
| 114 | Py_XINCREF(frame); |
| 115 | tb->tb_frame = frame; |
| 116 | tb->tb_lasti = frame->f_lasti; |
| 117 | tb->tb_lineno = PyFrame_GetLineNumber(frame); |
| 118 | PyObject_GC_Track(tb); |
| 119 | } |
| 120 | return tb; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 123 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 124 | PyTraceBack_Here(PyFrameObject *frame) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 125 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 126 | PyThreadState *tstate = PyThreadState_GET(); |
| 127 | PyTracebackObject *oldtb = (PyTracebackObject *) tstate->curexc_traceback; |
| 128 | PyTracebackObject *tb = newtracebackobject(oldtb, frame); |
| 129 | if (tb == NULL) |
| 130 | return -1; |
| 131 | tstate->curexc_traceback = (PyObject *)tb; |
| 132 | Py_XDECREF(oldtb); |
| 133 | return 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 136 | static PyObject * |
| 137 | _Py_FindSourceFile(PyObject *filename, char* namebuf, size_t namelen, PyObject *io) |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 138 | { |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 139 | Py_ssize_t i; |
| 140 | PyObject *binary; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | PyObject *v; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 142 | Py_ssize_t npath; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | size_t taillen; |
| 144 | PyObject *syspath; |
| 145 | const char* path; |
| 146 | const char* tail; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 147 | const char* filepath; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 148 | Py_ssize_t len; |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 149 | |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 150 | filepath = _PyUnicode_AsString(filename); |
| 151 | if (filepath == NULL) { |
| 152 | PyErr_Clear(); |
| 153 | return NULL; |
| 154 | } |
| 155 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 156 | /* Search tail of filename in sys.path before giving up */ |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 157 | tail = strrchr(filepath, SEP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | if (tail == NULL) |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 159 | tail = filepath; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 160 | else |
| 161 | tail++; |
| 162 | taillen = strlen(tail); |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | syspath = PySys_GetObject("path"); |
| 165 | if (syspath == NULL || !PyList_Check(syspath)) |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 166 | return NULL; |
| 167 | npath = PyList_Size(syspath); |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | for (i = 0; i < npath; i++) { |
| 170 | v = PyList_GetItem(syspath, i); |
| 171 | if (v == NULL) { |
| 172 | PyErr_Clear(); |
| 173 | break; |
| 174 | } |
| 175 | if (!PyUnicode_Check(v)) |
| 176 | continue; |
| 177 | path = _PyUnicode_AsStringAndSize(v, &len); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 178 | if (path == NULL) { |
| 179 | PyErr_Clear(); |
| 180 | continue; |
| 181 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 182 | if (len + 1 + (Py_ssize_t)taillen >= (Py_ssize_t)namelen - 1) |
| 183 | continue; /* Too long */ |
| 184 | strcpy(namebuf, path); |
| 185 | if (strlen(namebuf) != len) |
| 186 | continue; /* v contains '\0' */ |
| 187 | if (len > 0 && namebuf[len-1] != SEP) |
| 188 | namebuf[len++] = SEP; |
| 189 | strcpy(namebuf+len, tail); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 190 | |
| 191 | binary = PyObject_CallMethod(io, "open", "ss", namebuf, "rb"); |
| 192 | if (binary != NULL) |
| 193 | return binary; |
| 194 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 195 | } |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 196 | return NULL; |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 199 | int |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 200 | _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 201 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 202 | int err = 0; |
| 203 | int fd; |
| 204 | int i; |
| 205 | char *found_encoding; |
| 206 | char *encoding; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 207 | PyObject *io; |
| 208 | PyObject *binary; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | PyObject *fob = NULL; |
| 210 | PyObject *lineobj = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 211 | char buf[MAXPATHLEN+1]; |
| 212 | Py_UNICODE *u, *p; |
| 213 | Py_ssize_t len; |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 214 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | /* open the file */ |
| 216 | if (filename == NULL) |
| 217 | return 0; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 218 | |
| 219 | io = PyImport_ImportModuleNoBlock("io"); |
| 220 | if (io == NULL) |
| 221 | return -1; |
| 222 | binary = PyObject_CallMethod(io, "open", "Os", filename, "rb"); |
| 223 | |
| 224 | if (binary == NULL) { |
| 225 | binary = _Py_FindSourceFile(filename, buf, sizeof(buf), io); |
| 226 | if (binary == NULL) { |
| 227 | Py_DECREF(io); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 228 | return 0; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 229 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 230 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 232 | /* use the right encoding to decode the file as unicode */ |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 233 | fd = PyObject_AsFileDescriptor(binary); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 234 | found_encoding = PyTokenizer_FindEncoding(fd); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 235 | encoding = (found_encoding != NULL) ? found_encoding : "utf-8"; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 236 | lseek(fd, 0, 0); /* Reset position */ |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 237 | fob = PyObject_CallMethod(io, "TextIOWrapper", "Os", binary, encoding); |
| 238 | Py_DECREF(io); |
| 239 | Py_DECREF(binary); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 240 | PyMem_FREE(found_encoding); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | if (fob == NULL) { |
| 243 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 244 | return 0; |
| 245 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 247 | /* get the line number lineno */ |
| 248 | for (i = 0; i < lineno; i++) { |
| 249 | Py_XDECREF(lineobj); |
| 250 | lineobj = PyFile_GetLine(fob, -1); |
| 251 | if (!lineobj) { |
| 252 | err = -1; |
| 253 | break; |
| 254 | } |
| 255 | } |
| 256 | Py_DECREF(fob); |
| 257 | if (!lineobj || !PyUnicode_Check(lineobj)) { |
| 258 | Py_XDECREF(lineobj); |
| 259 | return err; |
| 260 | } |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 261 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 262 | /* remove the indentation of the line */ |
| 263 | u = PyUnicode_AS_UNICODE(lineobj); |
| 264 | len = PyUnicode_GET_SIZE(lineobj); |
| 265 | for (p=u; *p == ' ' || *p == '\t' || *p == '\014'; p++) |
| 266 | len--; |
| 267 | if (u != p) { |
| 268 | PyObject *truncated; |
| 269 | truncated = PyUnicode_FromUnicode(p, len); |
| 270 | if (truncated) { |
| 271 | Py_DECREF(lineobj); |
| 272 | lineobj = truncated; |
| 273 | } else { |
| 274 | PyErr_Clear(); |
| 275 | } |
| 276 | } |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | /* Write some spaces before the line */ |
| 279 | strcpy(buf, " "); |
| 280 | assert (strlen(buf) == 10); |
| 281 | while (indent > 0) { |
| 282 | if(indent < 10) |
| 283 | buf[indent] = '\0'; |
| 284 | err = PyFile_WriteString(buf, f); |
| 285 | if (err != 0) |
| 286 | break; |
| 287 | indent -= 10; |
| 288 | } |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 290 | /* finally display the line */ |
| 291 | if (err == 0) |
| 292 | err = PyFile_WriteObject(lineobj, f, Py_PRINT_RAW); |
| 293 | Py_DECREF(lineobj); |
| 294 | if (err == 0) |
| 295 | err = PyFile_WriteString("\n", f); |
| 296 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 299 | static int |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 300 | tb_displayline(PyObject *f, PyObject *filename, int lineno, PyObject *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 301 | { |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 302 | int err; |
| 303 | PyObject *line; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 305 | if (filename == NULL || name == NULL) |
| 306 | return -1; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 307 | line = PyUnicode_FromFormat(" File \"%U\", line %d, in %U\n", |
| 308 | filename, lineno, name); |
| 309 | if (line == NULL) |
| 310 | return -1; |
| 311 | err = PyFile_WriteObject(line, f, Py_PRINT_RAW); |
| 312 | Py_DECREF(line); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 313 | if (err != 0) |
| 314 | return err; |
| 315 | return _Py_DisplaySourceLine(f, filename, lineno, 4); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static int |
| 319 | tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit) |
| 320 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | int err = 0; |
| 322 | long depth = 0; |
| 323 | PyTracebackObject *tb1 = tb; |
| 324 | while (tb1 != NULL) { |
| 325 | depth++; |
| 326 | tb1 = tb1->tb_next; |
| 327 | } |
| 328 | while (tb != NULL && err == 0) { |
| 329 | if (depth <= limit) { |
| 330 | err = tb_displayline(f, |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame^] | 331 | tb->tb_frame->f_code->co_filename, |
| 332 | tb->tb_lineno, |
| 333 | tb->tb_frame->f_code->co_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 334 | } |
| 335 | depth--; |
| 336 | tb = tb->tb_next; |
| 337 | if (err == 0) |
| 338 | err = PyErr_CheckSignals(); |
| 339 | } |
| 340 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Christian Heimes | 0fbab7f | 2007-12-04 21:55:18 +0000 | [diff] [blame] | 343 | #define PyTraceBack_LIMIT 1000 |
| 344 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 345 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 346 | PyTraceBack_Print(PyObject *v, PyObject *f) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 347 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 348 | int err; |
| 349 | PyObject *limitv; |
| 350 | long limit = PyTraceBack_LIMIT; |
Christian Heimes | 0fbab7f | 2007-12-04 21:55:18 +0000 | [diff] [blame] | 351 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 352 | if (v == NULL) |
| 353 | return 0; |
| 354 | if (!PyTraceBack_Check(v)) { |
| 355 | PyErr_BadInternalCall(); |
| 356 | return -1; |
| 357 | } |
| 358 | limitv = PySys_GetObject("tracebacklimit"); |
| 359 | if (limitv) { |
| 360 | PyObject *exc_type, *exc_value, *exc_tb; |
Christian Heimes | 0fbab7f | 2007-12-04 21:55:18 +0000 | [diff] [blame] | 361 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | PyErr_Fetch(&exc_type, &exc_value, &exc_tb); |
| 363 | limit = PyLong_AsLong(limitv); |
| 364 | if (limit == -1 && PyErr_Occurred()) { |
| 365 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
| 366 | limit = PyTraceBack_LIMIT; |
| 367 | } |
| 368 | else { |
| 369 | Py_XDECREF(exc_type); |
| 370 | Py_XDECREF(exc_value); |
| 371 | Py_XDECREF(exc_tb); |
| 372 | return 0; |
| 373 | } |
| 374 | } |
| 375 | else if (limit <= 0) { |
| 376 | limit = PyTraceBack_LIMIT; |
| 377 | } |
| 378 | PyErr_Restore(exc_type, exc_value, exc_tb); |
| 379 | } |
| 380 | err = PyFile_WriteString("Traceback (most recent call last):\n", f); |
| 381 | if (!err) |
| 382 | err = tb_printinternal((PyTracebackObject *)v, f, limit); |
| 383 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 384 | } |