Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type-punning warning when accessing built-in types as objects #958

Closed
robertwb opened this issue Oct 6, 2009 · 2 comments
Closed

type-punning warning when accessing built-in types as objects #958

robertwb opened this issue Oct 6, 2009 · 2 comments

Comments

@robertwb
Copy link
Contributor

robertwb commented Oct 6, 2009

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.

Migrated from http://trac.cython.org/ticket/395

@robertwb
Copy link
Contributor Author

scoder changed resolution to invalid
status from new to closed
commented

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.

@robertwb
Copy link
Contributor Author

robertwb commented Nov 5, 2009

@robertwb changed milestone from wishlist to Dupe/Invalid
commented

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant