Skip to content

Commit 57a01d3

Browse files
Issue #26200: Added Py_SETREF and replaced Py_XSETREF with Py_SETREF
in places where Py_DECREF was used.
1 parent e0805cf commit 57a01d3

20 files changed

+52
-42
lines changed

Include/object.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,18 +851,28 @@ PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
851851
*
852852
* As in case of Py_CLEAR "the obvious" code can be deadly:
853853
*
854-
* Py_XDECREF(op);
854+
* Py_DECREF(op);
855855
* op = op2;
856856
*
857857
* The safe way is:
858858
*
859-
* Py_XSETREF(op, op2);
859+
* Py_SETREF(op, op2);
860860
*
861861
* That arranges to set `op` to `op2` _before_ decref'ing, so that any code
862862
* triggered as a side-effect of `op` getting torn down no longer believes
863863
* `op` points to a valid object.
864+
*
865+
* Py_XSETREF is a variant of Py_SETREF that uses Py_XDECREF instead of
866+
* Py_DECREF.
864867
*/
865868

869+
#define Py_SETREF(op, op2) \
870+
do { \
871+
PyObject *_py_tmp = (PyObject *)(op); \
872+
(op) = (op2); \
873+
Py_DECREF(_py_tmp); \
874+
} while (0)
875+
866876
#define Py_XSETREF(op, op2) \
867877
do { \
868878
PyObject *_py_tmp = (PyObject *)(op); \

Modules/_ctypes/_ctypes.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isSt
391391
Py_DECREF((PyObject *)dict);
392392
return NULL;
393393
}
394-
Py_XSETREF(result->tp_dict, (PyObject *)dict);
394+
Py_SETREF(result->tp_dict, (PyObject *)dict);
395395
dict->format = _ctypes_alloc_format_string(NULL, "B");
396396
if (dict->format == NULL) {
397397
Py_DECREF(result);
@@ -960,7 +960,7 @@ PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
960960
Py_DECREF((PyObject *)stgdict);
961961
return NULL;
962962
}
963-
Py_XSETREF(result->tp_dict, (PyObject *)stgdict);
963+
Py_SETREF(result->tp_dict, (PyObject *)stgdict);
964964

965965
return (PyObject *)result;
966966
}
@@ -1403,7 +1403,7 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
14031403
/* replace the class dict by our updated spam dict */
14041404
if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict))
14051405
goto error;
1406-
Py_XSETREF(result->tp_dict, (PyObject *)stgdict); /* steal the reference */
1406+
Py_SETREF(result->tp_dict, (PyObject *)stgdict); /* steal the reference */
14071407
stgdict = NULL;
14081408

