[Python-checkins] cpython (2.7): Issue #26200: Added Py_SETREF and replaced Py_XSETREF with Py_SETREF

serhiy.storchaka python-checkins at python.org
Sun Apr 10 11:12:35 EDT 2016


https://hg.python.org/cpython/rev/9cf8572abe58
changeset:   100908:9cf8572abe58
branch:      2.7
parent:      100906:a06654ca0134
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sun Apr 10 18:05:12 2016 +0300
summary:
  Issue #26200: Added Py_SETREF and replaced Py_XSETREF with Py_SETREF
in places where Py_DECREF was used.

files:
  Include/object.h             |  14 ++++++++++++--
  Modules/_bsddb.c             |   6 +++---
  Modules/_ctypes/_ctypes.c    |  10 +++++-----
  Modules/_curses_panel.c      |   2 +-
  Modules/_elementtree.c       |   8 ++++----
  Modules/_json.c              |   2 +-
  Modules/_sqlite/connection.c |   6 +++---
  Modules/_sqlite/cursor.c     |   6 +++---
  Modules/_sre.c               |   2 +-
  Modules/_ssl.c               |   2 +-
  Modules/bz2module.c          |   2 +-
  Modules/itertoolsmodule.c    |   2 +-
  Modules/signalmodule.c       |   2 +-
  Modules/zlibmodule.c         |   4 ++--
  Objects/exceptions.c         |   4 ++--
  Objects/fileobject.c         |   4 ++--
  Objects/funcobject.c         |   2 +-
  Objects/stringobject.c       |   4 ++--
  Objects/unicodeobject.c      |   2 +-
  Python/_warnings.c           |   2 +-
  Python/ceval.c               |   2 +-
  Python/errors.c              |   4 ++--
  22 files changed, 51 insertions(+), 41 deletions(-)


diff --git a/Include/object.h b/Include/object.h
--- a/Include/object.h
+++ b/Include/object.h
@@ -828,18 +828,28 @@
  *
  * As in case of Py_CLEAR "the obvious" code can be deadly:
  *
- *     Py_XDECREF(op);
+ *     Py_DECREF(op);
  *     op = op2;
  *
  * The safe way is:
  *
- *      Py_XSETREF(op, op2);
+ *      Py_SETREF(op, op2);
  *
  * That arranges to set `op` to `op2` _before_ decref'ing, so that any code
  * triggered as a side-effect of `op` getting torn down no longer believes
  * `op` points to a valid object.
+ *
+ * Py_XSETREF is a variant of Py_SETREF that uses Py_XDECREF instead of
+ * Py_DECREF.
  */
 
+#define Py_SETREF(op, op2)                      \
+    do {                                        \
+        PyObject *_py_tmp = (PyObject *)(op);   \
+        (op) = (op2);                           \
+        Py_DECREF(_py_tmp);                     \
+    } while (0)
+
 #define Py_XSETREF(op, op2)                     \
     do {                                        \
         PyObject *_py_tmp = (PyObject *)(op);   \
diff --git a/Modules/_bsddb.c b/Modules/_bsddb.c
--- a/Modules/_bsddb.c
+++ b/Modules/_bsddb.c
@@ -2498,7 +2498,7 @@
 {
     /* We can set the private field even if db is closed */
     Py_INCREF(private_obj);
-    Py_XSETREF(self->private_obj, private_obj);
+    Py_SETREF(self->private_obj, private_obj);
     RETURN_NONE();
 }
 
@@ -6997,7 +6997,7 @@
 {
     /* We can set the private field even if dbenv is closed */
     Py_INCREF(private_obj);
-    Py_XSETREF(self->private_obj, private_obj);
+    Py_SETREF(self->private_obj, private_obj);
     RETURN_NONE();
 }
 
@@ -7410,7 +7410,7 @@
     RETURN_IF_ERR();
 
     Py_INCREF(rep_transport);
-    Py_XSETREF(self->rep_transport, rep_transport);
+    Py_SETREF(self->rep_transport, rep_transport);
     RETURN_NONE();
 }
 
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -424,7 +424,7 @@
         Py_DECREF((PyObject *)dict);
         return NULL;
     }
