Skip to content

Commit

Permalink
tests: add python limited_api tests for find_installation and `de…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
lgarrison committed Jan 22, 2025
1 parent cf8f71e commit 4a06207
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
28 changes: 28 additions & 0 deletions test cases/python/11 extmodule limited api lib/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Test that we can build a library that uses the limited API
# and link it to an extension module.

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],
)

test('load-test',
py,
args: [files('test_module.py')],
env: { 'PYTHONPATH': meson.current_build_dir() },
workdir: meson.current_source_dir()
)
27 changes: 27 additions & 0 deletions test cases/python/11 extmodule limited api lib/module.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <Python.h>

#ifndef Py_LIMITED_API
#error Py_LIMITED_API must be defined.
#elif Py_LIMITED_API != 0x03070000
#error Wrong value for Py_LIMITED_API
#endif

PyObject *
hello(PyObject * Py_UNUSED(self), PyObject * Py_UNUSED(args));

static struct PyMethodDef methods[] = {
{ "hello", hello, METH_NOARGS, NULL },
{ NULL, NULL, 0, NULL },
};

static struct PyModuleDef mymodule = {
PyModuleDef_HEAD_INIT,
"mymodule",
NULL,
-1,
methods
};

PyMODINIT_FUNC PyInit_mymodule(void) {
return PyModule_Create(&mymodule);
}
12 changes: 12 additions & 0 deletions test cases/python/11 extmodule limited api lib/nanolib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <Python.h>

#ifndef Py_LIMITED_API
#error Py_LIMITED_API must be defined.
#elif Py_LIMITED_API != 0x03070000
#error Wrong value for Py_LIMITED_API
#endif

PyObject *
hello(PyObject * Py_UNUSED(self), PyObject * Py_UNUSED(args)) {
return PyUnicode_FromString("hello world");
}
6 changes: 6 additions & 0 deletions test cases/python/11 extmodule limited api lib/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"installed": [
{"type": "python_limited_lib", "file": "usr/@PYTHON_PLATLIB@/mymodule"},
{"type": "py_limited_implib", "file": "usr/@PYTHON_PLATLIB@/mymodule"}
]
}
6 changes: 6 additions & 0 deletions test cases/python/11 extmodule limited api lib/test_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from mymodule import hello

def test_hello():
assert hello() == "hello world"

test_hello()
29 changes: 29 additions & 0 deletions test cases/python/9 extmodule limited api/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,32 @@ test('load-test',
env: { 'PYTHONPATH': meson.current_build_dir() },
workdir: meson.current_source_dir()
)

# Test inheritance and override of limited_api

py_limited = py_mod.find_installation(limited_api: '3.7')

py_limited.extension_module('limited_inherit',
'limited.c',
)

py_limited.extension_module('not_limited_override',
'not_limited.c',
limited_api: '',
)

py_dep_limited = py.dependency(required: true, limited_api: '3.7')

static_library(
'limited_dependency',
'limited.c',
dependencies: [py_dep_limited],
)

py_dep_inherit = py_limited.dependency(required: true)

static_library(
'limited_dependency_inherit',
'limited.c',
dependencies: [py_dep_inherit],
)

0 comments on commit 4a06207

Please sign in to comment.