Skip to content

Commit 2dd3221

Browse files
committed
TST: add test package with internal shared library, installed in site-packages
1 parent d034a68 commit 2dd3221

File tree

8 files changed

+117
-0
lines changed

8 files changed

+117
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-FileCopyrightText: 2022 The meson-python developers
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
project('sharedlib-in-package', 'c', version: '1.0.0')
6+
7+
py = import('python').find_installation(pure: false)
8+
9+
subdir('mypkg')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# SPDX-FileCopyrightText: 2024 The meson-python developers
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
def _load_sharedlib():
6+
"""Load the `example_lib.dll` shared library on Windows
7+
8+
This shared library is installed alongside this __init__.py file. Due to
9+
lack of rpath support, Windows cannot find shared libraries installed
10+
within wheels. So pre-load it.
11+
"""
12+
if os.name == "nt":
13+
from ctypes import WinDLL
14+
basedir = os.path.dirname(__file__)
15+
dll_path = os.path.join(basedir, "example_lib.dll")
16+
WinDLL(dll_path)
17+
18+
19+
_load_sharedlib()
20+
21+
22+
from ._example import example_sum
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// SPDX-FileCopyrightText: 2022 The meson-python developers
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include <Python.h>
6+
7+
#include "examplelib.h"
8+
9+
static PyObject* example_sum(PyObject* self, PyObject *args)
10+
{
11+
int a, b;
12+
if (!PyArg_ParseTuple(args, "ii", &a, &b)) {
13+
return NULL;
14+
}
15+
16+
long result = sum(a, b);
17+
18+
return PyLong_FromLong(result);
19+
}
20+
21+
static PyMethodDef methods[] = {
22+
{"example_sum", (PyCFunction)example_sum, METH_VARARGS, NULL},
23+
{NULL, NULL, 0, NULL},
24+
};
25+
26+
static struct PyModuleDef module = {
27+
PyModuleDef_HEAD_INIT,
28+
"_example",
29+
NULL,
30+
-1,
31+
methods,
32+
};
33+
34+
PyMODINIT_FUNC PyInit__example(void)
35+
{
36+
return PyModule_Create(&module);
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// SPDX-FileCopyrightText: 2022 The meson-python developers
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
int sum(int a, int b) {
6+
return a + b;
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// SPDX-FileCopyrightText: 2022 The meson-python developers
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
int sum(int a, int b);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# SPDX-FileCopyrightText: 2022 The meson-python developers
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
example_lib = shared_library(
6+
'examplelib',
7+
'examplelib.c',
8+
install: true,
9+
install_dir: py.get_install_dir() / 'mypkg',
10+
)
11+
12+
py.extension_module(
13+
'_example',
14+
'_examplemod.c',
15+
link_with: example_lib,
16+
install: true,
17+
subdir: 'mypkg',
18+
install_rpath: '$ORIGIN',
19+
)
20+
21+
py.install_sources(
22+
['__init__.py'],
23+
subdir: 'mypkg',
24+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SPDX-FileCopyrightText: 2022 The meson-python developers
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
[build-system]
6+
build-backend = 'mesonpy'
7+
requires = ['meson-python']

tests/test_wheel.py

+6
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ def test_local_lib(venv, wheel_link_against_local_lib):
161161
assert int(output) == 3
162162

163163

164+
def test_sharedlib_in_package(venv, wheel_sharedlib_in_package):
165+
venv.pip('install', wheel_sharedlib_in_package)
166+
output = venv.python('-c', 'import mypkg; print(mypkg.example_sum(2, 5))')
167+
assert int(output) == 7
168+
169+
164170
@pytest.mark.skipif(sys.platform not in {'linux', 'darwin'}, reason='Not supported on this platform')
165171
def test_rpath(wheel_link_against_local_lib, tmp_path):
166172
artifact = wheel.wheelfile.WheelFile(wheel_link_against_local_lib)

0 commit comments

Comments
 (0)