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.

2
  • 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. Commented Jun 2, 2017 at 8:26
  • The API is relatively stable, the ABI 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. Commented Jun 2, 2017 at 9:00

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

For Python >=3.2, there is a stable API 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.
Thanks for the definite answer and @DavidW for the link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.