Skip to content

Commit 763a61c

Browse files
Issue #26200: Added Py_SETREF and replaced Py_XSETREF with Py_SETREF
in places where Py_DECREF was used.
1 parent 149d080 commit 763a61c

22 files changed

+51
-41
lines changed

Include/object.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -828,18 +828,28 @@ PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force);
828828
*
829829
* As in case of Py_CLEAR "the obvious" code can be deadly:
830830
*
831-
* Py_XDECREF(op);
831+
* Py_DECREF(op);
832832
* op = op2;
833833
*
834834
* The safe way is:
835835
*
836-
* Py_XSETREF(op, op2);
836+
* Py_SETREF(op, op2);
837837
*
838838
* That arranges to set `op` to `op2` _before_ decref'ing, so that any code
839839
* triggered as a side-effect of `op` getting torn down no longer believes
840840
* `op` points to a valid object.
841+
*
842+
* Py_XSETREF is a variant of Py_SETREF that uses Py_XDECREF instead of
843+
* Py_DECREF.
841844
*/
842845

846+
#define Py_SETREF(op, op2) \
847+
do { \
848+
PyObject *_py_tmp = (PyObject *)(op); \
849+
(op) = (op2); \
850+
Py_DECREF(_py_tmp); \
851+
} while (0)
852+
843853
#define Py_XSETREF(op, op2) \
844854
do { \
845855
PyObject *_py_tmp = (PyObject *)(op); \

Modules/_bsddb.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2498,7 +2498,7 @@ DB_set_private(DBObject* self, PyObject* private_obj)
24982498
{
24992499
/* We can set the private field even if db is closed */
25002500
Py_INCREF(private_obj);
2501-
Py_XSETREF(self->private_obj, private_obj);
2501+
Py_SETREF(self->private_obj, private_obj);
25022502
RETURN_NONE();
25032503
}
25042504

@@ -6997,7 +6997,7 @@ DBEnv_set_private(DBEnvObject* self, PyObject* private_obj)
69976997
{
69986998
/* We can set the private field even if dbenv is closed */
69996999
Py_INCREF(private_obj);
7000-
Py_XSETREF(self->private_obj, private_obj);
7000+
Py_SETREF(self->private_obj, private_obj);
70017001
RETURN_NONE();
70027002
}
70037003

@@ -7410,7 +7410,7 @@ DBEnv_rep_set_transport(DBEnvObject* self, PyObject* args)
74107410
RETURN_IF_ERR();
74117411

74127412
Py_INCREF(rep_transport);
7413-
Py_XSETREF(self->rep_transport, rep_transport);
7413+
Py_SETREF(self->rep_transport, rep_transport);
74147414
RETURN_NONE();
74157415
}
74167416

Modules/_ctypes/_ctypes.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isSt
424424
Py_DECREF((PyObject *)dict);
425425
return NULL;
426426
}
427-
Py_XSETREF(result->tp_dict, (PyObject *)dict);
427+
Py_SETREF(result->tp_dict, (PyObject *)dict);
428428
dict->format = _ctypes_alloc_format_string(NULL, "B");
429429
if (dict->format == NULL) {
430430
Py_DECREF(result);
@@ -992,7 +992,7 @@ PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
992992
Py_DECREF((PyObject *)stgdict);
993993
return NULL;
994994
}
995-
Py_XSETREF(result->tp_dict, (PyObject *)stgdict);
995+
Py_SETREF(result->tp_dict, (PyObject *)stgdict);
996996

997997
return (PyObject *)result;
998998
}
@@ -1457,7 +1457,7 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
14571457
Py_DECREF((PyObject *)stgdict);
14581458
return NULL;
14591459
}
1460-
Py_XSETREF(result->tp_dict, (PyObject *)stgdict);
1460+
Py_SETREF(result->tp_dict, (PyObject *)stgdict);
14611461

14621462
/* Special case for character arrays.
14631463
A permanent annoyance: char arrays are also strings!
@@ -1880,7 +1880,7 @@ static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject
18801880
Py_DECREF((PyObject *)stgdict);
18811881
return NULL;
18821882
}
1883-
Py_XSETREF(result->tp_dict, (PyObject *)stgdict);
1883+
Py_SETREF(result->tp_dict, (PyObject *)stgdict);
18841884

18851885
return (PyObject *)result;
18861886
}
@@ -2388,7 +2388,7 @@ PyCFuncPtrType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
23882388
Py_DECREF((PyObject *)stgdict);
23892389
return NULL;
23902390
}
2391-
Py_XSETREF(result->tp_dict, (PyObject *)stgdict);
2391+
Py_SETREF(result->tp_dict, (PyObject *)stgdict);
23922392

23932393
if (-1 == make_funcptrtype_dict(stgdict)) {
23942394
Py_DECREF(result);

Modules/_curses_panel.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ PyCursesPanel_replace_panel(PyCursesPanelObject *self, PyObject *args)
284284
return NULL;
285285
}
286286
Py_INCREF(temp);
287-
Py_XSETREF(po->wo, temp);
287+
Py_SETREF(po->wo, temp);
288288
Py_INCREF(Py_None);
289289
return Py_None;
290290
}

Modules/_elementtree.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,7 +1574,7 @@ element_setattr(ElementObject* self, const char* name, PyObject* value)
15741574

15751575
if (strcmp(name, "tag") == 0) {
15761576
Py_INCREF(value);
1577-
Py_XSETREF(self->tag, value);
1577+
Py_SETREF(self->tag, value);
15781578
} else if (strcmp(name, "text") == 0) {
15791579
Py_DECREF(JOIN_OBJ(self->text));
15801580
self->text = value;
@@ -1587,7 +1587,7 @@ element_setattr(ElementObject* self, const char* name, PyObject* value)
15871587
if (!self->extra)
15881588
element_new_extra(self, NULL);
15891589
Py_INCREF(value);
1590-
Py_XSETREF(self->extra->attrib, value);
1590+
Py_SETREF(self->extra->attrib, value);
15911591
} else {
15921592
PyErr_SetString(PyExc_AttributeError, name);
15931593
return -1;
@@ -1799,10 +1799,10 @@ treebuilder_handle_start(TreeBuilderObject* self, PyObject* tag,
17991799
self->index++;
18001800

18011801
Py_INCREF(node);
1802-
Py_XSETREF(self->this, (ElementObject*) node);
1802+
Py_SETREF(self->this, (ElementObject*) node);
18031803

18041804
Py_INCREF(node);
1805-
Py_XSETREF(self->last, (ElementObject*) node);
1805+
Py_SETREF(self->last, (ElementObject*) node);
18061806

18071807
if (treebuilder_append_event(self, self->start_event_obj, node) < 0)
18081808
goto error;

Modules/_json.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1717,7 +1717,7 @@ scanner_init(PyObject *self, PyObject *args, PyObject *kwds)
17171717
}
17181718
else if (PyUnicode_Check(s->encoding)) {
17191719
PyObject *tmp = PyUnicode_AsEncodedString(s->encoding, NULL, NULL);
1720-
Py_XSETREF(s->encoding, tmp);
1720+
Py_SETREF(s->encoding, tmp);
17211721
}
17221722
if (s->encoding == NULL)
17231723
goto bail;

Modules/_sqlite/connection.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ void pysqlite_flush_statement_cache(pysqlite_Connection* self)
228228
node = node->next;
229229
}
230230

231-
Py_XSETREF(self->statement_cache,
231+
Py_SETREF(self->statement_cache,
232232
(pysqlite_Cache *)PyObject_CallFunction((PyObject *)&pysqlite_CacheType, "O", self));
233233
Py_DECREF(self);
234234
self->statement_cache->decref_factory = 0;
@@ -815,7 +815,7 @@ static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self
815815
}
816816
}
817817

818-
Py_XSETREF(self->statements, new_list);
818+
Py_SETREF(self->statements, new_list);
819819
}
820820

821821
static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
@@ -846,7 +846,7 @@ static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
846846
}
847847
}
848848

849-
Py_XSETREF(self->cursors, new_list);
849+
Py_SETREF(self->cursors, new_list);
850850
}
851851

852852
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
@@ -544,7 +544,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
544544

545545
/* reset description and rowcount */
546546
Py_INCREF(Py_None);
547-
Py_XSETREF(self->description, Py_None);
547+
Py_SETREF(self->description, Py_None);
548548
self->rowcount = -1L;
549549

550550
func_args = PyTuple_New(1);
@@ -569,7 +569,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
569569
}
570570

571571
if (self->statement->in_use) {
572-
Py_XSETREF(self->statement,
572+
Py_SETREF(self->statement,
573573
PyObject_New(pysqlite_Statement, &pysqlite_StatementType));
574574
if (!self->statement) {
575575
goto error;
@@ -681,7 +681,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
681681
numcols = sqlite3_column_count(self->statement->st);
682682
Py_END_ALLOW_THREADS
683683

684-
Py_XSETREF(self->description, PyTuple_New(numcols));
684+
Py_SETREF(self->description, PyTuple_New(numcols));
685685
if (!self->description) {
686686
goto error;
687687
}

Modules/_sre.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2054,7 +2054,7 @@ deepcopy(PyObject** object, PyObject* memo)
20542054
if (!copy)
20552055
return 0;
20562056

2057-
Py_XSETREF(*object, copy);
2057+
Py_SETREF(*object, copy);
20582058

20592059
return 1; /* success */
20602060
}

Modules/_ssl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,7 @@ static int PySSL_set_context(PySSLSocket *self, PyObject *value,
14671467
return -1;
14681468
#else
14691469
Py_INCREF(value);
1470-
Py_XSETREF(self->ctx, (PySSLContext *)value);
1470+
Py_SETREF(self->ctx, (PySSLContext *)value);
14711471
SSL_set_SSL_CTX(self->ssl, self->ctx->ctx);
14721472
#endif
14731473
} else {

0 commit comments

Comments
 (0)