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