]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-106320: Remove private _PyErr_WriteUnraisableMsg() (#108863)
authorVictor Stinner <vstinner@python.org>
Mon, 4 Sep 2023 23:54:55 +0000 (01:54 +0200)
committerGitHub <noreply@github.com>
Mon, 4 Sep 2023 23:54:55 +0000 (01:54 +0200)
Move the private _PyErr_WriteUnraisableMsg() functions to the
internal C API (pycore_pyerrors.h).

Move write_unraisable_exc() from _testcapi to _testinternalcapi.

16 files changed:
Include/cpython/pyerrors.h
Include/internal/pycore_pyerrors.h
Lib/test/audit-tests.py
Lib/test/test_sys.py
Modules/_ctypes/_ctypes.c
Modules/_ctypes/callbacks.c
Modules/_lsprof.c
Modules/_testcapi/clinic/exceptions.c.h
Modules/_testcapi/exceptions.c
Modules/_testinternalcapi.c
Modules/_threadmodule.c
Modules/atexitmodule.c
Modules/clinic/_testinternalcapi.c.h
Modules/getpath.c
Python/compile.c
Python/perf_trampoline.c

index 9633a5407f28a615996f1f927a0970aaec45c20e..da96eec4b35aabd6a03d73e97dc9e256f042e7a1 100644 (file)
@@ -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);
index 0f16fb894d17e12791c08bb897157ff75835206b..184eb35e52b47b0603329ef19f6edad0c60b5ae7 100644 (file)
@@ -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
index ad8f72f556331da105264fe3cf7e80e148dedc98..f14c2635d39390074ac4bea40001cc5eb9d12e8c 100644 (file)
@@ -289,7 +289,7 @@ def test_excepthook():
 
 
 def test_unraisablehook():
-    from _testcapi import write_unraisable_exc
+    from _testinternalcapi import write_unraisable_exc
 
     def unraisablehook(hookargs):
         pass
index d8b684c8a008f09fd740081b3fcfd61bc480f0ce..e8a99244a3a28de5c9e8acfc04c9c412c8566c41 100644 (file)
@@ -1199,11 +1199,11 @@ class SysModuleTest(unittest.TestCase):
 @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__,
index 3af3a80bfb5e95ade206ca179afae0b766947123..184af2132c270782e0e3a4b9fa19159c8dcb5db4 100644 (file)
@@ -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>
index 0d8ecce009a67a206a0a02b0e5ace59d54606058..1bd8fec97179e99a9aebb5d6aef52a32d5132e0c 100644 (file)
@@ -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>
 
index e7dcb6e171321226ca99783888114c68f1737a38..d23a756ace887d1347e8e471f2c5cf6cf1b5eb10 100644 (file)
@@ -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"
 
 /************************************************************/
