[Python-Dev] RE: [Python-checkins] python/dist/src/Include objimpl.h, 2.58, 2.59

Raymond Hettinger python at rcn.com
Wed Jul 14 10:23:45 CEST 2004


> Documented the new Py_VISIT macro to simplify implementation of
> tp_traverse handlers. (Tim made me do it. ;)

That Tim has been tyrant lately ;-)



> + /* Utility macro to help write tp_traverse functions */
> + #define Py_VISIT(op)					\
> +         do { 						\
> +                 if (op) {				\
> +                         int vret = visit((op), arg);	\
> +                         if (vret)			\
> +                                 return vret;		\
> +                 }					\
> +         } while (0)

I tried out this macro with the itertools module and found that it did a
nice job of compacting boilerplate code.

One thought jumped out though.  The macro assumes that the enclosing
function has consistent with the use of the variable names "visit" and
"arg": 

  static int
  islice_traverse(isliceobject *lz, visitproc visit, void *arg)
  {
      Py_VISIT(lz->it);
      return 0;
  }

If the parameters are named something else ("vfunc" for example),
MSVC++6.0 complains:

C:\py24\Modules\itertoolsmodule.c(1118) : warning C4013: 'visit'
undefined; assuming extern returning int

Don't know if it is worth it, but you could safeguard the macro by
including visit and arg in the parameter list.


Raymond Hettinger



More information about the Python-Dev mailing list