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 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 16 | #define PUTS(fd, str) write(fd, str, strlen(str)) |
| 17 | #define MAX_STRING_LENGTH 100 |
| 18 | #define MAX_FRAME_DEPTH 100 |
| 19 | #define MAX_NTHREADS 100 |
| 20 | |
Victor Stinner | fe7c5b5 | 2011-04-05 01:48:03 +0200 | [diff] [blame] | 21 | /* Function from Parser/tokenizer.c */ |
| 22 | extern char * PyTokenizer_FindEncodingFilename(int, PyObject *); |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 23 | |
Collin Winter | 3eed765 | 2007-08-14 17:53:54 +0000 | [diff] [blame] | 24 | static PyObject * |
| 25 | tb_dir(PyTracebackObject *self) |
| 26 | { |
| 27 | return Py_BuildValue("[ssss]", "tb_frame", "tb_next", |
| 28 | "tb_lasti", "tb_lineno"); |
| 29 | } |
| 30 | |
| 31 | static PyMethodDef tb_methods[] = { |
| 32 | {"__dir__", (PyCFunction)tb_dir, METH_NOARGS}, |
| 33 | {NULL, NULL, 0, NULL}, |
| 34 | }; |
| 35 | |
Neal Norwitz | 8dfc4a9 | 2007-08-11 06:39:53 +0000 | [diff] [blame] | 36 | static PyMemberDef tb_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | {"tb_next", T_OBJECT, OFF(tb_next), READONLY}, |
| 38 | {"tb_frame", T_OBJECT, OFF(tb_frame), READONLY}, |
| 39 | {"tb_lasti", T_INT, OFF(tb_lasti), READONLY}, |
| 40 | {"tb_lineno", T_INT, OFF(tb_lineno), READONLY}, |
| 41 | {NULL} /* Sentinel */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 44 | static void |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 45 | tb_dealloc(PyTracebackObject *tb) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 46 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 47 | PyObject_GC_UnTrack(tb); |
| 48 | Py_TRASHCAN_SAFE_BEGIN(tb) |
| 49 | Py_XDECREF(tb->tb_next); |
| 50 | Py_XDECREF(tb->tb_frame); |
| 51 | PyObject_GC_Del(tb); |
| 52 | Py_TRASHCAN_SAFE_END(tb) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 55 | static int |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 56 | tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg) |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 57 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 58 | Py_VISIT(tb->tb_next); |
| 59 | Py_VISIT(tb->tb_frame); |
| 60 | return 0; |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static void |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 64 | tb_clear(PyTracebackObject *tb) |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 65 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 66 | Py_CLEAR(tb->tb_next); |
| 67 | Py_CLEAR(tb->tb_frame); |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Tim Peters | d7c3652 | 2001-10-22 19:34:09 +0000 | [diff] [blame] | 70 | PyTypeObject PyTraceBack_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 72 | "traceback", |
| 73 | sizeof(PyTracebackObject), |
| 74 | 0, |
| 75 | (destructor)tb_dealloc, /*tp_dealloc*/ |
| 76 | 0, /*tp_print*/ |
| 77 | 0, /*tp_getattr*/ |
| 78 | 0, /*tp_setattr*/ |
| 79 | 0, /*tp_reserved*/ |
| 80 | 0, /*tp_repr*/ |
| 81 | 0, /*tp_as_number*/ |
| 82 | 0, /*tp_as_sequence*/ |
| 83 | 0, /*tp_as_mapping*/ |
| 84 | 0, /* tp_hash */ |
| 85 | 0, /* tp_call */ |
| 86 | 0, /* tp_str */ |
| 87 | PyObject_GenericGetAttr, /* 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 | tb_methods, /* tp_methods */ |
| 99 | tb_memberlist, /* 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 | |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 105 | static PyTracebackObject * |
| 106 | newtracebackobject(PyTracebackObject *next, PyFrameObject *frame) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 107 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 108 | PyTracebackObject *tb; |
| 109 | if ((next != NULL && !PyTraceBack_Check(next)) || |
| 110 | frame == NULL || !PyFrame_Check(frame)) { |
| 111 | PyErr_BadInternalCall(); |
| 112 | return NULL; |
| 113 | } |
| 114 | tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type); |
| 115 | if (tb != NULL) { |
| 116 | Py_XINCREF(next); |
| 117 | tb->tb_next = next; |
| 118 | Py_XINCREF(frame); |
| 119 | tb->tb_frame = frame; |
| 120 | tb->tb_lasti = frame->f_lasti; |
| 121 | tb->tb_lineno = PyFrame_GetLineNumber(frame); |
| 122 | PyObject_GC_Track(tb); |
| 123 | } |
| 124 | return tb; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 127 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 128 | PyTraceBack_Here(PyFrameObject *frame) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 129 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 130 | PyThreadState *tstate = PyThreadState_GET(); |
| 131 | PyTracebackObject *oldtb = (PyTracebackObject *) tstate->curexc_traceback; |
| 132 | PyTracebackObject *tb = newtracebackobject(oldtb, frame); |
| 133 | if (tb == NULL) |
| 134 | return -1; |
| 135 | tstate->curexc_traceback = (PyObject *)tb; |
| 136 | Py_XDECREF(oldtb); |
| 137 | return 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 140 | static PyObject * |
| 141 | _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] | 142 | { |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 143 | Py_ssize_t i; |
| 144 | PyObject *binary; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | PyObject *v; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 146 | Py_ssize_t npath; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | size_t taillen; |
| 148 | PyObject *syspath; |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 149 | PyObject *path; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 150 | const char* tail; |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 151 | PyObject *filebytes; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 152 | const char* filepath; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 153 | Py_ssize_t len; |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 154 | PyObject* result; |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 155 | |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 156 | filebytes = PyUnicode_EncodeFSDefault(filename); |
| 157 | if (filebytes == NULL) { |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 158 | PyErr_Clear(); |
| 159 | return NULL; |
| 160 | } |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 161 | filepath = PyBytes_AS_STRING(filebytes); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 162 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 163 | /* Search tail of filename in sys.path before giving up */ |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 164 | tail = strrchr(filepath, SEP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | if (tail == NULL) |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 166 | tail = filepath; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 167 | else |
| 168 | tail++; |
| 169 | taillen = strlen(tail); |
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 | syspath = PySys_GetObject("path"); |
| 172 | if (syspath == NULL || !PyList_Check(syspath)) |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 173 | goto error; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 174 | npath = PyList_Size(syspath); |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 175 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | for (i = 0; i < npath; i++) { |
| 177 | v = PyList_GetItem(syspath, i); |
| 178 | if (v == NULL) { |
| 179 | PyErr_Clear(); |
| 180 | break; |
| 181 | } |
| 182 | if (!PyUnicode_Check(v)) |
| 183 | continue; |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 184 | path = PyUnicode_EncodeFSDefault(v); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 185 | if (path == NULL) { |
| 186 | PyErr_Clear(); |
| 187 | continue; |
| 188 | } |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 189 | len = PyBytes_GET_SIZE(path); |
| 190 | if (len + 1 + (Py_ssize_t)taillen >= (Py_ssize_t)namelen - 1) { |
| 191 | Py_DECREF(path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 192 | continue; /* Too long */ |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 193 | } |
| 194 | strcpy(namebuf, PyBytes_AS_STRING(path)); |
| 195 | Py_DECREF(path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 196 | if (strlen(namebuf) != len) |
| 197 | continue; /* v contains '\0' */ |
| 198 | if (len > 0 && namebuf[len-1] != SEP) |
| 199 | namebuf[len++] = SEP; |
| 200 | strcpy(namebuf+len, tail); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 201 | |
| 202 | binary = PyObject_CallMethod(io, "open", "ss", namebuf, "rb"); |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 203 | if (binary != NULL) { |
| 204 | result = binary; |
| 205 | goto finally; |
| 206 | } |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 207 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 208 | } |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 209 | goto error; |
| 210 | |
| 211 | error: |
| 212 | result = NULL; |
| 213 | finally: |
| 214 | Py_DECREF(filebytes); |
| 215 | return result; |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 218 | int |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 219 | _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 220 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 221 | int err = 0; |
| 222 | int fd; |
| 223 | int i; |
| 224 | char *found_encoding; |
| 225 | char *encoding; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 226 | PyObject *io; |
| 227 | PyObject *binary; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 228 | PyObject *fob = NULL; |
| 229 | PyObject *lineobj = NULL; |
Antoine Pitrou | b86680e | 2010-10-14 21:15:17 +0000 | [diff] [blame] | 230 | PyObject *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 231 | char buf[MAXPATHLEN+1]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 232 | int kind; |
| 233 | void *data; |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 235 | /* open the file */ |
| 236 | if (filename == NULL) |
| 237 | return 0; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 238 | |
| 239 | io = PyImport_ImportModuleNoBlock("io"); |
| 240 | if (io == NULL) |
| 241 | return -1; |
| 242 | binary = PyObject_CallMethod(io, "open", "Os", filename, "rb"); |
| 243 | |
| 244 | if (binary == NULL) { |
| 245 | binary = _Py_FindSourceFile(filename, buf, sizeof(buf), io); |
| 246 | if (binary == NULL) { |
| 247 | Py_DECREF(io); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 248 | return 0; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 249 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 252 | /* use the right encoding to decode the file as unicode */ |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 253 | fd = PyObject_AsFileDescriptor(binary); |
Victor Stinner | fe7c5b5 | 2011-04-05 01:48:03 +0200 | [diff] [blame] | 254 | found_encoding = PyTokenizer_FindEncodingFilename(fd, filename); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 255 | encoding = (found_encoding != NULL) ? found_encoding : "utf-8"; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 256 | lseek(fd, 0, 0); /* Reset position */ |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 257 | fob = PyObject_CallMethod(io, "TextIOWrapper", "Os", binary, encoding); |
| 258 | Py_DECREF(io); |
| 259 | Py_DECREF(binary); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 260 | PyMem_FREE(found_encoding); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 261 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 262 | if (fob == NULL) { |
| 263 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | return 0; |
| 265 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | /* get the line number lineno */ |
| 268 | for (i = 0; i < lineno; i++) { |
| 269 | Py_XDECREF(lineobj); |
| 270 | lineobj = PyFile_GetLine(fob, -1); |
| 271 | if (!lineobj) { |
| 272 | err = -1; |
| 273 | break; |
| 274 | } |
| 275 | } |
Antoine Pitrou | b86680e | 2010-10-14 21:15:17 +0000 | [diff] [blame] | 276 | res = PyObject_CallMethod(fob, "close", ""); |
| 277 | if (res) |
| 278 | Py_DECREF(res); |
| 279 | else |
| 280 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 281 | Py_DECREF(fob); |
| 282 | if (!lineobj || !PyUnicode_Check(lineobj)) { |
| 283 | Py_XDECREF(lineobj); |
| 284 | return err; |
| 285 | } |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 287 | /* remove the indentation of the line */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 288 | kind = PyUnicode_KIND(lineobj); |
| 289 | data = PyUnicode_DATA(lineobj); |
| 290 | for (i=0; i < PyUnicode_GET_LENGTH(lineobj); i++) { |
| 291 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 292 | if (ch != ' ' && ch != '\t' && ch != '\014') |
| 293 | break; |
| 294 | } |
| 295 | if (i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 296 | PyObject *truncated; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 297 | truncated = PyUnicode_Substring(lineobj, i, PyUnicode_GET_LENGTH(lineobj)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 298 | if (truncated) { |
| 299 | Py_DECREF(lineobj); |
| 300 | lineobj = truncated; |
| 301 | } else { |
| 302 | PyErr_Clear(); |
| 303 | } |
| 304 | } |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | /* Write some spaces before the line */ |
| 307 | strcpy(buf, " "); |
| 308 | assert (strlen(buf) == 10); |
| 309 | while (indent > 0) { |
| 310 | if(indent < 10) |
| 311 | buf[indent] = '\0'; |
| 312 | err = PyFile_WriteString(buf, f); |
| 313 | if (err != 0) |
| 314 | break; |
| 315 | indent -= 10; |
| 316 | } |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | /* finally display the line */ |
| 319 | if (err == 0) |
| 320 | err = PyFile_WriteObject(lineobj, f, Py_PRINT_RAW); |
| 321 | Py_DECREF(lineobj); |
| 322 | if (err == 0) |
| 323 | err = PyFile_WriteString("\n", f); |
| 324 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 327 | static int |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 328 | tb_displayline(PyObject *f, PyObject *filename, int lineno, PyObject *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 329 | { |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 330 | int err; |
| 331 | PyObject *line; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | if (filename == NULL || name == NULL) |
| 334 | return -1; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 335 | line = PyUnicode_FromFormat(" File \"%U\", line %d, in %U\n", |
| 336 | filename, lineno, name); |
| 337 | if (line == NULL) |
| 338 | return -1; |
| 339 | err = PyFile_WriteObject(line, f, Py_PRINT_RAW); |
| 340 | Py_DECREF(line); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | if (err != 0) |
| 342 | return err; |
| 343 | return _Py_DisplaySourceLine(f, filename, lineno, 4); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | static int |
| 347 | tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit) |
| 348 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 349 | int err = 0; |
| 350 | long depth = 0; |
| 351 | PyTracebackObject *tb1 = tb; |
| 352 | while (tb1 != NULL) { |
| 353 | depth++; |
| 354 | tb1 = tb1->tb_next; |
| 355 | } |
| 356 | while (tb != NULL && err == 0) { |
| 357 | if (depth <= limit) { |
| 358 | err = tb_displayline(f, |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 359 | tb->tb_frame->f_code->co_filename, |
| 360 | tb->tb_lineno, |
| 361 | tb->tb_frame->f_code->co_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | } |
| 363 | depth--; |
| 364 | tb = tb->tb_next; |
| 365 | if (err == 0) |
| 366 | err = PyErr_CheckSignals(); |
| 367 | } |
| 368 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Christian Heimes | 0fbab7f | 2007-12-04 21:55:18 +0000 | [diff] [blame] | 371 | #define PyTraceBack_LIMIT 1000 |
| 372 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 373 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 374 | PyTraceBack_Print(PyObject *v, PyObject *f) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 375 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 376 | int err; |
| 377 | PyObject *limitv; |
| 378 | long limit = PyTraceBack_LIMIT; |
Christian Heimes | 0fbab7f | 2007-12-04 21:55:18 +0000 | [diff] [blame] | 379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | if (v == NULL) |
| 381 | return 0; |
| 382 | if (!PyTraceBack_Check(v)) { |
| 383 | PyErr_BadInternalCall(); |
| 384 | return -1; |
| 385 | } |
| 386 | limitv = PySys_GetObject("tracebacklimit"); |
| 387 | if (limitv) { |
| 388 | PyObject *exc_type, *exc_value, *exc_tb; |
Christian Heimes | 0fbab7f | 2007-12-04 21:55:18 +0000 | [diff] [blame] | 389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 390 | PyErr_Fetch(&exc_type, &exc_value, &exc_tb); |
| 391 | limit = PyLong_AsLong(limitv); |
| 392 | if (limit == -1 && PyErr_Occurred()) { |
| 393 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
| 394 | limit = PyTraceBack_LIMIT; |
| 395 | } |
| 396 | else { |
| 397 | Py_XDECREF(exc_type); |
| 398 | Py_XDECREF(exc_value); |
| 399 | Py_XDECREF(exc_tb); |
| 400 | return 0; |
| 401 | } |
| 402 | } |
| 403 | else if (limit <= 0) { |
| 404 | limit = PyTraceBack_LIMIT; |
| 405 | } |
| 406 | PyErr_Restore(exc_type, exc_value, exc_tb); |
| 407 | } |
| 408 | err = PyFile_WriteString("Traceback (most recent call last):\n", f); |
| 409 | if (!err) |
| 410 | err = tb_printinternal((PyTracebackObject *)v, f, limit); |
| 411 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 412 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 413 | |
| 414 | /* Reverse a string. For example, "abcd" becomes "dcba". |
| 415 | |
| 416 | This function is signal safe. */ |
| 417 | |
| 418 | static void |
| 419 | reverse_string(char *text, const size_t len) |
| 420 | { |
| 421 | char tmp; |
| 422 | size_t i, j; |
| 423 | if (len == 0) |
| 424 | return; |
| 425 | for (i=0, j=len-1; i < j; i++, j--) { |
| 426 | tmp = text[i]; |
| 427 | text[i] = text[j]; |
| 428 | text[j] = tmp; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /* Format an integer in range [0; 999999] to decimal, |
| 433 | and write it into the file fd. |
| 434 | |
| 435 | This function is signal safe. */ |
| 436 | |
| 437 | static void |
| 438 | dump_decimal(int fd, int value) |
| 439 | { |
| 440 | char buffer[7]; |
| 441 | int len; |
| 442 | if (value < 0 || 999999 < value) |
| 443 | return; |
| 444 | len = 0; |
| 445 | do { |
| 446 | buffer[len] = '0' + (value % 10); |
| 447 | value /= 10; |
| 448 | len++; |
| 449 | } while (value); |
| 450 | reverse_string(buffer, len); |
| 451 | write(fd, buffer, len); |
| 452 | } |
| 453 | |
| 454 | /* Format an integer in range [0; 0xffffffff] to hexdecimal of 'width' digits, |
| 455 | and write it into the file fd. |
| 456 | |
| 457 | This function is signal safe. */ |
| 458 | |
| 459 | static void |
| 460 | dump_hexadecimal(int width, unsigned long value, int fd) |
| 461 | { |
| 462 | const char *hexdigits = "0123456789abcdef"; |
| 463 | int len; |
| 464 | char buffer[sizeof(unsigned long) * 2 + 1]; |
| 465 | len = 0; |
| 466 | do { |
| 467 | buffer[len] = hexdigits[value & 15]; |
| 468 | value >>= 4; |
| 469 | len++; |
| 470 | } while (len < width || value); |
| 471 | reverse_string(buffer, len); |
| 472 | write(fd, buffer, len); |
| 473 | } |
| 474 | |
| 475 | /* Write an unicode object into the file fd using ascii+backslashreplace. |
| 476 | |
| 477 | This function is signal safe. */ |
| 478 | |
| 479 | static void |
| 480 | dump_ascii(int fd, PyObject *text) |
| 481 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 482 | PyASCIIObject *ascii = (PyASCIIObject *)text; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 483 | Py_ssize_t i, size; |
| 484 | int truncated; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 485 | int kind; |
Victor Stinner | a336de7 | 2011-10-05 22:44:12 +0200 | [diff] [blame] | 486 | void *data = NULL; |
| 487 | wchar_t *wstr = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 488 | Py_UCS4 ch; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 489 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 490 | size = ascii->length; |
| 491 | kind = ascii->state.kind; |
| 492 | if (ascii->state.compact) { |
| 493 | if (ascii->state.ascii) |
| 494 | data = ((PyASCIIObject*)text) + 1; |
| 495 | else |
| 496 | data = ((PyCompactUnicodeObject*)text) + 1; |
| 497 | } |
Victor Stinner | a336de7 | 2011-10-05 22:44:12 +0200 | [diff] [blame] | 498 | else if (kind != PyUnicode_WCHAR_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 499 | data = ((PyUnicodeObject *)text)->data.any; |
| 500 | if (data == NULL) |
| 501 | return; |
| 502 | } |
Victor Stinner | a336de7 | 2011-10-05 22:44:12 +0200 | [diff] [blame] | 503 | else { |
| 504 | wstr = ((PyASCIIObject *)text)->wstr; |
| 505 | if (wstr == NULL) |
| 506 | return; |
| 507 | size = ((PyCompactUnicodeObject *)text)->wstr_length; |
| 508 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 509 | |
| 510 | if (MAX_STRING_LENGTH < size) { |
| 511 | size = MAX_STRING_LENGTH; |
| 512 | truncated = 1; |
| 513 | } |
| 514 | else |
| 515 | truncated = 0; |
| 516 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 517 | for (i=0; i < size; i++) { |
Victor Stinner | a336de7 | 2011-10-05 22:44:12 +0200 | [diff] [blame] | 518 | if (kind != PyUnicode_WCHAR_KIND) |
| 519 | ch = PyUnicode_READ(kind, data, i); |
| 520 | else |
| 521 | ch = wstr[i]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 522 | if (ch < 128) { |
| 523 | char c = (char)ch; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 524 | write(fd, &c, 1); |
| 525 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 526 | else if (ch < 256) { |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 527 | PUTS(fd, "\\x"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 528 | dump_hexadecimal(2, ch, fd); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 529 | } |
| 530 | else |
| 531 | #ifdef Py_UNICODE_WIDE |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 532 | if (ch < 65536) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 533 | #endif |
| 534 | { |
| 535 | PUTS(fd, "\\u"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 536 | dump_hexadecimal(4, ch, fd); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 537 | #ifdef Py_UNICODE_WIDE |
| 538 | } |
| 539 | else { |
| 540 | PUTS(fd, "\\U"); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 541 | dump_hexadecimal(8, ch, fd); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 542 | #endif |
| 543 | } |
| 544 | } |
| 545 | if (truncated) |
| 546 | PUTS(fd, "..."); |
| 547 | } |
| 548 | |
| 549 | /* Write a frame into the file fd: "File "xxx", line xxx in xxx". |
| 550 | |
| 551 | This function is signal safe. */ |
| 552 | |
| 553 | static void |
| 554 | dump_frame(int fd, PyFrameObject *frame) |
| 555 | { |
| 556 | PyCodeObject *code; |
| 557 | int lineno; |
| 558 | |
| 559 | code = frame->f_code; |
| 560 | PUTS(fd, " File "); |
| 561 | if (code != NULL && code->co_filename != NULL |
| 562 | && PyUnicode_Check(code->co_filename)) |
| 563 | { |
| 564 | write(fd, "\"", 1); |
| 565 | dump_ascii(fd, code->co_filename); |
| 566 | write(fd, "\"", 1); |
| 567 | } else { |
| 568 | PUTS(fd, "???"); |
| 569 | } |
| 570 | |
| 571 | /* PyFrame_GetLineNumber() was introduced in Python 2.7.0 and 3.2.0 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 572 | lineno = PyCode_Addr2Line(code, frame->f_lasti); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 573 | PUTS(fd, ", line "); |
| 574 | dump_decimal(fd, lineno); |
| 575 | PUTS(fd, " in "); |
| 576 | |
| 577 | if (code != NULL && code->co_name != NULL |
| 578 | && PyUnicode_Check(code->co_name)) |
| 579 | dump_ascii(fd, code->co_name); |
| 580 | else |
| 581 | PUTS(fd, "???"); |
| 582 | |
| 583 | write(fd, "\n", 1); |
| 584 | } |
| 585 | |
Victor Stinner | fcb88c4 | 2011-04-01 15:34:01 +0200 | [diff] [blame] | 586 | static void |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 587 | dump_traceback(int fd, PyThreadState *tstate, int write_header) |
| 588 | { |
| 589 | PyFrameObject *frame; |
| 590 | unsigned int depth; |
| 591 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 592 | if (write_header) |
| 593 | PUTS(fd, "Traceback (most recent call first):\n"); |
Victor Stinner | fcb88c4 | 2011-04-01 15:34:01 +0200 | [diff] [blame] | 594 | |
| 595 | frame = _PyThreadState_GetFrame(tstate); |
| 596 | if (frame == NULL) |
| 597 | return; |
| 598 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 599 | depth = 0; |
| 600 | while (frame != NULL) { |
| 601 | if (MAX_FRAME_DEPTH <= depth) { |
| 602 | PUTS(fd, " ...\n"); |
| 603 | break; |
| 604 | } |
| 605 | if (!PyFrame_Check(frame)) |
| 606 | break; |
| 607 | dump_frame(fd, frame); |
| 608 | frame = frame->f_back; |
| 609 | depth++; |
| 610 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 611 | } |
| 612 | |
Victor Stinner | fcb88c4 | 2011-04-01 15:34:01 +0200 | [diff] [blame] | 613 | void |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 614 | _Py_DumpTraceback(int fd, PyThreadState *tstate) |
| 615 | { |
Victor Stinner | fcb88c4 | 2011-04-01 15:34:01 +0200 | [diff] [blame] | 616 | dump_traceback(fd, tstate, 1); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /* Write the thread identifier into the file 'fd': "Current thread 0xHHHH:\" if |
| 620 | is_current is true, "Thread 0xHHHH:\n" otherwise. |
| 621 | |
| 622 | This function is signal safe. */ |
| 623 | |
| 624 | static void |
| 625 | write_thread_id(int fd, PyThreadState *tstate, int is_current) |
| 626 | { |
| 627 | if (is_current) |
| 628 | PUTS(fd, "Current thread 0x"); |
| 629 | else |
| 630 | PUTS(fd, "Thread 0x"); |
| 631 | dump_hexadecimal(sizeof(long)*2, (unsigned long)tstate->thread_id, fd); |
| 632 | PUTS(fd, ":\n"); |
| 633 | } |
| 634 | |
| 635 | const char* |
| 636 | _Py_DumpTracebackThreads(int fd, PyInterpreterState *interp, |
| 637 | PyThreadState *current_thread) |
| 638 | { |
| 639 | PyThreadState *tstate; |
| 640 | unsigned int nthreads; |
| 641 | |
| 642 | /* Get the current interpreter from the current thread */ |
| 643 | tstate = PyInterpreterState_ThreadHead(interp); |
| 644 | if (tstate == NULL) |
| 645 | return "unable to get the thread head state"; |
| 646 | |
| 647 | /* Dump the traceback of each thread */ |
| 648 | tstate = PyInterpreterState_ThreadHead(interp); |
| 649 | nthreads = 0; |
| 650 | do |
| 651 | { |
| 652 | if (nthreads != 0) |
| 653 | write(fd, "\n", 1); |
| 654 | if (nthreads >= MAX_NTHREADS) { |
| 655 | PUTS(fd, "...\n"); |
| 656 | break; |
| 657 | } |
| 658 | write_thread_id(fd, tstate, tstate == current_thread); |
| 659 | dump_traceback(fd, tstate, 0); |
| 660 | tstate = PyThreadState_Next(tstate); |
| 661 | nthreads++; |
| 662 | } while (tstate != NULL); |
| 663 | |
| 664 | return NULL; |
| 665 | } |
| 666 | |