[Cython] Out of order side effects of argument evaluation in function calls (ticket #654)

Lisandro Dalcin dalcinl at gmail.com
Fri Mar 11 16:05:25 CET 2011

On 11 March 2011 09:45, Stefan Behnel <stefan_ml at behnel.de> wrote:
> Hi,
> ticket 654 describes a code generation problem where the arguments to a cdef
> function are not being evaluated in the order they are written down in the
> code.
> http://trac.cython.org/cython_trac/ticket/654
> This introduces problems when the arguments have side effects or are not
> simple, e.g. they are function calls themselves or are taken from object
> attributes or live in a closure. For example,
>    f(g(a), a.x, h(a))
> will produce different results depending on the evaluation order if g or h
> change the value of a.x.
> However, apparently, the order of evaluation is only guaranteed by Python,
> not by C. Now, the question is: what are the right semantics for Cython
> here: follow Python or C?
> Personally, I think it would be nice to keep up Python's semantics, but when
> I implemented this I broke quite some code in Sage (you may have noticed
> that the sage-build project in Hudson has been red for a while). There are
> things in C and especially in C++ that cannot be easily copied into a
> temporary variable in order to make sure they are evaluated before the
> following arguments. This is not a problem for Python function calls where
> all arguments end up being copied (and often converted) anyway. It is a
> problem for C function calls, though.
> What do you think about this?

Regarding our previous history, I would go for Python semantics...

Perhaps you could add a compiler directive for these rare cases where
you need/want C/C++ semantics?

Lisandro Dalcin
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
3000 Santa Fe, Argentina
Tel: +54-342-4511594 (ext 1011)
Tel/Fax: +54-342-4511169

More information about the cython-devel mailing list