annotate Python/traceback.c @ 13956:8e8a8df0fe73 legacy-trunk

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