-    Py_XSETREF(result->tp_dict, (PyObject *)dict);
+    Py_SETREF(result->tp_dict, (PyObject *)dict);
     dict->format = _ctypes_alloc_format_string(NULL, "B");
     if (dict->format == NULL) {
         Py_DECREF(result);
@@ -992,7 +992,7 @@
         Py_DECREF((PyObject *)stgdict);
         return NULL;
     }
-    Py_XSETREF(result->tp_dict, (PyObject *)stgdict);
+    Py_SETREF(result->tp_dict, (PyObject *)stgdict);
 
     return (PyObject *)result;
 }
@@ -1457,7 +1457,7 @@
         Py_DECREF((PyObject *)stgdict);
         return NULL;
     }
-    Py_XSETREF(result->tp_dict, (PyObject *)stgdict);
+    Py_SETREF(result->tp_dict, (PyObject *)stgdict);
 
     /* Special case for character arrays.
        A permanent annoyance: char arrays are also strings!
@@ -1880,7 +1880,7 @@
         Py_DECREF((PyObject *)stgdict);
         return NULL;
     }
-    Py_XSETREF(result->tp_dict, (PyObject *)stgdict);
+    Py_SETREF(result->tp_dict, (PyObject *)stgdict);
 
     return (PyObject *)result;
 }
@@ -2388,7 +2388,7 @@
         Py_DECREF((PyObject *)stgdict);
         return NULL;
     }
-    Py_XSETREF(result->tp_dict, (PyObject *)stgdict);
+    Py_SETREF(result->tp_dict, (PyObject *)stgdict);
 
     if (-1 == make_funcptrtype_dict(stgdict)) {
         Py_DECREF(result);
diff --git a/Modules/_curses_panel.c b/Modules/_curses_panel.c
--- a/Modules/_curses_panel.c
+++ b/Modules/_curses_panel.c
@@ -284,7 +284,7 @@
         return NULL;
     }
     Py_INCREF(temp);
-    Py_XSETREF(po->wo, temp);
+    Py_SETREF(po->wo, temp);
     Py_INCREF(Py_None);
     return Py_None;
 }
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -1574,7 +1574,7 @@
 
     if (strcmp(name, "tag") == 0) {
         Py_INCREF(value);
-        Py_XSETREF(self->tag, value);
+        Py_SETREF(self->tag, value);
     } else if (strcmp(name, "text") == 0) {
         Py_DECREF(JOIN_OBJ(self->text));
         self->text = value;
@@ -1587,7 +1587,7 @@
         if (!self->extra)
             element_new_extra(self, NULL);
         Py_INCREF(value);
-        Py_XSETREF(self->extra->attrib, value);
+        Py_SETREF(self->extra->attrib, value);
     } else {
         PyErr_SetString(PyExc_AttributeError, name);
         return -1;
@@ -1799,10 +1799,10 @@
     self->index++;
 
     Py_INCREF(node);
-    Py_XSETREF(self->this, (ElementObject*) node);
+    Py_SETREF(self->this, (ElementObject*) node);
 
     Py_INCREF(node);
-    Py_XSETREF(self->last, (ElementObject*) node);
+    Py_SETREF(self->last, (ElementObject*) node);
 
     if (treebuilder_append_event(self, self->start_event_obj, node) < 0)
         goto error;
diff --git a/Modules/_json.c b/Modules/_json.c
--- a/Modules/_json.c
+++ b/Modules/_json.c
@@ -1717,7 +1717,7 @@
     }
     else if (PyUnicode_Check(s->encoding)) {
         PyObject *tmp = PyUnicode_AsEncodedString(s->encoding, NULL, NULL);
-        Py_XSETREF(s->encoding, tmp);
+        Py_SETREF(s->encoding, tmp);
     }
     if (s->encoding == NULL)
         goto bail;
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -228,7 +228,7 @@
         node = node->next;
     }
 
-    Py_XSETREF(self->statement_cache,
+    Py_SETREF(self->statement_cache,
               (pysqlite_Cache *)PyObject_CallFunction((PyObject *)&pysqlite_CacheType, "O", self));
     Py_DECREF(self);
     self->statement_cache->decref_factory = 0;
@@ -815,7 +815,7 @@
         }
     }
 
-    Py_XSETREF(self->statements, new_list);
+    Py_SETREF(self->statements, new_list);
 }
 
 static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
@@ -846,7 +846,7 @@
         }
     }
 
-    Py_XSETREF(self->cursors, new_list);
+    Py_SETREF(self->cursors, new_list);
 }
 
 PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c
--- a/Modules/_sqlite/cursor.c
+++ b/Modules/_sqlite/cursor.c
@@ -544,7 +544,7 @@
 
     /* reset description and rowcount */
     Py_INCREF(Py_None);
