Skip to content

Commit 0373926

Browse files
authored
gh-129354: Use PyErr_FormatUnraisable() function (#129511)
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
1 parent c3ae5c9 commit 0373926

File tree

5 files changed

+40
-35
lines changed

5 files changed

+40
-35
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-26
Original file line numberDiff line numberDiff line change
@@ -487,39 +487,31 @@ long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
487487
{
488488
PyObject *func, *result;
489489
long retval;
490-
static PyObject *context;
491-
492-
if (context == NULL)
493-
context = PyUnicode_InternFromString("_ctypes.DllGetClassObject");
494490

495491
func = PyImport_ImportModuleAttrString("ctypes", "DllGetClassObject");
496492
if (!func) {
497-
PyErr_WriteUnraisable(context ? context : Py_None);
498493
/* There has been a warning before about this already */
499-
return E_FAIL;
494+
goto error;
500495
}
501496

502497
{
503498
PyObject *py_rclsid = PyLong_FromVoidPtr((void *)rclsid);
504499
if (py_rclsid == NULL) {
505500
Py_DECREF(func);
506-
PyErr_WriteUnraisable(context ? context : Py_None);
507-
return E_FAIL;
501+
goto error;
508502
}
509503
PyObject *py_riid = PyLong_FromVoidPtr((void *)riid);
510504
if (py_riid == NULL) {
511505
Py_DECREF(func);
512506
Py_DECREF(py_rclsid);
513-
PyErr_WriteUnraisable(context ? context : Py_None);
514-
return E_FAIL;
507+
goto error;
515508
}
516509
PyObject *py_ppv = PyLong_FromVoidPtr(ppv);
517510
if (py_ppv == NULL) {
518511
Py_DECREF(py_rclsid);
519512
Py_DECREF(py_riid);
520513
Py_DECREF(func);
521-
PyErr_WriteUnraisable(context ? context : Py_None);
522-
return E_FAIL;
514+
goto error;
523515
}
524516
result = PyObject_CallFunctionObjArgs(func,
525517
py_rclsid,
@@ -532,17 +524,21 @@ long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
532524
}
533525
Py_DECREF(func);
534526
if (!result) {
535-
PyErr_WriteUnraisable(context ? context : Py_None);
536-
return E_FAIL;
527+
goto error;
537528
}
538529

539530
retval = PyLong_AsLong(result);
540531
if (PyErr_Occurred()) {
541-
PyErr_WriteUnraisable(context ? context : Py_None);
542-
retval = E_FAIL;
532+
Py_DECREF(result);
533+
goto error;
543534
}
544535
Py_DECREF(result);
545536
return retval;
537+
538+
error:
539+
PyErr_FormatUnraisable("Exception ignored while calling "
540+
"ctypes.DllGetClassObject");
541+
return E_FAIL;
546542
}
547543

548544
STDAPI DllGetClassObject(REFCLSID rclsid,
@@ -563,10 +559,6 @@ long Call_CanUnloadNow(void)
563559
{
564560
PyObject *mod, *func, *result;
565561
long retval;
566-
static PyObject *context;
567-
568-
if (context == NULL)
569-
context = PyUnicode_InternFromString("_ctypes.DllCanUnloadNow");
570562

571563
mod = PyImport_ImportModule("ctypes");
572564
if (!mod) {
@@ -580,24 +572,27 @@ long Call_CanUnloadNow(void)
580572
func = PyObject_GetAttrString(mod, "DllCanUnloadNow");
581573
Py_DECREF(mod);
582574
if (!func) {
583-
PyErr_WriteUnraisable(context ? context : Py_None);
584-
return E_FAIL;
575+
goto error;
585576
}
586577

587578
result = _PyObject_CallNoArgs(func);
588579
Py_DECREF(func);
589580
if (!result) {
590-
PyErr_WriteUnraisable(context ? context : Py_None);
591-
return E_FAIL;
581+
goto error;
592582
}
593583

594584
retval = PyLong_AsLong(result);
595585
if (PyErr_Occurred()) {
596-
PyErr_WriteUnraisable(context ? context : Py_None);
597-
retval = E_FAIL;
586+
Py_DECREF(result);
587+
goto error;
598588
}
599589
Py_DECREF(result);
600590
return retval;
591+
592+
error:
593+
PyErr_FormatUnraisable("Exception ignored while calling "
594+
"ctypes.DllCanUnloadNow");
595+
return E_FAIL;
601596
}
602597

603598
/*

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)