[Cython] Supporting cython.operator in C

Robert Bradshaw robertwb at gmail.com
Wed Jun 1 23:43:21 EDT 2016


On Wed, Jun 1, 2016 at 1:56 PM, Jeroen Demeyer <jdemeyer at cage.ugent.be> wrote:
> On 2016-05-31 20:35, Robert Bradshaw wrote:
>>
>> I can't think of any fundamental reason these couldn't be implemented
>> in C, but there's really no need to do so as they can't be overridden
>> in C.
>
>
> Well, for the comma operator there might be no reason. However, it would be
> nice if one could generate arbitrary code of the form
>
> (x) (operator) (y)
>
> for any C/C++ values x and y and any operator. Two particular cases I
> encountered where this would be useful:
>
> 1. "x = y" where x is something that Cython does not consider an lvalue.

Cython doesn't support operator= even for C++, nor provide an
operator.X definition for it. (These are only provided for operators
that don't have a Python equivalent.) Are there cases in C where one
has an lvalue but Cython can't tell? (I suppose foo(x) if foo is a
macro--is that the only case?)

You can always define your own macro #define ASSIGN(a, b) (a = b).
Redefining what a valid lvalue is in the language (and syntax) is a
whole can of worms I'd rather avoid getting into...

> 2. "x || y" because Cython's "x or y" generates complicated code which isn't
> optimized as well as "x || y".

I just tried using gcc -O3 and the resulting assembly is identical.
Granted, "x or y" does have a different meaning in Python (and
Cython), returning the first of x or y that is True rather than a
boolean. To get the C behavior, write <bint>(x or y). I think this is
automatically done when it's used as a condition. The "more
complicated" code is necessary to get the correct short-circuiting
behavior, in particular when the right hand side is not expressible as
a pure C expression (e.g. requires intermediates).

- Robert


More information about the cython-devel mailing list