annotate Python/traceback.c @ 69888:8752a81ada2d

faulthandler: dump all threads by default * Set the default value of all_threads arguments to True * Py_FatalError() dumps all threads, instead of only the current thread Dump only the current thread is not reliable. In some cases, Python is unable to retrieve the state of the current thread and so is unable to dump the traceback. faulthandler keeps a reference to the interpreter and so is always able to dump the traceback of all threads.
author Victor Stinner <victor.stinner@haypocalc.com>
date Sat, 07 May 2011 12:43:00 +0200
parents 7b8d625eb6e4
children 8beaa9a37387
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
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Traceback implementation */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5372
424922afc1bb Quickly renamed.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
4 #include "Python.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 31739
diff changeset
6 #include "code.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 #include "frameobject.h"
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
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"
49484
0b7e023803fb Issues #2384 and #3975: Tracebacks were not correctly printed when the source file
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48660
diff changeset
10 #ifdef HAVE_FCNTL_H
0b7e023803fb Issues #2384 and #3975: Tracebacks were not correctly printed when the source file
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48660
diff changeset
11 #include <fcntl.h>
0b7e023803fb Issues #2384 and #3975: Tracebacks were not correctly printed when the source file
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48660
diff changeset
12 #endif
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
31739
49ec0d8f08ec Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents: 30968
diff changeset
14 #define OFF(x) offsetof(PyTracebackObject, x)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
69070
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
16 #define PUTS(fd, str) write(fd, str, strlen(str))
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
17 #define MAX_STRING_LENGTH 100
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
18 #define MAX_FRAME_DEPTH 100
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
19 #define MAX_NTHREADS 100
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
20
69148
7b8d625eb6e4 Issue #9319: Include the filename in "Non-UTF8 code ..." syntax error.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69102
diff changeset
21 /* Function from Parser/tokenizer.c */
7b8d625eb6e4 Issue #9319: Include the filename in "Non-UTF8 code ..." syntax error.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69102
diff changeset
22 extern char * PyTokenizer_FindEncodingFilename(int, PyObject *);
49484
0b7e023803fb Issues #2384 and #3975: Tracebacks were not correctly printed when the source file
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48660
diff changeset
23
42593
5391b5f8cd7a Bug #1772489: make dir() work on traceback objects again.
Collin Winter <collinw@gmail.com>
parents: 42591
diff changeset
24 static PyObject *
5391b5f8cd7a Bug #1772489: make dir() work on traceback objects again.
Collin Winter <collinw@gmail.com>
parents: 42591
diff changeset
25 tb_dir(PyTracebackObject *self)
5391b5f8cd7a Bug #1772489: make dir() work on traceback objects again.
Collin Winter <collinw@gmail.com>
parents: 42591
diff changeset
26 {
5391b5f8cd7a Bug #1772489: make dir() work on traceback objects again.
Collin Winter <collinw@gmail.com>
parents: 42591
diff changeset
27 return Py_BuildValue("[ssss]", "tb_frame", "tb_next",
5391b5f8cd7a Bug #1772489: make dir() work on traceback objects again.
Collin Winter <collinw@gmail.com>
parents: 42591
diff changeset
28 "tb_lasti", "tb_lineno");
5391b5f8cd7a Bug #1772489: make dir() work on traceback objects again.
Collin Winter <collinw@gmail.com>
parents: 42591
diff changeset
29 }
5391b5f8cd7a Bug #1772489: make dir() work on traceback objects again.
Collin Winter <collinw@gmail.com>
parents: 42591
diff changeset
30
5391b5f8cd7a Bug #1772489: make dir() work on traceback objects again.
Collin Winter <collinw@gmail.com>
parents: 42591
diff changeset
31 static PyMethodDef tb_methods[] = {
5391b5f8cd7a Bug #1772489: make dir() work on traceback objects again.
Collin Winter <collinw@gmail.com>
parents: 42591
diff changeset
32 {"__dir__", (PyCFunction)tb_dir, METH_NOARGS},
5391b5f8cd7a Bug #1772489: make dir() work on traceback objects again.
Collin Winter <collinw@gmail.com>
parents: 42591
diff changeset
33 {NULL, NULL, 0, NULL},
5391b5f8cd7a Bug #1772489: make dir() work on traceback objects again.
Collin Winter <collinw@gmail.com>
parents: 42591
diff changeset
34 };
5391b5f8cd7a Bug #1772489: make dir() work on traceback objects again.
Collin Winter <collinw@gmail.com>
parents: 42591
diff changeset
35
42537
74a3c6826683 Remove support for __members__ and __methods__. There still might be
Neal Norwitz <nnorwitz@gmail.com>
parents: 42334
diff changeset
36 static PyMemberDef tb_memberlist[] = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
37 {"tb_next", T_OBJECT, OFF(tb_next), READONLY},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
38 {"tb_frame", T_OBJECT, OFF(tb_frame), READONLY},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
39 {"tb_lasti", T_INT, OFF(tb_lasti), READONLY},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
40 {"tb_lineno", T_INT, OFF(tb_lineno), READONLY},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
41 {NULL} /* Sentinel */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 };
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 static void
31739
49ec0d8f08ec Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents: 30968
diff changeset
45 tb_dealloc(PyTracebackObject *tb)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
47 PyObject_GC_UnTrack(tb);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
48 Py_TRASHCAN_SAFE_BEGIN(tb)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
49 Py_XDECREF(tb->tb_next);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
50 Py_XDECREF(tb->tb_frame);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
51 PyObject_GC_Del(tb);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
52 Py_TRASHCAN_SAFE_END(tb)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
20623
4fb7727e4f47 Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20618
diff changeset
55 static int
31739
49ec0d8f08ec Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents: 30968
diff changeset
56 tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg)
20623
4fb7727e4f47 Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20618
diff changeset
57 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
58 Py_VISIT(tb->tb_next);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
59 Py_VISIT(tb->tb_frame);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
60 return 0;
20623
4fb7727e4f47 Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20618
diff changeset
61 }
4fb7727e4f47 Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20618
diff changeset
62
4fb7727e4f47 Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20618
diff changeset
63 static void
31739
49ec0d8f08ec Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents: 30968
diff changeset
64 tb_clear(PyTracebackObject *tb)
20623
4fb7727e4f47 Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20618
diff changeset
65 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
66 Py_CLEAR(tb->tb_next);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
67 Py_CLEAR(tb->tb_frame);
20623
4fb7727e4f47 Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20618
diff changeset
68 }
4fb7727e4f47 Make traceback objects collectable.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20618
diff changeset
69
20618
57b51966a4f1 Removed two pointless and obfuscating macros.
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
70 PyTypeObject PyTraceBack_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
71 PyVarObject_HEAD_INIT(&PyType_Type, 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
72 "traceback",
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
73 sizeof(PyTracebackObject),
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
74 0,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
75 (destructor)tb_dealloc, /*tp_dealloc*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
76 0, /*tp_print*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
77 0, /*tp_getattr*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
78 0, /*tp_setattr*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
79 0, /*tp_reserved*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
80 0, /*tp_repr*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
81 0, /*tp_as_number*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
82 0, /*tp_as_sequence*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
83 0, /*tp_as_mapping*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
84 0, /* tp_hash */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
85 0, /* tp_call */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
86 0, /* tp_str */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
87 PyObject_GenericGetAttr, /* tp_getattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
88 0, /* tp_setattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
89 0, /* tp_as_buffer */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
90 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
91 0, /* tp_doc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
92 (traverseproc)tb_traverse, /* tp_traverse */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
93 (inquiry)tb_clear, /* tp_clear */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
94 0, /* tp_richcompare */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
95 0, /* tp_weaklistoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
96 0, /* tp_iter */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
97 0, /* tp_iternext */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
98 tb_methods, /* tp_methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
99 tb_memberlist, /* tp_members */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
100 0, /* tp_getset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
101 0, /* tp_base */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
102 0, /* tp_dict */
86
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
31739
49ec0d8f08ec Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents: 30968
diff changeset
105 static PyTracebackObject *
49ec0d8f08ec Moved tracebackobject to traceback.h, Closes SF Bug #497067
Nicholas Bastin <nick.bastin@gmail.com>
parents: 30968
diff changeset
106 newtracebackobject(PyTracebackObject *next, PyFrameObject *frame)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
108 PyTracebackObject *tb;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
109 if ((next != NULL && !PyTraceBack_Check(next)) ||
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
110 frame == NULL || !PyFrame_Check(frame)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
111 PyErr_BadInternalCall();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
112 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
113 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
114 tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
115 if (tb != NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
116 Py_XINCREF(next);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
117 tb->tb_next = next;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
118 Py_XINCREF(frame);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
119 tb->tb_frame = frame;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
120 tb->tb_lasti = frame->f_lasti;
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55372
diff changeset
121 tb->tb_lineno = PyFrame_GetLineNumber(frame);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
122 PyObject_GC_Track(tb);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
123 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
124 return tb;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
126
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
128 PyTraceBack_Here(PyFrameObject *frame)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
130 PyThreadState *tstate = PyThreadState_GET();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
131 PyTracebackObject *oldtb = (PyTracebackObject *) tstate->curexc_traceback;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
132 PyTracebackObject *tb = newtracebackobject(oldtb, frame);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
133 if (tb == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
134 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
135 tstate->curexc_traceback = (PyObject *)tb;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
136 Py_XDECREF(oldtb);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
137 return 0;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
139
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
140 static PyObject *
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
141 _Py_FindSourceFile(PyObject *filename, char* namebuf, size_t namelen, PyObject *io)
49484
0b7e023803fb Issues #2384 and #3975: Tracebacks were not correctly printed when the source file
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48660
diff changeset
142 {
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
143 Py_ssize_t i;
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
144 PyObject *binary;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
145 PyObject *v;
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
146 Py_ssize_t npath;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
147 size_t taillen;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
148 PyObject *syspath;
65536
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
149 PyObject *path;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
150 const char* tail;
65536
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
151 PyObject *filebytes;
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
152 const char* filepath;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
153 Py_ssize_t len;
65536
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
154 PyObject* result;
49484
0b7e023803fb Issues #2384 and #3975: Tracebacks were not correctly printed when the source file
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48660
diff changeset
155
65399
55ed5c524428 Issue #9713, #10114: Parser functions (eg. PyParser_ASTFromFile) expects
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65331
diff changeset
156 filebytes = PyUnicode_EncodeFSDefault(filename);
65536
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
157 if (filebytes == NULL) {
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
158 PyErr_Clear();
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
159 return NULL;
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
160 }
65536
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
161 filepath = PyBytes_AS_STRING(filebytes);
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
162
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
163 /* Search tail of filename in sys.path before giving up */
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
164 tail = strrchr(filepath, SEP);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
165 if (tail == NULL)
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
166 tail = filepath;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
167 else
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
168 tail++;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
169 taillen = strlen(tail);
49484
0b7e023803fb Issues #2384 and #3975: Tracebacks were not correctly printed when the source file
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48660
diff changeset
170
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
171 syspath = PySys_GetObject("path");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
172 if (syspath == NULL || !PyList_Check(syspath))
65536
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
173 goto error;
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
174 npath = PyList_Size(syspath);
49484
0b7e023803fb Issues #2384 and #3975: Tracebacks were not correctly printed when the source file
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48660
diff changeset
175
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
176 for (i = 0; i < npath; i++) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
177 v = PyList_GetItem(syspath, i);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
178 if (v == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
179 PyErr_Clear();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
180 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
181 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
182 if (!PyUnicode_Check(v))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
183 continue;
65399
55ed5c524428 Issue #9713, #10114: Parser functions (eg. PyParser_ASTFromFile) expects
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65331
diff changeset
184 path = PyUnicode_EncodeFSDefault(v);
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
185 if (path == NULL) {
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
186 PyErr_Clear();
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
187 continue;
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
188 }
65536
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
189 len = PyBytes_GET_SIZE(path);
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
190 if (len + 1 + (Py_ssize_t)taillen >= (Py_ssize_t)namelen - 1) {
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
191 Py_DECREF(path);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
192 continue; /* Too long */
65536
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
193 }
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
194 strcpy(namebuf, PyBytes_AS_STRING(path));
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
195 Py_DECREF(path);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
196 if (strlen(namebuf) != len)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
197 continue; /* v contains '\0' */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
198 if (len > 0 && namebuf[len-1] != SEP)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
199 namebuf[len++] = SEP;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
200 strcpy(namebuf+len, tail);
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
201
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
202 binary = PyObject_CallMethod(io, "open", "ss", namebuf, "rb");
65536
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
203 if (binary != NULL) {
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
204 result = binary;
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
205 goto finally;
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
206 }
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
207 PyErr_Clear();
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
208 }
65536
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
209 goto error;
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
210
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
211 error:
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
212 result = NULL;
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
213 finally:
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
214 Py_DECREF(filebytes);
42b84d3188d1 Recorded merge of revisions 85569-85570 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 65332
diff changeset
215 return result;
49484
0b7e023803fb Issues #2384 and #3975: Tracebacks were not correctly printed when the source file
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48660
diff changeset
216 }
0b7e023803fb Issues #2384 and #3975: Tracebacks were not correctly printed when the source file
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48660
diff changeset
217
46430
47d9bbe10897 Merged revisions 62260-62261,62266,62271,62277-62279,62289-62290,62293-62298,62302-62306,62308,62311,62313-62315,62319-62321 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44742
diff changeset
218 int
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
219 _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
221 int err = 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
222 int fd;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
223 int i;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
224 char *found_encoding;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
225 char *encoding;
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
226 PyObject *io;
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
227 PyObject *binary;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
228 PyObject *fob = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
229 PyObject *lineobj = NULL;
65332
6296632ad2cb Merged revisions 85497 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 62393
diff changeset
230 PyObject *res;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
231 char buf[MAXPATHLEN+1];
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
232 Py_UNICODE *u, *p;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
233 Py_ssize_t len;
44742
14d4104976fc Merged revisions 59985-60000,60002,60005-60007,60009-60042 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44157
diff changeset
234
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
235 /* open the file */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
236 if (filename == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
237 return 0;
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
238
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
239 io = PyImport_ImportModuleNoBlock("io");
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
240 if (io == NULL)
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
241 return -1;
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
242 binary = PyObject_CallMethod(io, "open", "Os", filename, "rb");
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
243
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
244 if (binary == NULL) {
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
245 binary = _Py_FindSourceFile(filename, buf, sizeof(buf), io);
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
246 if (binary == NULL) {
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
247 Py_DECREF(io);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
248 return 0;
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
249 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
250 }
49484
0b7e023803fb Issues #2384 and #3975: Tracebacks were not correctly printed when the source file
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48660
diff changeset
251
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
252 /* use the right encoding to decode the file as unicode */
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
253 fd = PyObject_AsFileDescriptor(binary);
69148
7b8d625eb6e4 Issue #9319: Include the filename in "Non-UTF8 code ..." syntax error.
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69102
diff changeset
254 found_encoding = PyTokenizer_FindEncodingFilename(fd, filename);
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
255 encoding = (found_encoding != NULL) ? found_encoding : "utf-8";
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
256 lseek(fd, 0, 0); /* Reset position */
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
257 fob = PyObject_CallMethod(io, "TextIOWrapper", "Os", binary, encoding);
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
258 Py_DECREF(io);
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
259 Py_DECREF(binary);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
260 PyMem_FREE(found_encoding);
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
261
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
262 if (fob == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
263 PyErr_Clear();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
264 return 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
265 }
49484
0b7e023803fb Issues #2384 and #3975: Tracebacks were not correctly printed when the source file
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48660
diff changeset
266
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
267 /* get the line number lineno */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
268 for (i = 0; i < lineno; i++) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
269 Py_XDECREF(lineobj);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
270 lineobj = PyFile_GetLine(fob, -1);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
271 if (!lineobj) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
272 err = -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
273 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
274 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
275 }
65332
6296632ad2cb Merged revisions 85497 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 62393
diff changeset
276 res = PyObject_CallMethod(fob, "close", "");
6296632ad2cb Merged revisions 85497 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 62393
diff changeset
277 if (res)
6296632ad2cb Merged revisions 85497 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 62393
diff changeset
278 Py_DECREF(res);
6296632ad2cb Merged revisions 85497 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 62393
diff changeset
279 else
6296632ad2cb Merged revisions 85497 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 62393
diff changeset
280 PyErr_Clear();
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
281 Py_DECREF(fob);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
282 if (!lineobj || !PyUnicode_Check(lineobj)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
283 Py_XDECREF(lineobj);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
284 return err;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
285 }
49484
0b7e023803fb Issues #2384 and #3975: Tracebacks were not correctly printed when the source file
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48660
diff changeset
286
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
287 /* remove the indentation of the line */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
288 u = PyUnicode_AS_UNICODE(lineobj);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
289 len = PyUnicode_GET_SIZE(lineobj);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
290 for (p=u; *p == ' ' || *p == '\t' || *p == '\014'; p++)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
291 len--;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
292 if (u != p) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
293 PyObject *truncated;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
294 truncated = PyUnicode_FromUnicode(p, len);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
295 if (truncated) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
296 Py_DECREF(lineobj);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
297 lineobj = truncated;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
298 } else {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
299 PyErr_Clear();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
300 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
301 }
46430
47d9bbe10897 Merged revisions 62260-62261,62266,62271,62277-62279,62289-62290,62293-62298,62302-62306,62308,62311,62313-62315,62319-62321 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44742
diff changeset
302
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
303 /* Write some spaces before the line */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
304 strcpy(buf, " ");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
305 assert (strlen(buf) == 10);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
306 while (indent > 0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
307 if(indent < 10)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
308 buf[indent] = '\0';
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
309 err = PyFile_WriteString(buf, f);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
310 if (err != 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
311 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
312 indent -= 10;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
313 }
48474
2ee09afee126 implement chained exception tracebacks
Benjamin Peterson <benjamin@python.org>
parents: 47301
diff changeset
314
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
315 /* finally display the line */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
316 if (err == 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
317 err = PyFile_WriteObject(lineobj, f, Py_PRINT_RAW);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
318 Py_DECREF(lineobj);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
319 if (err == 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
320 err = PyFile_WriteString("\n", f);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
321 return err;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
323
5608
79e49cbce2ef PyFile_WriteString now returns an error indicator instead of calling
Guido van Rossum <guido@python.org>
parents: 5405
diff changeset
324 static int
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
325 tb_displayline(PyObject *f, PyObject *filename, int lineno, PyObject *name)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 {
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
327 int err;
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
328 PyObject *line;
46430
47d9bbe10897 Merged revisions 62260-62261,62266,62271,62277-62279,62289-62290,62293-62298,62302-62306,62308,62311,62313-62315,62319-62321 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44742
diff changeset
329
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
330 if (filename == NULL || name == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
331 return -1;
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
332 line = PyUnicode_FromFormat(" File \"%U\", line %d, in %U\n",
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
333 filename, lineno, name);
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
334 if (line == NULL)
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
335 return -1;
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
336 err = PyFile_WriteObject(line, f, Py_PRINT_RAW);
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
337 Py_DECREF(line);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
338 if (err != 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
339 return err;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
340 return _Py_DisplaySourceLine(f, filename, lineno, 4);
46430
47d9bbe10897 Merged revisions 62260-62261,62266,62271,62277-62279,62289-62290,62293-62298,62302-62306,62308,62311,62313-62315,62319-62321 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44742
diff changeset
341 }
47d9bbe10897 Merged revisions 62260-62261,62266,62271,62277-62279,62289-62290,62293-62298,62302-62306,62308,62311,62313-62315,62319-62321 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44742
diff changeset
342
47d9bbe10897 Merged revisions 62260-62261,62266,62271,62277-62279,62289-62290,62293-62298,62302-62306,62308,62311,62313-62315,62319-62321 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44742
diff changeset
343 static int
47d9bbe10897 Merged revisions 62260-62261,62266,62271,62277-62279,62289-62290,62293-62298,62302-62306,62308,62311,62313-62315,62319-62321 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44742
diff changeset
344 tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
47d9bbe10897 Merged revisions 62260-62261,62266,62271,62277-62279,62289-62290,62293-62298,62302-62306,62308,62311,62313-62315,62319-62321 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44742
diff changeset
345 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
346 int err = 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
347 long depth = 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
348 PyTracebackObject *tb1 = tb;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
349 while (tb1 != NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
350 depth++;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
351 tb1 = tb1->tb_next;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
352 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
353 while (tb != NULL && err == 0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
354 if (depth <= limit) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
355 err = tb_displayline(f,
62393
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
356 tb->tb_frame->f_code->co_filename,
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
357 tb->tb_lineno,
fa830371c724 Merged revisions 82059,82061 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 61467
diff changeset
358 tb->tb_frame->f_code->co_name);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
359 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
360 depth--;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
361 tb = tb->tb_next;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
362 if (err == 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
363 err = PyErr_CheckSignals();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
364 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
365 return err;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
367
44157
565012d1123d Removed another occurrence of PyInt_ExactCheck()
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
368 #define PyTraceBack_LIMIT 1000
565012d1123d Removed another occurrence of PyInt_ExactCheck()
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
369
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
371 PyTraceBack_Print(PyObject *v, PyObject *f)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
373 int err;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
374 PyObject *limitv;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
375 long limit = PyTraceBack_LIMIT;
44157
565012d1123d Removed another occurrence of PyInt_ExactCheck()
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
376
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
377 if (v == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
378 return 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
379 if (!PyTraceBack_Check(v)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
380 PyErr_BadInternalCall();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
381 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
382 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
383 limitv = PySys_GetObject("tracebacklimit");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
384 if (limitv) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
385 PyObject *exc_type, *exc_value, *exc_tb;
44157
565012d1123d Removed another occurrence of PyInt_ExactCheck()
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
386
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
387 PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
388 limit = PyLong_AsLong(limitv);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
389 if (limit == -1 && PyErr_Occurred()) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
390 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
391 limit = PyTraceBack_LIMIT;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
392 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
393 else {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
394 Py_XDECREF(exc_type);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
395 Py_XDECREF(exc_value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
396 Py_XDECREF(exc_tb);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
397 return 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
398 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
399 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
400 else if (limit <= 0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
401 limit = PyTraceBack_LIMIT;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
402 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
403 PyErr_Restore(exc_type, exc_value, exc_tb);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
404 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
405 err = PyFile_WriteString("Traceback (most recent call last):\n", f);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
406 if (!err)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
407 err = tb_printinternal((PyTracebackObject *)v, f, limit);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51271
diff changeset
408 return err;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 }
69070
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
410
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
411 /* Reverse a string. For example, "abcd" becomes "dcba".
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
412
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
413 This function is signal safe. */
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
414
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
415 static void
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
416 reverse_string(char *text, const size_t len)
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
417 {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
418 char tmp;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
419 size_t i, j;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
420 if (len == 0)
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
421 return;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
422 for (i=0, j=len-1; i < j; i++, j--) {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
423 tmp = text[i];
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
424 text[i] = text[j];
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
425 text[j] = tmp;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
426 }
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
427 }
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
428
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
429 /* Format an integer in range [0; 999999] to decimal,
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
430 and write it into the file fd.
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
431
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
432 This function is signal safe. */
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
433
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
434 static void
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
435 dump_decimal(int fd, int value)
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
436 {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
437 char buffer[7];
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
438 int len;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
439 if (value < 0 || 999999 < value)
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
440 return;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
441 len = 0;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
442 do {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
443 buffer[len] = '0' + (value % 10);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
444 value /= 10;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
445 len++;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
446 } while (value);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
447 reverse_string(buffer, len);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
448 write(fd, buffer, len);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
449 }
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
450
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
451 /* Format an integer in range [0; 0xffffffff] to hexdecimal of 'width' digits,
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
452 and write it into the file fd.
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
453
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
454 This function is signal safe. */
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
455
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
456 static void
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
457 dump_hexadecimal(int width, unsigned long value, int fd)
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
458 {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
459 const char *hexdigits = "0123456789abcdef";
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
460 int len;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
461 char buffer[sizeof(unsigned long) * 2 + 1];
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
462 len = 0;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
463 do {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
464 buffer[len] = hexdigits[value & 15];
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
465 value >>= 4;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
466 len++;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
467 } while (len < width || value);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
468 reverse_string(buffer, len);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
469 write(fd, buffer, len);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
470 }
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
471
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
472 /* Write an unicode object into the file fd using ascii+backslashreplace.
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
473
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
474 This function is signal safe. */
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
475
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
476 static void
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
477 dump_ascii(int fd, PyObject *text)
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
478 {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
479 Py_ssize_t i, size;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
480 int truncated;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
481 Py_UNICODE *u;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
482 char c;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
483
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
484 size = PyUnicode_GET_SIZE(text);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
485 u = PyUnicode_AS_UNICODE(text);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
486
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
487 if (MAX_STRING_LENGTH < size) {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
488 size = MAX_STRING_LENGTH;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
489 truncated = 1;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
490 }
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
491 else
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
492 truncated = 0;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
493
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
494 for (i=0; i < size; i++, u++) {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
495 if (*u < 128) {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
496 c = (char)*u;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
497 write(fd, &c, 1);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
498 }
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
499 else if (*u < 256) {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
500 PUTS(fd, "\\x");
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
501 dump_hexadecimal(2, *u, fd);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
502 }
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
503 else
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
504 #ifdef Py_UNICODE_WIDE
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
505 if (*u < 65536)
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
506 #endif
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
507 {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
508 PUTS(fd, "\\u");
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
509 dump_hexadecimal(4, *u, fd);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
510 #ifdef Py_UNICODE_WIDE
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
511 }
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
512 else {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
513 PUTS(fd, "\\U");
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
514 dump_hexadecimal(8, *u, fd);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
515 #endif
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
516 }
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
517 }
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
518 if (truncated)
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
519 PUTS(fd, "...");
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
520 }
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
521
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
522 /* Write a frame into the file fd: "File "xxx", line xxx in xxx".
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
523
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
524 This function is signal safe. */
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
525
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
526 static void
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
527 dump_frame(int fd, PyFrameObject *frame)
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
528 {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
529 PyCodeObject *code;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
530 int lineno;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
531
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
532 code = frame->f_code;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
533 PUTS(fd, " File ");
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
534 if (code != NULL && code->co_filename != NULL
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
535 && PyUnicode_Check(code->co_filename))
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
536 {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
537 write(fd, "\"", 1);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
538 dump_ascii(fd, code->co_filename);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
539 write(fd, "\"", 1);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
540 } else {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
541 PUTS(fd, "???");
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
542 }
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
543
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
544 /* PyFrame_GetLineNumber() was introduced in Python 2.7.0 and 3.2.0 */
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
545 lineno = PyCode_Addr2Line(frame->f_code, frame->f_lasti);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
546 PUTS(fd, ", line ");
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
547 dump_decimal(fd, lineno);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
548 PUTS(fd, " in ");
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
549
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
550 if (code != NULL && code->co_name != NULL
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
551 && PyUnicode_Check(code->co_name))
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
552 dump_ascii(fd, code->co_name);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
553 else
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
554 PUTS(fd, "???");
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
555
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
556 write(fd, "\n", 1);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
557 }
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
558
69102
7e3ed426962f Issue #11393: _Py_DumpTraceback() writes the header even if there is no frame
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69070
diff changeset
559 static void
69070
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
560 dump_traceback(int fd, PyThreadState *tstate, int write_header)
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
561 {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
562 PyFrameObject *frame;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
563 unsigned int depth;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
564
69102
7e3ed426962f Issue #11393: _Py_DumpTraceback() writes the header even if there is no frame
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69070
diff changeset
565 if (write_header)
7e3ed426962f Issue #11393: _Py_DumpTraceback() writes the header even if there is no frame
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69070
diff changeset
566 PUTS(fd, "Traceback (most recent call first):\n");
7e3ed426962f Issue #11393: _Py_DumpTraceback() writes the header even if there is no frame
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69070
diff changeset
567
69070
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
568 frame = _PyThreadState_GetFrame(tstate);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
569 if (frame == NULL)
69102
7e3ed426962f Issue #11393: _Py_DumpTraceback() writes the header even if there is no frame
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69070
diff changeset
570 return;
69070
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
571
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
572 depth = 0;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
573 while (frame != NULL) {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
574 if (MAX_FRAME_DEPTH <= depth) {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
575 PUTS(fd, " ...\n");
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
576 break;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
577 }
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
578 if (!PyFrame_Check(frame))
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
579 break;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
580 dump_frame(fd, frame);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
581 frame = frame->f_back;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
582 depth++;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
583 }
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
584 }
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
585
69102
7e3ed426962f Issue #11393: _Py_DumpTraceback() writes the header even if there is no frame
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69070
diff changeset
586 void
69070
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
587 _Py_DumpTraceback(int fd, PyThreadState *tstate)
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
588 {
69102
7e3ed426962f Issue #11393: _Py_DumpTraceback() writes the header even if there is no frame
Victor Stinner <victor.stinner@haypocalc.com>
parents: 69070
diff changeset
589 dump_traceback(fd, tstate, 1);
69070
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
590 }
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
591
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
592 /* Write the thread identifier into the file 'fd': "Current thread 0xHHHH:\" if
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
593 is_current is true, "Thread 0xHHHH:\n" otherwise.
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
594
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
595 This function is signal safe. */
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
596
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
597 static void
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
598 write_thread_id(int fd, PyThreadState *tstate, int is_current)
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
599 {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
600 if (is_current)
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
601 PUTS(fd, "Current thread 0x");
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
602 else
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
603 PUTS(fd, "Thread 0x");
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
604 dump_hexadecimal(sizeof(long)*2, (unsigned long)tstate->thread_id, fd);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
605 PUTS(fd, ":\n");
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
606 }
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
607
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
608 const char*
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
609 _Py_DumpTracebackThreads(int fd, PyInterpreterState *interp,
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
610 PyThreadState *current_thread)
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
611 {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
612 PyThreadState *tstate;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
613 unsigned int nthreads;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
614
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
615 /* Get the current interpreter from the current thread */
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
616 tstate = PyInterpreterState_ThreadHead(interp);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
617 if (tstate == NULL)
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
618 return "unable to get the thread head state";
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
619
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
620 /* Dump the traceback of each thread */
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
621 tstate = PyInterpreterState_ThreadHead(interp);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
622 nthreads = 0;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
623 do
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
624 {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
625 if (nthreads != 0)
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
626 write(fd, "\n", 1);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
627 if (nthreads >= MAX_NTHREADS) {
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
628 PUTS(fd, "...\n");
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
629 break;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
630 }
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
631 write_thread_id(fd, tstate, tstate == current_thread);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
632 dump_traceback(fd, tstate, 0);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
633 tstate = PyThreadState_Next(tstate);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
634 nthreads++;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
635 } while (tstate != NULL);
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
636
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
637 return NULL;
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
638 }
b0680b5a5215 Issue #11393: Add the new faulthandler module
Victor Stinner <victor.stinner@haypocalc.com>
parents: 68257
diff changeset
639