Mercurial > cpython-lru_cache-weakref
annotate Python/traceback.c @ 13096:8c8de5b736dc legacy-trunk
Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
tests.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 28 Jun 2000 22:07:35 +0000 |
| parents | 1c7c2dd1beb1 |
| children | 916fb024ce94 |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
2062
d0d0a69b5ca3
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2052
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
d0d0a69b5ca3
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2052
diff
changeset
|
3 The Netherlands. |
| 145 | 4 |
| 5 All Rights Reserved | |
| 6 | |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3099
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3099
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
| 145 | 9 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3099
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
| 145 | 11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3099
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3099
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3099
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3099
diff
changeset
|
15 permission. |
| 145 | 16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3099
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3099
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3099
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3099
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3099
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3099
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3099
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3099
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3099
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3099
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3099
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
3099
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
| 145 | 29 |
| 30 ******************************************************************/ | |
| 31 | |
| 86 | 32 /* Traceback implementation */ |
| 33 | |
| 5372 | 34 #include "Python.h" |
| 86 | 35 |
| 36 #include "compile.h" | |
| 37 #include "frameobject.h" | |
| 38 #include "structmember.h" | |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
39 #include "osdefs.h" |
| 86 | 40 |
| 41 typedef struct _tracebackobject { | |
| 5372 | 42 PyObject_HEAD |
| 86 | 43 struct _tracebackobject *tb_next; |
| 5372 | 44 PyFrameObject *tb_frame; |
| 86 | 45 int tb_lasti; |
| 46 int tb_lineno; | |
| 47 } tracebackobject; | |
| 48 | |
| 49 #define OFF(x) offsetof(tracebackobject, x) | |
| 50 | |
| 51 static struct memberlist tb_memberlist[] = { | |
| 52 {"tb_next", T_OBJECT, OFF(tb_next)}, | |
| 53 {"tb_frame", T_OBJECT, OFF(tb_frame)}, | |
| 54 {"tb_lasti", T_INT, OFF(tb_lasti)}, | |
| 55 {"tb_lineno", T_INT, OFF(tb_lineno)}, | |
| 56 {NULL} /* Sentinel */ | |
| 57 }; | |
| 58 | |
| 5372 | 59 static PyObject * |
| 86 | 60 tb_getattr(tb, name) |
| 61 tracebackobject *tb; | |
| 62 char *name; | |
| 63 { | |
| 5372 | 64 return PyMember_Get((char *)tb, tb_memberlist, name); |
| 86 | 65 } |
| 66 | |
| 67 static void | |
| 68 tb_dealloc(tb) | |
| 69 tracebackobject *tb; | |
| 70 { | |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11294
diff
changeset
|
71 Py_TRASHCAN_SAFE_BEGIN(tb) |
| 5372 | 72 Py_XDECREF(tb->tb_next); |
| 73 Py_XDECREF(tb->tb_frame); | |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12137
diff
changeset
|
74 PyObject_DEL(tb); |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11294
diff
changeset
|
75 Py_TRASHCAN_SAFE_END(tb) |
| 86 | 76 } |
| 77 | |
|
3099
d249111d4ccc
spell TraceBack with capital B
Guido van Rossum <guido@python.org>
parents:
2844
diff
changeset
|
78 #define Tracebacktype PyTraceBack_Type |
|
d249111d4ccc
spell TraceBack with capital B
Guido van Rossum <guido@python.org>
parents:
2844
diff
changeset
|
79 #define is_tracebackobject PyTraceBack_Check |
|
2844
0261bf5b3819
keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents:
2811
diff
changeset
|
80 |
| 5372 | 81 PyTypeObject Tracebacktype = { |
| 82 PyObject_HEAD_INIT(&PyType_Type) | |
| 86 | 83 0, |
| 84 "traceback", | |
| 85 sizeof(tracebackobject), | |
| 86 0, | |
|
1831
216b755515e6
add function name to traceback info
Guido van Rossum <guido@python.org>
parents:
1425
diff
changeset
|
87 (destructor)tb_dealloc, /*tp_dealloc*/ |
| 86 | 88 0, /*tp_print*/ |
|
1831
216b755515e6
add function name to traceback info
Guido van Rossum <guido@python.org>
parents:
1425
diff
changeset
|
89 (getattrfunc)tb_getattr, /*tp_getattr*/ |
| 86 | 90 0, /*tp_setattr*/ |
| 91 0, /*tp_compare*/ | |
| 92 0, /*tp_repr*/ | |
| 93 0, /*tp_as_number*/ | |
| 94 0, /*tp_as_sequence*/ | |
| 95 0, /*tp_as_mapping*/ | |
| 96 }; | |
| 97 | |
| 98 static tracebackobject * | |
| 99 newtracebackobject(next, frame, lasti, lineno) | |
| 100 tracebackobject *next; | |
| 5372 | 101 PyFrameObject *frame; |
| 86 | 102 int lasti, lineno; |
| 103 { | |
| 104 tracebackobject *tb; | |
| 105 if ((next != NULL && !is_tracebackobject(next)) || | |
| 5372 | 106 frame == NULL || !PyFrame_Check(frame)) { |
| 107 PyErr_BadInternalCall(); | |
| 86 | 108 return NULL; |
| 109 } | |
| 5372 | 110 tb = PyObject_NEW(tracebackobject, &Tracebacktype); |
| 86 | 111 if (tb != NULL) { |
| 5372 | 112 Py_XINCREF(next); |
| 86 | 113 tb->tb_next = next; |
| 5372 | 114 Py_XINCREF(frame); |
| 86 | 115 tb->tb_frame = frame; |
| 116 tb->tb_lasti = lasti; | |
| 117 tb->tb_lineno = lineno; | |
| 118 } | |
| 119 return tb; | |
| 120 } | |
| 121 | |
| 122 int | |
| 5372 | 123 PyTraceBack_Here(frame) |
| 124 PyFrameObject *frame; | |
| 86 | 125 { |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5404
diff
changeset
|
126 PyThreadState *tstate = frame->f_tstate; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5404
diff
changeset
|
127 tracebackobject *oldtb = (tracebackobject *) tstate->curexc_traceback; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5404
diff
changeset
|
128 tracebackobject *tb = newtracebackobject(oldtb, |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5404
diff
changeset
|
129 frame, frame->f_lasti, frame->f_lineno); |
| 86 | 130 if (tb == NULL) |
| 131 return -1; | |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5404
diff
changeset
|
132 tstate->curexc_traceback = (PyObject *)tb; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5404
diff
changeset
|
133 Py_XDECREF(oldtb); |
| 86 | 134 return 0; |
| 135 } | |
| 136 | |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
137 static int |
|
1831
216b755515e6
add function name to traceback info
Guido van Rossum <guido@python.org>
parents:
1425
diff
changeset
|
138 tb_displayline(f, filename, lineno, name) |
| 5372 | 139 PyObject *f; |
| 86 | 140 char *filename; |
| 141 int lineno; | |
|
1831
216b755515e6
add function name to traceback info
Guido van Rossum <guido@python.org>
parents:
1425
diff
changeset
|
142 char *name; |
| 86 | 143 { |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
144 int err = 0; |
| 86 | 145 FILE *xfp; |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
146 char linebuf[1000]; |
| 86 | 147 int i; |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
148 if (filename == NULL || name == NULL) |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
149 return -1; |
|
1831
216b755515e6
add function name to traceback info
Guido van Rossum <guido@python.org>
parents:
1425
diff
changeset
|
150 #ifdef MPW |
|
216b755515e6
add function name to traceback info
Guido van Rossum <guido@python.org>
parents:
1425
diff
changeset
|
151 /* This is needed by MPW's File and Line commands */ |
|
216b755515e6
add function name to traceback info
Guido van Rossum <guido@python.org>
parents:
1425
diff
changeset
|
152 #define FMT " File \"%.900s\"; line %d # in %s\n" |
|
216b755515e6
add function name to traceback info
Guido van Rossum <guido@python.org>
parents:
1425
diff
changeset
|
153 #else |
|
216b755515e6
add function name to traceback info
Guido van Rossum <guido@python.org>
parents:
1425
diff
changeset
|
154 /* This is needed by Emacs' compile command */ |
|
216b755515e6
add function name to traceback info
Guido van Rossum <guido@python.org>
parents:
1425
diff
changeset
|
155 #define FMT " File \"%.900s\", line %d, in %s\n" |
|
216b755515e6
add function name to traceback info
Guido van Rossum <guido@python.org>
parents:
1425
diff
changeset
|
156 #endif |
| 86 | 157 xfp = fopen(filename, "r"); |
| 158 if (xfp == NULL) { | |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
159 /* Search tail of filename in sys.path before giving up */ |
| 5372 | 160 PyObject *path; |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
161 char *tail = strrchr(filename, SEP); |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
162 if (tail == NULL) |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
163 tail = filename; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
164 else |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
165 tail++; |
| 5372 | 166 path = PySys_GetObject("path"); |
| 167 if (path != NULL && PyList_Check(path)) { | |
| 168 int npath = PyList_Size(path); | |
|
13096
8c8de5b736dc
Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents:
12576
diff
changeset
|
169 size_t taillen = strlen(tail); |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
170 char namebuf[MAXPATHLEN+1]; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
171 for (i = 0; i < npath; i++) { |
| 5372 | 172 PyObject *v = PyList_GetItem(path, i); |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
173 if (v == NULL) { |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
174 PyErr_Clear(); |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
175 break; |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
176 } |
| 5372 | 177 if (PyString_Check(v)) { |
|
13096
8c8de5b736dc
Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents:
12576
diff
changeset
|
178 size_t len; |
| 5372 | 179 len = PyString_Size(v); |
|
1909
0c232194a64f
* Python/traceback.c: security fix -- check for buffer oveflow
Guido van Rossum <guido@python.org>
parents:
1831
diff
changeset
|
180 if (len + 1 + taillen >= MAXPATHLEN) |
|
0c232194a64f
* Python/traceback.c: security fix -- check for buffer oveflow
Guido van Rossum <guido@python.org>
parents:
1831
diff
changeset
|
181 continue; /* Too long */ |
| 5372 | 182 strcpy(namebuf, PyString_AsString(v)); |
|
13096
8c8de5b736dc
Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents:
12576
diff
changeset
|
183 if (strlen(namebuf) != len) |
|
1909
0c232194a64f
* Python/traceback.c: security fix -- check for buffer oveflow
Guido van Rossum <guido@python.org>
parents:
1831
diff
changeset
|
184 continue; /* v contains '\0' */ |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
185 if (len > 0 && namebuf[len-1] != SEP) |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
186 namebuf[len++] = SEP; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
187 strcpy(namebuf+len, tail); |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
188 xfp = fopen(namebuf, "r"); |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
189 if (xfp != NULL) { |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
190 filename = namebuf; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
191 break; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
192 } |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
193 } |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
194 } |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
195 } |
| 86 | 196 } |
|
1831
216b755515e6
add function name to traceback info
Guido van Rossum <guido@python.org>
parents:
1425
diff
changeset
|
197 sprintf(linebuf, FMT, filename, lineno, name); |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
198 err = PyFile_WriteString(linebuf, f); |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
199 if (xfp == NULL || err != 0) |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
200 return err; |
| 86 | 201 for (i = 0; i < lineno; i++) { |
|
11294
d3696bf08eee
Tim Peters fixed PR#75: very long lines cause incorrect tracebacks.
Guido van Rossum <guido@python.org>
parents:
5608
diff
changeset
|
202 char* pLastChar = &linebuf[sizeof(linebuf)-2]; |
|
d3696bf08eee
Tim Peters fixed PR#75: very long lines cause incorrect tracebacks.
Guido van Rossum <guido@python.org>
parents:
5608
diff
changeset
|
203 do { |
|
d3696bf08eee
Tim Peters fixed PR#75: very long lines cause incorrect tracebacks.
Guido van Rossum <guido@python.org>
parents:
5608
diff
changeset
|
204 *pLastChar = '\0'; |
|
d3696bf08eee
Tim Peters fixed PR#75: very long lines cause incorrect tracebacks.
Guido van Rossum <guido@python.org>
parents:
5608
diff
changeset
|
205 if (fgets(linebuf, sizeof linebuf, xfp) == NULL) |
|
d3696bf08eee
Tim Peters fixed PR#75: very long lines cause incorrect tracebacks.
Guido van Rossum <guido@python.org>
parents:
5608
diff
changeset
|
206 break; |
|
d3696bf08eee
Tim Peters fixed PR#75: very long lines cause incorrect tracebacks.
Guido van Rossum <guido@python.org>
parents:
5608
diff
changeset
|
207 /* fgets read *something*; if it didn't get as |
|
d3696bf08eee
Tim Peters fixed PR#75: very long lines cause incorrect tracebacks.
Guido van Rossum <guido@python.org>
parents:
5608
diff
changeset
|
208 far as pLastChar, it must have found a newline |
|
d3696bf08eee
Tim Peters fixed PR#75: very long lines cause incorrect tracebacks.
Guido van Rossum <guido@python.org>
parents:
5608
diff
changeset
|
209 or hit the end of the file; if pLastChar is \n, |
|
d3696bf08eee
Tim Peters fixed PR#75: very long lines cause incorrect tracebacks.
Guido van Rossum <guido@python.org>
parents:
5608
diff
changeset
|
210 it obviously found a newline; else we haven't |
|
d3696bf08eee
Tim Peters fixed PR#75: very long lines cause incorrect tracebacks.
Guido van Rossum <guido@python.org>
parents:
5608
diff
changeset
|
211 yet seen a newline, so must continue */ |
|
d3696bf08eee
Tim Peters fixed PR#75: very long lines cause incorrect tracebacks.
Guido van Rossum <guido@python.org>
parents:
5608
diff
changeset
|
212 } while (*pLastChar != '\0' && *pLastChar != '\n'); |
| 86 | 213 } |
| 214 if (i == lineno) { | |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
215 char *p = linebuf; |
|
2811
07fc4fefb683
ignore control-l (parallelling change to tokenizer.c)
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
216 while (*p == ' ' || *p == '\t' || *p == '\014') |
| 86 | 217 p++; |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
218 err = PyFile_WriteString(" ", f); |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
219 if (err == 0) { |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
220 err = PyFile_WriteString(p, f); |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
221 if (err == 0 && strchr(p, '\n') == NULL) |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
222 err = PyFile_WriteString("\n", f); |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
223 } |
| 86 | 224 } |
| 225 fclose(xfp); | |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
226 return err; |
| 86 | 227 } |
| 228 | |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
229 static int |
|
1425
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
230 tb_printinternal(tb, f, limit) |
| 86 | 231 tracebackobject *tb; |
| 5372 | 232 PyObject *f; |
|
1425
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
233 int limit; |
| 86 | 234 { |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
235 int err = 0; |
|
1425
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
236 int depth = 0; |
|
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
237 tracebackobject *tb1 = tb; |
|
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
238 while (tb1 != NULL) { |
|
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
239 depth++; |
|
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
240 tb1 = tb1->tb_next; |
|
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
241 } |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
242 while (tb != NULL && err == 0) { |
|
5046
658815230546
Get the line number from PyCode_Addr2Line instead of believing
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
243 if (depth <= limit) { |
|
5404
3780cce14f7d
Only use PyCode_Addr2Line to get tb_lineno when Py_OptimizeFlag is set.
Guido van Rossum <guido@python.org>
parents:
5372
diff
changeset
|
244 if (Py_OptimizeFlag) |
|
3780cce14f7d
Only use PyCode_Addr2Line to get tb_lineno when Py_OptimizeFlag is set.
Guido van Rossum <guido@python.org>
parents:
5372
diff
changeset
|
245 tb->tb_lineno = PyCode_Addr2Line( |
|
3780cce14f7d
Only use PyCode_Addr2Line to get tb_lineno when Py_OptimizeFlag is set.
Guido van Rossum <guido@python.org>
parents:
5372
diff
changeset
|
246 tb->tb_frame->f_code, tb->tb_lasti); |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
247 err = tb_displayline(f, |
| 5372 | 248 PyString_AsString( |
| 249 tb->tb_frame->f_code->co_filename), | |
|
1831
216b755515e6
add function name to traceback info
Guido van Rossum <guido@python.org>
parents:
1425
diff
changeset
|
250 tb->tb_lineno, |
| 5372 | 251 PyString_AsString(tb->tb_frame->f_code->co_name)); |
|
5046
658815230546
Get the line number from PyCode_Addr2Line instead of believing
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
252 } |
|
1425
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
253 depth--; |
| 86 | 254 tb = tb->tb_next; |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
255 if (err == 0) |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
256 err = PyErr_CheckSignals(); |
| 86 | 257 } |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
258 return err; |
| 86 | 259 } |
| 260 | |
| 261 int | |
| 5372 | 262 PyTraceBack_Print(v, f) |
| 263 PyObject *v; | |
| 264 PyObject *f; | |
| 86 | 265 { |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
266 int err; |
| 5372 | 267 PyObject *limitv; |
|
1425
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
268 int limit = 1000; |
| 86 | 269 if (v == NULL) |
| 270 return 0; | |
| 271 if (!is_tracebackobject(v)) { | |
| 5372 | 272 PyErr_BadInternalCall(); |
| 86 | 273 return -1; |
| 274 } | |
| 5372 | 275 limitv = PySys_GetObject("tracebacklimit"); |
| 276 if (limitv && PyInt_Check(limitv)) { | |
| 277 limit = PyInt_AsLong(limitv); | |
|
1425
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
278 if (limit <= 0) |
|
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
279 return 0; |
|
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
280 } |
|
12137
5b5581af951c
Change traceback error message to "most recent call last" from
Guido van Rossum <guido@python.org>
parents:
11999
diff
changeset
|
281 err = PyFile_WriteString("Traceback (most recent call last):\n", f); |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
282 if (!err) |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
283 err = tb_printinternal((tracebackobject *)v, f, limit); |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
284 return err; |
| 86 | 285 } |
