[Python-Dev] Py_DECREF causes spurious gcc warning

Tim Peters tim.one at comcast.net
Sat Dec 20 16:07:15 EST 2003

[Zack Weinberg]
> I thought you might come back at me with that.  Yes, you can avoid the
> syntax error by carefully leaving off the semicolon here.  I don't
> consider this to be good style, though.  It's easy to forget, and it's
> easy for someone to come along later and be confused and check in an
> 'obvious' patch that adds a semicolon - and it might not even break
> anything... then.

Zack, you have to be kidding.  Martin was clearly talking about the
expansion of Py_DECREF specifically, not "macros in general", as also was
the poster who started this thread.

> I'm really not speaking about the python source code in specific.

That's now obvious <wink>.

> I'm speaking about general style principles, and I really do think it is
> good style *always* to wrap complex macros in do { } while (0) whether
> or not they need it.

Py_DECREF doesn't need it; there may be some point to it if doing so shuts
up gcc nuisance complaints, and we can be sure that the extra complexity of
the expanded code doesn't provoke optimization bugs in any of the C
compilers used to compile Python.  Writing non-loops *as* loops is a good
way to tickle endcase bugs in sophisticated optimizers -- it's not without
risk, and there are several thousand instances of Py_DECREF and Py_XDECREF
just in the core.  Also because it's used a lot, in a debug build such a
change would generate a ton of useless extra instructions (at least MSVC
really does generate code to create a zero, test it against zero, and jump
back to the top of "the loop" if zero doesn't equal zero; I expect that most
C compilers would).

More information about the Python-Dev mailing list