gh-129354: Use PyErr_FormatUnraisable() function (#129518)

Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
This commit is contained in:
Victor Stinner 2025-01-31 15:06:14 +01:00 committed by GitHub
parent 5424e3b034
commit 79f85a0bc1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 46 additions and 22 deletions

View file

@ -4666,7 +4666,8 @@ _servername_callback(SSL *s, int *al, void *args)
servername_bytes = PyBytes_FromString(servername); servername_bytes = PyBytes_FromString(servername);
if (servername_bytes == NULL) { if (servername_bytes == NULL) {
PyErr_WriteUnraisable((PyObject *) sslctx); PyErr_FormatUnraisable("Exception ignored "
"in ssl servername callback");
goto error; goto error;
} }
/* server_hostname was encoded to an A-label by our caller; put it /* 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); servername_str = PyUnicode_FromEncodedObject(servername_bytes, "ascii", NULL);
if (servername_str == 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); Py_DECREF(servername_bytes);
goto error; goto error;
} }
@ -4687,7 +4691,10 @@ _servername_callback(SSL *s, int *al, void *args)
Py_DECREF(ssl_socket); Py_DECREF(ssl_socket);
if (result == NULL) { 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; *al = SSL_AD_HANDSHAKE_FAILURE;
ret = SSL_TLSEXT_ERR_ALERT_FATAL; ret = SSL_TLSEXT_ERR_ALERT_FATAL;
} }
@ -4700,7 +4707,11 @@ _servername_callback(SSL *s, int *al, void *args)
} else { } else {
*al = (int) PyLong_AsLong(result); *al = (int) PyLong_AsLong(result);
if (PyErr_Occurred()) { 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; *al = SSL_AD_INTERNAL_ERROR;
} }
ret = SSL_TLSEXT_ERR_ALERT_FATAL; ret = SSL_TLSEXT_ERR_ALERT_FATAL;
@ -5007,7 +5018,8 @@ static unsigned int psk_client_callback(SSL *s,
error: error:
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
PyErr_WriteUnraisable(callback); PyErr_FormatUnraisable("Exception ignored in ssl PSK client callback "
"while calling callback %R", callback);
} }
PyGILState_Release(gstate); PyGILState_Release(gstate);
return 0; return 0;
@ -5116,7 +5128,8 @@ static unsigned int psk_server_callback(SSL *s,
error: error:
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
PyErr_WriteUnraisable(callback); PyErr_FormatUnraisable("Exception ignored in ssl PSK server callback "
"while calling callback %R", callback);
} }
PyGILState_Release(gstate); PyGILState_Release(gstate);
return 0; return 0;

View file

@ -1539,17 +1539,20 @@ create_localsdict(localobject *self, thread_module_state *state,
goto err; goto err;
} }
if (PyDict_SetItem(self->localdicts, tstate->threading_local_key, ldict) < if (PyDict_SetItem(self->localdicts, tstate->threading_local_key,
0) { ldict) < 0)
{
goto err; goto err;
} }
wr = create_sentinel_wr(self); wr = create_sentinel_wr(self);
if (wr == NULL) { if (wr == NULL) {
PyObject *exc = PyErr_GetRaisedException(); PyObject *exc = PyErr_GetRaisedException();
if (PyDict_DelItem(self->localdicts, tstate->threading_local_key) < if (PyDict_DelItem(self->localdicts,
0) { tstate->threading_local_key) < 0)
PyErr_WriteUnraisable((PyObject *)self); {
PyErr_FormatUnraisable("Exception ignored while deleting "
"thread local of %R", self);
} }
PyErr_SetRaisedException(exc); PyErr_SetRaisedException(exc);
goto err; goto err;
@ -1557,9 +1560,11 @@ create_localsdict(localobject *self, thread_module_state *state,
if (PySet_Add(self->thread_watchdogs, wr) < 0) { if (PySet_Add(self->thread_watchdogs, wr) < 0) {
PyObject *exc = PyErr_GetRaisedException(); PyObject *exc = PyErr_GetRaisedException();
if (PyDict_DelItem(self->localdicts, tstate->threading_local_key) < if (PyDict_DelItem(self->localdicts,
0) { tstate->threading_local_key) < 0)
PyErr_WriteUnraisable((PyObject *)self); {
PyErr_FormatUnraisable("Exception ignored while deleting "
"thread local of %R", self);
} }
PyErr_SetRaisedException(exc); PyErr_SetRaisedException(exc);
goto err; 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 we create a new one the next time we do an attr
access */ access */
PyObject *exc = PyErr_GetRaisedException(); PyObject *exc = PyErr_GetRaisedException();
if (PyDict_DelItem(self->localdicts, tstate->threading_local_key) < if (PyDict_DelItem(self->localdicts,
0) { tstate->threading_local_key) < 0)
PyErr_WriteUnraisable((PyObject *)self); {
PyErr_Clear(); PyErr_FormatUnraisable("Exception ignored while deleting "
"thread local of %R", self);
assert(!PyErr_Occurred());
} }
if (PySet_Discard(self->thread_watchdogs, wr) < 0) { 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); PyErr_SetRaisedException(exc);
Py_DECREF(ldict); Py_DECREF(ldict);
@ -1746,12 +1754,14 @@ clear_locals(PyObject *locals_and_key, PyObject *dummyweakref)
if (self->localdicts != NULL) { if (self->localdicts != NULL) {
PyObject *key = PyTuple_GetItem(locals_and_key, 1); PyObject *key = PyTuple_GetItem(locals_and_key, 1);
if (PyDict_Pop(self->localdicts, key, NULL) < 0) { 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 (self->thread_watchdogs != NULL) {
if (PySet_Discard(self->thread_watchdogs, dummyweakref) < 0) { 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 // Wait for the thread to finish. If we're interrupted, such
// as by a ctrl-c we print the error and exit early. // as by a ctrl-c we print the error and exit early.
if (ThreadHandle_join(handle, -1) < 0) { if (ThreadHandle_join(handle, -1) < 0) {
PyErr_WriteUnraisable(NULL); PyErr_FormatUnraisable("Exception ignored while joining a thread "
"in _thread._shutdown()");
ThreadHandle_decref(handle); ThreadHandle_decref(handle);
Py_RETURN_NONE; Py_RETURN_NONE;
} }