Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
using a custom, nearly-identical macro. This probably changes how some of
these functions are compiled, which may result in fractionally slower (or
faster) execution. Considering the nature of traversal, visiting much of the
address space in unpredictable patterns, I'd argue the code readability and
maintainability is well worth it ;P
diff --git a/Modules/_csv.c b/Modules/_csv.c
index 500d36e..67d5d9f 100644
--- a/Modules/_csv.c
+++ b/Modules/_csv.c
@@ -48,7 +48,16 @@
} \
} while (0)
#endif
-
+#ifndef Py_VISIT
+#define Py_VISIT(op) \
+ do { \
+ if (op) { \
+ int vret = visit((PyObject *)(op), arg); \
+ if (vret) \
+ return vret; \
+ } \
+ } while (0)
+#endif
/* end 2.2 compatibility macros */
@@ -825,16 +834,9 @@
static int
Reader_traverse(ReaderObj *self, visitproc visit, void *arg)
{
- int err;
-#define VISIT(SLOT) \
- if (SLOT) { \
- err = visit((PyObject *)(SLOT), arg); \
- if (err) \
- return err; \
- }
- VISIT(self->dialect);
- VISIT(self->input_iter);
- VISIT(self->fields);
+ Py_VISIT(self->dialect);
+ Py_VISIT(self->input_iter);
+ Py_VISIT(self->fields);
return 0;
}
@@ -1255,15 +1257,8 @@
static int
Writer_traverse(WriterObj *self, visitproc visit, void *arg)
{
- int err;
-#define VISIT(SLOT) \
- if (SLOT) { \
- err = visit((PyObject *)(SLOT), arg); \
- if (err) \
- return err; \
- }
- VISIT(self->dialect);
- VISIT(self->writeline);
+ Py_VISIT(self->dialect);
+ Py_VISIT(self->writeline);
return 0;
}
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index 4324f39..4551342 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -2061,8 +2061,7 @@
static int
arrayiter_traverse(arrayiterobject *it, visitproc visit, void *arg)
{
- if (it->ao != NULL)
- return visit((PyObject *)(it->ao), arg);
+ Py_VISIT(it->ao);
return 0;
}
diff --git a/Modules/cPickle.c b/Modules/cPickle.c
index 1d99fcb..18df599 100644
--- a/Modules/cPickle.c
+++ b/Modules/cPickle.c
@@ -2909,22 +2909,14 @@
static int
Pickler_traverse(Picklerobject *self, visitproc visit, void *arg)
{
- int err;
-#define VISIT(SLOT) \
- if (SLOT) { \
- err = visit((PyObject *)(SLOT), arg); \
- if (err) \
- return err; \
- }
- VISIT(self->write);
- VISIT(self->memo);
- VISIT(self->fast_memo);
- VISIT(self->arg);
- VISIT(self->file);
- VISIT(self->pers_func);
- VISIT(self->inst_pers_func);
- VISIT(self->dispatch_table);
-#undef VISIT
+ Py_VISIT(self->write);
+ Py_VISIT(self->memo);
+ Py_VISIT(self->fast_memo);
+ Py_VISIT(self->arg);
+ Py_VISIT(self->file);
+ Py_VISIT(self->pers_func);
+ Py_VISIT(self->inst_pers_func);
+ Py_VISIT(self->dispatch_table);
return 0;
}
@@ -5258,24 +5250,15 @@
static int
Unpickler_traverse(Unpicklerobject *self, visitproc visit, void *arg)
{
- int err;
-
-#define VISIT(SLOT) \
- if (SLOT) { \
- err = visit((PyObject *)(SLOT), arg); \
- if (err) \
- return err; \
- }
- VISIT(self->readline);
- VISIT(self->read);
- VISIT(self->file);
- VISIT(self->memo);
- VISIT(self->stack);
- VISIT(self->pers_func);
- VISIT(self->arg);
- VISIT(self->last_string);
- VISIT(self->find_class);
-#undef VISIT
+ Py_VISIT(self->readline);
+ Py_VISIT(self->read);
+ Py_VISIT(self->file);
+ Py_VISIT(self->memo);
+ Py_VISIT(self->stack);
+ Py_VISIT(self->pers_func);
+ Py_VISIT(self->arg);
+ Py_VISIT(self->last_string);
+ Py_VISIT(self->find_class);
return 0;
}
diff --git a/Modules/operator.c b/Modules/operator.c
index 53144f1..25b3999 100644
--- a/Modules/operator.c
+++ b/Modules/operator.c
@@ -358,8 +358,7 @@
static int
itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg)
{
- if (ig->item)
- return visit(ig->item, arg);
+ Py_VISIT(ig->item);
return 0;
}
@@ -497,8 +496,7 @@
static int
attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg)
{
- if (ag->attr)
- return visit(ag->attr, arg);
+ Py_VISIT(ag->attr);
return 0;
}
diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c
index fbef4e1..16f0137 100644
--- a/Modules/pyexpat.c
+++ b/Modules/pyexpat.c
@@ -1655,13 +1655,8 @@
xmlparse_traverse(xmlparseobject *op, visitproc visit, void *arg)
{
int i, err;
- for (i = 0; handler_info[i].name != NULL; i++) {
- if (!op->handlers[i])
- continue;
- err = visit(op->handlers[i], arg);
- if (err)
- return err;
- }
+ for (i = 0; handler_info[i].name != NULL; i++)
+ Py_VISIT(op->handlers[i]);
return 0;
}
diff --git a/Modules/zipimport.c b/Modules/zipimport.c
index 3e37656..d59ebd8 100644
--- a/Modules/zipimport.c
+++ b/Modules/zipimport.c
@@ -170,13 +170,7 @@
zipimporter_traverse(PyObject *obj, visitproc visit, void *arg)
{
ZipImporter *self = (ZipImporter *)obj;
- int err;
-
- if (self->files != NULL) {
- err = visit(self->files, arg);
- if (err)
- return err;
- }
+ Py_VISIT(self->files);
return 0;
}