Mercurial > cpython-lru_cache-weakref
annotate Python/traceback.c @ 55900:5dec8a12e455 legacy-trunk
tabbify
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Wed, 09 Sep 2009 11:40:54 +0000 |
| parents | 9ef683d41c9b |
| children | 8f6cf39c96dd |
| rev | line source |
|---|---|
| 145 | 1 |
| 86 | 2 /* Traceback implementation */ |
| 3 | |
| 5372 | 4 #include "Python.h" |
| 86 | 5 |
|
35742
1c4d192f1568
Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
31739
diff
changeset
|
6 #include "code.h" |
| 86 | 7 #include "frameobject.h" |
| 8 #include "structmember.h" | |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
9 #include "osdefs.h" |
|
31739
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
10 #include "traceback.h" |
| 86 | 11 |
|
31739
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
12 #define OFF(x) offsetof(PyTracebackObject, x) |
| 86 | 13 |
|
52221
fb80b281e05a
Merged revisions 70463 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47933
diff
changeset
|
14 static PyMemberDef tb_memberlist[] = { |
|
fb80b281e05a
Merged revisions 70463 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47933
diff
changeset
|
15 {"tb_lasti", T_INT, OFF(tb_lasti), READONLY}, |
|
fb80b281e05a
Merged revisions 70463 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47933
diff
changeset
|
16 {"tb_lineno", T_INT, OFF(tb_lineno), READONLY}, |
| 86 | 17 {NULL} /* Sentinel */ |
| 18 }; | |
| 19 | |
| 20 static void | |
|
31739
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
21 tb_dealloc(PyTracebackObject *tb) |
| 86 | 22 { |
|
22650
2288c724fe2b
Call PyObject_GC_UnTrack before putting an object on the list of trash.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
21077
diff
changeset
|
23 PyObject_GC_UnTrack(tb); |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11294
diff
changeset
|
24 Py_TRASHCAN_SAFE_BEGIN(tb) |
| 5372 | 25 Py_XDECREF(tb->tb_next); |
| 26 Py_XDECREF(tb->tb_frame); | |
|
20623
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
27 PyObject_GC_Del(tb); |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11294
diff
changeset
|
28 Py_TRASHCAN_SAFE_END(tb) |
| 86 | 29 } |
| 30 | |
|
20623
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
31 static int |
|
31739
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
32 tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg) |
|
20623
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
33 { |
|
37756
7b2e436a4a21
Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents:
37748
diff
changeset
|
34 Py_VISIT(tb->tb_next); |
|
7b2e436a4a21
Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents:
37748
diff
changeset
|
35 Py_VISIT(tb->tb_frame); |
|
7b2e436a4a21
Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents:
37748
diff
changeset
|
36 return 0; |
|
20623
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
37 } |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
38 |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
39 static void |
|
31739
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
40 tb_clear(PyTracebackObject *tb) |
|
20623
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
41 { |
|
37748
d1547426ab61
Use Py_CLEAR instead of in-place DECREF/XDECREF or custom macros, for
Thomas Wouters <thomas@python.org>
parents:
37086
diff
changeset
|
42 Py_CLEAR(tb->tb_next); |
|
d1547426ab61
Use Py_CLEAR instead of in-place DECREF/XDECREF or custom macros, for
Thomas Wouters <thomas@python.org>
parents:
37086
diff
changeset
|
43 Py_CLEAR(tb->tb_frame); |
|
20623
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
44 } |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
45 |
| 55900 | 46 static PyObject * |
| 47 tb_get_next(PyTracebackObject *tb) { | |
| 48 if (tb->tb_next) { | |
| 49 Py_INCREF(tb->tb_next); | |
| 50 return (PyObject *)tb->tb_next; | |
| 51 } | |
| 52 Py_RETURN_NONE; | |
| 53 } | |
| 54 | |
| 55 static int | |
| 56 tb_set_next(PyTracebackObject *tb, PyObject *new, void *context) { | |
| 57 if (!new) { | |
| 58 PyErr_SetString(PyExc_TypeError, "can't delete tb_next"); | |
| 59 return -1; | |
| 60 } | |
| 61 else if (new == Py_None) { | |
| 62 new = NULL; | |
| 63 } | |
| 64 else if (PyTraceBack_Check(new)) { | |
| 65 /* Check for cycles in the traceback. */ | |
| 66 PyTracebackObject *current = (PyTracebackObject *)new; | |
| 67 do { | |
| 68 if (tb == current) { | |
| 69 PyErr_SetString(PyExc_ValueError, "cycle in traceback"); | |
| 70 return -1; | |
| 71 } | |
| 72 } while ((current = current->tb_next)); | |
| 73 Py_INCREF(new); | |
| 74 } | |
| 75 else { | |
| 76 PyErr_SetString(PyExc_TypeError, | |
| 77 "tb_next must be an traceback object"); | |
| 78 return -1; | |
| 79 } | |
| 80 Py_XDECREF(tb->tb_next); | |
| 81 tb->tb_next = (PyTracebackObject *)new; | |
| 82 return 0; | |
| 83 } | |
| 84 | |
| 85 static PyObject * | |
| 86 tb_get_frame(PyTracebackObject *tb) { | |
| 87 if (tb->tb_frame) { | |
| 88 Py_INCREF(tb->tb_frame); | |
| 89 return (PyObject *)tb->tb_frame; | |
| 90 } | |
| 91 Py_RETURN_NONE; | |
| 92 } | |
| 93 | |
| 94 static int | |
| 95 tb_set_frame(PyTracebackObject *tb, PyObject *new, void *context) { | |
| 96 if (!new) { | |
| 97 PyErr_SetString(PyExc_TypeError, | |
| 98 "can't delete tb_frame attribute"); | |
| 99 return -1; | |
| 100 } | |
| 101 else if (new == Py_None) { | |
| 102 new = NULL; | |
| 103 } | |
| 104 else if (PyFrame_Check(new)) { | |
| 105 Py_INCREF(new); | |
| 106 } | |
| 107 else { | |
| 108 PyErr_SetString(PyExc_TypeError, | |
| 109 "tb_frame must be a frame object"); | |
| 110 return -1; | |
| 111 } | |
| 112 tb->tb_frame = (PyFrameObject *)new; | |
| 113 return 0; | |
| 114 } | |
| 115 | |
| 116 static PyGetSetDef tb_getset[] = { | |
| 117 {"tb_next", (getter)tb_get_next, (setter)tb_set_next, NULL}, | |
| 118 {"tb_frame", (getter)tb_get_frame, (setter)tb_set_frame, NULL}, | |
| 119 }; | |
| 120 | |
|
20618
57b51966a4f1
Removed two pointless and obfuscating macros.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
121 PyTypeObject PyTraceBack_Type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
40703
diff
changeset
|
122 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 86 | 123 "traceback", |
|
31739
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
124 sizeof(PyTracebackObject), |
| 86 | 125 0, |
|
1831
216b755515e6
add function name to traceback info
Guido van Rossum <guido@python.org>
parents:
1425
diff
changeset
|
126 (destructor)tb_dealloc, /*tp_dealloc*/ |
| 86 | 127 0, /*tp_print*/ |
|
52221
fb80b281e05a
Merged revisions 70463 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47933
diff
changeset
|
128 0, /*tp_getattr*/ |
| 86 | 129 0, /*tp_setattr*/ |
| 130 0, /*tp_compare*/ | |
| 131 0, /*tp_repr*/ | |
| 132 0, /*tp_as_number*/ | |
| 133 0, /*tp_as_sequence*/ | |
| 134 0, /*tp_as_mapping*/ | |
|
20623
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
135 0, /* tp_hash */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
136 0, /* tp_call */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
137 0, /* tp_str */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
138 0, /* tp_getattro */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
139 0, /* tp_setattro */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
140 0, /* tp_as_buffer */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
141 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
142 0, /* tp_doc */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
143 (traverseproc)tb_traverse, /* tp_traverse */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
144 (inquiry)tb_clear, /* tp_clear */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
145 0, /* tp_richcompare */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
146 0, /* tp_weaklistoffset */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
147 0, /* tp_iter */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
148 0, /* tp_iternext */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
149 0, /* tp_methods */ |
|
52221
fb80b281e05a
Merged revisions 70463 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47933
diff
changeset
|
150 tb_memberlist, /* tp_members */ |
| 55900 | 151 tb_getset, /* tp_getset */ |
|
20623
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
152 0, /* tp_base */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
153 0, /* tp_dict */ |
| 86 | 154 }; |
| 155 | |
|
31739
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
156 static PyTracebackObject * |
|
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
157 newtracebackobject(PyTracebackObject *next, PyFrameObject *frame) |
| 86 | 158 { |
|
31739
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
159 PyTracebackObject *tb; |
|
20618
57b51966a4f1
Removed two pointless and obfuscating macros.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
160 if ((next != NULL && !PyTraceBack_Check(next)) || |
| 5372 | 161 frame == NULL || !PyFrame_Check(frame)) { |
| 162 PyErr_BadInternalCall(); | |
| 86 | 163 return NULL; |
| 164 } | |
|
31739
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
165 tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type); |
| 86 | 166 if (tb != NULL) { |
| 5372 | 167 Py_XINCREF(next); |
| 86 | 168 tb->tb_next = next; |
| 5372 | 169 Py_XINCREF(frame); |
| 86 | 170 tb->tb_frame = frame; |
| 24844 | 171 tb->tb_lasti = frame->f_lasti; |
|
53931
9ef683d41c9b
Issue 5954, PyFrame_GetLineNumber:
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
52221
diff
changeset
|
172 tb->tb_lineno = PyFrame_GetLineNumber(frame); |
|
22650
2288c724fe2b
Call PyObject_GC_UnTrack before putting an object on the list of trash.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
21077
diff
changeset
|
173 PyObject_GC_Track(tb); |
| 86 | 174 } |
| 175 return tb; | |
| 176 } | |
| 177 | |
| 178 int | |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
179 PyTraceBack_Here(PyFrameObject *frame) |
| 86 | 180 { |
|
40703
df294bc5ce6e
Make PyTraceBack_Here use the current thread, not the
Martin v. Löwis <martin@v.loewis.de>
parents:
37756
diff
changeset
|
181 PyThreadState *tstate = PyThreadState_GET(); |
|
31739
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
182 PyTracebackObject *oldtb = (PyTracebackObject *) tstate->curexc_traceback; |
|
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
183 PyTracebackObject *tb = newtracebackobject(oldtb, frame); |
| 86 | 184 if (tb == NULL) |
| 185 return -1; | |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5404
diff
changeset
|
186 tstate->curexc_traceback = (PyObject *)tb; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5404
diff
changeset
|
187 Py_XDECREF(oldtb); |
| 86 | 188 return 0; |
| 189 } | |
| 190 | |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
191 int |
|
47933
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
192 _Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno, int indent) |
| 86 | 193 { |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
194 int err = 0; |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
195 FILE *xfp = NULL; |
|
21057
2c0a9a5086e1
SF bug 485175: buffer overflow in traceback.c.
Tim Peters <tim.peters@gmail.com>
parents:
20623
diff
changeset
|
196 char linebuf[2000]; |
| 86 | 197 int i; |
|
44739
a3bf0e24b91e
Coverity issue CID #169
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
198 char namebuf[MAXPATHLEN+1]; |
|
a3bf0e24b91e
Coverity issue CID #169
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
199 |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
200 if (filename == NULL) |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
201 return -1; |
|
1831
216b755515e6
add function name to traceback info
Guido van Rossum <guido@python.org>
parents:
1425
diff
changeset
|
202 /* This is needed by Emacs' compile command */ |
|
21057
2c0a9a5086e1
SF bug 485175: buffer overflow in traceback.c.
Tim Peters <tim.peters@gmail.com>
parents:
20623
diff
changeset
|
203 #define FMT " File \"%.500s\", line %d, in %.500s\n" |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22650
diff
changeset
|
204 xfp = fopen(filename, "r" PY_STDIOTEXTMODE); |
| 86 | 205 if (xfp == NULL) { |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
206 /* Search tail of filename in sys.path before giving up */ |
| 5372 | 207 PyObject *path; |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
208 const char *tail = strrchr(filename, SEP); |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
209 if (tail == NULL) |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
210 tail = filename; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
211 else |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
212 tail++; |
| 5372 | 213 path = PySys_GetObject("path"); |
| 214 if (path != NULL && PyList_Check(path)) { | |
|
36521
30aef3ce2b26
Use Py_ssize_t to count the
Martin v. Löwis <martin@v.loewis.de>
parents:
35742
diff
changeset
|
215 Py_ssize_t _npath = PyList_Size(path); |
|
30aef3ce2b26
Use Py_ssize_t to count the
Martin v. Löwis <martin@v.loewis.de>
parents:
35742
diff
changeset
|
216 int npath = Py_SAFE_DOWNCAST(_npath, Py_ssize_t, int); |
|
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
|
217 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
|
218 for (i = 0; i < npath; i++) { |
| 5372 | 219 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
|
220 if (v == NULL) { |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
221 PyErr_Clear(); |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
222 break; |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
223 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
224 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
|
225 size_t len; |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
226 len = PyString_GET_SIZE(v); |
|
1909
0c232194a64f
* Python/traceback.c: security fix -- check for buffer oveflow
Guido van Rossum <guido@python.org>
parents:
1831
diff
changeset
|
227 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
|
228 continue; /* Too long */ |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
229 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
|
230 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
|
231 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
|
232 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
|
233 namebuf[len++] = SEP; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
234 strcpy(namebuf+len, tail); |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22650
diff
changeset
|
235 xfp = fopen(namebuf, "r" PY_STDIOTEXTMODE); |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
236 if (xfp != NULL) { |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
237 filename = namebuf; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
238 break; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
239 } |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
240 } |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
241 } |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
242 } |
| 86 | 243 } |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
244 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
245 if (xfp == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
246 return err; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
247 if (err != 0) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
248 fclose(xfp); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
249 return err; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
250 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
251 |
| 86 | 252 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
|
253 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
|
254 do { |
|
d3696bf08eee
Tim Peters fixed PR#75: very long lines cause incorrect tracebacks.
Guido van Rossum <guido@python.org>
parents:
5608
diff
changeset
|
255 *pLastChar = '\0'; |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22650
diff
changeset
|
256 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, xfp, NULL) == NULL) |
|
11294
d3696bf08eee
Tim Peters fixed PR#75: very long lines cause incorrect tracebacks.
Guido van Rossum <guido@python.org>
parents:
5608
diff
changeset
|
257 break; |
|
d3696bf08eee
Tim Peters fixed PR#75: very long lines cause incorrect tracebacks.
Guido van Rossum <guido@python.org>
parents:
5608
diff
changeset
|
258 /* 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
|
259 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
|
260 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
|
261 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
|
262 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
|
263 } while (*pLastChar != '\0' && *pLastChar != '\n'); |
| 86 | 264 } |
| 265 if (i == lineno) { | |
|
47933
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
266 char buf[11]; |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
267 char *p = linebuf; |
|
2811
07fc4fefb683
ignore control-l (parallelling change to tokenizer.c)
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
268 while (*p == ' ' || *p == '\t' || *p == '\014') |
| 86 | 269 p++; |
|
47933
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
270 |
|
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
271 /* Write some spaces before the line */ |
|
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
272 strcpy(buf, " "); |
|
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
273 assert (strlen(buf) == 10); |
|
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
274 while (indent > 0) { |
|
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
275 if(indent < 10) |
|
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
276 buf[indent] = '\0'; |
|
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
277 err = PyFile_WriteString(buf, f); |
|
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
278 if (err != 0) |
|
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
279 break; |
|
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
280 indent -= 10; |
|
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
281 } |
|
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
282 |
|
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
283 if (err == 0) |
|
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
284 err = PyFile_WriteString(p, f); |
|
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
285 if (err == 0 && strchr(p, '\n') == NULL) |
|
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
286 err = PyFile_WriteString("\n", f); |
| 86 | 287 } |
| 288 fclose(xfp); | |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
289 return err; |
| 86 | 290 } |
| 291 | |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
292 static int |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
293 tb_displayline(PyObject *f, const char *filename, int lineno, const char *name) |
| 86 | 294 { |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
295 int err = 0; |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
296 char linebuf[2000]; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
297 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
298 if (filename == NULL || name == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
299 return -1; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
300 /* This is needed by Emacs' compile command */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
301 #define FMT " File \"%.500s\", line %d, in %.500s\n" |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
302 PyOS_snprintf(linebuf, sizeof(linebuf), FMT, filename, lineno, name); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
303 err = PyFile_WriteString(linebuf, f); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
304 if (err != 0) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
305 return err; |
|
47933
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
306 return _Py_DisplaySourceLine(f, filename, lineno, 4); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
307 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
308 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
309 static int |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
310 tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
311 { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
312 int err = 0; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
313 long depth = 0; |
|
31739
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
314 PyTracebackObject *tb1 = tb; |
|
1425
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
315 while (tb1 != NULL) { |
|
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
316 depth++; |
|
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
317 tb1 = tb1->tb_next; |
|
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
318 } |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
319 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
|
320 if (depth <= limit) { |
| 55900 | 321 PyFrameObject *frame = tb->tb_frame; |
| 322 char *filename, *name; | |
| 323 if (frame) { | |
| 324 filename = PyString_AS_STRING(frame->f_code->co_filename); | |
| 325 name = PyString_AS_STRING(frame->f_code->co_name); | |
| 326 } | |
| 327 else { | |
| 328 filename = name = "unkown (no frame on traceback)"; | |
| 329 } | |
| 330 err = tb_displayline(f, filename, tb->tb_lineno, name); | |
|
5046
658815230546
Get the line number from PyCode_Addr2Line instead of believing
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
331 } |
|
1425
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
332 depth--; |
| 86 | 333 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
|
334 if (err == 0) |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
335 err = PyErr_CheckSignals(); |
| 86 | 336 } |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
337 return err; |
| 86 | 338 } |
| 339 | |
| 340 int | |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
341 PyTraceBack_Print(PyObject *v, PyObject *f) |
| 86 | 342 { |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
343 int err; |
| 5372 | 344 PyObject *limitv; |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
345 long limit = 1000; |
| 86 | 346 if (v == NULL) |
| 347 return 0; | |
|
20618
57b51966a4f1
Removed two pointless and obfuscating macros.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
348 if (!PyTraceBack_Check(v)) { |
| 5372 | 349 PyErr_BadInternalCall(); |
| 86 | 350 return -1; |
| 351 } | |
| 5372 | 352 limitv = PySys_GetObject("tracebacklimit"); |
| 353 if (limitv && PyInt_Check(limitv)) { | |
| 354 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
|
355 if (limit <= 0) |
|
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
356 return 0; |
|
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
357 } |
|
12137
5b5581af951c
Change traceback error message to "most recent call last" from
Guido van Rossum <guido@python.org>
parents:
11999
diff
changeset
|
358 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
|
359 if (!err) |
|
31739
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
360 err = tb_printinternal((PyTracebackObject *)v, f, limit); |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
361 return err; |
| 86 | 362 } |
