Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#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


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_r = x;

I am not certain if this truly fixes the problem, or just stops gcc from warning.

Change History (2)

comment:1 Changed 7 years ago by scoder

  • Resolution set to invalid
  • Status changed from new to closed

This is most likely the normal Py2.x strict-aliasing problem. Please build modules with -fno-strict-aliasing under Py2. This problem has been fixed in Py3.

comment:2 Changed 7 years ago by robertwb

  • Milestone changed from wishlist to Dupe/Invalid
Note: See TracTickets for help on using tickets.