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

Avoid exporting incorrect PyInit_* symbols #12891

Merged
merged 1 commit into from
Aug 3, 2024

Conversation

jakelishman
Copy link
Member

Summary

Using the #[pymodule] derive macro in PyO3 0.21 always causes a PyInit_* symbol with a matching name to be exported in the output cdylib. This is required for the top-level module, in order for Python to import it---it needs to know which symbol in a shared library file it should call---but submodules must be manually initialised, so do not need it. Including it is typically harmless (and something we've been doing for a long time), but it is technically against the coding rules for CPython extensions1.

Recent versions of abi3audit (0.0.11+) have tightened their symbol checkers to better match the CPython guidelines, which causes our wheels to be rejected by their audits. This is, in theory, not a break of abi3 because CPython could never introduce an API-elvel PyInit_* function themselves without causing problems, so there ought to be no problems for our users, even with future Python versions. That said, we still want to pass the audit, because the coding guidelines are useful.

This commit is not the cleanest way of doing things. PyO3 0.22 includes a #[pymodule(submodule)] option on the attribute macro, which lets us use all the standard code generation while suppressing the unnecessary PyInit_* symbol. When we are ready to move to PyO3 0.22, we probably want to revert this commit to switch to that form.

Details and comments

Manual backport of #12889 for the 1.1 branch.

Footnotes

  1. https://docs.python.org/3/c-api/intro.html

Using the `#[pymodule]` derive macro in PyO3 0.21 always causes a
`PyInit_*` symbol with a matching name to be exported in the output
`cdylib`.  This is required for the top-level module, in order for
Python to import it---it needs to know which symbol in a shared library
file it should call---but submodules must be manually initialised, so do
not need it.  Including it is typically harmless (and something we've
been doing for a long time), but it is technically against the coding
rules for CPython extensions[^1].

Recent versions of `abi3audit` (0.0.11+) have tightened their symbol
checkers to better match the CPython guidelines, which causes our wheels
to be rejected by their audits.  This is, in theory, not a break of abi3
because CPython could never introduce an API-elvel `PyInit_*` function
themselves without causing problems, so there ought to be no problems
for our users, even with future Python versions. That said, we still
want to pass the audit, because the coding guidelines are useful.

This commit is not the cleanest way of doing things.  PyO3 0.22 includes
a `#[pymodule(submodule)]` option on the attribute macro, which lets us
use all the standard code generation while suppressing the unnecessary
`PyInit_*` symbol.  When we are ready to move to PyO3 0.22, we probably
want to revert this commit to switch to that form.

[^1]: https://docs.python.org/3/c-api/intro.html
@jakelishman jakelishman added type: qa Issues and PRs that relate to testing and code quality Changelog: None Do not include in changelog labels Aug 2, 2024
@jakelishman jakelishman added this to the 1.1.2 milestone Aug 2, 2024
@jakelishman jakelishman requested a review from a team as a code owner August 2, 2024 12:21
@qiskit-bot
Copy link
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core
  • @kevinhartman
  • @levbishop
  • @mtreinish

@mtreinish mtreinish added this pull request to the merge queue Aug 3, 2024
Merged via the queue into Qiskit:stable/1.1 with commit e447dc7 Aug 3, 2024
15 checks passed
@jakelishman jakelishman deleted the no-unnecessary-pyinit-1.1 branch October 4, 2024 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: None Do not include in changelog type: qa Issues and PRs that relate to testing and code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants