]> granicus.if.org Git - python/commitdiff
Issue #26200: Added Py_SETREF and replaced Py_XSETREF with Py_SETREF
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 10 Apr 2016 15:05:12 +0000 (18:05 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Sun, 10 Apr 2016 15:05:12 +0000 (18:05 +0300)
in places where Py_DECREF was used.

22 files changed:
Include/object.h
Modules/_bsddb.c
Modules/_ctypes/_ctypes.c
Modules/_curses_panel.c
Modules/_elementtree.c
Modules/_json.c
Modules/_sqlite/connection.c
Modules/_sqlite/cursor.c
Modules/_sre.c
Modules/_ssl.c
Modules/bz2module.c
Modules/itertoolsmodule.c
Modules/signalmodule.c
Modules/zlibmodule.c
Objects/exceptions.c
Objects/fileobject.c
Objects/funcobject.c
Objects/stringobject.c
Objects/unicodeobject.c
Python/_warnings.c
Python/ceval.c
Python/errors.c

index 6481a5b65f16ed66e70c082a9e95be7e208dc251..9fc05831f94925f2fcbbbd6214ba6cf655e752e1 100644 (file)
@@ -828,18 +828,28 @@ PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force);
  *
  * 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);   \
index 5fd52d99fa65b10c253f35d6f2bf983b8b670408..a319333b8eaf848f3a95ab27acacd0c51a65fea9 100644 (file)
@@ -2498,7 +2498,7 @@ DB_set_private(DBObject* self, PyObject* private_obj)
 {
     /* 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 @@ DBEnv_set_private(DBEnvObject* self, PyObject* private_obj)
 {
     /* 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 @@ DBEnv_rep_set_transport(DBEnvObject* self, PyObject* args)
     RETURN_IF_ERR();
 
     Py_INCREF(rep_transport);
-    Py_XSETREF(self->rep_transport, rep_transport);
+    Py_SETREF(self->rep_transport, rep_transport);
     RETURN_NONE();
 }
 
index 7f66d6a85f997f3fe3b57026fe6e319e3c7ad778..65c55cb3f805662c6503ffe680ce0866e4701fd9 100644 (file)
@@ -424,7 +424,7 @@ StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isSt
         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 @@ PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
         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 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
         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 @@ static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject
         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 @@ PyCFuncPtrType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
         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);
index dbc0f5332a7efd709e88b48db1d15012694a7329..d61281ed06bf083a3319785cef718693f8b3a997 100644 (file)
@@ -284,7 +284,7 @@ PyCursesPanel_replace_panel(PyCursesPanelObject *self, PyObject *args)
         return NULL;
     }
     Py_INCREF(temp);
-    Py_XSETREF(po->wo, temp);
+    Py_SETREF(po->wo, temp);
     Py_INCREF(Py_None);
     return Py_None;
 }
index 793d4c73c631d5833bedb0c53913ef18f5c65500..0d16a697a0ea1d06a795b6ed81fae8d51d21eb26 100644 (file)
@@ -1574,7 +1574,7 @@ element_setattr(ElementObject* self, const char* name, PyObject* value)
 
     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 @@ element_setattr(ElementObject* self, const char* name, PyObject* value)
         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 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag,
     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;
index 9c8f66ce110b333db755debedc0c6cfa553734ac..fede6b14a17cfaf1e4e75d13bcf5234947887da8 100644 (file)
@@ -1717,7 +1717,7 @@ scanner_init(PyObject *self, PyObject *args, PyObject *kwds)
     }
     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;
index 9ced405acafc921c332c04a74c4c8ecb4a00051b..6b0e3c628b764b0a4b675b55aca7d5dc5a9b1471 100644 (file)
@@ -228,7 +228,7 @@ void pysqlite_flush_statement_cache(pysqlite_Connection* self)
         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 @@ static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self
         }
     }
 
-    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 @@ static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
         }
     }
 
-    Py_XSETREF(self->cursors, new_list);
+    Py_SETREF(self->cursors, new_list);
 }
 
 PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
index b3ec8f253c904e146b7c2d8f1537e8ea257956b6..f2b48ffa3a5638e40023d7e2b9e490a99e8d9f99 100644 (file)
@@ -544,7 +544,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
 
     /* 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 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
     }
 
     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 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
                 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;
                 }
index 485020e127c03f4844ac3f2a0c72fe6365b14784..829fb6bdcc8734c3569cf79080874d2dafa5d8d3 100644 (file)
@@ -2054,7 +2054,7 @@ deepcopy(PyObject** object, PyObject* memo)
     if (!copy)
         return 0;
 
-    Py_XSETREF(*object, copy);
+    Py_SETREF(*object, copy);
 
     return 1; /* success */
 }
