Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ 17 compatability with Python 2.7

The most recent version of python 2.7 (2.7.13) includes a header unicodeobject.h that uses the register keyword. My understanding is that C++ 17 has removed this keyword. When compiling against this header using C++ 17, unsurprising a slew of warnings are triggered including:

/opt/anaconda/include/python2.7/unicodeobject.h:534:24: warning: ISO C++1z does not allow ‘register’ storage class specifier [-Wregister]
 register PyObject *obj,     /* Object */
                    ^~~
/opt/anaconda/include/python2.7/unicodeobject.h:553:24: warning: ISO C++1z does not allow ‘register’ storage class specifier [-Wregister]
 register PyObject *obj      /* Object */
                    ^~~
/opt/anaconda/include/python2.7/unicodeobject.h:575:29: warning: ISO C++1z does not allow ‘register’ storage class specifier [-Wregister]
 register const wchar_t *w,  /* wchar_t buffer */
                         ^
/opt/anaconda/include/python2.7/unicodeobject.h:593:23: warning: ISO C++1z does not allow ‘register’ storage class specifier [-Wregister]
 register wchar_t *w,        /* wchar_t buffer */

That said, I am still able to compile and run python extensions despite these warnings. Is it safe to continue doing so? Is there any way (besides explicitly ignoring the warnings) to resolve these messages such as upgrading to a different version of 2.7 (though as mentioned the newest version seems to still use the register keyword)?

like image 842
mcguip Avatar asked Apr 06 '18 12:04

mcguip


1 Answers

The use of register has already been removed in the Python 3 version of this header, so if possible, you should use the Python 3 headers (Python 2 will no longer be maintained in a few years).

Otherwise, you have a few options.

  • If you are using local versions of these headers (e.g. distributed with the module), you can simply delete the uses of register yourself (there are only four in the file). This is obviously a poor solution if you are compiling against the system headers
  • CPython is actually written in C, not C++, and C has neither deprecated nor removed the register keyword, so if the module you are compiling is written in C, you can use a C compiler (e.g. gcc) instead of a C++ compiler. (I expect the module is written in C++, so this probably won't be an option).
  • Ignore the warnings. As I mentioned, there are only 4 uses of the register keyword, so you could consider living with it.
  • Disable the register warning. You can do this for your entire project by passing the flag -Wno-register on the command line, or you can just disable it for this header by doing this:

    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wregister"`
    #include <unicodeobject.h> // or whatever header includes unicodeobject.h
    #pragma GCC diagnostic pop
    

    This will save the current warning settings, ignore the register warning for that header, then restore the original warning settings after the header has been included. While the pragma says GCC, Clang will also understand this pragma and behave how you'd like.

like image 105
SJL Avatar answered Oct 24 '22 09:10

SJL