Issue #20437: Fixed 22 potential bugs when deleting objects references.
diff --git a/Misc/NEWS b/Misc/NEWS
index 20cf656..8bd0dd3 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@
Core and Builtins
-----------------
+- Issue #20437: Fixed 22 potential bugs when deleting objects references.
+
- Issue #20500: Displaying an exception at interpreter shutdown no longer
risks triggering an assertion failure in PyObject_Str.
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 820a606..97ae798 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -159,10 +159,8 @@
if (-1 == PyDict_DelItem(self->dict, self->key))
/* XXX Error context */
PyErr_WriteUnraisable(Py_None);
- Py_DECREF(self->key);
- self->key = NULL;
- Py_DECREF(self->dict);
- self->dict = NULL;
+ Py_CLEAR(self->key);
+ Py_CLEAR(self->dict);
}
Py_INCREF(Py_None);
return Py_None;
@@ -2934,10 +2932,8 @@
PyCFuncPtr_set_restype(PyCFuncPtrObject *self, PyObject *ob)
{
if (ob == NULL) {
- Py_XDECREF(self->restype);
- self->restype = NULL;
- Py_XDECREF(self->checker);
- self->checker = NULL;
+ Py_CLEAR(self->restype);
+ Py_CLEAR(self->checker);
return 0;
}
if (ob != Py_None && !PyType_stgdict(ob) && !PyCallable_Check(ob)) {
@@ -2980,10 +2976,8 @@
PyObject *converters;
if (ob == NULL || ob == Py_None) {
- Py_XDECREF(self->converters);
- self->converters = NULL;
- Py_XDECREF(self->argtypes);
- self->argtypes = NULL;
+ Py_CLEAR(self->converters);
+ Py_CLEAR(self->argtypes);
} else {
converters = converters_from_argtypes(ob);
if (!converters)
diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c
index 8d10890..ce92af6 100644
--- a/Modules/_sqlite/cursor.c
+++ b/Modules/_sqlite/cursor.c
@@ -229,8 +229,7 @@
if (converter != Py_None) {
Py_DECREF(converter);
}
- Py_XDECREF(self->row_cast_map);
- self->row_cast_map = NULL;
+ Py_CLEAR(self->row_cast_map);
return -1;
}
@@ -447,8 +446,7 @@
self->locked = 1;
self->reset = 0;
- Py_XDECREF(self->next_row);
- self->next_row = NULL;
+ Py_CLEAR(self->next_row);
if (multiple) {
/* executemany() */
@@ -864,8 +862,7 @@
if (!self->next_row) {
if (self->statement) {
(void)pysqlite_statement_reset(self->statement);
- Py_DECREF(self->statement);
- self->statement = NULL;
+ Py_CLEAR(self->statement);
}
return NULL;
}
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 1428220..60d0099 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -780,8 +780,7 @@
static void
path_cleanup(path_t *path) {
if (path->cleanup) {
- Py_DECREF(path->cleanup);
- path->cleanup = NULL;
+ Py_CLEAR(path->cleanup);
}
}
diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c
index 3f51c12..a71ecc5 100644
--- a/Modules/pyexpat.c
+++ b/Modules/pyexpat.c
@@ -317,8 +317,7 @@
}
else {
if (trace_frame(tstate, f, PyTrace_RETURN, res) < 0) {
- Py_XDECREF(res);
- res = NULL;
+ Py_CLEAR(res);
}
}
#else
diff --git a/Modules/readline.c b/Modules/readline.c
index 0be39da..4bba0db 100644
--- a/Modules/readline.c
+++ b/Modules/readline.c
@@ -281,8 +281,7 @@
if (!PyArg_ParseTuple(args, buf, &function))
return NULL;
if (function == Py_None) {
- Py_XDECREF(*hook_var);
- *hook_var = NULL;
+ Py_CLEAR(*hook_var);
}
else if (PyCallable_Check(function)) {
PyObject *tmp = *hook_var;
@@ -885,7 +884,7 @@
(r != Py_None && PyLong_AsLong(r) == -1 && PyErr_Occurred())) {
goto error;
}
- Py_XDECREF(r); r=NULL;
+ Py_CLEAR(r);
if (0) {
error:
diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c
index 0c9b9d9..0b11a01 100644
--- a/Modules/selectmodule.c
+++ b/Modules/selectmodule.c
@@ -66,8 +66,7 @@
{
int i;
for (i = 0; i < FD_SETSIZE + 1 && fd2obj[i].sentinel >= 0; i++) {
- Py_XDECREF(fd2obj[i].obj);
- fd2obj[i].obj = NULL;
+ Py_CLEAR(fd2obj[i].obj);
}
fd2obj[0].sentinel = -1;
}
diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
index 9a0e8e3..43e3ca1 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -1305,12 +1305,9 @@
Py_XDECREF(func);
}
- Py_XDECREF(IntHandler);
- IntHandler = NULL;
- Py_XDECREF(DefaultHandler);
- DefaultHandler = NULL;
- Py_XDECREF(IgnoreHandler);
- IgnoreHandler = NULL;
+ Py_CLEAR(IntHandler);
+ Py_CLEAR(DefaultHandler);
+ Py_CLEAR(IgnoreHandler);
}
diff --git a/Modules/syslogmodule.c b/Modules/syslogmodule.c
index 9d79eec..f2d44ff 100644
--- a/Modules/syslogmodule.c
+++ b/Modules/syslogmodule.c
@@ -197,8 +197,7 @@
{
if (S_log_open) {
closelog();
- Py_XDECREF(S_ident_o);
- S_ident_o = NULL;
+ Py_CLEAR(S_ident_o);
S_log_open = 0;
}
Py_INCREF(Py_None);
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c
index 2ba264f..52744b9 100644
--- a/Modules/zlibmodule.c
+++ b/Modules/zlibmodule.c
@@ -463,8 +463,7 @@
}
error:
- Py_XDECREF(self);
- self = NULL;
+ Py_CLEAR(self);
success:
return (PyObject*)self;
}
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c
index 5625a65..c63afcc 100644
--- a/Objects/tupleobject.c
+++ b/Objects/tupleobject.c
@@ -879,8 +879,7 @@
_Py_ForgetReference((PyObject *) v);
/* DECREF items deleted by shrinkage */
for (i = newsize; i < oldsize; i++) {
- Py_XDECREF(v->ob_item[i]);
- v->ob_item[i] = NULL;
+ Py_CLEAR(v->ob_item[i]);
}
sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
if (sv == NULL) {
@@ -926,8 +925,7 @@
#if PyTuple_MAXSAVESIZE > 0
/* empty tuples are used all over the place and applications may
* rely on the fact that an empty tuple is a singleton. */
- Py_XDECREF(free_list[0]);
- free_list[0] = NULL;
+ Py_CLEAR(free_list[0]);
(void)PyTuple_ClearFreeList();
#endif
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index eae4bc5..994c4f5 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -1902,8 +1902,7 @@
{
_Py_Identifier *tmp, *s = static_strings;
while (s) {
- Py_DECREF(s->object);
- s->object = NULL;
+ Py_CLEAR(s->object);
tmp = s->next;
s->next = NULL;
s = tmp;
@@ -4005,8 +4004,7 @@
return;
onError:
- Py_DECREF(*exceptionObject);
- *exceptionObject = NULL;
+ Py_CLEAR(*exceptionObject);
}
#ifdef HAVE_MBCS
@@ -6366,8 +6364,7 @@
goto onError;
return;
onError:
- Py_DECREF(*exceptionObject);
- *exceptionObject = NULL;
+ Py_CLEAR(*exceptionObject);
}
}
@@ -8410,8 +8407,7 @@
goto onError;
return;
onError:
- Py_DECREF(*exceptionObject);
- *exceptionObject = NULL;
+ Py_CLEAR(*exceptionObject);
}
}
@@ -13502,8 +13498,7 @@
PyObject *newbuffer;
newbuffer = resize_compact(writer->buffer, writer->pos);
if (newbuffer == NULL) {
- Py_DECREF(writer->buffer);
- writer->buffer = NULL;
+ Py_CLEAR(writer->buffer);
return NULL;
}
writer->buffer = newbuffer;
diff --git a/Python/ceval.c b/Python/ceval.c
index 34c52b0..5db88be 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -3189,8 +3189,7 @@
if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
tstate, f,
PyTrace_RETURN, retval)) {
- Py_XDECREF(retval);
- retval = NULL;
+ Py_CLEAR(retval);
why = WHY_EXCEPTION;
}
}
@@ -3209,8 +3208,7 @@
else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
tstate, f,
PyTrace_RETURN, retval)) {
- Py_XDECREF(retval);
- retval = NULL;
+ Py_CLEAR(retval);
/* why = WHY_EXCEPTION; */
}
}
@@ -3568,8 +3566,7 @@
if (co->co_flags & CO_GENERATOR) {
/* Don't need to keep the reference to f_back, it will be set
* when the generator is resumed. */
- Py_XDECREF(f->f_back);
- f->f_back = NULL;
+ Py_CLEAR(f->f_back);
PCALL(PCALL_GENERATOR);
diff --git a/Python/import.c b/Python/import.c
index 001d745..d0115e4 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -349,8 +349,7 @@
void
_PyImport_Fini(void)
{
- Py_XDECREF(extensions);
- extensions = NULL;
+ Py_CLEAR(extensions);
#ifdef WITH_THREAD
if (import_lock != NULL) {
PyThread_free_lock(import_lock);
@@ -598,8 +597,7 @@
/* Somebody already imported the module,
likely under a different name.
XXX this should really not happen. */
- Py_DECREF(def->m_base.m_copy);
- def->m_base.m_copy = NULL;
+ Py_CLEAR(def->m_base.m_copy);
}
dict = PyModule_GetDict(mod);
if (dict == NULL)
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 976d5a0..9ec3bfa 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -430,8 +430,7 @@
result = call_trampoline(callback, frame, what, arg);
if (result == NULL) {
PyEval_SetTrace(NULL, NULL);
- Py_XDECREF(frame->f_trace);
- frame->f_trace = NULL;
+ Py_CLEAR(frame->f_trace);
return -1;
}
if (result != Py_None) {