Skip to content

Commit

Permalink
pythongh-129354: Use PyErr_FormatUnraisable() function
Browse files Browse the repository at this point in the history
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
  • Loading branch information
vstinner committed Jan 31, 2025
1 parent 3447f4a commit bb514fe
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
25 changes: 19 additions & 6 deletions Modules/_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -4666,15 +4666,19 @@ _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
* back into a str object, but still as an A-label (bpo-28414)
*/
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;
}
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
43 changes: 27 additions & 16 deletions Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1539,27 +1539,32 @@ 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;
}

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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit bb514fe

Please sign in to comment.