index 01881534329c9d066468be10f3e50ca08306212a..39b5f8b91a00db0d93de7c1d2155c758a23b5d14 100644 (file)
@@ -394,38 +394,6 @@ PyDoc_STRVAR(_testcapi_set_exception__doc__,
 #define _TESTCAPI_SET_EXCEPTION_METHODDEF    \
     {"set_exception", (PyCFunction)_testcapi_set_exception, METH_O, _testcapi_set_exception__doc__},
 
-PyDoc_STRVAR(_testcapi_write_unraisable_exc__doc__,
-"write_unraisable_exc($module, exception, err_msg, obj, /)\n"
-"--\n"
-"\n");
-
-#define _TESTCAPI_WRITE_UNRAISABLE_EXC_METHODDEF    \
-    {"write_unraisable_exc", _PyCFunction_CAST(_testcapi_write_unraisable_exc), METH_FASTCALL, _testcapi_write_unraisable_exc__doc__},
-
-static PyObject *
-_testcapi_write_unraisable_exc_impl(PyObject *module, PyObject *exc,
-                                    PyObject *err_msg, PyObject *obj);
-
-static PyObject *
-_testcapi_write_unraisable_exc(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
-{
-    PyObject *return_value = NULL;
-    PyObject *exc;
-    PyObject *err_msg;
-    PyObject *obj;
-
-    if (!_PyArg_CheckPositional("write_unraisable_exc", nargs, 3, 3)) {
-        goto exit;
-    }
-    exc = args[0];
-    err_msg = args[1];
-    obj = args[2];
-    return_value = _testcapi_write_unraisable_exc_impl(module, exc, err_msg, obj);
-
-exit:
-    return return_value;
-}
-
 PyDoc_STRVAR(_testcapi_traceback_print__doc__,
 "traceback_print($module, traceback, file, /)\n"
 "--\n"
@@ -487,4 +455,4 @@ _testcapi_unstable_exc_prep_reraise_star(PyObject *module, PyObject *const *args
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=8f273949da28ffb5 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=ff19512450b3bbdb input=a9049054013a1b77]*/
index 025b42db247e81526c6a303921480901b1e65350..b1388d75711774bf9034193d87624c2959b89ab0 100644 (file)
@@ -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
@@ -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},
 };
index 96d568294a9710d93e9ff78465e8d01dc566a51a..b6792e38fa98c26091c251d8c6cd1de1798eadf4 100644 (file)
@@ -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()
@@ -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},
@@ -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 */
 };
 
index 2cf866c5a114160ceca686efa3d3043754e28149..49f34fcb9feb70f84c0e481ce107381e717cd34b 100644 (file)
@@ -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()
index cec177cfc2f9c8299c928bfc276587e21b4b3863..57e2ea67450453c0c8500a6d8378a3fd8d1bc584 100644 (file)
@@ -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
 
 /* ===================================================================== */
index 28761387023508ecabad6fe5f626efaaaf7b054e..38a3579d7dec77b0c0b31f5839ba65e1a9f5305b 100644 (file)
@@ -264,4 +264,36 @@ _testinternalcapi_assemble_code_object(PyObject *module, PyObject *const *args,
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=cba1c94ff4015b82 input=a9049054013a1b77]*/
+
+PyDoc_STRVAR(_testinternalcapi_write_unraisable_exc__doc__,
+"write_unraisable_exc($module, exception, err_msg, obj, /)\n"
+"--\n"
+"\n");
+
+#define _TESTINTERNALCAPI_WRITE_UNRAISABLE_EXC_METHODDEF    \
+    {"write_unraisable_exc", _PyCFunction_CAST(_testinternalcapi_write_unraisable_exc), METH_FASTCALL, _testinternalcapi_write_unraisable_exc__doc__},
+
+static PyObject *
+_testinternalcapi_write_unraisable_exc_impl(PyObject *module, PyObject *exc,
+                                            PyObject *err_msg, PyObject *obj);
+
+static PyObject *
+_testinternalcapi_write_unraisable_exc(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
+{
+    PyObject *return_value = NULL;
+    PyObject *exc;
+    PyObject *err_msg;
+    PyObject *obj;
+
+    if (!_PyArg_CheckPositional("write_unraisable_exc", nargs, 3, 3)) {
+        goto exit;
+    }
+    exc = args[0];
+    err_msg = args[1];
+    obj = args[2];
+    return_value = _testinternalcapi_write_unraisable_exc_impl(module, exc, err_msg, obj);
+
+exit:
+    return return_value;
+}
+/*[clinic end generated code: output=c7156622e80df1ce input=a9049054013a1b77]*/
index 71e23e1edaf11c3b70b5f7c5e12dec4648a61306..3b926cac0d3f24ec2ebf30006ececad6da098500 100644 (file)
@@ -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
index 50e29b4607f7ce219f713d962b8ba86158d8ed1d..ae9edc90ea436762839c199fa16b3b408dfc9d6a 100644 (file)
@@ -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()
index 10675bf9f8292a65b6d1e6ccc6333fc2936c8c01..0f1af30226d9fb50e0cc10ab223948c488a20525 100644 (file)
@@ -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