Skip to content

Commit

Permalink
TST: add test package with internal shared library, installed in site…
Browse files Browse the repository at this point in the history
…-packages
  • Loading branch information
rgommers committed Oct 27, 2024
1 parent d034a68 commit 44f372e
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/packages/sharedlib-in-package/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SPDX-FileCopyrightText: 2022 The meson-python developers
#
# SPDX-License-Identifier: MIT

project('sharedlib-in-package', 'c', version: '1.0.0')

py = import('python').find_installation(pure: false)

subdir('mypkg')
25 changes: 25 additions & 0 deletions tests/packages/sharedlib-in-package/mypkg/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# SPDX-FileCopyrightText: 2024 The meson-python developers
#
# SPDX-License-Identifier: MIT

import os


def _load_sharedlib():
"""Load the `example_lib.dll` shared library on Windows
This shared library is installed alongside this __init__.py file. Due to
lack of rpath support, Windows cannot find shared libraries installed
within wheels. So pre-load it.
"""
if os.name == "nt":
from ctypes import WinDLL
basedir = os.path.dirname(__file__)
dll_path = os.path.join(basedir, "examplelib.dll")
WinDLL(dll_path)


_load_sharedlib()


from ._example import example_sum
37 changes: 37 additions & 0 deletions tests/packages/sharedlib-in-package/mypkg/_examplemod.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-FileCopyrightText: 2022 The meson-python developers
//
// SPDX-License-Identifier: MIT

#include <Python.h>

#include "examplelib.h"

static PyObject* example_sum(PyObject* self, PyObject *args)
{
int a, b;
if (!PyArg_ParseTuple(args, "ii", &a, &b)) {
return NULL;
}

long result = sum(a, b);

return PyLong_FromLong(result);
}

static PyMethodDef methods[] = {
{"example_sum", (PyCFunction)example_sum, METH_VARARGS, NULL},
{NULL, NULL, 0, NULL},
};

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

PyMODINIT_FUNC PyInit__example(void)
{
return PyModule_Create(&module);
}
7 changes: 7 additions & 0 deletions tests/packages/sharedlib-in-package/mypkg/examplelib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-FileCopyrightText: 2022 The meson-python developers
//
// SPDX-License-Identifier: MIT

int sum(int a, int b) {
return a + b;
}
5 changes: 5 additions & 0 deletions tests/packages/sharedlib-in-package/mypkg/examplelib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// SPDX-FileCopyrightText: 2022 The meson-python developers
//
// SPDX-License-Identifier: MIT

int sum(int a, int b);
24 changes: 24 additions & 0 deletions tests/packages/sharedlib-in-package/mypkg/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# SPDX-FileCopyrightText: 2022 The meson-python developers
#
# SPDX-License-Identifier: MIT

example_lib = shared_library(
'examplelib',
'examplelib.c',
install: true,
install_dir: py.get_install_dir() / 'mypkg',
)

py.extension_module(
'_example',
'_examplemod.c',
link_with: example_lib,
install: true,
subdir: 'mypkg',
install_rpath: '$ORIGIN',
)

py.install_sources(
['__init__.py'],
subdir: 'mypkg',
)
7 changes: 7 additions & 0 deletions tests/packages/sharedlib-in-package/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-FileCopyrightText: 2022 The meson-python developers
#
# SPDX-License-Identifier: MIT

[build-system]
build-backend = 'mesonpy'
requires = ['meson-python']
7 changes: 7 additions & 0 deletions tests/test_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ def test_local_lib(venv, wheel_link_against_local_lib):
assert int(output) == 3


@pytest.mark.skipif(MESON_VERSION < (0, 64, 0), reason='Meson version too old')
def test_sharedlib_in_package(venv, wheel_sharedlib_in_package):
venv.pip('install', wheel_sharedlib_in_package)
output = venv.python('-c', 'import mypkg; print(mypkg.example_sum(2, 5))')
assert int(output) == 7


@pytest.mark.skipif(sys.platform not in {'linux', 'darwin'}, reason='Not supported on this platform')
def test_rpath(wheel_link_against_local_lib, tmp_path):
artifact = wheel.wheelfile.WheelFile(wheel_link_against_local_lib)
Expand Down

0 comments on commit 44f372e

Please sign in to comment.