[Python-Dev] if/else and macros (was: CVS: python/dist/src/Objects unicodeobject.c,2.48,2.49)

Fredrik Lundh Fredrik Lundh" <effbot@telia.com
Mon, 17 Jul 2000 11:45:18 +0200

thomas wrote:

> Even more arguable is the coding style issue ;) I haven't seen this =
'if (1)
> {} else' trick used in macros elsewhere, not in Python and not in =
> code

hey, it's saving a whopping ten lines of code, or so ;-)

If I'd written that code, I'd spelled it out...  and come to
think of it, I *did* write the original version of that code!

FYI, I don't like that macro.  Instead of arguing about how
it should look, just get rid of it.

here's how the original code looked:

    if (ch < 0x80)
        goto bogus; /* illegal UTF-8 encoding */


        PyExc_ValueError, "invalid UTF-8 code"

I would have implemented the changes like this:

    if (ch < 0x80) {
        detail =3D "illegal encoding";
        goto bogus;


    if (!utf8_decoding_error(&s, &p, errors, detail))
        goto ignore;

This solution gets rids of the macro mess, and what's more
important, it moves exception handling out of the inner loop.

(yes, it's using goto's, but it's a decoding loop, and it should
be as fast as it possibly can -- especially if you're feeding it
valid data!)

The result is smaller source, smaller code, a smaller inner loop,
faster decoding, and less discussion on this list.