I came across this while working on allowing proper overloading of the
assignment operator.
The problem is, essentially, that Cython does not follow the C/C++
ordering for cascaded assignment, even when dealing with objects at
the C/C++ level. For example, calling the test function from the
following Cython file and running the following C++ file should
both give the same output:
def test():
cdef:
long long a = (2LL << 36) - 257
int b
char c
b = c = a
print a, b, c
#include <iostream>
int main(){
long long a = (2LL << 36) - 257;
int b;
char c;
b = c = a;
std::cout << a << std::endl;
std::cout << b << std::endl;
std::cout << (int)c << std::endl;
}
The Cython version prints 137438953215 -257 -1
and the C++ version prints 137438953215 -1 -1
To mimic C/C++, it should go through the arguments from right to left
and perform each assignment operation in that order, i.e.
c = a
b = c
As it stands, the current code does:
b = a
c = a
This does appear to follow what Python does. For example, the
following gives the same output as the Cython version:
import numpy as np
a = np.array([(2 << 36) - 257], np.int64)
b = np.empty(1, np.int32)
c = np.empty(1, np.int8)
b[:] = c[:] = a
print a[0], b[0], c[0]
Is this a deliberate design decision? If this weren't already included in
the language, I'd be inclined to not allow cascaded assignment to C
objects at all since it can give such surprising results. On the other
hand I don't see any particularly clean way to deal with this
incompatibility between C/C++ and Python.
Thanks!
-Ian Henriksen
_______________________________________________
cython-devel mailing list
[email protected]
https://mail.python.org/mailman/listinfo/cython-devel