Skip to content

Commit

Permalink
pythongh-129354: Use PyErr_FormatUnraisable() function
Browse files Browse the repository at this point in the history
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
  • Loading branch information
vstinner committed Jan 31, 2025
1 parent 8df5193 commit f9d4359
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 35 deletions.
9 changes: 6 additions & 3 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,8 @@ CType_Type_traverse(PyObject *self, visitproc visit, void *arg)
{
StgInfo *info = _PyStgInfo_FromType_NoState(self);
if (!info) {
PyErr_WriteUnraisable(self);
PyErr_FormatUnraisable("Exception ignored while "
"calling ctypes traverse function %R", self);
}
if (info) {
Py_VISIT(info->proto);
Expand Down Expand Up @@ -495,7 +496,8 @@ CType_Type_clear(PyObject *self)
{
StgInfo *info = _PyStgInfo_FromType_NoState(self);
if (!info) {
PyErr_WriteUnraisable(self);
PyErr_FormatUnraisable("Exception ignored while "
"clearing ctypes %R", self);
}
if (info) {
ctype_clear_stginfo(info);
Expand All @@ -508,7 +510,8 @@ CType_Type_dealloc(PyObject *self)
{
StgInfo *info = _PyStgInfo_FromType_NoState(self);
if (!info) {
PyErr_WriteUnraisable(NULL); // NULL avoids segfault here
PyErr_FormatUnraisable("Exception ignored while "
"deallocating ctypes %R", self);
}
if (info) {
PyMem_Free(info->ffi_type_pointer.elements);
Expand Down
45 changes: 22 additions & 23 deletions Modules/_ctypes/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,32 +494,28 @@ long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)

func = PyImport_ImportModuleAttrString("ctypes", "DllGetClassObject");
if (!func) {
PyErr_WriteUnraisable(context ? context : Py_None);
/* There has been a warning before about this already */
return E_FAIL;
goto error;
}

{
PyObject *py_rclsid = PyLong_FromVoidPtr((void *)rclsid);
if (py_rclsid == NULL) {
Py_DECREF(func);
PyErr_WriteUnraisable(context ? context : Py_None);
return E_FAIL;
goto error;
}
PyObject *py_riid = PyLong_FromVoidPtr((void *)riid);
if (py_riid == NULL) {
Py_DECREF(func);
Py_DECREF(py_rclsid);
PyErr_WriteUnraisable(context ? context : Py_None);
return E_FAIL;
goto error;
}
PyObject *py_ppv = PyLong_FromVoidPtr(ppv);
if (py_ppv == NULL) {
Py_DECREF(py_rclsid);
Py_DECREF(py_riid);
Py_DECREF(func);
PyErr_WriteUnraisable(context ? context : Py_None);
return E_FAIL;
goto error;
}
result = PyObject_CallFunctionObjArgs(func,
py_rclsid,
Expand All @@ -532,17 +528,21 @@ long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
}
Py_DECREF(func);
if (!result) {
PyErr_WriteUnraisable(context ? context : Py_None);
return E_FAIL;
goto error;
}

retval = PyLong_AsLong(result);
if (PyErr_Occurred()) {
PyErr_WriteUnraisable(context ? context : Py_None);
retval = E_FAIL;
Py_DECREF(result);
goto error;
}
Py_DECREF(result);
return retval;

error:
PyErr_FormatUnraisable("Exception ignored while calling "
"ctypes.DllGetClassObject");
return E_FAIL;
}

STDAPI DllGetClassObject(REFCLSID rclsid,
Expand Down Expand Up @@ -570,34 +570,33 @@ long Call_CanUnloadNow(void)

mod = PyImport_ImportModule("ctypes");
if (!mod) {
/* OutputDebugString("Could not import ctypes"); */
/* We assume that this error can only occur when shutting
down, so we silently ignore it */
PyErr_Clear();
return E_FAIL;
goto error;
}
/* Other errors cannot be raised, but are printed to stderr */
func = PyObject_GetAttrString(mod, "DllCanUnloadNow");
Py_DECREF(mod);
if (!func) {
PyErr_WriteUnraisable(context ? context : Py_None);
return E_FAIL;
goto error;
}

result = _PyObject_CallNoArgs(func);
Py_DECREF(func);
if (!result) {
PyErr_WriteUnraisable(context ? context : Py_None);
return E_FAIL;
goto error;
}

retval = PyLong_AsLong(result);
if (PyErr_Occurred()) {
PyErr_WriteUnraisable(context ? context : Py_None);
retval = E_FAIL;
Py_DECREF(result);
goto error;
}
Py_DECREF(result);
return retval;

error:
PyErr_FormatUnraisable("Exception ignored while calling "
"ctypes.DllCanUnloadNow");
return E_FAIL;
}

/*
Expand Down
6 changes: 4 additions & 2 deletions Modules/_lsprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ static PyTime_t CallExternalTimer(ProfilerObject *pObj)
pObj->flags &= ~POF_EXT_TIMER;

if (o == NULL) {
PyErr_WriteUnraisable(pObj->externalTimer);
PyErr_FormatUnraisable("Exception ignored while calling "
"_lsprof timer %R", pObj->externalTimer);
return 0;
}

Expand All @@ -116,7 +117,8 @@ static PyTime_t CallExternalTimer(ProfilerObject *pObj)
}
Py_DECREF(o);
if (err < 0) {
PyErr_WriteUnraisable(pObj->externalTimer);
PyErr_FormatUnraisable("Exception ignored while calling "
"_lsprof timer %R", pObj->externalTimer);
return 0;
}
return result;
Expand Down
9 changes: 6 additions & 3 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -10288,10 +10288,13 @@ slot_tp_finalize(PyObject *self)
del = lookup_maybe_method(self, &_Py_ID(__del__), &unbound);
if (del != NULL) {
res = call_unbound_noarg(unbound, del, self);
if (res == NULL)
PyErr_WriteUnraisable(del);
else
if (res == NULL) {
PyErr_FormatUnraisable("Exception ignored while "
"calling deallocator %R", del);
}
else {
Py_DECREF(res);
}
Py_DECREF(del);
}

Expand Down
4 changes: 3 additions & 1 deletion Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1735,7 +1735,9 @@ unicode_dealloc(PyObject *unicode)
PyObject *popped;
int r = PyDict_Pop(interned, unicode, &popped);
if (r == -1) {
PyErr_WriteUnraisable(unicode);
PyErr_FormatUnraisable("Exception ignored while "
"removing an interned string %R",
unicode);
// We don't know what happened to the string. It's probably
// best to leak it:
// - if it was popped, there are no more references to it
Expand Down
9 changes: 6 additions & 3 deletions Objects/weakrefobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -987,10 +987,13 @@ handle_callback(PyWeakReference *ref, PyObject *callback)
{
PyObject *cbresult = PyObject_CallOneArg(callback, (PyObject *)ref);

if (cbresult == NULL)
PyErr_WriteUnraisable(callback);
else
if (cbresult == NULL) {
PyErr_FormatUnraisable("Exception ignored while "
"calling weakref callback %R", callback);
}
else {
Py_DECREF(cbresult);
}
}

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

0 comments on commit f9d4359

Please sign in to comment.