Skip to content

Commit efdf49c

Browse files
committed
gh-129354: Use PyErr_FormatUnraisable() function
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
1 parent 8df5193 commit efdf49c

File tree

5 files changed

+40
-27
lines changed

5 files changed

+40
-27
lines changed

Modules/_ctypes/_ctypes.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,8 @@ CType_Type_traverse(PyObject *self, visitproc visit, void *arg)
464464
{
465465
StgInfo *info = _PyStgInfo_FromType_NoState(self);
466466
if (!info) {
467-
PyErr_WriteUnraisable(self);
467+
PyErr_FormatUnraisable("Exception ignored while "
468+
"calling ctypes traverse function %R", self);
468469
}
469470
if (info) {
470471
Py_VISIT(info->proto);
@@ -495,7 +496,8 @@ CType_Type_clear(PyObject *self)
495496
{
496497
StgInfo *info = _PyStgInfo_FromType_NoState(self);
497498
if (!info) {
498-
PyErr_WriteUnraisable(self);
499+
PyErr_FormatUnraisable("Exception ignored while "
500+
"clearing ctypes %R", self);
499501
}
500502
if (info) {
501503
ctype_clear_stginfo(info);
@@ -508,7 +510,8 @@ CType_Type_dealloc(PyObject *self)
508510
{
509511
StgInfo *info = _PyStgInfo_FromType_NoState(self);
510512
if (!info) {
511-
PyErr_WriteUnraisable(NULL); // NULL avoids segfault here
513+
PyErr_FormatUnraisable("Exception ignored while "
514+
"deallocating ctypes %R", self);
512515
}
513516
if (info) {
514517
PyMem_Free(info->ffi_type_pointer.elements);

Modules/_ctypes/callbacks.c

+21-18
Original file line numberDiff line numberDiff line change
@@ -494,32 +494,28 @@ long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
494494

495495
func = PyImport_ImportModuleAttrString("ctypes", "DllGetClassObject");
496496
if (!func) {
497-
PyErr_WriteUnraisable(context ? context : Py_None);
498497
/* There has been a warning before about this already */
499-
return E_FAIL;
498+
goto error;
500499
}
501500

502501
{
503502
PyObject *py_rclsid = PyLong_FromVoidPtr((void *)rclsid);
504503
if (py_rclsid == NULL) {
505504
Py_DECREF(func);
506-
PyErr_WriteUnraisable(context ? context : Py_None);
507-
return E_FAIL;
505+
goto error;
508506
}
509507
PyObject *py_riid = PyLong_FromVoidPtr((void *)riid);
510508
if (py_riid == NULL) {
511509
Py_DECREF(func);
512510
Py_DECREF(py_rclsid);
513-
PyErr_WriteUnraisable(context ? context : Py_None);
514-
return E_FAIL;
511+
goto error;
515512
}
516513
PyObject *py_ppv = PyLong_FromVoidPtr(ppv);
517514
if (py_ppv == NULL) {
518515
Py_DECREF(py_rclsid);
519516
Py_DECREF(py_riid);
520517
Py_DECREF(func);
521-
PyErr_WriteUnraisable(context ? context : Py_None);
522-
return E_FAIL;
518+
goto error;
523519
}
524520
result = PyObject_CallFunctionObjArgs(func,
525521
py_rclsid,
@@ -532,17 +528,21 @@ long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
532528
}
533529
Py_DECREF(func);
534530
if (!result) {
535-
PyErr_WriteUnraisable(context ? context : Py_None);
536-
return E_FAIL;
531+
goto error;
537532
}
538533

539534
retval = PyLong_AsLong(result);
540535
if (PyErr_Occurred()) {
541-
PyErr_WriteUnraisable(context ? context : Py_None);
542-
retval = E_FAIL;
536+
Py_DECREF(result);
537+
goto error;
543538
}
544539
Py_DECREF(result);
545540
return retval;
541+
542+
error:
543+
PyErr_FormatUnraisable("Exception ignored while calling "
544+
"ctypes.DllGetClassObject");
545+
return E_FAIL;
546546
}
547547

548548
STDAPI DllGetClassObject(REFCLSID rclsid,
@@ -580,24 +580,27 @@ long Call_CanUnloadNow(void)
580580
func = PyObject_GetAttrString(mod, "DllCanUnloadNow");
581581
Py_DECREF(mod);
582582
if (!func) {
583-
PyErr_WriteUnraisable(context ? context : Py_None);
584-
return E_FAIL;
583+
goto error;
585584
}
586585

587586
result = _PyObject_CallNoArgs(func);
588587
Py_DECREF(func);
589588
if (!result) {
590-
PyErr_WriteUnraisable(context ? context : Py_None);
591-
return E_FAIL;
589+
goto error;
592590
}
593591

594592
retval = PyLong_AsLong(result);
595593
if (PyErr_Occurred()) {
596-
PyErr_WriteUnraisable(context ? context : Py_None);
597-
retval = E_FAIL;
594+
Py_DECREF(result);
595+
goto error;
598596
}
599597
Py_DECREF(result);
600598
return retval;
599+
600+
error:
601+
PyErr_FormatUnraisable("Exception ignored while calling "
602+
"ctypes.DllCanUnloadNow");
603+
return E_FAIL;
601604
}
602605

603606
/*

Modules/_lsprof.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ static PyTime_t CallExternalTimer(ProfilerObject *pObj)
9797
pObj->flags &= ~POF_EXT_TIMER;
9898

9999
if (o == NULL) {
100-
PyErr_WriteUnraisable(pObj->externalTimer);
100+
PyErr_FormatUnraisable("Exception ignored while calling "
101+
"_lsprof timer %R", pObj->externalTimer);
101102
return 0;
102103
}
103104

@@ -116,7 +117,8 @@ static PyTime_t CallExternalTimer(ProfilerObject *pObj)
116117
}
117118
Py_DECREF(o);
118119
if (err < 0) {
119-
PyErr_WriteUnraisable(pObj->externalTimer);
120+
PyErr_FormatUnraisable("Exception ignored while calling "
121+
"_lsprof timer %R", pObj->externalTimer);
120122
return 0;
121123
}
122124
return result;

Objects/unicodeobject.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -1735,7 +1735,9 @@ unicode_dealloc(PyObject *unicode)
17351735
PyObject *popped;
17361736
int r = PyDict_Pop(interned, unicode, &popped);
17371737
if (r == -1) {
1738-
PyErr_WriteUnraisable(unicode);
1738+
PyErr_FormatUnraisable("Exception ignored while "
1739+
"removing an interned string %R",
1740+
unicode);
17391741
// We don't know what happened to the string. It's probably
17401742
// best to leak it:
17411743
// - if it was popped, there are no more references to it

Objects/weakrefobject.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -987,10 +987,13 @@ handle_callback(PyWeakReference *ref, PyObject *callback)
987987
{
988988
PyObject *cbresult = PyObject_CallOneArg(callback, (PyObject *)ref);
989989

990-
if (cbresult == NULL)
991-
PyErr_WriteUnraisable(callback);
992-
else
990+
if (cbresult == NULL) {
991+
PyErr_FormatUnraisable("Exception ignored while "
992+
"calling weakref callback %R", callback);
993+
}
994+
else {
993995
Py_DECREF(cbresult);
996+
}
994997
}
995998

996999
/* This function is called by the tp_dealloc handler to clear weak references.

0 commit comments

Comments
 (0)