[issue39689] test_struct failure on s390x Fedora Clang buildbot

Petr Viktorin report at bugs.python.org
Thu Feb 27 11:32:48 EST 2020


Petr Viktorin <encukou at gmail.com> added the comment:

C compiler dev that it's indeed undefined behavior.

> Quick and obvious fix:
> 
>       static PyObject *
>       nu_bool(const char *p, const formatdef *f)
>       {
>           char x;
>           memcpy((char *)&x, p, sizeof x);
>           return PyBool_FromLong(x != 0);
>       }
>
> Which is optimized to
>
>     static PyObject *
>     nu_bool(const char *p, const formatdef *f)
>     {
>         return PyBool_FromLong(*p != 0);
>     }


I'm left with a question for CPython's struct experts:

The above would be my preferred fix, but the Python code is asking to convert a memory buffer to bool *using platform-specific semantics*.
Is this fix OK if C treats a \xf0 _Bool as falsey?


(Also, this assumes size of _Bool is the same as size of char.
I guess we can add a build-time assertion for that, and say we don't support platforms where that's not the case.)

----------
nosy: +mark.dickinson, meador.inge

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39689>
_______________________________________


More information about the Python-bugs-list mailing list