8000 bpo-35134: Split traceback.h header (GH-13430) · python/cpython@ed48866 · GitHub
[go: up one dir, main page]

Skip to content

Commit ed48866

Browse files
authored
bpo-35134: Split traceback.h header (GH-13430)
Add new Include/cpython/traceback.h and Include/internal/traceback.h header files.
1 parent d673810 commit ed48866

File tree

8 files changed

+123
-96
lines changed

8 files changed

+123
-96
lines changed

Include/cpython/traceback.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef Py_CPYTHON_TRACEBACK_H
2+
# error "this header file must not be included directly"
3+
#endif
4+
5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
8+
9+
typedef struct _traceback {
10+
PyObject_HEAD
11+
struct _traceback *tb_next;
12+
struct _frame *tb_frame;
13+
int tb_lasti;
14+
int tb_lineno;
15+
} PyTracebackObject;
16+
17+
PyAPI_FUNC(int) _Py_DisplaySourceLine(PyObject *, PyObject *, int, int);
18+
PyAPI_FUNC(void) _PyTraceback_Add(const char *, const char *, int);
19+
20+
#ifdef __cplusplus
21+
}
22+
#endif

Include/frameobject.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/* Frame object interface */
32

43
#ifndef Py_LIMITED_API

Include/genobject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
extern "C" {
99
#endif
1010

11+
#include "pystate.h" /* _PyErr_StackItem */
12+
1113
struct _frame; /* Avoid including frameobject.h */
1214

1315
/* _PyGenObject_HEAD defines the initial segment of generator

Include/internal/pycore_traceback.h

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#ifndef Py_INTERNAL_TRACEBACK_H
2+
#define Py_INTERNAL_TRACEBACK_H
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
#ifndef Py_BUILD_CORE
8+
# error "this header requires Py_BUILD_CORE define"
9+
#endif
10+
11+
#include "pystate.h" /* PyInterpreterState */
< 629A /td>
12+
13+
/* Write the Python traceback into the file 'fd'. For example:
14+
15+
Traceback (most recent call first):
16+
File "xxx", line xxx in <xxx>
17+
File "xxx", line xxx in <xxx>
18+
...
19+
File "xxx", line xxx in <xxx>
20+
21+
This function is written for debug purpose only, to dump the traceback in
22+
the worst case: after a segmentation fault, at fatal error, etc. That's why,
23+
it is very limited. Strings are truncated to 100 characters and encoded to
24+
ASCII with backslashreplace. It doesn't write the source code, only the
25+
function name, filename and line number of each frame. Write only the first
26+
100 frames: if the traceback is truncated, write the line " ...".
27+
28+
This function is signal safe. */
29+
30+
PyAPI_FUNC(void) _Py_DumpTraceback(
31+
int fd,
32+
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+
_Py_DumpTraceback() for each thread, and so has the same limitations. It
41+
only write 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 these informations.
54+
55+
This function is signal safe. */
56+
57+
PyAPI_FUNC(const char*) _Py_DumpTracebackThreads(
58+
int fd,
59+
PyInterpreterState *interp,
60+
PyThreadState *current_tstate);
61+
62+
/* Write a Unicode object into the file descriptor fd. Encode the string to
63+
ASCII using the backslashreplace error handler.
64+
65+
Do nothing if text is not a Unicode object. The function accepts Unicode
66+
string which is not ready (PyUnicode_WCHAR_KIND).
67+
68+
This function is signal safe. */
69+
PyAPI_FUNC(void) _Py_DumpASCII(int fd, PyObject *text);
70+
71+
/* Format an integer as decimal into the file descriptor fd.
72+
73+
This function is signal safe. */
74+
PyAPI_FUNC(void) _Py_DumpDecimal(
75+
int fd,
76+
unsigned long value);
77+
78+
/* Format an integer as hexadecimal into the file descriptor fd with at least
79+
width digits.
80+
81+
The maximum width is sizeof(unsigned long)*2 digits.
82+
83+
This function is signal safe. */
84+
PyAPI_FUNC(void) _Py_DumpHexadecimal(
85+
int fd,
86+
unsigned long value,
87+
Py_ssize_t width);
88+
89+
#ifdef __cplusplus
90+
}
91+
#endif
92+
#endif /* !Py_INTERNAL_TRACEBACK_H */

Include/traceback.h

Lines changed: 4 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,26 @@
1-
21
#ifndef Py_TRACEBACK_H
32
#define Py_TRACEBACK_H
43
#ifdef __cplusplus
54
extern "C" {
65
#endif
76

8-
#include "pystate.h"
9-
107
struct _frame;
118

129
/* Traceback interface */
13-
#ifndef Py_LIMITED_API
14-
typedef struct _traceback {
15-
PyObject_HEAD
16-
struct _traceback *tb_next;
17-
struct _frame *tb_frame;
18-
int tb_lasti;
19-
int tb_lineno;
20-
} PyTracebackObject;
21-
#endif
2210

2311
PyAPI_FUNC(int) PyTraceBack_Here(struct _frame *);
2412
PyAPI_FUNC(int) PyTraceBack_Print(PyObject *, PyObject *);
25-
#ifndef Py_LIMITED_API
26-
PyAPI_FUNC(int) _Py_DisplaySourceLine(PyObject *, PyObject *, int, int);
27-
PyAPI_FUNC(void) _PyTraceback_Add(const char *, const char *, int);
28-
#endif
2913

3014
/* Reveal traceback type so we can typecheck traceback objects */
3115
PyAPI_DATA(PyTypeObject) PyTraceBack_Type;
3216
#define PyTraceBack_Check(v) (Py_TYPE(v) == &PyTraceBack_Type)
3317

34-
#ifndef Py_LIMITED_API
35-
/* Write the Python traceback into the file 'fd'. For example:
36-
37-
Traceback (most recent call first):
38-
File "xxx", line xxx in <xxx>
39-
File "xxx", line xxx in <xxx>
40-
...
41-
File "xxx", line xxx in <xxx>
42-
43-
This function is written for debug purpose only, to dump the traceback in
44-
the worst case: after a segmentation fault, at fatal error, etc. That's why,
45-
it is very limited. Strings are truncated to 100 characters and encoded to
46-
ASCII with backslashreplace. It doesn't write the source code, only the
47-
function name, filename and line number of each frame. Write only the first
48-
100 frames: if the traceback is truncated, write the line " ...".
49-
50-
This function is signal safe. */
51-
52-
PyAPI_FUNC(void) _Py_DumpTraceback(
53-
int fd,
54-
PyThreadState *tstate);
55-
56-
/* Write the traceback of all threads into the file 'fd'. current_thread can be
57-
NULL.
58-
59-
Return NULL on success, or an error message on error.
60-
61-
This function is written for debug purpose only. It calls
62-
_Py_DumpTraceback() for each thread, and so has the same limitations. It
63-
only write the traceback of the first 100 threads: write "..." if there are
64-
more threads.
65-
66-
If current_tstate is NULL, the function tries to get the Python thread state
67-
of the current thread. It is not an error if the function is unable to get
68-
the current Python thread state.
69-
70-
If interp is NULL, the function tries to get the interpreter state from
71-
the current Python thread state, or from
72-
_PyGILState_GetInterpreterStateUnsafe() in last resort.
73-
74-
It is better to pass NULL to interp and current_tstate, the function tries
75-
different options to retrieve these informations.
76-
77-
This function is signal safe. */
78-
79-
PyAPI_FUNC(const char*) _Py_DumpTracebackThreads(
80-
int fd,
81-
PyInterpreterState *interp,
82-
PyThreadState *current_tstate);
83-
#endif /* !Py_LIMITED_API */
8418

8519
#ifndef Py_LIMITED_API
86-
87-
/* Write a Unicode object into the file descriptor fd. Encode the string to
88-
ASCII using the backslashreplace error handler.
89-
90-
Do nothing if text is not a Unicode object. The function accepts Unicode
91-
string which is not ready (PyUnicode_WCHAR_KIND).
92-
93-
This function is signal safe. */
94-
PyAPI_FUNC(void) _Py_DumpASCII(int fd, PyObject *text);
95-
96-
/* Format an integer as decimal into the file descriptor fd.
97-
98-
This function is signal safe. */
99-
PyAPI_FUNC(void) _Py_DumpDecimal(
100-
int fd,
101-
unsigned long value);
102-
103-
/* Format an integer as hexadecimal into the file descriptor fd with at least
104-
width digits.
105-
106-
The maximum width is sizeof(unsigned long)*2 digits.
107-
108-
This function is signal safe. */
109-
PyAPI_FUNC(void) _Py_DumpHexadecimal(
110-
int fd,
111-
unsigned long value,
112-
Py_ssize_t width);
113-
114-
#endif /* !Py_LIMITED_API */
20+
# define Py_CPYTHON_TRACEBACK_H
21+
# include "cpython/traceback.h"
22+
# undef Py_CPYTHON_TRACEBACK_H
23+
#endif
11524

11625
#ifdef __cplusplus
11726
}

Modules/_tracemalloc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "Python.h"
2+
#include "pycore_traceback.h"
23
#include "hashtable.h"
34
#include "frameobject.h"
45
#include "pythread.h"

Modules/faulthandler.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Python.h"
22
#include "pycore_coreconfig.h"
3+
#include "pycore_traceback.h"
34
#include "pythread.h"
45
#include <signal.h>
56
#include <object.h>

Python/pylifecycle.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "pycore_pylifecycle.h"
1414
#include "pycore_pymem.h"
1515
#include "pycore_pystate.h"
16+
#include "pycore_traceback.h"
1617
#include "grammar.h"
1718
#include "node.h"
1819
#include "token.h"

0 commit comments

Comments
 (0)
0