index a44414b1888e484dbbd7af62cb0630e6d2c891bb..23d4d5ceab22075721d1bcb15cf9f4dd37e87540 100644 (file)
@@ -1467,7 +1467,7 @@ static int PySSL_set_context(PySSLSocket *self, PyObject *value,
         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 {
index c33e03ca223afeaa718fbf1147d3ba769ce72826..fe417c5ffa0bfef254992bee43b9870a90fb0b3e 100644 (file)
@@ -1953,7 +1953,7 @@ BZ2Decomp_decompress(BZ2DecompObject *self, PyObject *args)
             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;
index 24933211f98f564c8f793d565ac1d297b4533f79..68285b82f6c98c4184912f68159dee7232a4281a 100644 (file)
@@ -492,7 +492,7 @@ tee_next(teeobject *to)
         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);
index 81674c80f57040bdd69083ff42f31772b10e40ce..c0e17f3a2e5625d3b0bde4b745a1e4da2e80e1bf 100644 (file)
@@ -621,7 +621,7 @@ initsignal(void)
     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);
     }
 
index f3282b2eeb8425953737915c35b09973e1596d0d..799e7dfeb02fb27917c810a4e9b2fd518c7f4914 100644 (file)
@@ -491,7 +491,7 @@ save_unconsumed_input(compobject *self, int err)
                       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 @@ save_unconsumed_input(compobject *self, int err)
                 (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;
 }
index 8906f1a786d74f3989073895b28549ab9564922b..3fc0e137860cbcf39b68d3f1b40d50e1a2075648 100644 (file)
@@ -59,7 +59,7 @@ BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
         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 @@ EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,
         if (!subslice)
             return -1;
 
-        Py_XSETREF(self->args, subslice);
+        Py_SETREF(self->args, subslice);
     }
     return 0;
 }
index e4f7fc404dd68c81009a31b5d303ff0e85054644..9ae068cfc59d403b1fa7fc40dc187373f95cf21f 100644 (file)
@@ -574,8 +574,8 @@ PyFile_SetEncodingAndErrors(PyObject *f, const char *enc, char* errors)
         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;
 }
 
index 26369626f959ba6f673cdcd070fdce435b49d9e9..0e76a446afc92a5aaeeb4752e8865f6ffd520a75 100644 (file)
@@ -428,7 +428,7 @@ func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
 
     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);
index faaa11125cda86fdff88e3423c0c3efcc5bf5ab9..f585ffc7282111bd798c6a81ed521d6d84f0dfe3 100644 (file)
@@ -3865,7 +3865,7 @@ PyString_Concat(register PyObject **pv, register PyObject *w)
         return;
     }
     v = string_concat((PyStringObject *) *pv, w);
-    Py_XSETREF(*pv, v);
+    Py_SETREF(*pv, v);
 }
 
 void
@@ -4750,7 +4750,7 @@ PyString_InternInPlace(PyObject **p)
     t = PyDict_GetItem(interned, (PyObject *)s);
     if (t) {
         Py_INCREF(t);
-        Py_XSETREF(*p, t);
+        Py_SETREF(*p, t);
         return;
     }
 
index d06ce2cb97026078f1d0cd5b2d19fc53a29ad5c9..0b81a3cbf6536204c4eaf7567785c886c7e6d7b7 100644 (file)
@@ -436,7 +436,7 @@ int _PyUnicode_Resize(PyUnicodeObject **unicode, Py_ssize_t length)
             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;
     }
 
index 932f2f8d13d25015550ef3cad4a81af05c1b5c59..8e8c0cceb311df6752f4ebe751318128c38f723d 100644 (file)
@@ -528,7 +528,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
                     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;
                 }
index e56e9574900fe80b5a56849a1a1dbe64d0fc4301..6e5e27280e9cf1e50fca104511d50256b4e6e73c 100644 (file)
@@ -4360,7 +4360,7 @@ call_function(PyObject ***pp_stack, int oparg
             Py_INCREF(self);
             func = PyMethod_GET_FUNCTION(func);
             Py_INCREF(func);
-            Py_XSETREF(*pfunc, self);
+            Py_SETREF(*pfunc, self);
             na++;
             n++;
         } else
index 466a2534dd6a1997af9241fe5d0530fc39f5b24e..d823e1397ccb6a56778f2d4be70965fa887e3d80 100644 (file)
@@ -229,9 +229,9 @@ finally:
         --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;
     }