04.12.19 18:54, Skip Montanaro пише:
I don't think stable code which uses macros should be changed (though
I see the INCREF/DECREF macros just call private inline functions, so
some conversion has clearly been done). Still, in new code, shouldn't
the use of macros for more than trivial use cases (constant defs,
simple one-liners) be discouraged at this point?
You can't goto from the inline function.
Thanks, that is true, and if needed would add another case where
macros are preferred over inline functions (as would use of the cpp
token pasting operator, ##). I see relatively few goto-using macros
spread across a number of source files, but the examples I highlighted
in Python/ast_opt.c use return, not goto. It seems they could easily
be crafted as inline functions which return 0 (forcing early return
from enclosing function) or 1 (equivalent to current fall through).
In this case it is the same as a goto. Both affect the control flow of
the caller.
In these files (symtable.c, compile.c, ast_opt.c, etc) there are
sequences of calls for child nodes. Every call can return an error, so
you need to check every call and return an error immediately after the
call. With inline functions you would need to write
if (!VISIT(...)) {
return 0;
}
if (!VISIT(...)) {
return 0;
}
if (!VISIT(...)) {
return 0;
}
instead of just
VISIT(...);
VISIT(...);
VISIT(...);
This will increase the number of lines by two or three (according to the
current PEP 7 recommendations) times. This will add a lot of boilerplate
which distracts the attention from the logic. It will need to pass all
arguments explicitly (macros allow to pass some arguments implicitly).
This will add a lot of spots for introducing errors. If you will need to
change some details, for example decrement state->recursion_depth before
return you will need to change handreds lines instead of few sites (and
guess how much bugs you will introduce).
Macros exist to get rid of the boilerplate. They are used not only in
private files, this idiom is used in the public API. See the Py_VISIT
macro. It passes implicit arguments and returns from the caller.
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/python-dev@python.org/message/I2FAPRXQMKEDLZURJIMU7P274OLLPSGT/
Code of Conduct: http://python.org/psf/codeofconduct/