Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f01e408

Browse files
Issue #26200: Added Py_SETREF and replaced Py_XSETREF with Py_SETREF
in places where Py_DECREF was used.
2 parents 47c5474 + 57a01d3 commit f01e408

23 files changed

Lines changed: 58 additions & 48 deletions

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);
@@ -5150,7 +5150,7 @@ comerror_init(PyObject *self, PyObject *args, PyObject *kwds)
51505150
return -1;
51515151

51525152
Py_INCREF(args);
5153-
Py_XSETREF(((PyBaseExceptionObject *)self)->args, args);
5153+
Py_SETREF(((PyBaseExceptionObject *)self)->args, args);
51545154

51555155
return 0;
51565156
}

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/_datetimemodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ format_utcoffset(char *buf, size_t buflen, const char *sep,
10581058
/* Offset is normalized, so it is negative if days < 0 */
10591059
if (GET_TD_DAYS(offset) < 0) {
10601060
sign = '-';
1061-
Py_XSETREF(offset, delta_negative((PyDateTime_Delta *)offset));
1061+
Py_SETREF(offset, delta_negative((PyDateTime_Delta *)offset));
10621062
if (offset == NULL)
10631063
return -1;
10641064
}
@@ -3045,7 +3045,7 @@ tzinfo_fromutc(PyDateTime_TZInfo *self, PyObject *dt)
30453045
if (dst == Py_None)
30463046
goto Inconsistent;
30473047
if (delta_bool((PyDateTime_Delta *)dst) != 0) {
3048-
Py_XSETREF(result, add_datetime_timedelta((PyDateTime_DateTime *)result,
3048+
Py_SETREF(result, add_datetime_timedelta((PyDateTime_DateTime *)result,
30493049
(PyDateTime_Delta *)dst, 1));
30503050
if (result == NULL)
30513051
goto Fail;
@@ -4445,7 +4445,7 @@ datetime_subtract(PyObject *left, PyObject *right)
44454445
return NULL;
44464446

44474447
if (offdiff != NULL) {
4448-
Py_XSETREF(result, delta_subtract(result, offdiff));
4448+
Py_SETREF(result, delta_subtract(result, offdiff));
44494449
Py_DECREF(offdiff);
44504450
}
44514451
}

Modules/_elementtree.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,7 +1957,7 @@ element_tag_setter(ElementObject *self, PyObject *value, void *closure)
19571957
{
19581958
_VALIDATE_ATTR_VALUE(value);
19591959
Py_INCREF(value);
1960-
Py_XSETREF(self->tag, value);
1960+
Py_SETREF(self->tag, value);
19611961
return 0;
19621962
}
19631963

@@ -1990,7 +1990,7 @@ element_attrib_setter(ElementObject *self, PyObject *value, void *closure)
19901990
return -1;
19911991
}
19921992
Py_INCREF(value);
1993-
Py_XSETREF(self->extra->attrib, value);
1993+
Py_SETREF(self->extra->attrib, value);
19941994
return 0;
19951995
}
19961996

@@ -2524,9 +2524,9 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag,
25242524
self->index++;
25252525

25262526
Py_INCREF(node);
2527-
Py_XSETREF(self->this, node);
2527+
Py_SETREF(self->this, node);
25282528
Py_INCREF(node);
2529-
Py_XSETREF(self->last, node);
2529+
Py_SETREF(self->last, node);
25302530

25312531
if (treebuilder_append_event(self, self->start_event_obj, node) < 0)
25322532
goto error;

Modules/_io/bytesio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ unshare_buffer(bytesio *self, size_t size)
9696
return -1;
9797
memcpy(PyBytes_AS_STRING(new_buf), PyBytes_AS_STRING(self->buf),
9898
self->string_size);
99-
Py_XSETREF(self->buf, new_buf);
99+
Py_SETREF(self->buf, new_buf);
100100
return 0;
101101
}
102102

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;
@@ -793,7 +793,7 @@ static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self
793793
}
794794
}
795795

796-
Py_XSETREF(self->statements, new_list);
796+
Py_SETREF(self->statements, new_list);
797797
}
798798

799799
static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
@@ -824,7 +824,7 @@ static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
824824
}
825825
}
826826

827-
Py_XSETREF(self->cursors, new_list);
827+
Py_SETREF(self->cursors, new_list);
828828
}
829829

830830
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
@@ -509,7 +509,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
509509

510510
/* reset description and rowcount */
511511
Py_INCREF(Py_None);
512-
Py_XSETREF(self->description, Py_None);
512+
Py_SETREF(self->description, Py_None);
513513
self->rowcount = -1L;
514514

515515
func_args = PyTuple_New(1);
@@ -534,7 +534,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
534534
}
535535

536536
if (self->statement->in_use) {
537-
Py_XSETREF(self->statement,
537+
Py_SETREF(self->statement,
538538
PyObject_New(pysqlite_Statement, &pysqlite_StatementType));
539539
if (!self->statement) {
540540
goto error;
@@ -651,7 +651,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
651651
numcols = sqlite3_column_count(self->statement->st);
652652
Py_END_ALLOW_THREADS
653653

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

Modules/_sre.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ deepcopy(PyObject** object, PyObject* memo)
753753
if (!copy)
754754
return 0;
755755

756-
Py_XSETREF(*object, copy);
756+
Py_SETREF(*object, copy);
757757

758758
return 1; /* success */
759759
}

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 {

0 commit comments

Comments
 (0)