[Python-Dev] cpython: fix compiler warning by implementing this more cleverly

Nick Coghlan ncoghlan at gmail.com
Wed Nov 23 08:07:15 CET 2011


On Wed, Nov 23, 2011 at 4:49 PM, Terry Reedy <tjreedy at udel.edu> wrote:
> I personally strongly prefer the one-line formula to the hardcoded magic
> numbers calculated from the formula. I find it much more readable. To me,
> the only justification for the switch would be if there is a serious worry
> about the kind being changed to something other than 1, 2, or 4. But the
> fact that this is checked with an assert that can be optimized away negates
> that. The one-liner could be followed by
>  assert(kind==1 || kind==2 || kind==4)
> which would also serve to remind the reader of the possibilities. You could
> even follow the formula with /* 4, 6, or 10 */ I think you reverted too
> soon.

+1 to what Terry said here, although I would add a genuinely
explanatory comment that gives the calculation meaning:

    /* For each character, allow for "\U" prefix and 2 hex digits per byte */
    expandsize = 2 + 2 * kind;

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list