On Fri, Oct 4, 2019 at 9:20 PM Ian Pilcher <arequipeno@xxxxxxxxx> wrote: > > I am writing a Python extension that calls strerror_r. Unfortunately, > the Python extension build system requires that Python.h be the first > header file included; it also sets the _GNU_SOURCE feature test macro > *and* includes string.h. > > If I define _GNU_SOURCE before including Python.h, I receive a warning > about the macro being redefined. AFAICT, gcc does not provide a way to > suppress this warning. > > My tests show that I am getting the GNU version of strerror_r, but I > hate relying on the internal structure of the Python header files (even > more that I hate warnings). > > So I'm trying to come up with a compile-time check, to ensure that > strerror_r has the desired signature (i.e. it returns a char *, not an > int). Here's what I've got: > I did this: PyObject* _strerror() { char buf[1024] = {0}; // this is a compile time assertion to ensure the GNU version of // strerror_r is used. if (0) { // ensures the strerror_r() returns a pointer int ret = *strerror_r(errno, buf, sizeof(buf)); return PyString_FromString(buf + ret); } // now for the real implementation return PyString_FromString(strerror_r(errno, buf, sizeof(buf))); } I don’t mind unhelpful error messages as long as it is commented.