Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 438a12d

Browse files
authored
bpo-36710: Add tstate parameter in ceval.c (pythonGH-13547)
* Fix a possible reference leak in _PyErr_Print() if exception is NULL. * PyErr_BadInternalCall(): replace PyErr_Format() with _PyErr_SetString(). * Add pycore_pyerrors.h header file. * New functions: * _PyErr_Clear() * _PyErr_Fetch() * _PyErr_Print() * _PyErr_Restore() * _PyErr_SetObject() * _PyErr_SetString() * Add 'tstate' parameter to _PyEval_AddPendingCall().
1 parent 13d4e6a commit 438a12d

11 files changed

+563
-403
lines changed

Include/internal/pycore_ceval.h

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ PyAPI_FUNC(void) _PyEval_FiniThreads(
1919
PyAPI_FUNC(void) _PyEval_SignalReceived(
2020
struct _ceval_runtime_state *ceval);
2121
PyAPI_FUNC(int) _PyEval_AddPendingCall(
22+
PyThreadState *tstate,
2223
struct _ceval_runtime_state *ceval,
2324
int (*func)(void *),
2425
void *arg);

Include/internal/pycore_pyerrors.h

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#ifndef Py_INTERNAL_PYERRORS_H
2+
#define Py_INTERNAL_PYERRORS_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+
static inline PyObject* _PyErr_Occurred(PyThreadState *tstate)
12+
{
13+
return tstate == NULL ? NULL : tstate->curexc_type;
14+
}
15+
16+
17+
PyAPI_FUNC(void) _PyErr_Fetch(
18+
PyThreadState *tstate,
19+
PyObject **type,
20+
PyObject **value,
21+
PyObject **traceback);
22+
23+
PyAPI_FUNC(int) _PyErr_ExceptionMatches(
24+
PyThreadState *tstate,
25+
PyObject *exc);
26+
27+
PyAPI_FUNC(void) _PyErr_Restore(
28+
PyThreadState *tstate,
29+
PyObject *type,
30+
PyObject *value,
31+
PyObject *traceback);
32+
33+
PyAPI_FUNC(void) _PyErr_SetObject(
34+
PyThreadState *tstate,
35+
PyObject *type,
36+
PyObject *value);
37+
38+
PyAPI_FUNC(void) _PyErr_Clear(PyThreadState *tstate);
39+
40+
PyAPI_FUNC(void) _PyErr_SetNone(PyThreadState *tstate, PyObject *exception);
41+
42+
PyAPI_FUNC(void) _PyErr_SetString(
43+
PyThreadState *tstate,
44+
PyObject *exception,
45+
const char *string);
46+
47+
PyAPI_FUNC(PyObject *) _PyErr_Format(
48+
PyThreadState *tstate,
49+
PyObject *exception,
50+
const char *format,
51+
...);
52+
53+
PyAPI_FUNC(void) _PyErr_NormalizeException(
54+
PyThreadState *tstate,
55+
PyObject **exc,
56+
PyObject **val,
57+
PyObject **tb);
58+
59+
#ifdef __cplusplus
60+
}
61+
#endif
62+
#endif /* !Py_INTERNAL_PYERRORS_H */

Include/internal/pycore_pylifecycle.h

+2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ PyAPI_FUNC(int) _Py_HandleSystemExit(int *exitcode_p);
106106

107107
PyAPI_FUNC(PyObject*) _PyErr_WriteUnraisableDefaultHook(PyObject *unraisable);
108108

109+
PyAPI_FUNC(void) _PyErr_Print(PyThreadState *tstate);
110+
109111
#ifdef __cplusplus
110112
}
111113
#endif

Include/internal/pycore_pymem.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef Py_INTERNAL_MEM_H
2-
#define Py_INTERNAL_MEM_H
1+
#ifndef Py_INTERNAL_PYMEM_H
2+
#define Py_INTERNAL_PYMEM_H
33
#ifdef __cplusplus
44
extern "C" {
55
#endif
@@ -191,4 +191,4 @@ PyAPI_FUNC(int) _PyMem_SetupAllocators(PyMemAllocatorName allocator);
191191
#ifdef __cplusplus
192192
}
193193
#endif
194-
#endif /* !Py_INTERNAL_MEM_H */
194+
#endif /* !Py_INTERNAL_PYMEM_H */

Makefile.pre.in

+1
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,7 @@ PYTHON_HEADERS= \
10771077
$(srcdir)/Include/internal/pycore_hamt.h \
10781078
$(srcdir)/Include/internal/pycore_object.h \
10791079
$(srcdir)/Include/internal/pycore_pathconfig.h \
1080+
$(srcdir)/Include/internal/pycore_pyerrors.h \
10801081
$(srcdir)/Include/internal/pycore_pyhash.h \
10811082
$(srcdir)/Include/internal/pycore_pylifecycle.h \
10821083
$(srcdir)/Include/internal/pycore_pymem.h \

Modules/signalmodule.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ trip_signal(int sig_num)
258258

259259
/* Notify ceval.c */
260260
_PyRuntimeState *runtime = &_PyRuntime;
261+
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
261262
_PyEval_SignalReceived(&runtime->ceval);
262263

263264
/* And then write to the wakeup fd *after* setting all the globals and
@@ -298,7 +299,7 @@ trip_signal(int sig_num)
298299
{
299300
/* Py_AddPendingCall() isn't signal-safe, but we
300301
still use it for this exceptional case. */
301-
_PyEval_AddPendingCall(&runtime->ceval,
302+
_PyEval_AddPendingCall(tstate, &runtime->ceval,
302303
report_wakeup_send_error,
303304
(void *)(intptr_t) last_error);
304305
}
@@ -317,7 +318,7 @@ trip_signal(int sig_num)
317318
{
318319
/* Py_AddPendingCall() isn't signal-safe, but we
319320
still use it for this exceptional case. */
320-
_PyEval_AddPendingCall(&runtime->ceval,
321+
_PyEval_AddPendingCall(tstate, &runtime->ceval,
321322
report_wakeup_write_error,
322323
(void *)(intptr_t)errno);
323324
}

PCbuild/pythoncore.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
<ClInclude Include="..\Include\internal\pycore_hamt.h" />
169169
<ClInclude Include="..\Include\internal\pycore_object.h" />
170170
<ClInclude Include="..\Include\internal\pycore_pathconfig.h" />
171+
<ClInclude Include="..\Include\internal\pycore_pyerrors.h" />
171172
<ClInclude Include="..\Include\internal\pycore_pyhash.h" />
172173
<ClInclude Include="..\Include\internal\pycore_pylifecycle.h" />
173174
<ClInclude Include="..\Include\internal\pycore_pymem.h" />

PCbuild/pythoncore.vcxproj.filters

+3
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@
207207
<ClInclude Include="..\Include\internal\pycore_pathconfig.h">
208208
<Filter>Include</Filter>
209209
</ClInclude>
210+
<ClInclude Include="..\Include\internal\pycore_pyerrors.h">
211+
<Filter>Include</Filter>
212+
</ClInclude>
210213
<ClInclude Include="..\Include\internal\pycore_pyhash.h">
211214
<Filter>Include</Filter>
212215
</ClInclude>

0 commit comments

Comments
 (0)