On Fri, Oct 11, 2013 at 2:15 AM, David Cournapeau <courn...@gmail.com> wrote: > > On Tue, Jul 2, 2013 at 5:07 PM, Robert Bradshaw <rober...@gmail.com> wrote: >> >> On Tue, Jul 2, 2013 at 4:54 AM, Yury V. Zaytsev <y...@shurup.com> wrote: >> > Hi, >> > >> > The simplest possible program using memory views compiles with a large >> > number of warnings for me, even for a rather outdated version of gcc: >> > >> > def hello(int [:] a): >> > print(a, "world") >> > >> > If I translate it with the latest released version of Cython like this: >> > >> > cython cpp.pyx >> > cython --cplus cpp.pyx >> > >> > and compile like this: >> > >> > gcc -O3 -march=native -Wall -fPIC >> > -I/opt/ActivePython-2.7/include/python2.7 -c ./cpp.c -o cpp.o >> > g++ -O3 -march=native -Wall -fPIC >> > -I/opt/ActivePython-2.7/include/python2.7 -c ./cpp.cpp -o cpp.o >> > >> > I get lots of warnings (see attached). >> > >> > It doesn't seem to be related to C++ as such, but rather it seems that >> > the memory views code indeed somehow violates strict-aliasing rules. >> > >> > I'm not sure of how severe it is, but the documentation seems to suggest >> > that this might even lead to incorrect results. >> > >> > Can this possibly be fixed in Cython and how important is that? Shall I >> > create a bug report on the Trac? Is my only resort to test whether the >> > compiler supports -fno-strict-aliasing and use that? >> >> You should compile with -fno-strict-aliasing--if you were using >> distutils rather than gcc directly it should add the all necessary >> flags for you. >> >> Aliasing different pointer types is necessary for Cython--it's how it >> implements inheritance (in plain C, a PyObject* could be a pointer to >> a list or dict or your own cdef class--pointer aliasing right there. >> Also with memory views (and numpy arrays), the underlying data is >> allocated as a char* and interpreted as a float* or int* or according >> to the metadata in the array. > > > I think this is a relatively serious issue. Using pointer aliasing is not > valid C, and we can usually get the same feature with union (which is > usually supported). See > http://stackoverflow.com/questions/11639947/is-type-punning-through-a-union-unspecified-in-c99-and-has-it-become-specified > > I have been bitten by this recently: how difficult of a change would it be ?
I don't think this change is even possible. NumPy has exactly the same issue, as will any library trying to store vectors of arbitrary data types (whose layout may even be defined at runtime) in C. The Python buffer API doesn't provide a way around it. Also, Python and Cython in general breaks pointer aliasing as objects are simultaneously generic PyObject* and PyListObject*, PyDictObject*, etc. As the set of possible types is large and open, unions won't work. This is how object oriented programming (with subclassing) is done in C. - Robert _______________________________________________ cython-devel mailing list cython-devel@python.org https://mail.python.org/mailman/listinfo/cython-devel