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

python: add limited_api kwarg to find_installation() #14176

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

lgarrison
Copy link

As discussed in #13824.

To recap: we'd like libraries (like nanobind) to be able to target the Python Limited API in addition to extensions. Basically, we'd like this to work:

project('Python limited api lib', 'c',
  default_options : ['buildtype=release', 'werror=true'])

py_mod = import('python')
py = py_mod.find_installation(limited_api: '3.7')
py_dep = py.dependency(required: true)

nanolib = static_library(
    'nanolib',
    'nanolib.c',
    dependencies: [py_dep],
)

ext_mod = py.extension_module('mymodule',
  'module.c',
  install: true,
  link_with: [nanolib],
)

Right now, this isn't possible because limited_api is only an arg to extension_module. This PR makes it an arg to find_installation and dependency, too. If the extension_module or dependency value isn't specified, it is inherited from find_installation by default. Making it an arg to dependency isn't strictly necessary, but it does make it symmetrical with extension_module.

Before I update the documentation, I wanted to open the PR to get feedback and make sure CI is passing.

To support libraries that target the Python Limited API but are not
themselves Python extensions, we add a `limited_api` kwarg to
find_installation. This follows the example of the `pure` kwarg.
`python_dependency` objects now hold a `limited_api` value and accept it as
a kwarg in `python_installation.dependency()`. By default, the value is
inherited from the `python_installation` but can be overridden.

If `limited_api` is set, the appropriate `-DPy_LIMITED_API` hex define is
propagated to compile targets.
…i` value internally via the dependency

Now that dependencies can propagate the `limited_api` define to targets,
let the `extension_module`'s internal dependency handle that.

From the user's point of view, nothing about the API has changed,
except that if `find_installation(limited_api: ...)` was used,
then `extension_module` will now inherit that value by default
(but can override it).
…pendency`

Check that extension_modules and dependencies inherit the installation's
limited_api value by default but can override it.

Also test the end-to-end case of compiling a library and linking it with
an extension, both compiled against the Limited API. This is similar to
how nanobind is used.
@lgarrison lgarrison requested a review from jpakkane as a code owner January 22, 2025 21:28
@lgarrison
Copy link
Author

CC @eli-schwartz @rgommers

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

Successfully merging this pull request may close these issues.

1 participant