Skip to content

Commit 8ae68e3

Browse files
author
thomas.wouters
committed
Use Py_CLEAR instead of in-place DECREF/XDECREF or custom macros, for tp_clear methods. git-svn-id: http://svn.python.org/projects/python/trunk@45435 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 4c138ad commit 8ae68e3

8 files changed

Lines changed: 30 additions & 57 deletions

File tree

Modules/_csv.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -828,12 +828,9 @@ Reader_traverse(ReaderObj *self, visitproc visit, void *arg)
828828
static int
829829
Reader_clear(ReaderObj *self)
830830
{
831-
Py_XDECREF(self->dialect);
832-
Py_XDECREF(self->input_iter);
833-
Py_XDECREF(self->fields);
834-
self->dialect = NULL;
835-
self->input_iter = NULL;
836-
self->fields = NULL;
831+
Py_CLEAR(self->dialect);
832+
Py_CLEAR(self->input_iter);
833+
Py_CLEAR(self->fields);
837834
return 0;
838835
}
839836

@@ -1260,10 +1257,8 @@ Writer_traverse(WriterObj *self, visitproc visit, void *arg)
12601257
static int
12611258
Writer_clear(WriterObj *self)
12621259
{
1263-
Py_XDECREF(self->dialect);
1264-
Py_XDECREF(self->writeline);
1265-
self->dialect = NULL;
1266-
self->writeline = NULL;
1260+
Py_CLEAR(self->dialect);
1261+
Py_CLEAR(self->writeline);
12671262
return 0;
12681263
}
12691264

Modules/cPickle.c

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2931,16 +2931,14 @@ Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
29312931
static int
29322932
Pickler_clear(Picklerobject *self)
29332933
{
2934-
#define CLEAR(SLOT) Py_XDECREF(SLOT); SLOT = NULL;
2935-
CLEAR(self->write);
2936-
CLEAR(self->memo);
2937-
CLEAR(self->fast_memo);
2938-
CLEAR(self->arg);
2939-
CLEAR(self->file);
2940-
CLEAR(self->pers_func);
2941-
CLEAR(self->inst_pers_func);
2942-
CLEAR(self->dispatch_table);
2943-
#undef CLEAR
2934+
Py_CLEAR(self->write);
2935+
Py_CLEAR(self->memo);
2936+
Py_CLEAR(self->fast_memo);
2937+
Py_CLEAR(self->arg);
2938+
Py_CLEAR(self->file);
2939+
Py_CLEAR(self->pers_func);
2940+
Py_CLEAR(self->inst_pers_func);
2941+
Py_CLEAR(self->dispatch_table);
29442942
return 0;
29452943
}
29462944

@@ -5284,17 +5282,15 @@ Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
52845282
static int
52855283
Unpickler_clear(Unpicklerobject *self)
52865284
{
5287-
#define CLEAR(SLOT) Py_XDECREF(SLOT); SLOT = NULL
5288-
CLEAR(self->readline);
5289-
CLEAR(self->read);
5290-
CLEAR(self->file);
5291-
CLEAR(self->memo);
5292-
CLEAR(self->stack);
5293-
CLEAR(self->pers_func);
5294-
CLEAR(self->arg);
5295-
CLEAR(self->last_string);
5296-
CLEAR(self->find_class);
5297-
#undef CLEAR
5285+
Py_CLEAR(self->readline);
5286+
Py_CLEAR(self->read);
5287+
Py_CLEAR(self->file);
5288+
Py_CLEAR(self->memo);
5289+
Py_CLEAR(self->stack);
5290+
Py_CLEAR(self->pers_func);
5291+
Py_CLEAR(self->arg);
5292+
Py_CLEAR(self->last_string);
5293+
Py_CLEAR(self->find_class);
52985294
return 0;
52995295
}
53005296

Modules/collectionsmodule.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,10 +1236,7 @@ defdict_traverse(PyObject *self, visitproc visit, void *arg)
12361236
static int
12371237
defdict_tp_clear(defdictobject *dd)
12381238
{
1239-
if (dd->default_factory != NULL) {
1240-
Py_DECREF(dd->default_factory);
1241-
dd->default_factory = NULL;
1242-
}
1239+
Py_CLEAR(dd->default_factory);
12431240
return PyDict_Type.tp_clear((PyObject *)dd);
12441241
}
12451242

Modules/pyexpat.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,8 +1669,7 @@ static int
16691669
xmlparse_clear(xmlparseobject *op)
16701670
{
16711671
clear_handlers(op, 0);
1672-
Py_XDECREF(op->intern);
1673-
op->intern = 0;
1672+
Py_CLEAR(op->intern);
16741673
return 0;
16751674
}
16761675
#endif

Objects/cellobject.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ cell_traverse(PyCellObject *op, visitproc visit, void *arg)
8181
static int
8282
cell_clear(PyCellObject *op)
8383
{
84-
Py_XDECREF(op->ob_ref);
85-
op->ob_ref = NULL;
84+
Py_CLEAR(op->ob_ref);
8685
return 0;
8786
}
8887

Objects/funcobject.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -655,9 +655,7 @@ cm_traverse(classmethod *cm, visitproc visit, void *arg)
655655
static int
656656
cm_clear(classmethod *cm)
657657
{
658-
Py_XDECREF(cm->cm_callable);
659-
cm->cm_callable = NULL;
660-
658+
Py_CLEAR(cm->cm_callable);
661659
return 0;
662660
}
663661

Objects/typeobject.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,8 @@ clear_slots(PyTypeObject *type, PyObject *self)
559559
char *addr = (char *)self + mp->offset;
560560
PyObject *obj = *(PyObject **)addr;
561561
if (obj != NULL) {
562-
Py_DECREF(obj);
563562
*(PyObject **)addr = NULL;
563+
Py_DECREF(obj);
564564
}
565565
}
566566
}
@@ -2236,13 +2236,6 @@ type_clear(PyTypeObject *type)
22362236
for heaptypes. */
22372237
assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
22382238

2239-
#define CLEAR(SLOT) \
2240-
if (SLOT) { \
2241-
tmp = (PyObject *)(SLOT); \
2242-
SLOT = NULL; \
2243-
Py_DECREF(tmp); \
2244-
}
2245-
22462239
/* The only field we need to clear is tp_mro, which is part of a
22472240
hard cycle (its first element is the class itself) that won't
22482241
be broken otherwise (it's a tuple and tuples don't have a
@@ -2268,9 +2261,7 @@ type_clear(PyTypeObject *type)
22682261
A tuple of strings can't be part of a cycle.
22692262
*/
22702263

2271-
CLEAR(type->tp_mro);
2272-
2273-
#undef CLEAR
2264+
Py_CLEAR(type->tp_mro);
22742265

22752266
return 0;
22762267
}

Python/traceback.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,8 @@ tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg)
5353
static void
5454
tb_clear(PyTracebackObject *tb)
5555
{
56-
Py_XDECREF(tb->tb_next);
57-
Py_XDECREF(tb->tb_frame);
58-
tb->tb_next = NULL;
59-
tb->tb_frame = NULL;
56+
Py_CLEAR(tb->tb_next);
57+
Py_CLEAR(tb->tb_frame);
6058
}
6159

6260
PyTypeObject PyTraceBack_Type = {

0 commit comments

Comments
 (0)