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

Tim Peters tim.peters at gmail.com
Thu Jul 15 04:48:20 CEST 2004

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

[Raymond Hettinger]
> That Tim has been tyrant lately ;-)

It's the only way left to get things done <wink>.

>> + /* 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.

-1.  Instead it should be documented that the tp_traverse
implementation *must* name its arguments "visit" and "arg" if it wants
to use this macro.  Since tp_traverse implementations *can* look so
relentlessy the same, it's a Positive Good to encourage them to look
relentlessly the same.  I want the process of writing a correct
tp_traverse implementation to be mindlessly simple, and likewise I
want the process of reviewing one to be as simple as asking "did it
apply VISIT to each member and then return 0?".  Rigid uniformity
helps both goals -- and as there's no creative joy to be had in trying
to write a clever tp_traverse either, deviation from a rigid template
would just be gratuitous deviation.

principled-perversion-is-a-different-story-ly y'rs  - tim

More information about the Python-Dev mailing list