1

In an open source project1 we have Python/Cython and C/C++ modules mixed with one C++ library using the Python C API. The API changed only a few function's names from 2 to 3. Assume the library is written without those functions. Will it link to Python3 if compiled with Python2, and vice versa? Is this prevented by macros in the API headers?

Having a library binary that may link to both would spare us major packaging hassles.

rwst
  • 2,515
  • 2
  • 30
  • 36
  • The names of some pretty fundamental functions have changed (e.g. module initialization I think) mostly for the purpose of preventing you from doing this. – DavidW Jun 02 '17 at 08:26
  • The API is relatively stable, the A*B*I is not. So you would need to recompile per Python version. Edit: See the link in @DavidW comment to Antti Haapala's answer below for more explanation on this. – Toby Jun 02 '17 at 09:00

1 Answers1

1

No, it wouldn't work. Don't try it.

Binary modules are not guaranteed to be binary-portable even say from 3.5 to 3.6. If you're lucky, then there is some mechanism that will prohibit you from doing this insanity. If however you manage to link the library somehow, there will be some subtle differences that will cause serious bugs, such as the layout of PyObject changing and so forth.

The Python interface must be recompiled for the exact Python version. Source compatibility between Python 2 and 3 is a different thing and is relatively easy to achieve.

  • 2
    For Python >=3.2, there is [a stable API](https://docs.python.org/3/c-api/stable.html) that can be used (but is a bit more restricted that the full API). Obviously that doesn't help with OPs problem, but it would allow you to do 3.5 and 3.6 in one binary module. – DavidW Jun 02 '17 at 08:24
  • Thanks for the definite answer and @DavidW for the link. – rwst Jun 05 '17 at 05:03