-    Py_XSETREF(self->description, Py_None);
+    Py_SETREF(self->description, Py_None);
     self->rowcount = -1L;
 
     func_args = PyTuple_New(1);
@@ -569,7 +569,7 @@
     }
 
     if (self->statement->in_use) {
-        Py_XSETREF(self->statement,
+        Py_SETREF(self->statement,
                   PyObject_New(pysqlite_Statement, &pysqlite_StatementType));
         if (!self->statement) {
             goto error;
@@ -681,7 +681,7 @@
                 numcols = sqlite3_column_count(self->statement->st);
                 Py_END_ALLOW_THREADS
 
-                Py_XSETREF(self->description, PyTuple_New(numcols));
+                Py_SETREF(self->description, PyTuple_New(numcols));
                 if (!self->description) {
                     goto error;
                 }
diff --git a/Modules/_sre.c b/Modules/_sre.c
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -2054,7 +2054,7 @@
     if (!copy)
         return 0;
 
-    Py_XSETREF(*object, copy);
+    Py_SETREF(*object, copy);
 
     return 1; /* success */
 }
diff --git a/Modules/_ssl.c b/Modules/_ssl.c
--- a/Modules/_ssl.c
+++ b/Modules/_ssl.c
@@ -1467,7 +1467,7 @@
         return -1;
 #else
         Py_INCREF(value);
-        Py_XSETREF(self->ctx, (PySSLContext *)value);
+        Py_SETREF(self->ctx, (PySSLContext *)value);
         SSL_set_SSL_CTX(self->ssl, self->ctx->ctx);
 #endif
     } else {
diff --git a/Modules/bz2module.c b/Modules/bz2module.c
--- a/Modules/bz2module.c
+++ b/Modules/bz2module.c
@@ -1953,7 +1953,7 @@
             self->running = 0;
             input_left += bzs->avail_in;
             if (input_left != 0) {
-                Py_XSETREF(self->unused_data,
+                Py_SETREF(self->unused_data,
                           PyString_FromStringAndSize(bzs->next_in, input_left));
                 if (self->unused_data == NULL)
                     goto error;
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -492,7 +492,7 @@
         link = teedataobject_jumplink(to->dataobj);
         if (link == NULL)
             return NULL;
-        Py_XSETREF(to->dataobj, (teedataobject *)link);
+        Py_SETREF(to->dataobj, (teedataobject *)link);
         to->index = 0;
     }
     value = teedataobject_getitem(to->dataobj, to->index);
diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -621,7 +621,7 @@
     if (Handlers[SIGINT].func == DefaultHandler) {
         /* Install default int handler */
         Py_INCREF(IntHandler);
-        Py_XSETREF(Handlers[SIGINT].func, IntHandler);
+        Py_SETREF(Handlers[SIGINT].func, IntHandler);
         old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
     }
 
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c
--- a/Modules/zlibmodule.c
+++ b/Modules/zlibmodule.c
@@ -491,7 +491,7 @@
                       PyString_AS_STRING(self->unused_data), old_size);
             Py_MEMCPY(PyString_AS_STRING(new_data) + old_size,
                       self->zst.next_in, self->zst.avail_in);
-            Py_XSETREF(self->unused_data, new_data);
+            Py_SETREF(self->unused_data, new_data);
             self->zst.avail_in = 0;
         }
     }
