Mercurial > cpython-lru_cache-weakref
annotate Python/traceback.c @ 31649:243642bbd03f legacy-trunk
LIST_APPEND is predicably followed by JUMP_ABSOLUTE.
Reduces loop overhead by an additional 10%.
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Fri, 12 Mar 2004 16:33:17 +0000 |
| parents | b6c139544c16 |
| children | 49ec0d8f08ec |
| rev | line source |
|---|---|
| 145 | 1 |
| 86 | 2 /* Traceback implementation */ |
| 3 | |
| 5372 | 4 #include "Python.h" |
| 86 | 5 |
| 6 #include "compile.h" | |
| 7 #include "frameobject.h" | |
| 8 #include "structmember.h" | |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
9 #include "osdefs.h" |
| 86 | 10 |
| 11 typedef struct _tracebackobject { | |
| 5372 | 12 PyObject_HEAD |
| 86 | 13 struct _tracebackobject *tb_next; |
| 5372 | 14 PyFrameObject *tb_frame; |
| 86 | 15 int tb_lasti; |
| 16 int tb_lineno; | |
| 17 } tracebackobject; | |
| 18 | |
| 19 #define OFF(x) offsetof(tracebackobject, x) | |
| 20 | |
| 21 static struct memberlist tb_memberlist[] = { | |
| 22 {"tb_next", T_OBJECT, OFF(tb_next)}, | |
| 23 {"tb_frame", T_OBJECT, OFF(tb_frame)}, | |
| 24 {"tb_lasti", T_INT, OFF(tb_lasti)}, | |
| 25 {"tb_lineno", T_INT, OFF(tb_lineno)}, | |
| 26 {NULL} /* Sentinel */ | |
| 27 }; | |
| 28 | |
| 5372 | 29 static PyObject * |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
30 tb_getattr(tracebackobject *tb, char *name) |
| 86 | 31 { |
| 5372 | 32 return PyMember_Get((char *)tb, tb_memberlist, name); |
| 86 | 33 } |
| 34 | |
| 35 static void | |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
36 tb_dealloc(tracebackobject *tb) |
| 86 | 37 { |
|
22650
2288c724fe2b
Call PyObject_GC_UnTrack before putting an object on the list of trash.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
21077
diff
changeset
|
38 PyObject_GC_UnTrack(tb); |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11294
diff
changeset
|
39 Py_TRASHCAN_SAFE_BEGIN(tb) |
| 5372 | 40 Py_XDECREF(tb->tb_next); |
| 41 Py_XDECREF(tb->tb_frame); | |
|
20623
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
42 PyObject_GC_Del(tb); |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11294
diff
changeset
|
43 Py_TRASHCAN_SAFE_END(tb) |
| 86 | 44 } |
| 45 | |
|
20623
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
46 static int |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
47 tb_traverse(tracebackobject *tb, visitproc visit, void *arg) |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
48 { |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
49 int err = 0; |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
50 if (tb->tb_next) { |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
51 err = visit((PyObject *)tb->tb_next, arg); |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
52 if (err) |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
53 return err; |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
54 } |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
55 if (tb->tb_frame) |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
56 err = visit((PyObject *)tb->tb_frame, arg); |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
57 return err; |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
58 } |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
59 |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
60 static void |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
61 tb_clear(tracebackobject *tb) |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
62 { |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
63 Py_XDECREF(tb->tb_next); |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
64 Py_XDECREF(tb->tb_frame); |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
65 tb->tb_next = NULL; |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
66 tb->tb_frame = NULL; |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
67 } |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
68 |
|
20618
57b51966a4f1
Removed two pointless and obfuscating macros.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
69 PyTypeObject PyTraceBack_Type = { |
| 5372 | 70 PyObject_HEAD_INIT(&PyType_Type) |
| 86 | 71 0, |
| 72 "traceback", | |
| 73 sizeof(tracebackobject), | |
| 74 0, | |
|
1831
216b755515e6
add function name to traceback info
Guido van Rossum <guido@python.org>
parents:
1425
diff
changeset
|
75 (destructor)tb_dealloc, /*tp_dealloc*/ |
| 86 | 76 0, /*tp_print*/ |
|
1831
216b755515e6
add function name to traceback info
Guido van Rossum <guido@python.org>
parents:
1425
diff
changeset
|
77 (getattrfunc)tb_getattr, /*tp_getattr*/ |
| 86 | 78 0, /*tp_setattr*/ |
| 79 0, /*tp_compare*/ | |
| 80 0, /*tp_repr*/ | |
| 81 0, /*tp_as_number*/ | |
| 82 0, /*tp_as_sequence*/ | |
| 83 0, /*tp_as_mapping*/ | |
|
20623
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
84 0, /* tp_hash */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
85 0, /* tp_call */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
86 0, /* tp_str */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
87 0, /* tp_getattro */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
88 0, /* tp_setattro */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
89 0, /* tp_as_buffer */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
90 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
91 0, /* tp_doc */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
92 (traverseproc)tb_traverse, /* tp_traverse */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
93 (inquiry)tb_clear, /* tp_clear */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
94 0, /* tp_richcompare */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
95 0, /* tp_weaklistoffset */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
96 0, /* tp_iter */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
97 0, /* tp_iternext */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
98 0, /* tp_methods */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
99 0, /* tp_members */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
100 0, /* tp_getset */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
101 0, /* tp_base */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
102 0, /* tp_dict */ |
| 86 | 103 }; |
| 104 | |
| 105 static tracebackobject * | |
| 24844 | 106 newtracebackobject(tracebackobject *next, PyFrameObject *frame) |
| 86 | 107 { |
| 108 tracebackobject *tb; | |
|
20618
57b51966a4f1
Removed two pointless and obfuscating macros.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
109 if ((next != NULL && !PyTraceBack_Check(next)) || |
| 5372 | 110 frame == NULL || !PyFrame_Check(frame)) { |
| 111 PyErr_BadInternalCall(); | |
| 86 | 112 return NULL; |
| 113 } | |
|
20623
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
114 tb = PyObject_GC_New(tracebackobject, &PyTraceBack_Type); |
| 86 | 115 if (tb != NULL) { |
| 5372 | 116 Py_XINCREF(next); |
| 86 | 117 tb->tb_next = next; |
| 5372 | 118 Py_XINCREF(frame); |
| 86 | 119 tb->tb_frame = frame; |
| 24844 | 120 tb->tb_lasti = frame->f_lasti; |
| 121 tb->tb_lineno = PyCode_Addr2Line(frame->f_code, | |
| 122 frame->f_lasti); | |
|
22650
2288c724fe2b
Call PyObject_GC_UnTrack before putting an object on the list of trash.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
21077
diff
changeset
|
123 PyObject_GC_Track(tb); |
| 86 | 124 } |
| 125 return tb; | |
| 126 } | |
| 127 | |
| 128 int | |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
129 PyTraceBack_Here(PyFrameObject *frame) |
| 86 | 130 { |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5404
diff
changeset
|
131 PyThreadState *tstate = frame->f_tstate; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5404
diff
changeset
|
132 tracebackobject *oldtb = (tracebackobject *) tstate->curexc_traceback; |
| 24844 | 133 tracebackobject *tb = newtracebackobject(oldtb, frame); |
| 86 | 134 if (tb == NULL) |
| 135 return -1; | |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5404
diff
changeset
|
136 tstate->curexc_traceback = (PyObject *)tb; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5404
diff
changeset
|
137 Py_XDECREF(oldtb); |
| 86 | 138 return 0; |
| 139 } | |
| 140 | |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
141 static int |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
142 tb_displayline(PyObject *f, char *filename, int lineno, 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; |
|
21057
2c0a9a5086e1
SF bug 485175: buffer overflow in traceback.c.
Tim Peters <tim.peters@gmail.com>
parents:
20623
diff
changeset
|
146 char linebuf[2000]; |
| 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 /* This is needed by Emacs' compile command */ |
|
21057
2c0a9a5086e1
SF bug 485175: buffer overflow in traceback.c.
Tim Peters <tim.peters@gmail.com>
parents:
20623
diff
changeset
|
151 #define FMT " File \"%.500s\", line %d, in %.500s\n" |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22650
diff
changeset
|
152 xfp = fopen(filename, "r" PY_STDIOTEXTMODE); |
| 86 | 153 if (xfp == NULL) { |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
154 /* Search tail of filename in sys.path before giving up */ |
| 5372 | 155 PyObject *path; |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
156 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
|
157 if (tail == NULL) |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
158 tail = filename; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
159 else |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
160 tail++; |
| 5372 | 161 path = PySys_GetObject("path"); |
| 162 if (path != NULL && PyList_Check(path)) { | |
| 163 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
|
164 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
|
165 char namebuf[MAXPATHLEN+1]; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
166 for (i = 0; i < npath; i++) { |
| 5372 | 167 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
|
168 if (v == NULL) { |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
169 PyErr_Clear(); |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
170 break; |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
171 } |
| 5372 | 172 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
|
173 size_t len; |
| 5372 | 174 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
|
175 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
|
176 continue; /* Too long */ |
| 5372 | 177 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
|
178 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
|
179 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
|
180 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
|
181 namebuf[len++] = SEP; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
182 strcpy(namebuf+len, tail); |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22650
diff
changeset
|
183 xfp = fopen(namebuf, "r" PY_STDIOTEXTMODE); |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
184 if (xfp != NULL) { |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
185 filename = namebuf; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
186 break; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
187 } |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
188 } |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
189 } |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
190 } |
| 86 | 191 } |
|
21077
379fc66f4c96
Use PyOS_snprintf instead of sprintf.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21057
diff
changeset
|
192 PyOS_snprintf(linebuf, sizeof(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
|
193 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
|
194 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
|
195 return err; |
| 86 | 196 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
|
197 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
|
198 do { |
|
d3696bf08eee
Tim Peters fixed PR#75: very long lines cause incorrect tracebacks.
Guido van Rossum <guido@python.org>
parents:
5608
diff
changeset
|
199 *pLastChar = '\0'; |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22650
diff
changeset
|
200 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, xfp, NULL) == NULL) |
|
11294
d3696bf08eee
Tim Peters fixed PR#75: very long lines cause incorrect tracebacks.
Guido van Rossum <guido@python.org>
parents:
5608
diff
changeset
|
201 break; |
|
d3696bf08eee
Tim Peters fixed PR#75: very long lines cause incorrect tracebacks.
Guido van Rossum <guido@python.org>
parents:
5608
diff
changeset
|
202 /* 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
|
203 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
|
204 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
|
205 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
|
206 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
|
207 } while (*pLastChar != '\0' && *pLastChar != '\n'); |
| 86 | 208 } |
| 209 if (i == lineno) { | |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
210 char *p = linebuf; |
|
2811
07fc4fefb683
ignore control-l (parallelling change to tokenizer.c)
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
211 while (*p == ' ' || *p == '\t' || *p == '\014') |
| 86 | 212 p++; |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
213 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
|
214 if (err == 0) { |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
215 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
|
216 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
|
217 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
|
218 } |
| 86 | 219 } |
| 220 fclose(xfp); | |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
221 return err; |
| 86 | 222 } |
| 223 | |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
224 static int |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
225 tb_printinternal(tracebackobject *tb, PyObject *f, int limit) |
| 86 | 226 { |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
227 int err = 0; |
|
1425
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
228 int depth = 0; |
|
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
229 tracebackobject *tb1 = tb; |
|
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
230 while (tb1 != NULL) { |
|
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
231 depth++; |
|
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
232 tb1 = tb1->tb_next; |
|
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
233 } |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
234 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
|
235 if (depth <= limit) { |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
236 err = tb_displayline(f, |
| 5372 | 237 PyString_AsString( |
| 238 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
|
239 tb->tb_lineno, |
| 5372 | 240 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
|
241 } |
|
1425
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
242 depth--; |
| 86 | 243 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
|
244 if (err == 0) |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
245 err = PyErr_CheckSignals(); |
| 86 | 246 } |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
247 return err; |
| 86 | 248 } |
| 249 | |
| 250 int | |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
251 PyTraceBack_Print(PyObject *v, PyObject *f) |
| 86 | 252 { |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
253 int err; |
| 5372 | 254 PyObject *limitv; |
|
1425
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
255 int limit = 1000; |
| 86 | 256 if (v == NULL) |
| 257 return 0; | |
|
20618
57b51966a4f1
Removed two pointless and obfuscating macros.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
258 if (!PyTraceBack_Check(v)) { |
| 5372 | 259 PyErr_BadInternalCall(); |
| 86 | 260 return -1; |
| 261 } | |
| 5372 | 262 limitv = PySys_GetObject("tracebacklimit"); |
| 263 if (limitv && PyInt_Check(limitv)) { | |
| 264 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
|
265 if (limit <= 0) |
|
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
266 return 0; |
|
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
267 } |
|
12137
5b5581af951c
Change traceback error message to "most recent call last" from
Guido van Rossum <guido@python.org>
parents:
11999
diff
changeset
|
268 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
|
269 if (!err) |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
270 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
|
271 return err; |
| 86 | 272 } |
