annotate Python/traceback.c @ 17337:96ea0eccdb31 legacy-trunk

SF patch #413552 - Premature decref on object Jeffery Collins pointed out that filterstring decrefs a character object before it's done using it. This works by accident today because another module always happens to have an active reference too at the time. The accident doesn't work after his Pippy modifications, and since it *is* an accident even in the mainline Python, it should work by design there too. The patch accomplishes that.
author Tim Peters <tim.peters@gmail.com>
date Sat, 07 Apr 2001 20:34:48 +0000
parents 6f6f965c5e2d
children 82ac83474462 57b51966a4f1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Traceback implementation */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
4 #include "Python.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 #include "compile.h"
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 #include "frameobject.h"
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 typedef struct _tracebackobject {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
12 PyObject_HEAD
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 struct _tracebackobject *tb_next;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
14 PyFrameObject *tb_frame;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 int tb_lasti;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 int tb_lineno;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 } tracebackobject;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 #define OFF(x) offsetof(tracebackobject, x)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 static struct memberlist tb_memberlist[] = {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 {"tb_next", T_OBJECT, OFF(tb_next)},
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 {"tb_frame", T_OBJECT, OFF(tb_frame)},
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 {"tb_lasti", T_INT, OFF(tb_lasti)},
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 {"tb_lineno", T_INT, OFF(tb_lineno)},
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 {NULL} /* Sentinel */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 };
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
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
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
32 return PyMember_Get((char *)tb, tb_memberlist, name);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 {
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11294
diff changeset
38 Py_TRASHCAN_SAFE_BEGIN(tb)
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
39 Py_XDECREF(tb->tb_next);
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
40 Py_XDECREF(tb->tb_frame);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12137
diff changeset
41 PyObject_DEL(tb);
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11294
diff changeset
42 Py_TRASHCAN_SAFE_END(tb)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
3099
d249111d4ccc spell TraceBack with capital B
Guido van Rossum <guido@python.org>
parents: 2844
diff changeset
45 #define Tracebacktype PyTraceBack_Type
d249111d4ccc spell TraceBack with capital B
Guido van Rossum <guido@python.org>
parents: 2844
diff changeset
46 #define is_tracebackobject PyTraceBack_Check
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2811
diff changeset
47
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
48 PyTypeObject Tracebacktype = {
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
49 PyObject_HEAD_INIT(&PyType_Type)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 0,
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 "traceback",
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 sizeof(tracebackobject),
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 0,
1831
216b755515e6 add function name to traceback info
Guido van Rossum <guido@python.org>
parents: 1425
diff changeset
54 (destructor)tb_dealloc, /*tp_dealloc*/
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 0, /*tp_print*/
1831
216b755515e6 add function name to traceback info
Guido van Rossum <guido@python.org>
parents: 1425
diff changeset
56 (getattrfunc)tb_getattr, /*tp_getattr*/
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 0, /*tp_setattr*/
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 0, /*tp_compare*/
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 0, /*tp_repr*/
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 0, /*tp_as_number*/
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 0, /*tp_as_sequence*/
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 0, /*tp_as_mapping*/
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 };
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 static tracebackobject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
66 newtracebackobject(tracebackobject *next, PyFrameObject *frame, int lasti,
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
67 int lineno)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 tracebackobject *tb;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 if ((next != NULL && !is_tracebackobject(next)) ||
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
71 frame == NULL || !PyFrame_Check(frame)) {
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
72 PyErr_BadInternalCall();
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 }
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
75 tb = PyObject_NEW(tracebackobject, &Tracebacktype);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 if (tb != NULL) {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
77 Py_XINCREF(next);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 tb->tb_next = next;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
79 Py_XINCREF(frame);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 tb->tb_frame = frame;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 tb->tb_lasti = lasti;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 tb->tb_lineno = lineno;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 return tb;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
88 PyTraceBack_Here(PyFrameObject *frame)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 {
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5404
diff changeset
90 PyThreadState *tstate = frame->f_tstate;
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5404
diff changeset
91 tracebackobject *oldtb = (tracebackobject *) tstate->curexc_traceback;
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5404
diff changeset
92 tracebackobject *tb = newtracebackobject(oldtb,
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5404
diff changeset
93 frame, frame->f_lasti, frame->f_lineno);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 if (tb == NULL)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 return -1;
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5404
diff changeset
96 tstate->curexc_traceback = (PyObject *)tb;
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5404
diff changeset
97 Py_XDECREF(oldtb);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 return 0;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
5608
79e49cbce2ef PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5405
diff changeset
101 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
102 tb_displayline(PyObject *f, char *filename, int lineno, char *name)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 {
5608
79e49cbce2ef PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5405
diff changeset
104 int err = 0;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 FILE *xfp;
674
4e2d239dc64c Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents: 597
diff changeset
106 char linebuf[1000];
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 int i;
5608
79e49cbce2ef PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5405
diff changeset
108 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
109 return -1;
1831
216b755515e6 add function name to traceback info
Guido van Rossum <guido@python.org>
parents: 1425
diff changeset
110 #ifdef MPW
216b755515e6 add function name to traceback info
Guido van Rossum <guido@python.org>
parents: 1425
diff changeset
111 /* 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
112 #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
113 #else
216b755515e6 add function name to traceback info
Guido van Rossum <guido@python.org>
parents: 1425
diff changeset
114 /* This is needed by Emacs' compile command */
216b755515e6 add function name to traceback info
Guido van Rossum <guido@python.org>
parents: 1425
diff changeset
115 #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
116 #endif
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 xfp = fopen(filename, "r");
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 if (xfp == NULL) {
674
4e2d239dc64c Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents: 597
diff changeset
119 /* Search tail of filename in sys.path before giving up */
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
120 PyObject *path;
674
4e2d239dc64c Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents: 597
diff changeset
121 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
122 if (tail == NULL)
4e2d239dc64c Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents: 597
diff changeset
123 tail = filename;
4e2d239dc64c Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents: 597
diff changeset
124 else
4e2d239dc64c Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents: 597
diff changeset
125 tail++;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
126 path = PySys_GetObject("path");
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
127 if (path != NULL && PyList_Check(path)) {
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
128 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
129 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
130 char namebuf[MAXPATHLEN+1];
4e2d239dc64c Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents: 597
diff changeset
131 for (i = 0; i < npath; i++) {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
132 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
133 if (v == NULL) {
79e49cbce2ef PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5405
diff changeset
134 PyErr_Clear();
79e49cbce2ef PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5405
diff changeset
135 break;
79e49cbce2ef PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5405
diff changeset
136 }
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
137 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
138 size_t len;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
139 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
140 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
141 continue; /* Too long */
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
142 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
143 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
144 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
145 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
146 namebuf[len++] = SEP;
4e2d239dc64c Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents: 597
diff changeset
147 strcpy(namebuf+len, tail);
4e2d239dc64c Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents: 597
diff changeset
148 xfp = fopen(namebuf, "r");
4e2d239dc64c Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents: 597
diff changeset
149 if (xfp != NULL) {
4e2d239dc64c Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents: 597
diff changeset
150 filename = namebuf;
4e2d239dc64c Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents: 597
diff changeset
151 break;
4e2d239dc64c Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents: 597
diff changeset
152 }
4e2d239dc64c Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents: 597
diff changeset
153 }
4e2d239dc64c Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents: 597
diff changeset
154 }
4e2d239dc64c Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents: 597
diff changeset
155 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 }
1831
216b755515e6 add function name to traceback info
Guido van Rossum <guido@python.org>
parents: 1425
diff changeset
157 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
158 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
159 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
160 return err;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 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
162 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
163 do {
d3696bf08eee Tim Peters fixed PR#75: very long lines cause incorrect tracebacks.
Guido van Rossum <guido@python.org>
parents: 5608
diff changeset
164 *pLastChar = '\0';
d3696bf08eee Tim Peters fixed PR#75: very long lines cause incorrect tracebacks.
Guido van Rossum <guido@python.org>
parents: 5608
diff changeset
165 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
166 break;
d3696bf08eee Tim Peters fixed PR#75: very long lines cause incorrect tracebacks.
Guido van Rossum <guido@python.org>
parents: 5608
diff changeset
167 /* 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
168 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
169 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
170 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
171 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
172 } while (*pLastChar != '\0' && *pLastChar != '\n');
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 if (i == lineno) {
674
4e2d239dc64c Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents: 597
diff changeset
175 char *p = linebuf;
2811
07fc4fefb683 ignore control-l (parallelling change to tokenizer.c)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
176 while (*p == ' ' || *p == '\t' || *p == '\014')
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 p++;
5608
79e49cbce2ef PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5405
diff changeset
178 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
179 if (err == 0) {
79e49cbce2ef PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5405
diff changeset
180 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
181 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
182 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
183 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 fclose(xfp);
5608
79e49cbce2ef PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5405
diff changeset
186 return err;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
188
5608
79e49cbce2ef PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5405
diff changeset
189 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
190 tb_printinternal(tracebackobject *tb, PyObject *f, int limit)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 {
5608
79e49cbce2ef PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5405
diff changeset
192 int err = 0;
1425
72bddfad351a * mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
193 int depth = 0;
72bddfad351a * mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
194 tracebackobject *tb1 = tb;
72bddfad351a * mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
195 while (tb1 != NULL) {
72bddfad351a * mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
196 depth++;
72bddfad351a * mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
197 tb1 = tb1->tb_next;
72bddfad351a * mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
198 }
5608
79e49cbce2ef PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5405
diff changeset
199 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
200 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
201 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
202 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
203 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
204 err = tb_displayline(f,
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
205 PyString_AsString(
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
206 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
207 tb->tb_lineno,
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
208 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
209 }
1425
72bddfad351a * mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
210 depth--;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 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
212 if (err == 0)
79e49cbce2ef PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5405
diff changeset
213 err = PyErr_CheckSignals();
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 }
5608
79e49cbce2ef PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5405
diff changeset
215 return err;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
217
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
219 PyTraceBack_Print(PyObject *v, PyObject *f)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 {
5608
79e49cbce2ef PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5405
diff changeset
221 int err;
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
222 PyObject *limitv;
1425
72bddfad351a * mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
223 int limit = 1000;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 if (v == NULL)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 return 0;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 if (!is_tracebackobject(v)) {
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
227 PyErr_BadInternalCall();
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 return -1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 }
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
230 limitv = PySys_GetObject("tracebacklimit");
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
231 if (limitv && PyInt_Check(limitv)) {
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
232 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
233 if (limit <= 0)
72bddfad351a * mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
234 return 0;
72bddfad351a * mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
235 }
12137
5b5581af951c Change traceback error message to "most recent call last" from
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
236 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
237 if (!err)
79e49cbce2ef PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5405
diff changeset
238 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
239 return err;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 }