@@ -503,7 +503,7 @@
                 (char *)self->zst.next_in, self->zst.avail_in);
         if (new_data == NULL)
             return -1;
-        Py_XSETREF(self->unconsumed_tail, new_data);
+        Py_SETREF(self->unconsumed_tail, new_data);
     }
     return 0;
 }
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -59,7 +59,7 @@
         return -1;
 
     Py_INCREF(args);
-    Py_XSETREF(self->args, args);
+    Py_SETREF(self->args, args);
 
     if (PyTuple_GET_SIZE(self->args) == 1) {
         Py_INCREF(PyTuple_GET_ITEM(self->args, 0));
@@ -622,7 +622,7 @@
         if (!subslice)
             return -1;
 
-        Py_XSETREF(self->args, subslice);
+        Py_SETREF(self->args, subslice);
     }
     return 0;
 }
diff --git a/Objects/fileobject.c b/Objects/fileobject.c
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -574,8 +574,8 @@
         oerrors = Py_None;
         Py_INCREF(Py_None);
     }
-    Py_XSETREF(file->f_encoding, str);
-    Py_XSETREF(file->f_errors, oerrors);
+    Py_SETREF(file->f_encoding, str);
+    Py_SETREF(file->f_errors, oerrors);
     return 1;
 }
 
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -428,7 +428,7 @@
 
     if (name != Py_None) {
         Py_INCREF(name);
-        Py_XSETREF(newfunc->func_name, name);
+        Py_SETREF(newfunc->func_name, name);
     }
     if (defaults != Py_None) {
         Py_INCREF(defaults);
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -3865,7 +3865,7 @@
         return;
     }
     v = string_concat((PyStringObject *) *pv, w);
-    Py_XSETREF(*pv, v);
+    Py_SETREF(*pv, v);
 }
 
 void
@@ -4750,7 +4750,7 @@
     t = PyDict_GetItem(interned, (PyObject *)s);
     if (t) {
         Py_INCREF(t);
-        Py_XSETREF(*p, t);
+        Py_SETREF(*p, t);
         return;
     }
 
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -436,7 +436,7 @@
             return -1;
         Py_UNICODE_COPY(w->str, v->str,
                         length < v->length ? length : v->length);
-        Py_XSETREF(*unicode, w);
+        Py_SETREF(*unicode, w);
         return 0;
     }
 
diff --git a/Python/_warnings.c b/Python/_warnings.c
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -528,7 +528,7 @@
                     goto handle_error;
                 }
                 else if (!is_true) {
-                    Py_XSETREF(*filename, PyString_FromString("__main__"));
+                    Py_SETREF(*filename, PyString_FromString("__main__"));
                     if (*filename == NULL)
                         goto handle_error;
                 }
diff --git a/Python/ceval.c b/Python/ceval.c
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -4360,7 +4360,7 @@
             Py_INCREF(self);
             func = PyMethod_GET_FUNCTION(func);
             Py_INCREF(func);
-            Py_XSETREF(*pfunc, self);
+            Py_SETREF(*pfunc, self);
             na++;
             n++;
         } else
diff --git a/Python/errors.c b/Python/errors.c
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -229,9 +229,9 @@
         --tstate->recursion_depth;
         /* throw away the old exception and use the recursion error instead */
         Py_INCREF(PyExc_RuntimeError);
-        Py_XSETREF(*exc, PyExc_RuntimeError);
+        Py_SETREF(*exc, PyExc_RuntimeError);
         Py_INCREF(PyExc_RecursionErrorInst);
-        Py_XSETREF(*val, PyExc_RecursionErrorInst);
+        Py_SETREF(*val, PyExc_RecursionErrorInst);
         /* just keeping the old traceback */
         return;
     }

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list