Skip to content

Commit

Permalink
pythongh-106320: Remove private _PyErr_WriteUnraisableMsg() (python#1…
Browse files Browse the repository at this point in the history
…08863)

Move the private _PyErr_WriteUnraisableMsg() functions to the
internal C API (pycore_pyerrors.h).

Move write_unraisable_exc() from _testcapi to _testinternalcapi.
  • Loading branch information
vstinner authored Sep 4, 2023
1 parent 6304d98 commit 6765938
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 77 deletions.
4 changes: 0 additions & 4 deletions Include/cpython/pyerrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ PyAPI_FUNC(PyObject *) PyErr_ProgramTextObject(
PyObject *filename,
int lineno);

PyAPI_FUNC(void) _PyErr_WriteUnraisableMsg(
const char *err_msg,
PyObject *obj);

PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalErrorFunc(
const char *func,
const char *message);
Expand Down
5 changes: 5 additions & 0 deletions Include/internal/pycore_pyerrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ Py_DEPRECATED(3.12) extern void _PyErr_ChainExceptions(PyObject *, PyObject *, P
// Export for '_zoneinfo' shared extension
PyAPI_FUNC(void) _PyErr_ChainExceptions1(PyObject *);

// Export for '_lsprof' shared extension
PyAPI_FUNC(void) _PyErr_WriteUnraisableMsg(
const char *err_msg,
PyObject *obj);

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/audit-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def hook(event, args):


def test_unraisablehook():
from _testcapi import write_unraisable_exc
from _testinternalcapi import write_unraisable_exc

def unraisablehook(hookargs):
pass
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1199,11 +1199,11 @@ class MyType:
@test.support.cpython_only
class UnraisableHookTest(unittest.TestCase):
def write_unraisable_exc(self, exc, err_msg, obj):
import _testcapi
import _testinternalcapi
import types
err_msg2 = f"Exception ignored {err_msg}"
try:
_testcapi.write_unraisable_exc(exc, err_msg, obj)
_testinternalcapi.write_unraisable_exc(exc, err_msg, obj)
return types.SimpleNamespace(exc_type=type(exc),
exc_value=exc,
exc_traceback=exc.__traceback__,
Expand Down
1 change: 1 addition & 0 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ bytes(cdata)

#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()


#include <ffi.h>
Expand Down
6 changes: 3 additions & 3 deletions Modules/_ctypes/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
# include <windows.h>
#endif

#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_runtime.h" // _PyRuntime
#include "pycore_global_objects.h" // _Py_ID()
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()
#include "pycore_runtime.h" // _Py_ID()

#include <stdbool.h>

Expand Down
2 changes: 2 additions & 0 deletions Modules/_lsprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include "Python.h"
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_ceval.h" // _PyEval_SetProfile()
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()
#include "pycore_pystate.h" // _PyThreadState_GET()

#include "rotatingtree.h"

/************************************************************/
Expand Down
34 changes: 1 addition & 33 deletions Modules/_testcapi/clinic/exceptions.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 0 additions & 31 deletions Modules/_testcapi/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,36 +278,6 @@ _testcapi_set_exception(PyObject *module, PyObject *new_exc)
return exc;
}

/*[clinic input]
_testcapi.write_unraisable_exc
exception as exc: object
err_msg: object
obj: object
/
[clinic start generated code]*/

static PyObject *
_testcapi_write_unraisable_exc_impl(PyObject *module, PyObject *exc,
PyObject *err_msg, PyObject *obj)
/*[clinic end generated code: output=39827c5e0a8c2092 input=582498da5b2ee6cf]*/
{

const char *err_msg_utf8;
if (err_msg != Py_None) {
err_msg_utf8 = PyUnicode_AsUTF8(err_msg);
if (err_msg_utf8 == NULL) {
return NULL;
}
}
else {
err_msg_utf8 = NULL;
}

PyErr_SetObject((PyObject *)Py_TYPE(exc), exc);
_PyErr_WriteUnraisableMsg(err_msg_utf8, obj);
Py_RETURN_NONE;
}

/*[clinic input]
_testcapi.traceback_print
traceback: object
Expand Down Expand Up @@ -384,7 +354,6 @@ static PyMethodDef test_methods[] = {
_TESTCAPI_SET_EXC_INFO_METHODDEF
_TESTCAPI_SET_EXCEPTION_METHODDEF
_TESTCAPI_TRACEBACK_PRINT_METHODDEF
_TESTCAPI_WRITE_UNRAISABLE_EXC_METHODDEF
_TESTCAPI_UNSTABLE_EXC_PREP_RERAISE_STAR_METHODDEF
{NULL},
};
Expand Down
33 changes: 32 additions & 1 deletion Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "pycore_object.h" // _PyObject_IsFreed()
#include "pycore_pathconfig.h" // _PyPathConfig_ClearGlobal()
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
#include "pycore_pyerrors.h" // _Py_UTF8_Edit_Cost()
#include "pycore_pystate.h" // _PyThreadState_GET()

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


/*[clinic input]
_testinternalcapi.write_unraisable_exc
exception as exc: object
err_msg: object
obj: object
/
[clinic start generated code]*/

static PyObject *
_testinternalcapi_write_unraisable_exc_impl(PyObject *module, PyObject *exc,
PyObject *err_msg, PyObject *obj)
/*[clinic end generated code: output=a0f063cdd04aad83 input=274381b1a3fa5cd6]*/
{

const char *err_msg_utf8;
if (err_msg != Py_None) {
err_msg_utf8 = PyUnicode_AsUTF8(err_msg);
if (err_msg_utf8 == NULL) {
return NULL;
}
}
else {
err_msg_utf8 = NULL;
}

PyErr_SetObject((PyObject *)Py_TYPE(exc), exc);
_PyErr_WriteUnraisableMsg(err_msg_utf8, obj);
Py_RETURN_NONE;
}


static PyMethodDef module_functions[] = {
{"get_configs", get_configs, METH_NOARGS},
{"get_recursion_depth", get_recursion_depth, METH_NOARGS},
Expand Down Expand Up @@ -1503,6 +1533,7 @@ static PyMethodDef module_functions[] = {
{"run_in_subinterp_with_config",
_PyCFunction_CAST(run_in_subinterp_with_config),
METH_VARARGS | METH_KEYWORDS},
_TESTINTERNALCAPI_WRITE_UNRAISABLE_EXC_METHODDEF
{NULL, NULL} /* sentinel */
};

Expand Down
1 change: 1 addition & 0 deletions Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "pycore_dict.h" // _PyDict_Pop()
#include "pycore_interp.h" // _PyInterpreterState.threads.count
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()
#include "pycore_pylifecycle.h"
#include "pycore_pystate.h" // _PyThreadState_SetCurrent()
#include "pycore_sysmodule.h" // _PySys_GetAttr()
Expand Down
3 changes: 2 additions & 1 deletion Modules/atexitmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
*/

#include "Python.h"
#include "pycore_atexit.h"
#include "pycore_atexit.h" // export _Py_AtExit()
#include "pycore_initconfig.h" // _PyStatus_NO_MEMORY
#include "pycore_interp.h" // PyInterpreterState.atexit
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()
#include "pycore_pystate.h" // _PyInterpreterState_GET

/* ===================================================================== */
Expand Down
34 changes: 33 additions & 1 deletion Modules/clinic/_testinternalcapi.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Modules/getpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "pycore_fileutils.h" // _Py_abspath()
#include "pycore_initconfig.h" // _PyStatus_EXCEPTION()
#include "pycore_pathconfig.h" // _PyPathConfig_ReadGlobal()
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()
#include "pycore_pymem.h" // _PyMem_RawWcsdup()

#include "marshal.h" // PyMarshal_ReadObjectFromString
Expand Down
1 change: 1 addition & 0 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "pycore_flowgraph.h"
#include "pycore_intrinsics.h"
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()
#include "pycore_pystate.h" // _Py_GetConfig()
#include "pycore_setobject.h" // _PySet_NextEntry()
#include "pycore_symtable.h" // PySTEntryObject, _PyFuture_FromAST()
Expand Down
1 change: 1 addition & 0 deletions Python/perf_trampoline.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ any DWARF information available for them).
#include "pycore_ceval.h"
#include "pycore_frame.h"
#include "pycore_interp.h"
#include "pycore_pyerrors.h" // _PyErr_WriteUnraisableMsg()


#ifdef PY_HAVE_PERF_TRAMPOLINE
Expand Down

0 comments on commit 6765938

Please sign in to comment.