Skip to content

Commit 538a1cd

Browse files
committed
Add written docs, move decls to cpython/traceback.h to avoid modifying Limited API
1 parent d0c95ed commit 538a1cd

File tree

3 files changed

+62
-45
lines changed

3 files changed

+62
-45
lines changed

Doc/c-api/exceptions.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,3 +1346,20 @@ Tracebacks
13461346
13471347
This function returns ``0`` on success, and returns ``-1`` with an
13481348
exception set on failure.
1349+
1350+
.. c:function:: void PyUnstable_DumpTraceback(int fd, PyThreadState *tstate)
1351+
1352+
Write a trace of the Python stack in *tstate* into the file *fd*.
1353+
1354+
This function is safe to use from signal handlers.
1355+
1356+
.. c:function:: const char* PyUnstable_DumpTracebackThreads(int fd, PyInterpreterState *interp, PyThreadState *current_tstate)
1357+
1358+
Write the traces of all Python threads in *interp* into the file *fd*. If
1359+
*current_state* is not NULL then it will be used to identify what the current
1360+
thread is in the written output. If it is NULL then this function will
1361+
identify the current thread using thread-specific storage.
1362+
1363+
This function will return NULL on success, or an error message on error.
1364+
1365+
This function is safe to use from signal handlers.

Include/cpython/traceback.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,48 @@ struct _traceback {
1111
int tb_lasti;
1212
int tb_lineno;
1313
};
14+
15+
/* Write the Python traceback into the file 'fd'. For example:
16+
17+
Traceback (most recent call first):
18+
File "xxx", line xxx in <xxx>
19+
File "xxx", line xxx in <xxx>
20+
...
21+
File "xxx", line xxx in <xxx>
22+
23+
This function is written for debug purpose only, to dump the traceback in
24+
the worst case: after a segmentation fault, at fatal error, etc. That's why,
25+
it is very limited. Strings are truncated to 100 characters and encoded to
26+
ASCII with backslashreplace. It doesn't write the source code, only the
27+
function name, filename and line number of each frame. Write only the first
28+
100 frames: if the traceback is truncated, write the line " ...".
29+
30+
This function is signal safe. */
31+
PyAPI_FUNC(void) PyUnstable_DumpTraceback(int fd, PyThreadState *tstate);
32+
33+
/* Write the traceback of all threads into the file 'fd'. current_thread can be
34+
NULL.
35+
36+
Return NULL on success, or an error message on error.
37+
38+
This function is written for debug purpose only. It calls
39+
PyUnstable_DumpTraceback() for each thread, and so has the same limitations. It
40+
only writes the traceback of the first 100 threads: write "..." if there are
41+
more threads.
42+
43+
If current_tstate is NULL, the function tries to get the Python thread state
44+
of the current thread. It is not an error if the function is unable to get
45+
the current Python thread state.
46+
47+
If interp is NULL, the function tries to get the interpreter state from
48+
the current Python thread state, or from
49+
_PyGILState_GetInterpreterStateUnsafe() in last resort.
50+
51+
It is better to pass NULL to interp and current_tstate, the function tries
52+
different options to retrieve this information.
53+
54+
This function is signal safe. */
55+
PyAPI_FUNC(const char*) PyUnstable_DumpTracebackThreads(
56+
int fd,
57+
PyInterpreterState *interp,
58+
PyThreadState *current_tstate);

Include/traceback.h

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,51 +13,6 @@ PyAPI_FUNC(int) PyTraceBack_Print(PyObject *, PyObject *);
1313
PyAPI_DATA(PyTypeObject) PyTraceBack_Type;
1414
#define PyTraceBack_Check(v) Py_IS_TYPE((v), &PyTraceBack_Type)
1515

16-
/* Write the Python traceback into the file 'fd'. For example:
17-
18-
Traceback (most recent call first):
19-
File "xxx", line xxx in <xxx>
20-
File "xxx", line xxx in <xxx>
21-
...
22-
File "xxx", line xxx in <xxx>
23-
24-
This function is written for debug purpose only, to dump the traceback in
25-
the worst case: after a segmentation fault, at fatal error, etc. That's why,
26-
it is very limited. Strings are truncated to 100 characters and encoded to
27-
ASCII with backslashreplace. It doesn't write the source code, only the
28-
function name, filename and line number of each frame. Write only the first
29-
100 frames: if the traceback is truncated, write the line " ...".
30-
31-
This function is signal safe. */
32-
PyAPI_FUNC(void) PyUnstable_DumpTraceback(int fd, PyThreadState *tstate);
33-
34-
/* Write the traceback of all threads into the file 'fd'. current_thread can be
35-
NULL.
36-
37-
Return NULL on success, or an error message on error.
38-
39-
This function is written for debug purpose only. It calls
40-
PyUnstable_DumpTraceback() for each thread, and so has the same limitations. It
41-
only writes the traceback of the first 100 threads: write "..." if there are
42-
more threads.
43-
44-
If current_tstate is NULL, the function tries to get the Python thread state
45-
of the current thread. It is not an error if the function is unable to get
46-
the current Python thread state.
47-
48-
If interp is NULL, the function tries to get the interpreter state from
49-
the current Python thread state, or from
50-
_PyGILState_GetInterpreterStateUnsafe() in last resort.
51-
52-
It is better to pass NULL to interp and current_tstate, the function tries
53-
different options to retrieve this information.
54-
55-
This function is signal safe. */
56-
PyAPI_FUNC(const char*) PyUnstable_DumpTracebackThreads(
57-
int fd,
58-
PyInterpreterState *interp,
59-
PyThreadState *current_tstate);
60-
6116
#ifndef Py_LIMITED_API
6217
# define Py_CPYTHON_TRACEBACK_H
6318
# include "cpython/traceback.h"

0 commit comments

Comments
 (0)