gh-129354: Use PyErr_FormatUnraisable() function (#129511)

Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
This commit is contained in:
Victor Stinner 2025-01-31 13:16:08 +01:00 committed by GitHub
parent c3ae5c9e4a
commit 0373926260
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 40 additions and 35 deletions

View file

@ -464,7 +464,8 @@ CType_Type_traverse(PyObject *self, visitproc visit, void *arg)
{ {
StgInfo *info = _PyStgInfo_FromType_NoState(self); StgInfo *info = _PyStgInfo_FromType_NoState(self);
if (!info) { if (!info) {
PyErr_WriteUnraisable(self); PyErr_FormatUnraisable("Exception ignored while "
"calling ctypes traverse function %R", self);
} }
if (info) { if (info) {
Py_VISIT(info->proto); Py_VISIT(info->proto);
@ -495,7 +496,8 @@ CType_Type_clear(PyObject *self)
{ {
StgInfo *info = _PyStgInfo_FromType_NoState(self); StgInfo *info = _PyStgInfo_FromType_NoState(self);
if (!info) { if (!info) {
PyErr_WriteUnraisable(self); PyErr_FormatUnraisable("Exception ignored while "
"clearing ctypes %R", self);
} }
if (info) { if (info) {
ctype_clear_stginfo(info); ctype_clear_stginfo(info);
@ -508,7 +510,8 @@ CType_Type_dealloc(PyObject *self)
{ {
StgInfo *info = _PyStgInfo_FromType_NoState(self); StgInfo *info = _PyStgInfo_FromType_NoState(self);
if (!info) { if (!info) {
PyErr_WriteUnraisable(NULL); // NULL avoids segfault here PyErr_FormatUnraisable("Exception ignored while "
"deallocating ctypes %R", self);
} }
if (info) { if (info) {
PyMem_Free(info->ffi_type_pointer.elements); PyMem_Free(info->ffi_type_pointer.elements);

View file

@ -487,39 +487,31 @@ long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
{ {
PyObject *func, *result; PyObject *func, *result;
long retval; long retval;
static PyObject *context;
if (context == NULL)
context = PyUnicode_InternFromString("_ctypes.DllGetClassObject");
func = PyImport_ImportModuleAttrString("ctypes", "DllGetClassObject"); func = PyImport_ImportModuleAttrString("ctypes", "DllGetClassObject");
if (!func) { if (!func) {
PyErr_WriteUnraisable(context ? context : Py_None);
/* There has been a warning before about this already */ /* There has been a warning before about this already */
return E_FAIL; goto error;
} }
{ {
PyObject *py_rclsid = PyLong_FromVoidPtr((void *)rclsid); PyObject *py_rclsid = PyLong_FromVoidPtr((void *)rclsid);
if (py_rclsid == NULL) { if (py_rclsid == NULL) {
Py_DECREF(func); Py_DECREF(func);
PyErr_WriteUnraisable(context ? context : Py_None); goto error;
return E_FAIL;
} }
PyObject *py_riid = PyLong_FromVoidPtr((void *)riid); PyObject *py_riid = PyLong_FromVoidPtr((void *)riid);
if (py_riid == NULL) { if (py_riid == NULL) {
Py_DECREF(func); Py_DECREF(func);
Py_DECREF(py_rclsid); Py_DECREF(py_rclsid);
PyErr_WriteUnraisable(context ? context : Py_None); goto error;
return E_FAIL;
} }
PyObject *py_ppv = PyLong_FromVoidPtr(ppv); PyObject *py_ppv = PyLong_FromVoidPtr(ppv);
if (py_ppv == NULL) { if (py_ppv == NULL) {
Py_DECREF(py_rclsid); Py_DECREF(py_rclsid);
Py_DECREF(py_riid); Py_DECREF(py_riid);
Py_DECREF(func); Py_DECREF(func);
PyErr_WriteUnraisable(context ? context : Py_None); goto error;
return E_FAIL;
} }
result = PyObject_CallFunctionObjArgs(func, result = PyObject_CallFunctionObjArgs(func,
py_rclsid, py_rclsid,
@ -532,17 +524,21 @@ long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
} }
Py_DECREF(func); Py_DECREF(func);
if (!result) { if (!result) {
PyErr_WriteUnraisable(context ? context : Py_None); goto error;
return E_FAIL;
} }
retval = PyLong_AsLong(result); retval = PyLong_AsLong(result);
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
PyErr_WriteUnraisable(context ? context : Py_None); Py_DECREF(result);
retval = E_FAIL; goto error;
} }
Py_DECREF(result); Py_DECREF(result);
return retval; return retval;
error:
PyErr_FormatUnraisable("Exception ignored while calling "
"ctypes.DllGetClassObject");
return E_FAIL;
} }
STDAPI DllGetClassObject(REFCLSID rclsid, STDAPI DllGetClassObject(REFCLSID rclsid,
@ -563,10 +559,6 @@ long Call_CanUnloadNow(void)
{ {
PyObject *mod, *func, *result; PyObject *mod, *func, *result;
long retval; long retval;
static PyObject *context;
if (context == NULL)
context = PyUnicode_InternFromString("_ctypes.DllCanUnloadNow");
mod = PyImport_ImportModule("ctypes"); mod = PyImport_ImportModule("ctypes");
if (!mod) { if (!mod) {
@ -580,24 +572,27 @@ long Call_CanUnloadNow(void)
func = PyObject_GetAttrString(mod, "DllCanUnloadNow"); func = PyObject_GetAttrString(mod, "DllCanUnloadNow");
Py_DECREF(mod); Py_DECREF(mod);
if (!func) { if (!func) {
PyErr_WriteUnraisable(context ? context : Py_None); goto error;
return E_FAIL;
} }
result = _PyObject_CallNoArgs(func); result = _PyObject_CallNoArgs(func);
Py_DECREF(func); Py_DECREF(func);
if (!result) { if (!result) {
PyErr_WriteUnraisable(context ? context : Py_None); goto error;
return E_FAIL;
} }
retval = PyLong_AsLong(result); retval = PyLong_AsLong(result);
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
PyErr_WriteUnraisable(context ? context : Py_None); Py_DECREF(result);
retval = E_FAIL; goto error;
} }
Py_DECREF(result); Py_DECREF(result);
return retval; return retval;
error:
PyErr_FormatUnraisable("Exception ignored while calling "
"ctypes.DllCanUnloadNow");
return E_FAIL;
} }
/* /*

View file

@ -97,7 +97,8 @@ static PyTime_t CallExternalTimer(ProfilerObject *pObj)
pObj->flags &= ~POF_EXT_TIMER; pObj->flags &= ~POF_EXT_TIMER;
if (o == NULL) { if (o == NULL) {
PyErr_WriteUnraisable(pObj->externalTimer); PyErr_FormatUnraisable("Exception ignored while calling "
"_lsprof timer %R", pObj->externalTimer);
return 0; return 0;
} }
@ -116,7 +117,8 @@ static PyTime_t CallExternalTimer(ProfilerObject *pObj)
} }
Py_DECREF(o); Py_DECREF(o);
if (err < 0) { if (err < 0) {
PyErr_WriteUnraisable(pObj->externalTimer); PyErr_FormatUnraisable("Exception ignored while calling "
"_lsprof timer %R", pObj->externalTimer);
return 0; return 0;
} }
return result; return result;

View file

@ -1735,7 +1735,9 @@ unicode_dealloc(PyObject *unicode)
PyObject *popped; PyObject *popped;
int r = PyDict_Pop(interned, unicode, &popped); int r = PyDict_Pop(interned, unicode, &popped);
if (r == -1) { 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 // We don't know what happened to the string. It's probably
// best to leak it: // best to leak it:
// - if it was popped, there are no more references to it // - if it was popped, there are no more references to it

View file

@ -987,10 +987,13 @@ handle_callback(PyWeakReference *ref, PyObject *callback)
{ {
PyObject *cbresult = PyObject_CallOneArg(callback, (PyObject *)ref); PyObject *cbresult = PyObject_CallOneArg(callback, (PyObject *)ref);
if (cbresult == NULL) if (cbresult == NULL) {
PyErr_WriteUnraisable(callback); PyErr_FormatUnraisable("Exception ignored while "
else "calling weakref callback %R", callback);
}
else {
Py_DECREF(cbresult); Py_DECREF(cbresult);
}
} }
/* This function is called by the tp_dealloc handler to clear weak references. /* This function is called by the tp_dealloc handler to clear weak references.