[Python-Dev] ANSI strict aliasing and Python

Neil Schemenauer nas@python.ca
Thu, 17 Jul 2003 10:26:44 -0700


Recently the GCC option -fno-strict-aliasing flag got added.  The
intention was to silence GCC warnings like this:

    ../Objects/object.c: In function `PyObject_IsTrue':
    ../Objects/object.c:1565: warning: dereferencing type-punned pointer
    will break strict-aliasing rules

It looks like most (maybe all) of those warnings are triggered by
Py_True and Py_False.  Is there some other way of defining Py_True and
Py_False so that we can avoid those errors?  Maybe it's a lost cause
anyhow, I don't really understand the ANSI spec on this issue.  This is
what I found:

"""
An object shall have its stored value accessed only by an lvalue that
has one of the following types:

    * the declared type of the object,
    * a qualified version of the declared type of the object,
    * a type that is the signed or unsigned type corresponding to the
      declared type of the object,
    * a type that is the signed or unsigned type corresponding to a
      qualified version of the declared type of the object,
    * an aggregate or union type that includes one of the aforementioned
      types among its members (including, recursively, a member of a
      sub-aggregate or contained union), or
    * a character type.
"""

Does this mean that code like:

    void f (PyObject *a, PyDictObject *b)
    {
        a->ob_refcnt += 1;
        b->ob_refcnt -= 1;
    }
    [...]
        f((PyObject *)somedict, somedict);

is disallowed?

  Neil