Skip to content

Commit f9d4359

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

File tree

6 files changed

+47
-35
lines changed

6 files changed

+47
-35
lines changed

Modules/_ctypes/_ctypes.c

Lines changed: 6 additions & 3 deletions
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

Lines changed: 22 additions & 23 deletions
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,
@@ -570,34 +570,33 @@ long Call_CanUnloadNow(void)
570570

571571
mod = PyImport_ImportModule("ctypes");
572572
if (!mod) {
573-
/* OutputDebugString("Could not import ctypes"); */
574-
/* We assume that this error can only occur when shutting
575-
down, so we silently ignore it */
576-
PyErr_Clear();
577-
return E_FAIL;
573+
goto error;
578574
}
579575
/* Other errors cannot be raised, but are printed to stderr */
580576
func = PyObject_GetAttrString(mod, "DllCanUnloadNow");
581577
Py_DECREF(mod);
582578
if (!func) {
583-
PyErr_WriteUnraisable(context ? context : Py_None);
584-
return E_FAIL;
579+
goto error;
585580
}
586581

587582
result = _PyObject_CallNoArgs(func);
588583
Py_DECREF(func);
589584
if (!result) {
590-
PyErr_WriteUnraisable(context ? context : Py_None);
591-
return E_FAIL;
585+
goto error;
592586
}
593587

594588
retval = PyLong_AsLong(result);
595589
if (PyErr_Occurred()) {
596-
PyErr_WriteUnraisable(context ? context : Py_None);
597-
retval = E_FAIL;
590+
Py_DECREF(result);
591+
goto error;
598592
}
599593
Py_DECREF(result);
600594
return retval;
595+
596+
error:
597+
PyErr_FormatUnraisable("Exception ignored while calling "
598+
"ctypes.DllCanUnloadNow");
599+
return E_FAIL;
601600
}
602601

603602
/*

Modules/_lsprof.c

Lines changed: 4 additions & 2 deletions
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/typeobject.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10288,10 +10288,13 @@ slot_tp_finalize(PyObject *self)
1028810288
del = lookup_maybe_method(self, &_Py_ID(__del__), &unbound);
1028910289
if (del != NULL) {
1029010290
res = call_unbound_noarg(unbound, del, self);
10291-
if (res == NULL)
10292-
PyErr_WriteUnraisable(del);
10293-
else
10291+
if (res == NULL) {
10292+
PyErr_FormatUnraisable("Exception ignored while "
10293+
"calling deallocator %R", del);
10294+
}
10295+
else {
1029410296
Py_DECREF(res);
10297+
}
1029510298
Py_DECREF(del);
1029610299
}
1029710300

Objects/unicodeobject.c

Lines changed: 3 additions & 1 deletion
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

Lines changed: 6 additions & 3 deletions
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)