14091409
/* Special case for character arrays.
@@ -1816,7 +1816,7 @@ static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject
18161816
Py_DECREF((PyObject *)stgdict);
18171817
return NULL;
18181818
}
1819-
Py_XSETREF(result->tp_dict, (PyObject *)stgdict);
1819+
Py_SETREF(result->tp_dict, (PyObject *)stgdict);
18201820

18211821
return (PyObject *)result;
18221822
}
@@ -1944,7 +1944,7 @@ PyCSimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
19441944
Py_DECREF((PyObject *)stgdict);
19451945
return NULL;
19461946
}
1947-
Py_XSETREF(result->tp_dict, (PyObject *)stgdict);
1947+
Py_SETREF(result->tp_dict, (PyObject *)stgdict);
19481948

19491949
/* Install from_param class methods in ctypes base classes.
19501950
Overrides the PyCSimpleType_from_param generic method.
@@ -2307,7 +2307,7 @@ PyCFuncPtrType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
23072307
Py_DECREF((PyObject *)stgdict);
23082308
return NULL;
23092309
}
2310-
Py_XSETREF(result->tp_dict, (PyObject *)stgdict);
2310+
Py_SETREF(result->tp_dict, (PyObject *)stgdict);
23112311

23122312
if (-1 == make_funcptrtype_dict(stgdict)) {
23132313
Py_DECREF(result);
@@ -5152,7 +5152,7 @@ comerror_init(PyObject *self, PyObject *args, PyObject *kwds)
51525152

51535153
bself = (PyBaseExceptionObject *)self;
51545154
Py_INCREF(args);
5155-
Py_XSETREF(bself->args, args);
5155+
Py_SETREF(bself->args, args);
51565156

51575157
return 0;
51585158
}

Modules/_curses_panel.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ PyCursesPanel_replace_panel(PyCursesPanelObject *self, PyObject *args)
313313
return NULL;
314314
}
315315
Py_INCREF(temp);
316-
Py_XSETREF(po->wo, temp);
316+
Py_SETREF(po->wo, temp);
317317
Py_INCREF(Py_None);
318318
return Py_None;
319319
}

Modules/_elementtree.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,7 +1943,7 @@ element_setattro(ElementObject* self, PyObject* nameobj, PyObject* value)
19431943

19441944
if (strcmp(name, "tag") == 0) {
19451945
Py_INCREF(value);
1946-
Py_XSETREF(self->tag, value);
1946+
Py_SETREF(self->tag, value);
19471947
} else if (strcmp(name, "text") == 0) {
19481948
Py_DECREF(JOIN_OBJ(self->text));
19491949
self->text = value;
@@ -1958,7 +1958,7 @@ element_setattro(ElementObject* self, PyObject* nameobj, PyObject* value)
19581958
return -1;
19591959
}
19601960
Py_INCREF(value);
1961-
Py_XSETREF(self->extra->attrib, value);
1961+
Py_SETREF(self->extra->attrib, value);
19621962
} else {
19631963
PyErr_SetString(PyExc_AttributeError,
19641964
"Can't set arbitrary attributes on Element");
@@ -2551,9 +2551,9 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag,
25512551
self->index++;
25522552

25532553
Py_INCREF(node);
2554-
Py_XSETREF(self->this, node);
2554+
Py_SETREF(self->this, node);
25552555
Py_INCREF(node);
2556-
Py_XSETREF(self->last, node);
2556+
Py_SETREF(self->last, node);
25572557

25582558
if (treebuilder_append_event(self, self->start_event_obj, node) < 0)
25592559
goto error;

Modules/_sqlite/connection.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ void pysqlite_flush_statement_cache(pysqlite_Connection* self)
204204
node = node->next;
205205
}
206206

207-
Py_XSETREF(self->statement_cache,
207+
Py_SETREF(self->statement_cache,
208208
(pysqlite_Cache *)PyObject_CallFunction((PyObject *)&pysqlite_CacheType, "O", self));
209209
Py_DECREF(self);
210210
self->statement_cache->decref_factory = 0;
@@ -794,7 +794,7 @@ static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self
794794
}
795795
}
796796

797-
Py_XSETREF(self->statements, new_list);
797+
Py_SETREF(self->statements, new_list);
798798
}
799799

800800
static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
@@ -825,7 +825,7 @@ static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
825825
}
826826
}
827827

828-
Py_XSETREF(self->cursors, new_list);
828+
Py_SETREF(self->cursors, new_list);
829829
}
830830

831831
PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)

Modules/_sqlite/cursor.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
510510

511511
/* reset description and rowcount */
512512
Py_INCREF(Py_None);
513-
Py_XSETREF(self->description, Py_None);
513+
Py_SETREF(self->description, Py_None);
514514
self->rowcount = -1L;
515515

516516
func_args = PyTuple_New(1);
@@ -535,7 +535,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
535535
}
536536

537537
if (self->statement->in_use) {
538-
Py_XSETREF(self->statement,
538+
Py_SETREF(self->statement,
539539
PyObject_New(pysqlite_Statement, &pysqlite_StatementType));
540540
if (!self->statement) {
541541
goto error;
@@ -652,7 +652,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
652652
numcols = sqlite3_column_count(self->statement->st);
653653
Py_END_ALLOW_THREADS
654654

655-
Py_XSETREF(self->description, PyTuple_New(numcols));
655+
Py_SETREF(self->description, PyTuple_New(numcols));
656656
if (!self->description) {
657657
goto error;
658658
}

Modules/_sre.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ deepcopy(PyObject** object, PyObject* memo)
756756
if (!copy)
757757
return 0;
758758

759-
Py_XSETREF(*object, copy);
759+
Py_SETREF(*object, copy);
760760

761761
return 1; /* success */
762762
}

Modules/_ssl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1589,7 +1589,7 @@ static int PySSL_set_context(PySSLSocket *self, PyObject *value,
15891589
return -1;
15901590
#else
15911591
Py_INCREF(value);
1592-
Py_XSETREF(self->ctx, (PySSLContext *)value);
1592+
Py_SETREF(self->ctx, (PySSLContext *)value);
15931593
SSL_set_SSL_CTX(self->ssl, self->ctx->ctx);
15941594
#endif
15951595
} else {

Modules/itertoolsmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ tee_next(teeobject *to)
631631
link = teedataobject_jumplink(to->dataobj);
632632
if (link == NULL)
633633
return NULL;
634-
Py_XSETREF(to->dataobj, (teedataobject *)link);
634+
Py_SETREF(to->dataobj, (teedataobject *)link);
635635
to->index = 0;
636636
}
637637
value = teedataobject_getitem(to->dataobj, to->index);

Modules/signalmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ PyInit__signal(void)
12661266
if (Handlers[SIGINT].func == DefaultHandler) {
12671267
/* Install default int handler */
12681268
Py_INCREF(IntHandler);
1269-
Py_XSETREF(Handlers[SIGINT].func, IntHandler);
1269+
Py_SETREF(Handlers[SIGINT].func, IntHandler);
12701270
old_siginthandler = PyOS_setsig(SIGINT, signal_handler);
12711271
}
12721272

0 commit comments

Comments
 (0)