Mercurial > cpython-lru_cache-weakref
annotate Python/traceback.c @ 56959:032d9749047d 2.6
Merged revisions 75931 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r75931 | benjamin.peterson | 2009-10-28 20:49:07 -0500 (Wed, 28 Oct 2009) | 5 lines
do a backport of r75928
The added test does not fail without the patch, but we still fix the issue of
surrogates being used in wide builds where they should not be.
........
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Thu, 29 Oct 2009 02:02:47 +0000 |
| parents | fb80b281e05a |
| children | e38335d4df91 9ef683d41c9b |
| 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_next", T_OBJECT, OFF(tb_next), READONLY}, |
|
fb80b281e05a
Merged revisions 70463 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47933
diff
changeset
|
16 {"tb_frame", T_OBJECT, OFF(tb_frame), READONLY}, |
|
fb80b281e05a
Merged revisions 70463 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47933
diff
changeset
|
17 {"tb_lasti", T_INT, OFF(tb_lasti), READONLY}, |
|
fb80b281e05a
Merged revisions 70463 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47933
diff
changeset
|
18 {"tb_lineno", T_INT, OFF(tb_lineno), READONLY}, |
| 86 | 19 {NULL} /* Sentinel */ |
| 20 }; | |
| 21 | |
| 22 static void | |
|
31739
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
23 tb_dealloc(PyTracebackObject *tb) |
| 86 | 24 { |
|
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
|
25 PyObject_GC_UnTrack(tb); |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11294
diff
changeset
|
26 Py_TRASHCAN_SAFE_BEGIN(tb) |
| 5372 | 27 Py_XDECREF(tb->tb_next); |
| 28 Py_XDECREF(tb->tb_frame); | |
|
20623
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
29 PyObject_GC_Del(tb); |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11294
diff
changeset
|
30 Py_TRASHCAN_SAFE_END(tb) |
| 86 | 31 } |
| 32 | |
|
20623
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
33 static int |
|
31739
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
34 tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg) |
|
20623
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
35 { |
|
37756
7b2e436a4a21
Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents:
37748
diff
changeset
|
36 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
|
37 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
|
38 return 0; |
|
20623
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
39 } |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
40 |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
41 static void |
|
31739
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
42 tb_clear(PyTracebackObject *tb) |
|
20623
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
43 { |
|
37748
d1547426ab61
Use Py_CLEAR instead of in-place DECREF/XDECREF or custom macros, for
Thomas Wouters <thomas@python.org>
parents:
37086
diff
changeset
|
44 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
|
45 Py_CLEAR(tb->tb_frame); |
|
20623
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
46 } |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
47 |
|
20618
57b51966a4f1
Removed two pointless and obfuscating macros.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
48 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
|
49 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 86 | 50 "traceback", |
|
31739
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
51 sizeof(PyTracebackObject), |
| 86 | 52 0, |
|
1831
216b755515e6
add function name to traceback info
Guido van Rossum <guido@python.org>
parents:
1425
diff
changeset
|
53 (destructor)tb_dealloc, /*tp_dealloc*/ |
| 86 | 54 0, /*tp_print*/ |
|
52221
fb80b281e05a
Merged revisions 70463 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47933
diff
changeset
|
55 0, /*tp_getattr*/ |
| 86 | 56 0, /*tp_setattr*/ |
| 57 0, /*tp_compare*/ | |
| 58 0, /*tp_repr*/ | |
| 59 0, /*tp_as_number*/ | |
| 60 0, /*tp_as_sequence*/ | |
| 61 0, /*tp_as_mapping*/ | |
|
20623
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
62 0, /* tp_hash */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
63 0, /* tp_call */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
64 0, /* tp_str */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
65 0, /* tp_getattro */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
66 0, /* tp_setattro */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
67 0, /* tp_as_buffer */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
68 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
69 0, /* tp_doc */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
70 (traverseproc)tb_traverse, /* tp_traverse */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
71 (inquiry)tb_clear, /* tp_clear */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
72 0, /* tp_richcompare */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
73 0, /* tp_weaklistoffset */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
74 0, /* tp_iter */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
75 0, /* tp_iternext */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
76 0, /* tp_methods */ |
|
52221
fb80b281e05a
Merged revisions 70463 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47933
diff
changeset
|
77 tb_memberlist, /* tp_members */ |
|
fb80b281e05a
Merged revisions 70463 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47933
diff
changeset
|
78 0, /* tp_getset */ |
|
20623
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
79 0, /* tp_base */ |
|
4fb7727e4f47
Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
20618
diff
changeset
|
80 0, /* tp_dict */ |
| 86 | 81 }; |
| 82 | |
|
31739
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
83 static PyTracebackObject * |
|
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
84 newtracebackobject(PyTracebackObject *next, PyFrameObject *frame) |
| 86 | 85 { |
|
31739
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
86 PyTracebackObject *tb; |
|
20618
57b51966a4f1
Removed two pointless and obfuscating macros.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
87 if ((next != NULL && !PyTraceBack_Check(next)) || |
| 5372 | 88 frame == NULL || !PyFrame_Check(frame)) { |
| 89 PyErr_BadInternalCall(); | |
| 86 | 90 return NULL; |
| 91 } | |
|
31739
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
92 tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type); |
| 86 | 93 if (tb != NULL) { |
| 5372 | 94 Py_XINCREF(next); |
| 86 | 95 tb->tb_next = next; |
| 5372 | 96 Py_XINCREF(frame); |
| 86 | 97 tb->tb_frame = frame; |
| 24844 | 98 tb->tb_lasti = frame->f_lasti; |
| 99 tb->tb_lineno = PyCode_Addr2Line(frame->f_code, | |
| 100 frame->f_lasti); | |
|
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
|
101 PyObject_GC_Track(tb); |
| 86 | 102 } |
| 103 return tb; | |
| 104 } | |
| 105 | |
| 106 int | |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
107 PyTraceBack_Here(PyFrameObject *frame) |
| 86 | 108 { |
|
40703
df294bc5ce6e
Make PyTraceBack_Here use the current thread, not the
Martin v. Löwis <martin@v.loewis.de>
parents:
37756
diff
changeset
|
109 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
|
110 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
|
111 PyTracebackObject *tb = newtracebackobject(oldtb, frame); |
| 86 | 112 if (tb == NULL) |
| 113 return -1; | |
|
5405
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5404
diff
changeset
|
114 tstate->curexc_traceback = (PyObject *)tb; |
|
b7871ca930ad
Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents:
5404
diff
changeset
|
115 Py_XDECREF(oldtb); |
| 86 | 116 return 0; |
| 117 } | |
| 118 | |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
119 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
|
120 _Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno, int indent) |
| 86 | 121 { |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
122 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
|
123 FILE *xfp = NULL; |
|
21057
2c0a9a5086e1
SF bug 485175: buffer overflow in traceback.c.
Tim Peters <tim.peters@gmail.com>
parents:
20623
diff
changeset
|
124 char linebuf[2000]; |
| 86 | 125 int i; |
|
44739
a3bf0e24b91e
Coverity issue CID #169
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
126 char namebuf[MAXPATHLEN+1]; |
|
a3bf0e24b91e
Coverity issue CID #169
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
127 |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
128 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
|
129 return -1; |
|
1831
216b755515e6
add function name to traceback info
Guido van Rossum <guido@python.org>
parents:
1425
diff
changeset
|
130 /* 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
|
131 #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
|
132 xfp = fopen(filename, "r" PY_STDIOTEXTMODE); |
| 86 | 133 if (xfp == NULL) { |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
134 /* Search tail of filename in sys.path before giving up */ |
| 5372 | 135 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
|
136 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
|
137 if (tail == NULL) |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
138 tail = filename; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
139 else |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
140 tail++; |
| 5372 | 141 path = PySys_GetObject("path"); |
| 142 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
|
143 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
|
144 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
|
145 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
|
146 for (i = 0; i < npath; i++) { |
| 5372 | 147 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
|
148 if (v == NULL) { |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
149 PyErr_Clear(); |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
150 break; |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
151 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
152 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
|
153 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
|
154 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
|
155 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
|
156 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
|
157 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
|
158 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
|
159 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
|
160 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
|
161 namebuf[len++] = SEP; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
162 strcpy(namebuf+len, tail); |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22650
diff
changeset
|
163 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
|
164 if (xfp != NULL) { |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
165 filename = namebuf; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
166 break; |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
167 } |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
168 } |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
169 } |
|
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
170 } |
| 86 | 171 } |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
172 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
173 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
|
174 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
|
175 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
|
176 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
|
177 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
|
178 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
179 |
| 86 | 180 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
|
181 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
|
182 do { |
|
d3696bf08eee
Tim Peters fixed PR#75: very long lines cause incorrect tracebacks.
Guido van Rossum <guido@python.org>
parents:
5608
diff
changeset
|
183 *pLastChar = '\0'; |
|
23029
8291473fde3e
Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents:
22650
diff
changeset
|
184 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
|
185 break; |
|
d3696bf08eee
Tim Peters fixed PR#75: very long lines cause incorrect tracebacks.
Guido van Rossum <guido@python.org>
parents:
5608
diff
changeset
|
186 /* 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
|
187 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
|
188 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
|
189 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
|
190 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
|
191 } while (*pLastChar != '\0' && *pLastChar != '\n'); |
| 86 | 192 } |
| 193 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
|
194 char buf[11]; |
|
674
4e2d239dc64c
Move printing of filename and lineno to tb_displayline.
Guido van Rossum <guido@python.org>
parents:
597
diff
changeset
|
195 char *p = linebuf; |
|
2811
07fc4fefb683
ignore control-l (parallelling change to tokenizer.c)
Guido van Rossum <guido@python.org>
parents:
2062
diff
changeset
|
196 while (*p == ' ' || *p == '\t' || *p == '\014') |
| 86 | 197 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
|
198 |
|
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
199 /* 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
|
200 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
|
201 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
|
202 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
|
203 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
|
204 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
|
205 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
|
206 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
|
207 break; |
|
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
208 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
|
209 } |
|
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
210 |
|
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47527
diff
changeset
|
211 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
|
212 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
|
213 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
|
214 err = PyFile_WriteString("\n", f); |
| 86 | 215 } |
| 216 fclose(xfp); | |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
217 return err; |
| 86 | 218 } |
| 219 | |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
220 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
|
221 tb_displayline(PyObject *f, const char *filename, int lineno, const char *name) |
| 86 | 222 { |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
223 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
|
224 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
|
225 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
226 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
|
227 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
|
228 /* 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
|
229 #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
|
230 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
|
231 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
|
232 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
|
233 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
|
234 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
|
235 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
236 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
237 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
|
238 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
|
239 { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
44739
diff
changeset
|
240 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
|
241 long depth = 0; |
|
31739
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
242 PyTracebackObject *tb1 = tb; |
|
1425
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
243 while (tb1 != NULL) { |
|
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
244 depth++; |
|
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
245 tb1 = tb1->tb_next; |
|
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
246 } |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
247 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
|
248 if (depth <= limit) { |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
249 err = tb_displayline(f, |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
250 PyString_AsString( |
| 5372 | 251 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
|
252 tb->tb_lineno, |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
253 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
|
254 } |
|
1425
72bddfad351a
* mpzmodule.c: cast some methods to the proper type.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
255 depth--; |
| 86 | 256 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
|
257 if (err == 0) |
|
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
258 err = PyErr_CheckSignals(); |
| 86 | 259 } |
|
5608
79e49cbce2ef
PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents:
5405
diff
changeset
|
260 return err; |
| 86 | 261 } |
| 262 | |
| 263 int | |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
264 PyTraceBack_Print(PyObject *v, PyObject *f) |
| 86 | 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 | 267 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
|
268 long limit = 1000; |
| 86 | 269 if (v == NULL) |
| 270 return 0; | |
|
20618
57b51966a4f1
Removed two pointless and obfuscating macros.
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
271 if (!PyTraceBack_Check(v)) { |
| 5372 | 272 PyErr_BadInternalCall(); |
| 86 | 273 return -1; |
| 274 } | |
| 5372 | 275 limitv = PySys_GetObject("tracebacklimit"); |
| 276 if (limitv && PyInt_Check(limitv)) { | |
| 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) |
|
31739
49ec0d8f08ec
Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents:
30968
diff
changeset
|
283 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
|
284 return err; |
| 86 | 285 } |
