Ticket #395 (closed defect: invalid)
type-punning warning when accessing built-in types as objects
| Reported by: | lodatom | Owned by: | somebody |
|---|---|---|---|
| Priority: | minor | Milestone: | Dupe/Invalid |
| Component: | Code Generation | Keywords: | warning |
| Cc: |
Description
If you access a built-in type as an object, such as in map(float, x), Cython generates code that gcc -O2 -Wall warns about type-punning. Here's an example:
$ cat test.pyx
def foo():
return float
$ cython test.pyx
$ gcc -fPIC -g -O2 -Wall -I/usr/include/python2.6 -c -o test.o test.c
test.c: In function ‘__pyx_pf_4test_foo’:
test.c:352: warning: dereferencing type-punned pointer will break strict-aliasing rules
Here's the offending code:
__Pyx_INCREF(((PyObject *)((PyObject*)&PyFloat_Type))); __pyx_r = ((PyObject *)((PyObject*)&PyFloat_Type));
I believe the problem is taking the address of the extern variable and then dereferencing it, e.g. (&x)->y. A way to remove the warning assign to a temporary variable, like so:
{
PyObject *x = (PyObject*)&PyFloat_Type;
__Pyx_INCREF(x);
__pyx_r = x;
}
I am not certain if this truly fixes the problem, or just stops gcc from warning.
Change History
Note: See
TracTickets for help on using
tickets.
