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

+12-2
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

+7-7
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

+1-1
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

+4-4
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

+3-3
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

+3-3
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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

Modules/zipimport.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds)
155155
tmp = PyUnicode_FromFormat("%U%c", self->prefix, SEP);
156156
if (tmp == NULL)
157157
goto error;
158-
Py_XSETREF(self->prefix, tmp);
158+
Py_SETREF(self->prefix, tmp);
159159
}
160160
}
161161
else

Modules/zlibmodule.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ save_unconsumed_input(compobject *self, int err)
668668
PyBytes_AS_STRING(self->unused_data), old_size);
669669
Py_MEMCPY(PyBytes_AS_STRING(new_data) + old_size,
670670
self->zst.next_in, self->zst.avail_in);
671-
Py_XSETREF(self->unused_data, new_data);
671+
Py_SETREF(self->unused_data, new_data);
672672
self->zst.avail_in = 0;
673673
}
674674
}
@@ -680,7 +680,7 @@ save_unconsumed_input(compobject *self, int err)
680680
(char *)self->zst.next_in, self->zst.avail_in);
681681
if (new_data == NULL)
682682
return -1;
683-
Py_XSETREF(self->unconsumed_tail, new_data);
683+
Py_SETREF(self->unconsumed_tail, new_data);
684684
}
685685
return 0;
686686
}

Objects/bytesobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3520,7 +3520,7 @@ PyBytes_Concat(PyObject **pv, PyObject *w)
35203520
/* Multiple references, need to create new object */
35213521
PyObject *v;
35223522
v = bytes_concat(*pv, w);
3523-
Py_XSETREF(*pv, v);
3523+
Py_SETREF(*pv, v);
35243524
}
35253525
}
35263526

Objects/funcobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ func_new(PyTypeObject* type, PyObject* args, PyObject* kw)
527527

528528
if (name != Py_None) {
529529
Py_INCREF(name);
530-
Py_XSETREF(newfunc->func_name, name);
530+
Py_SETREF(newfunc->func_name, name);
531531
}
532532
if (defaults != Py_None) {
533533
Py_INCREF(defaults);

Objects/rangeobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ longrangeiter_next(longrangeiterobject *r)
10641064
result = PyNumber_Add(r->start, product);
10651065
Py_DECREF(product);
10661066
if (result) {
1067-
Py_XSETREF(r->index, new_index);
1067+
Py_SETREF(r->index, new_index);
10681068
}
10691069
else {
10701070
Py_DECREF(new_index);

Objects/typeobject.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ type_set_name(PyTypeObject *type, PyObject *value, void *context)
424424

425425
type->tp_name = tp_name;
426426
Py_INCREF(value);
427-
Py_XSETREF(((PyHeapTypeObject*)type)->ht_name, value);
427+
Py_SETREF(((PyHeapTypeObject*)type)->ht_name, value);
428428

429429
return 0;
430430
}
@@ -445,7 +445,7 @@ type_set_qualname(PyTypeObject *type, PyObject *value, void *context)
445445

446446
et = (PyHeapTypeObject*)type;
447447
Py_INCREF(value);
448-
Py_XSETREF(et->ht_qualname, value);
448+
Py_SETREF(et->ht_qualname, value);
449449
return 0;
450450
}
451451

@@ -2897,7 +2897,7 @@ _PyType_Lookup(PyTypeObject *type, PyObject *name)
28972897
else
28982898
method_cache_misses++;
28992899
#endif
2900-
Py_XSETREF(method_cache[h].name, name);
2900+
Py_SETREF(method_cache[h].name, name);
29012901
}
29022902
return res;
29032903
}

Objects/unicodeobject.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1667,15 +1667,15 @@ unicode_resize(PyObject **p_unicode, Py_ssize_t length)
16671667
_Py_INCREF_UNICODE_EMPTY();
16681668
if (!unicode_empty)
16691669
return -1;
1670-
Py_XSETREF(*p_unicode, unicode_empty);
1670+
Py_SETREF(*p_unicode, unicode_empty);
16711671
return 0;
16721672
}
16731673

16741674
if (!unicode_modifiable(unicode)) {
16751675
PyObject *copy = resize_copy(unicode, length);
16761676
if (copy == NULL)
16771677
return -1;
1678-
Py_XSETREF(*p_unicode, copy);
1678+
Py_SETREF(*p_unicode, copy);
16791679
return 0;
16801680
}
16811681

@@ -13326,7 +13326,7 @@ _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer,
1332613326
return -1;
1332713327
_PyUnicode_FastCopyCharacters(newbuffer, 0,
1332813328
writer->buffer, 0, writer->pos);
13329-
Py_XSETREF(writer->buffer, newbuffer);
13329+
Py_SETREF(writer->buffer, newbuffer);
1333013330
}
1333113331
_PyUnicodeWriter_Update(writer);
1333213332
return 0;
@@ -15012,7 +15012,7 @@ PyUnicode_InternInPlace(PyObject **p)
1501215012

1501315013
if (t) {
1501415014
Py_INCREF(t);
15015-
Py_XSETREF(*p, t);
15015+
Py_SETREF(*p, t);
1501615016
return;
1501715017
}
1501815018

Python/_warnings.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
680680
goto handle_error;
681681
}
682682
else if (!is_true) {
683-
Py_XSETREF(*filename, PyUnicode_FromString("__main__"));
683+
Py_SETREF(*filename, PyUnicode_FromString("__main__"));
684684
if (*filename == NULL)
685685
goto handle_error;
686686
}

Python/ceval.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -3229,7 +3229,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
32293229
Py_INCREF(self);
32303230
func = PyMethod_GET_FUNCTION(func);
32313231
Py_INCREF(func);
3232-
Py_XSETREF(*pfunc, self);
3232+
Py_SETREF(*pfunc, self);
32333233
na++;
32343234
/* n++; */
32353235
} else
@@ -4682,7 +4682,7 @@ call_function(PyObject ***pp_stack, int oparg
46824682
Py_INCREF(self);
46834683
func = PyMethod_GET_FUNCTION(func);
46844684
Py_INCREF(func);
4685-
Py_XSETREF(*pfunc, self);
4685+
Py_SETREF(*pfunc, self);
46864686
na++;
46874687
n++;
46884688
} else

Python/errors.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,9 @@ PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
311311
--tstate->recursion_depth;
312312
/* throw away the old exception and use the recursion error instead */
313313
Py_INCREF(PyExc_RecursionError);
314-
Py_XSETREF(*exc, PyExc_RecursionError);
314+
Py_SETREF(*exc, PyExc_RecursionError);
315315
Py_INCREF(PyExc_RecursionErrorInst);
316-
Py_XSETREF(*val, PyExc_RecursionErrorInst);
316+
Py_SETREF(*val, PyExc_RecursionErrorInst);
317317
/* just keeping the old traceback */
318318
return;
319319
}

0 commit comments

Comments
 (0)