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