Skip to content

Commit 79f85a0

Browse files
authored
gh-129354: Use PyErr_FormatUnraisable() function (#129518)
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
1 parent 5424e3b commit 79f85a0

File tree

2 files changed

+46
-22
lines changed

2 files changed

+46
-22
lines changed

Modules/_ssl.c

+19-6
Original file line numberDiff line numberDiff line change
@@ -4666,15 +4666,19 @@ _servername_callback(SSL *s, int *al, void *args)
46664666

46674667
servername_bytes = PyBytes_FromString(servername);
46684668
if (servername_bytes == NULL) {
4669-
PyErr_WriteUnraisable((PyObject *) sslctx);
4669+
PyErr_FormatUnraisable("Exception ignored "
4670+
"in ssl servername callback");
46704671
goto error;
46714672
}
46724673
/* server_hostname was encoded to an A-label by our caller; put it
46734674
* back into a str object, but still as an A-label (bpo-28414)
46744675
*/
46754676
servername_str = PyUnicode_FromEncodedObject(servername_bytes, "ascii", NULL);
46764677
if (servername_str == NULL) {
4677-
PyErr_WriteUnraisable(servername_bytes);
4678+
PyErr_FormatUnraisable("Exception ignored "
4679+
"in ssl servername callback "
4680+
"while decoding name %R",
4681+
servername_bytes);
46784682
Py_DECREF(servername_bytes);
46794683
goto error;
46804684
}
@@ -4687,7 +4691,10 @@ _servername_callback(SSL *s, int *al, void *args)
46874691
Py_DECREF(ssl_socket);
46884692

46894693
if (result == NULL) {
4690-
PyErr_WriteUnraisable(sslctx->set_sni_cb);
4694+
PyErr_FormatUnraisable("Exception ignored "
4695+
"in ssl servername callback "
4696+
"while calling set SNI callback %R",
4697+
sslctx->set_sni_cb);
46914698
*al = SSL_AD_HANDSHAKE_FAILURE;
46924699
ret = SSL_TLSEXT_ERR_ALERT_FATAL;
46934700
}
@@ -4700,7 +4707,11 @@ _servername_callback(SSL *s, int *al, void *args)
47004707
} else {
47014708
*al = (int) PyLong_AsLong(result);
47024709
if (PyErr_Occurred()) {
4703-
PyErr_WriteUnraisable(result);
4710+
PyErr_FormatUnraisable("Exception ignored "
4711+
"in ssl servername callback "
4712+
"while calling set SNI callback "
4713+
"(result=%R)",
4714+
result);
47044715
*al = SSL_AD_INTERNAL_ERROR;
47054716
}
47064717
ret = SSL_TLSEXT_ERR_ALERT_FATAL;
@@ -5007,7 +5018,8 @@ static unsigned int psk_client_callback(SSL *s,
50075018

50085019
error:
50095020
if (PyErr_Occurred()) {
5010-
PyErr_WriteUnraisable(callback);
5021+
PyErr_FormatUnraisable("Exception ignored in ssl PSK client callback "
5022+
"while calling callback %R", callback);
50115023
}
50125024
PyGILState_Release(gstate);
50135025
return 0;
@@ -5116,7 +5128,8 @@ static unsigned int psk_server_callback(SSL *s,
51165128

51175129
error:
51185130
if (PyErr_Occurred()) {
5119-
PyErr_WriteUnraisable(callback);
5131+
PyErr_FormatUnraisable("Exception ignored in ssl PSK server callback "
5132+
"while calling callback %R", callback);
51205133
}
51215134
PyGILState_Release(gstate);
51225135
return 0;

Modules/_threadmodule.c

+27-16
Original file line numberDiff line numberDiff line change
@@ -1539,27 +1539,32 @@ create_localsdict(localobject *self, thread_module_state *state,
15391539
goto err;
15401540
}
15411541

1542-
if (PyDict_SetItem(self->localdicts, tstate->threading_local_key, ldict) <
1543-
0) {
1542+
if (PyDict_SetItem(self->localdicts, tstate->threading_local_key,
1543+
ldict) < 0)
1544+
{
15441545
goto err;
15451546
}
15461547

15471548
wr = create_sentinel_wr(self);
15481549
if (wr == NULL) {
15491550
PyObject *exc = PyErr_GetRaisedException();
1550-
if (PyDict_DelItem(self->localdicts, tstate->threading_local_key) <
1551-
0) {
1552-
PyErr_WriteUnraisable((PyObject *)self);
1551+
if (PyDict_DelItem(self->localdicts,
1552+
tstate->threading_local_key) < 0)
1553+
{
1554+
PyErr_FormatUnraisable("Exception ignored while deleting "
1555+
"thread local of %R", self);
15531556
}
15541557
PyErr_SetRaisedException(exc);
15551558
goto err;
15561559
}
15571560

15581561
if (PySet_Add(self->thread_watchdogs, wr) < 0) {
15591562
PyObject *exc = PyErr_GetRaisedException();
1560-
if (PyDict_DelItem(self->localdicts, tstate->threading_local_key) <
1561-
0) {
1562-
PyErr_WriteUnraisable((PyObject *)self);
1563+
if (PyDict_DelItem(self->localdicts,
1564+
tstate->threading_local_key) < 0)
1565+
{
1566+
PyErr_FormatUnraisable("Exception ignored while deleting "
1567+
"thread local of %R", self);
15631568
}
15641569
PyErr_SetRaisedException(exc);
15651570
goto err;
@@ -1609,13 +1614,16 @@ _ldict(localobject *self, thread_module_state *state)
16091614
we create a new one the next time we do an attr
16101615
access */
16111616
PyObject *exc = PyErr_GetRaisedException();
1612-
if (PyDict_DelItem(self->localdicts, tstate->threading_local_key) <
1613-
0) {
1614-
PyErr_WriteUnraisable((PyObject *)self);
1615-
PyErr_Clear();
1617+
if (PyDict_DelItem(self->localdicts,
1618+
tstate->threading_local_key) < 0)
1619+
{
1620+
PyErr_FormatUnraisable("Exception ignored while deleting "
1621+
"thread local of %R", self);
1622+
assert(!PyErr_Occurred());
16161623
}
16171624
if (PySet_Discard(self->thread_watchdogs, wr) < 0) {
1618-
PyErr_WriteUnraisable((PyObject *)self);
1625+
PyErr_FormatUnraisable("Exception ignored while discarding "
1626+
"thread watchdog of %R", self);
16191627
}
16201628
PyErr_SetRaisedException(exc);
16211629
Py_DECREF(ldict);
@@ -1746,12 +1754,14 @@ clear_locals(PyObject *locals_and_key, PyObject *dummyweakref)
17461754
if (self->localdicts != NULL) {
17471755
PyObject *key = PyTuple_GetItem(locals_and_key, 1);
17481756
if (PyDict_Pop(self->localdicts, key, NULL) < 0) {
1749-
PyErr_WriteUnraisable((PyObject*)self);
1757+
PyErr_FormatUnraisable("Exception ignored while clearing "
1758+
"thread local %R", (PyObject *)self);
17501759
}
17511760
}
17521761
if (self->thread_watchdogs != NULL) {
17531762
if (PySet_Discard(self->thread_watchdogs, dummyweakref) < 0) {
1754-
PyErr_WriteUnraisable((PyObject *)self);
1763+
PyErr_FormatUnraisable("Exception ignored while clearing "
1764+
"thread local %R", (PyObject *)self);
17551765
}
17561766
}
17571767

@@ -2314,7 +2324,8 @@ thread_shutdown(PyObject *self, PyObject *args)
23142324
// Wait for the thread to finish. If we're interrupted, such
23152325
// as by a ctrl-c we print the error and exit early.
23162326
if (ThreadHandle_join(handle, -1) < 0) {
2317-
PyErr_WriteUnraisable(NULL);
2327+
PyErr_FormatUnraisable("Exception ignored while joining a thread "
2328+
"in _thread._shutdown()");
23182329
ThreadHandle_decref(handle);
23192330
Py_RETURN_NONE;
23202331
}

0 commit comments

Comments
 (0)