Skip to content

Commit 6765938

Browse files
authored
gh-106320: Remove private _PyErr_WriteUnraisableMsg() (#108863)
Move the private _PyErr_WriteUnraisableMsg() functions to the internal C API (pycore_pyerrors.h). Move write_unraisable_exc() from _testcapi to _testinternalcapi.
1 parent 6304d98 commit 6765938

16 files changed

+86
-77
lines changed

Diff for: Include/cpython/pyerrors.h

-4
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,6 @@ PyAPI_FUNC(PyObject *) PyErr_ProgramTextObject(
116116
PyObject *filename,
117117
int lineno);
118118

119-
PyAPI_FUNC(void) _PyErr_WriteUnraisableMsg(
120-
const char *err_msg,
121-
PyObject *obj);
122-
123119
PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalErrorFunc(
124120
const char *func,
125121
const char *message);

Diff for: Include/internal/pycore_pyerrors.h

+5
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ Py_DEPRECATED(3.12) extern void _PyErr_ChainExceptions(PyObject *, PyObject *, P
170170
// Export for '_zoneinfo' shared extension
171171
PyAPI_FUNC(void) _PyErr_ChainExceptions1(PyObject *);
172172

173+
// Export for '_lsprof' shared extension
174+
PyAPI_FUNC(void) _PyErr_WriteUnraisableMsg(
175+
const char *err_msg,
176+
PyObject *obj);
177+
173178
#ifdef __cplusplus
174179
}
175180
#endif

Diff for: Lib/test/audit-tests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def hook(event, args):
289289

290290

291291
def test_unraisablehook():
292-
from _testcapi import write_unraisable_exc
292+
from _testinternalcapi import write_unraisable_exc
293293

294294
def unraisablehook(hookargs):
295295
pass

Diff for: Lib/test/test_sys.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1199,11 +1199,11 @@ class MyType:
11991199
@test.support.cpython_only
12001200
class UnraisableHookTest(unittest.TestCase):
12011201
def write_unraisable_exc(self, exc, err_msg, obj):
1202-
import _testcapi
1202+
import _testinternalcapi
12031203
import types
12041204
err_msg2 = f"Exception ignored {err_msg}"
12051205
try:
1206-
_testcapi.write_unraisable_exc(exc, err_msg, obj)
1206+
_testinternalcapi.write_unraisable_exc(exc, err_msg, obj)
12071207
return types.SimpleNamespace(exc_type=type(exc),
12081208
exc_value=exc,
12091209
exc_traceback=exc.__traceback__,

Diff for: Modules/_ctypes/_ctypes.c

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ bytes(cdata)
110110

111111
#include "pycore_call.h" // _PyObject_CallNoArgs()
112112
#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
113+
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()
113114

114115

115116
#include <ffi.h>

Diff for: Modules/_ctypes/callbacks.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
# include <windows.h>
99
#endif
1010

11-
#include "pycore_call.h" // _PyObject_CallNoArgs()
12-
#include "pycore_runtime.h" // _PyRuntime
13-
#include "pycore_global_objects.h" // _Py_ID()
11+
#include "pycore_call.h" // _PyObject_CallNoArgs()
12+
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()
13+
#include "pycore_runtime.h" // _Py_ID()
1414

1515
#include <stdbool.h>
1616

Diff for: Modules/_lsprof.c

+2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
#include "Python.h"
66
#include "pycore_call.h" // _PyObject_CallNoArgs()
77
#include "pycore_ceval.h" // _PyEval_SetProfile()
8+
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()
89
#include "pycore_pystate.h" // _PyThreadState_GET()
10+
911
#include "rotatingtree.h"
1012

1113
/************************************************************/

Diff for: Modules/_testcapi/clinic/exceptions.c.h

+1-33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Modules/_testcapi/exceptions.c

-31
Original file line numberDiff line numberDiff line change
@@ -278,36 +278,6 @@ _testcapi_set_exception(PyObject *module, PyObject *new_exc)
278278
return exc;
279279
}
280280

281-
/*[clinic input]
282-
_testcapi.write_unraisable_exc
283-
exception as exc: object
284-
err_msg: object
285-
obj: object
286-
/
287-
[clinic start generated code]*/
288-
289-
static PyObject *
290-
_testcapi_write_unraisable_exc_impl(PyObject *module, PyObject *exc,
291-
PyObject *err_msg, PyObject *obj)
292-
/*[clinic end generated code: output=39827c5e0a8c2092 input=582498da5b2ee6cf]*/
293-
{
294-
295-
const char *err_msg_utf8;
296-
if (err_msg != Py_None) {
297-
err_msg_utf8 = PyUnicode_AsUTF8(err_msg);
298-
if (err_msg_utf8 == NULL) {
299-
return NULL;
300-
}
301-
}
302-
else {
303-
err_msg_utf8 = NULL;
304-
}
305-
306-
PyErr_SetObject((PyObject *)Py_TYPE(exc), exc);
307-
_PyErr_WriteUnraisableMsg(err_msg_utf8, obj);
308-
Py_RETURN_NONE;
309-
}
310-
311281
/*[clinic input]
312282
_testcapi.traceback_print
313283
traceback: object
@@ -384,7 +354,6 @@ static PyMethodDef test_methods[] = {
384354
_TESTCAPI_SET_EXC_INFO_METHODDEF
385355
_TESTCAPI_SET_EXCEPTION_METHODDEF
386356
_TESTCAPI_TRACEBACK_PRINT_METHODDEF
387-
_TESTCAPI_WRITE_UNRAISABLE_EXC_METHODDEF
388357
_TESTCAPI_UNSTABLE_EXC_PREP_RERAISE_STAR_METHODDEF
389358
{NULL},
390359
};

Diff for: Modules/_testinternalcapi.c

+32-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "pycore_object.h" // _PyObject_IsFreed()
2727
#include "pycore_pathconfig.h" // _PyPathConfig_ClearGlobal()
2828
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
29-
#include "pycore_pyerrors.h" // _Py_UTF8_Edit_Cost()
3029
#include "pycore_pystate.h" // _PyThreadState_GET()
3130

3231
#include "interpreteridobject.h" // PyInterpreterID_LookUp()
@@ -1448,6 +1447,37 @@ run_in_subinterp_with_config(PyObject *self, PyObject *args, PyObject *kwargs)
14481447
}
14491448

14501449

1450+
/*[clinic input]
1451+
_testinternalcapi.write_unraisable_exc
1452+
exception as exc: object
1453+
err_msg: object
1454+
obj: object
1455+
/
1456+
[clinic start generated code]*/
1457+
1458+
static PyObject *
1459+
_testinternalcapi_write_unraisable_exc_impl(PyObject *module, PyObject *exc,
1460+
PyObject *err_msg, PyObject *obj)
1461+
/*[clinic end generated code: output=a0f063cdd04aad83 input=274381b1a3fa5cd6]*/
1462+
{
1463+
1464+
const char *err_msg_utf8;
1465+
if (err_msg != Py_None) {
1466+
err_msg_utf8 = PyUnicode_AsUTF8(err_msg);
1467+
if (err_msg_utf8 == NULL) {
1468+
return NULL;
1469+
}
1470+
}
1471+
else {
1472+
err_msg_utf8 = NULL;
1473+
}
1474+
1475+
PyErr_SetObject((PyObject *)Py_TYPE(exc), exc);
1476+
_PyErr_WriteUnraisableMsg(err_msg_utf8, obj);
1477+
Py_RETURN_NONE;
1478+
}
1479+
1480+
14511481
static PyMethodDef module_functions[] = {
14521482
{"get_configs", get_configs, METH_NOARGS},
14531483
{"get_recursion_depth", get_recursion_depth, METH_NOARGS},
@@ -1503,6 +1533,7 @@ static PyMethodDef module_functions[] = {
15031533
{"run_in_subinterp_with_config",
15041534
_PyCFunction_CAST(run_in_subinterp_with_config),
15051535
METH_VARARGS | METH_KEYWORDS},
1536+
_TESTINTERNALCAPI_WRITE_UNRAISABLE_EXC_METHODDEF
15061537
{NULL, NULL} /* sentinel */
15071538
};
15081539

Diff for: Modules/_threadmodule.c

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "pycore_dict.h" // _PyDict_Pop()
88
#include "pycore_interp.h" // _PyInterpreterState.threads.count
99
#include "pycore_moduleobject.h" // _PyModule_GetState()
10+
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()
1011
#include "pycore_pylifecycle.h"
1112
#include "pycore_pystate.h" // _PyThreadState_SetCurrent()
1213
#include "pycore_sysmodule.h" // _PySys_GetAttr()

Diff for: Modules/atexitmodule.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
*/
88

99
#include "Python.h"
10-
#include "pycore_atexit.h"
10+
#include "pycore_atexit.h" // export _Py_AtExit()
1111
#include "pycore_initconfig.h" // _PyStatus_NO_MEMORY
1212
#include "pycore_interp.h" // PyInterpreterState.atexit
13+
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()
1314
#include "pycore_pystate.h" // _PyInterpreterState_GET
1415

1516
/* ===================================================================== */

Diff for: Modules/clinic/_testinternalcapi.c.h

+33-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Modules/getpath.c

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "pycore_fileutils.h" // _Py_abspath()
55
#include "pycore_initconfig.h" // _PyStatus_EXCEPTION()
66
#include "pycore_pathconfig.h" // _PyPathConfig_ReadGlobal()
7+
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()
78
#include "pycore_pymem.h" // _PyMem_RawWcsdup()
89

910
#include "marshal.h" // PyMarshal_ReadObjectFromString

Diff for: Python/compile.c

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "pycore_flowgraph.h"
3535
#include "pycore_intrinsics.h"
3636
#include "pycore_long.h" // _PyLong_GetZero()
37+
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()
3738
#include "pycore_pystate.h" // _Py_GetConfig()
3839
#include "pycore_setobject.h" // _PySet_NextEntry()
3940
#include "pycore_symtable.h" // PySTEntryObject, _PyFuture_FromAST()

Diff for: Python/perf_trampoline.c

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ any DWARF information available for them).
133133
#include "pycore_ceval.h"
134134
#include "pycore_frame.h"
135135
#include "pycore_interp.h"
136+
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()
136137

137138

138139
#ifdef PY_HAVE_PERF_TRAMPOLINE

0 commit comments

Comments
 (0)