[issue43181] Python macros don’t shield arguments

2021-02-09 Thread Vitaliy

New submission from Vitaliy :

There is a lot of macros like:
#define PyObject_TypeCheck(ob, tp) \
(Py_IS_TYPE(ob, tp) || PyType_IsSubtype(Py_TYPE(ob), (tp)))
These work fine until an argument happen to contain a comma. That’s possible as 
a result of other macro’s expansion. E.g. if U(x) is defined as x,
PyObject_TypeCheck(ob, U(f(c)))
expands to
(Py_IS_TYPE(ob, f(c)) || ...)
but < and > aren’t treated as brackets by the preprocessor so Py_IS_TYPE is now 
invoked with 3 arguments instead of just 2, breaking module compilation.

As arguments are expected to be values, surrounding each with parentheses 
solves the problem. But there are many such macros so that’s not an one-line 
fix.

Note: the example is from PyGLM (simplified), it doesn’t compile on 3.9 due to 
this issue.

--
components: C API
messages: 386746
nosy: numberZero
priority: normal
severity: normal
status: open
title: Python macros don’t shield arguments
versions: Python 3.9

___
Python tracker 
<https://bugs.python.org/issue43181>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43181] Python macros don’t shield arguments

2021-02-19 Thread Vitaliy

Vitaliy  added the comment:

Thanks for the fix, it works for my use case. (btw that was
#define U(...) __VA_ARGS__
and not what I wrote).

> I don't think that PR 24533 should be backported to Python 3.8 and Python 
> 3.9. I prefer to avoid any risk of regression, and so only change Python 3.10.

> For Python 3.9 and older, a workaround is to wrap the call to 
> PyObject_TypeCheck() with your own static inline function.

For Python 3.8 that fix wouldn’t be needed as the `tp` argument was 
parenthesised in the macro.

Yet... the first argument is still unshielded, passed to a macro that expects 
one single macro argument. That’s not a regression, it wasn’t shielded in 3.8 
either, but why not just parenthesise each macro argument that denotes an 
expression (as opposed to e.g. name)?

--

___
Python tracker 
<https://bugs.python.org/issue43181>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com