Looping [was Re: Python and the need for speed]

Chris Angelico rosuav at gmail.com
Sun Apr 16 14:42:09 EDT 2017


On Mon, Apr 17, 2017 at 4:21 AM, bartc <bc at freeuk.com> wrote:
> Here is a function from some old CPython source that appears to be something
> to do with While statements:
>
> static int
> validate_while(node *tree)
> {
>     int nch = NCH(tree);
>     int res = (validate_ntype(tree, while_stmt)
>                && ((nch == 4) || (nch == 7))
>                && validate_name(CHILD(tree, 0), "while")
>                && validate_test(CHILD(tree, 1))
>                && validate_colon(CHILD(tree, 2))
>                && validate_suite(CHILD(tree, 3)));
>
>     if (res && (nch == 7))
>         res = (validate_name(CHILD(tree, 4), "else")
>                && validate_colon(CHILD(tree, 5))
>                && validate_suite(CHILD(tree, 6)));
>
>     return (res);
> }
>
> Look, no comments! Are you going to castigate the developers of CPython for
> that? (I guess not.)

Can you find the equivalent in the current sources? I think it might be here:

https://github.com/python/cpython/blob/master/Grammar/Grammar#L73

while_stmt: 'while' test ':' suite ['else' ':' suite]

A single, extremely readable line. Abstract, not concrete. And my
suspicion is that the code you're looking at above just might have
been generated from the same kind of grammar file - which means that
it is NOT the "CPython source", but rather an intermediate file. It's
the equivalent of looking at a .pyc file and complaining that it has
no comments.

Want to show me your source for this source? The best I can find (by
searching the git history (which was imported from the hg history) for
'validate_while') is the code prior to this commit:

https://github.com/python/cpython/commit/53595c

And I rather suspect, from some of the other comments in the area,
that this file may have been autogenerated. Even if it wasn't, it was
still not the "primary source", but exists merely to implement
Grammar/Grammar, as linked above.

Next complaint, please.

ChrisA



More information about the Python-list mailing list