annotate Python/traceback.c @ 12536:cc05edcccf52 legacy-trunk

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