Ticket #395 (closed defect: invalid)

Opened 5 years ago

Last modified 4 years ago

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

Changed 5 years ago by scoder

  • status changed from new to closed
  • resolution set to invalid

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.

Changed 4 years ago by robertwb

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