Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extension with py_limited_api=True not linked with PYTHON3.DLL #1248

Closed
anthrotype opened this issue Jan 7, 2018 · 2 comments
Closed

extension with py_limited_api=True not linked with PYTHON3.DLL #1248

anthrotype opened this issue Jan 7, 2018 · 2 comments

Comments

@anthrotype
Copy link

I tried to write a simple CPython extension module that just prints "hello world" in order to try out the py_limited_api=True option of setuptools Extension class.
The code can be found here: https://github.com/anthrotype/hello-world-cpython-extension

When I build on macOS or Linux, the generated extension module has abi3 suffix and can be loaded on different CPython versions (e.g. 3.5 and 3.6).

On Windows however, the module (which doesn't get abi3 suffix, but is simply called hello_world.pyd when the py_limited_api=True option is set) cannot be imported by different CPython 3.x minor versions.

Dependency Walker says that the .pyd file is linked with PYTHON36.DLL, whereas I would have expected it to be linked with the stable API version of that which is called PYTHON3.DLL.

I came across this issue while debugging another related issue with bdist_wheel --py-limited-api that raises AssertionError on Windows because abi3 tag isn't supported on that platform:
hynek/argon2-cffi#32

cc @agronholm @dholt @zooba @pfmoore @dstufft @jaraco etc.

@anthrotype
Copy link
Author

It turns out it was my fault, not setuptools'...

I was doing #define Py_LIMITED_API when it was too late, i.e. after #include <Python.h>.
Whereas, it must be defined before Python.h is included. That's because the latter in turn includes the pyconfig.h header which on Windows has an MSVC specific compiler directive to link with the correct import library when compiling extension modules and Py_LIMITED_API is defined: #pragma comment(lib,"python3.lib")

If I move the Py_LIMITED_API definition at the top before including the Python.h header, then I can successfully compile an extension module that can be imported by multiple versions of CPython 3.x on Windows.

Sorry for the noise.

@dholth
Copy link
Member

dholth commented Jul 17, 2018

You could probably pass the #define as an argument to Extension() also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants