Skip to content

Commit f3865b0

Browse files
committed
pythongh-129354: Use PyErr_FormatUnraisable() function
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
1 parent 3447f4a commit f3865b0

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

Modules/_ssl.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4666,15 +4666,18 @@ _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 in ssl servername "
4679+
"callback while decoding name %R",
4680+
servername_bytes);
46784681
Py_DECREF(servername_bytes);
46794682
goto error;
46804683
}
@@ -4687,7 +4690,9 @@ _servername_callback(SSL *s, int *al, void *args)
46874690
Py_DECREF(ssl_socket);
46884691

46894692
if (result == NULL) {
4690-
PyErr_WriteUnraisable(sslctx->set_sni_cb);
4693+
PyErr_FormatUnraisable("Exception ignored in ssl servername "
4694+
"callback while calling set SNI callback %R",
4695+
sslctx->set_sni_cb);
46914696
*al = SSL_AD_HANDSHAKE_FAILURE;
46924697
ret = SSL_TLSEXT_ERR_ALERT_FATAL;
46934698
}
@@ -4700,7 +4705,10 @@ _servername_callback(SSL *s, int *al, void *args)
47004705
} else {
47014706
*al = (int) PyLong_AsLong(result);
47024707
if (PyErr_Occurred()) {
4703-
PyErr_WriteUnraisable(result);
4708+
PyErr_FormatUnraisable("Exception ignored in ssl servername "
4709+
"callback while calling set SNI callback "
4710+
"(result=%R)",
4711+
result);
47044712
*al = SSL_AD_INTERNAL_ERROR;
47054713
}
47064714
ret = SSL_TLSEXT_ERR_ALERT_FATAL;
@@ -5007,7 +5015,8 @@ static unsigned int psk_client_callback(SSL *s,
50075015

50085016
error:
50095017
if (PyErr_Occurred()) {
5010-
PyErr_WriteUnraisable(callback);
5018+
PyErr_FormatUnraisable("Exception ignored in ssl PSK client callback "
5019+
"while calling callback %R", callback);
50115020
}
50125021
PyGILState_Release(gstate);
50135022
return 0;
@@ -5116,7 +5125,8 @@ static unsigned int psk_server_callback(SSL *s,
51165125

51175126
error:
51185127
if (PyErr_Occurred()) {
5119-
PyErr_WriteUnraisable(callback);
5128+
PyErr_FormatUnraisable("Exception ignored in ssl PSK server callback "
5129+
"while calling callback %R", callback);
51205130
}
51215131
PyGILState_Release(gstate);
51225132
return 0;

Modules/_threadmodule.c

Lines changed: 27 additions & 16 deletions
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)