1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Traceback implementation */
37 #include "frameobject.h"
38 #include "structmember.h"
41 typedef struct _tracebackobject
{
43 struct _tracebackobject
*tb_next
;
44 PyFrameObject
*tb_frame
;
49 #define OFF(x) offsetof(tracebackobject, x)
51 static struct memberlist tb_memberlist
[] = {
52 {"tb_next", T_OBJECT
, OFF(tb_next
)},
53 {"tb_frame", T_OBJECT
, OFF(tb_frame
)},
54 {"tb_lasti", T_INT
, OFF(tb_lasti
)},
55 {"tb_lineno", T_INT
, OFF(tb_lineno
)},
64 return PyMember_Get((char *)tb
, tb_memberlist
, name
);
71 Py_TRASHCAN_SAFE_BEGIN(tb
)
72 Py_XDECREF(tb
->tb_next
);
73 Py_XDECREF(tb
->tb_frame
);
75 Py_TRASHCAN_SAFE_END(tb
)
78 #define Tracebacktype PyTraceBack_Type
79 #define is_tracebackobject PyTraceBack_Check
81 PyTypeObject Tracebacktype
= {
82 PyObject_HEAD_INIT(&PyType_Type
)
85 sizeof(tracebackobject
),
87 (destructor
)tb_dealloc
, /*tp_dealloc*/
89 (getattrfunc
)tb_getattr
, /*tp_getattr*/
98 static tracebackobject
*
99 newtracebackobject(next
, frame
, lasti
, lineno
)
100 tracebackobject
*next
;
101 PyFrameObject
*frame
;
105 if ((next
!= NULL
&& !is_tracebackobject(next
)) ||
106 frame
== NULL
|| !PyFrame_Check(frame
)) {
107 PyErr_BadInternalCall();
110 tb
= PyObject_NEW(tracebackobject
, &Tracebacktype
);
115 tb
->tb_frame
= frame
;
116 tb
->tb_lasti
= lasti
;
117 tb
->tb_lineno
= lineno
;
123 PyTraceBack_Here(frame
)
124 PyFrameObject
*frame
;
126 PyThreadState
*tstate
= frame
->f_tstate
;
127 tracebackobject
*oldtb
= (tracebackobject
*) tstate
->curexc_traceback
;
128 tracebackobject
*tb
= newtracebackobject(oldtb
,
129 frame
, frame
->f_lasti
, frame
->f_lineno
);
132 tstate
->curexc_traceback
= (PyObject
*)tb
;
138 tb_displayline(f
, filename
, lineno
, name
)
148 if (filename
== NULL
|| name
== NULL
)
151 /* This is needed by MPW's File and Line commands */
152 #define FMT " File \"%.900s\"; line %d # in %s\n"
154 /* This is needed by Emacs' compile command */
155 #define FMT " File \"%.900s\", line %d, in %s\n"
157 xfp
= fopen(filename
, "r");
159 /* Search tail of filename in sys.path before giving up */
161 char *tail
= strrchr(filename
, SEP
);
166 path
= PySys_GetObject("path");
167 if (path
!= NULL
&& PyList_Check(path
)) {
168 int npath
= PyList_Size(path
);
169 int taillen
= strlen(tail
);
170 char namebuf
[MAXPATHLEN
+1];
171 for (i
= 0; i
< npath
; i
++) {
172 PyObject
*v
= PyList_GetItem(path
, i
);
177 if (PyString_Check(v
)) {
179 len
= PyString_Size(v
);
180 if (len
+ 1 + taillen
>= MAXPATHLEN
)
181 continue; /* Too long */
182 strcpy(namebuf
, PyString_AsString(v
));
183 if ((int)strlen(namebuf
) != len
)
184 continue; /* v contains '\0' */
185 if (len
> 0 && namebuf
[len
-1] != SEP
)
186 namebuf
[len
++] = SEP
;
187 strcpy(namebuf
+len
, tail
);
188 xfp
= fopen(namebuf
, "r");
197 sprintf(linebuf
, FMT
, filename
, lineno
, name
);
198 err
= PyFile_WriteString(linebuf
, f
);
199 if (xfp
== NULL
|| err
!= 0)
201 for (i
= 0; i
< lineno
; i
++) {
202 char* pLastChar
= &linebuf
[sizeof(linebuf
)-2];
205 if (fgets(linebuf
, sizeof linebuf
, xfp
) == NULL
)
207 /* fgets read *something*; if it didn't get as
208 far as pLastChar, it must have found a newline
209 or hit the end of the file; if pLastChar is \n,
210 it obviously found a newline; else we haven't
211 yet seen a newline, so must continue */
212 } while (*pLastChar
!= '\0' && *pLastChar
!= '\n');
216 while (*p
== ' ' || *p
== '\t' || *p
== '\014')
218 err
= PyFile_WriteString(" ", f
);
220 err
= PyFile_WriteString(p
, f
);
221 if (err
== 0 && strchr(p
, '\n') == NULL
)
222 err
= PyFile_WriteString("\n", f
);
230 tb_printinternal(tb
, f
, limit
)
237 tracebackobject
*tb1
= tb
;
238 while (tb1
!= NULL
) {
242 while (tb
!= NULL
&& err
== 0) {
243 if (depth
<= limit
) {
245 tb
->tb_lineno
= PyCode_Addr2Line(
246 tb
->tb_frame
->f_code
, tb
->tb_lasti
);
247 err
= tb_displayline(f
,
249 tb
->tb_frame
->f_code
->co_filename
),
251 PyString_AsString(tb
->tb_frame
->f_code
->co_name
));
256 err
= PyErr_CheckSignals();
262 PyTraceBack_Print(v
, f
)
271 if (!is_tracebackobject(v
)) {
272 PyErr_BadInternalCall();
275 limitv
= PySys_GetObject("tracebacklimit");
276 if (limitv
&& PyInt_Check(limitv
)) {
277 limit
= PyInt_AsLong(limitv
);
281 err
= PyFile_WriteString("Traceback (most recent call last):\n", f
);
283 err
= tb_printinternal((tracebackobject
*)v
, f
, limit
);