]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-129354: Use PyErr_FormatUnraisable() function (#129518)
authorVictor Stinner <vstinner@python.org>
Fri, 31 Jan 2025 14:06:14 +0000 (15:06 +0100)
committerGitHub <noreply@github.com>
Fri, 31 Jan 2025 14:06:14 +0000 (15:06 +0100)
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().

Modules/_ssl.c
Modules/_threadmodule.c

index c15a582a92aa4a01a3bbf76d73407d7c9fa123e4..85e917fbbb709388615d3633f83a9ac893edd101 100644 (file)
@@ -4666,7 +4666,8 @@ _servername_callback(SSL *s, int *al, void *args)
 
         servername_bytes = PyBytes_FromString(servername);
         if (servername_bytes == NULL) {
-            PyErr_WriteUnraisable((PyObject *) sslctx);
+            PyErr_FormatUnraisable("Exception ignored "
+                                   "in ssl servername callback");
             goto error;
         }
         /* server_hostname was encoded to an A-label by our caller; put it
@@ -4674,7 +4675,10 @@ _servername_callback(SSL *s, int *al, void *args)
          */
         servername_str = PyUnicode_FromEncodedObject(servername_bytes, "ascii", NULL);
         if (servername_str == NULL) {
-            PyErr_WriteUnraisable(servername_bytes);
+            PyErr_FormatUnraisable("Exception ignored "
+                                   "in ssl servername callback "
+                                   "while decoding name %R",
+                                   servername_bytes);
             Py_DECREF(servername_bytes);
             goto error;
         }
@@ -4687,7 +4691,10 @@ _servername_callback(SSL *s, int *al, void *args)
     Py_DECREF(ssl_socket);
 
     if (result == NULL) {
-        PyErr_WriteUnraisable(sslctx->set_sni_cb);
+        PyErr_FormatUnraisable("Exception ignored "
+                               "in ssl servername callback "
+                               "while calling set SNI callback %R",
+                               sslctx->set_sni_cb);
         *al = SSL_AD_HANDSHAKE_FAILURE;
         ret = SSL_TLSEXT_ERR_ALERT_FATAL;
     }
@@ -4700,7 +4707,11 @@ _servername_callback(SSL *s, int *al, void *args)
         } else {
             *al = (int) PyLong_AsLong(result);
             if (PyErr_Occurred()) {
-                PyErr_WriteUnraisable(result);
+                PyErr_FormatUnraisable("Exception ignored "
+                                       "in ssl servername callback "
+                                       "while calling set SNI callback "
+                                       "(result=%R)",
+                                       result);
                 *al = SSL_AD_INTERNAL_ERROR;
             }
             ret = SSL_TLSEXT_ERR_ALERT_FATAL;
@@ -5007,7 +5018,8 @@ static unsigned int psk_client_callback(SSL *s,
 
 error:
     if (PyErr_Occurred()) {
-        PyErr_WriteUnraisable(callback);
+        PyErr_FormatUnraisable("Exception ignored in ssl PSK client callback "
+                               "while calling callback %R", callback);
     }
     PyGILState_Release(gstate);
     return 0;
@@ -5116,7 +5128,8 @@ static unsigned int psk_server_callback(SSL *s,
 
 error:
     if (PyErr_Occurred()) {
-        PyErr_WriteUnraisable(callback);
+        PyErr_FormatUnraisable("Exception ignored in ssl PSK server callback "
+                               "while calling callback %R", callback);
     }
     PyGILState_Release(gstate);
     return 0;
index dbc574f7816b85cc9ece64d597c2a1f9caaab44b..e251736fb36aa985957f108ca2c5241498f8c525 100644 (file)
@@ -1539,17 +1539,20 @@ create_localsdict(localobject *self, thread_module_state *state,
         goto err;
     }
 
-    if (PyDict_SetItem(self->localdicts, tstate->threading_local_key, ldict) <
-        0) {
+    if (PyDict_SetItem(self->localdicts, tstate->threading_local_key,
+                       ldict) < 0)
+    {
         goto err;
     }
 
     wr = create_sentinel_wr(self);
     if (wr == NULL) {
         PyObject *exc = PyErr_GetRaisedException();
-        if (PyDict_DelItem(self->localdicts, tstate->threading_local_key) <
-            0) {
-            PyErr_WriteUnraisable((PyObject *)self);
+        if (PyDict_DelItem(self->localdicts,
+                           tstate->threading_local_key) < 0)
+        {
+            PyErr_FormatUnraisable("Exception ignored while deleting "
+                                   "thread local of %R", self);
         }
         PyErr_SetRaisedException(exc);
         goto err;
@@ -1557,9 +1560,11 @@ create_localsdict(localobject *self, thread_module_state *state,
 
     if (PySet_Add(self->thread_watchdogs, wr) < 0) {
         PyObject *exc = PyErr_GetRaisedException();
-        if (PyDict_DelItem(self->localdicts, tstate->threading_local_key) <
-            0) {
-            PyErr_WriteUnraisable((PyObject *)self);
+        if (PyDict_DelItem(self->localdicts,
+                           tstate->threading_local_key) < 0)
+        {
+            PyErr_FormatUnraisable("Exception ignored while deleting "
+                                   "thread local of %R", self);
         }
         PyErr_SetRaisedException(exc);
         goto err;
@@ -1609,13 +1614,16 @@ _ldict(localobject *self, thread_module_state *state)
            we create a new one the next time we do an attr
            access */
         PyObject *exc = PyErr_GetRaisedException();
-        if (PyDict_DelItem(self->localdicts, tstate->threading_local_key) <
-            0) {
-            PyErr_WriteUnraisable((PyObject *)self);
-            PyErr_Clear();
+        if (PyDict_DelItem(self->localdicts,
+                           tstate->threading_local_key) < 0)
+        {
+            PyErr_FormatUnraisable("Exception ignored while deleting "
+                                   "thread local of %R", self);
+            assert(!PyErr_Occurred());
         }
         if (PySet_Discard(self->thread_watchdogs, wr) < 0) {
-            PyErr_WriteUnraisable((PyObject *)self);
+            PyErr_FormatUnraisable("Exception ignored while discarding "
+                                   "thread watchdog of %R", self);
         }
         PyErr_SetRaisedException(exc);
         Py_DECREF(ldict);
@@ -1746,12 +1754,14 @@ clear_locals(PyObject *locals_and_key, PyObject *dummyweakref)
     if (self->localdicts != NULL) {
         PyObject *key = PyTuple_GetItem(locals_and_key, 1);
         if (PyDict_Pop(self->localdicts, key, NULL) < 0) {
-            PyErr_WriteUnraisable((PyObject*)self);
+            PyErr_FormatUnraisable("Exception ignored while clearing "
+                                   "thread local %R", (PyObject *)self);
         }
     }
     if (self->thread_watchdogs != NULL) {
         if (PySet_Discard(self->thread_watchdogs, dummyweakref) < 0) {
-            PyErr_WriteUnraisable((PyObject *)self);
+            PyErr_FormatUnraisable("Exception ignored while clearing "
+                                   "thread local %R", (PyObject *)self);
         }
     }
 
@@ -2314,7 +2324,8 @@ thread_shutdown(PyObject *self, PyObject *args)
         // Wait for the thread to finish. If we're interrupted, such
         // as by a ctrl-c we print the error and exit early.
         if (ThreadHandle_join(handle, -1) < 0) {
-            PyErr_WriteUnraisable(NULL);
+            PyErr_FormatUnraisable("Exception ignored while joining a thread "
+                                   "in _thread._shutdown()");
             ThreadHandle_decref(handle);
             Py_RETURN_NONE;
         }