Skip to content

Commit e1cee14

Browse files
committed
TST: add a shared library to the example_pkg package
This tests using a shared library from a Python extension module, which is relevant for SciPy (scipy.special contains such a shared library).
1 parent b0ab6ab commit e1cee14

File tree

9 files changed

+91
-6
lines changed

9 files changed

+91
-6
lines changed

example_pkg/example_pkg/__init__.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
1-
from ._core import echo
1+
import os
2+
import sys
23

3-
__all__ = ["echo"]
4+
__all__ = ["echo", "example_sum"]
45
__version__ = "0.0.0dev0"
6+
7+
8+
def _enable_sharedlib_loading():
9+
basedir = os.path.dirname(__file__)
10+
subdir = os.path.join(basedir, "submodule")
11+
if os.name == "nt":
12+
os.add_dll_directory(subdir)
13+
elif sys.platform == "cygwin":
14+
os.environ["PATH"] = f'os.environ["PATH"]{os.pathsep}{subdir}'
15+
16+
17+
_enable_sharedlib_loading()
18+
19+
20+
from ._core import echo, example_sum # noqa: E402

example_pkg/example_pkg/_core.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
def echo(str) -> None: ...
2+
def example_sum(a: int, b: int) -> int: ...

example_pkg/example_pkg/coremodule.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#define PY_SSIZE_T_CLEAN
22
#include <Python.h>
33

4+
#include "shlib.h"
5+
46
static PyObject *
57
core_echo(PyObject *self, PyObject *args)
68
{
@@ -17,8 +19,21 @@ core_echo(PyObject *self, PyObject *args)
1719
return ret;
1820
}
1921

22+
static PyObject *
23+
example_sum(PyObject *self, PyObject *args)
24+
{
25+
int a, b;
26+
if (!PyArg_ParseTuple(args, "ii", &a, &b)) {
27+
return NULL;
28+
}
29+
30+
long result = sum(a, b);
31+
return PyLong_FromLong(result);
32+
}
33+
2034
static PyMethodDef CoreMethods[] = {
2135
{"echo", core_echo, METH_VARARGS, "Echo a string and return 42"},
36+
{"example_sum", example_sum, METH_VARARGS, "Sum up two integers"},
2237
{NULL, NULL, 0, NULL} /* Sentinel */
2338
};
2439

example_pkg/example_pkg/meson.build

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
subdir('submodule')
2+
13
py.extension_module(
24
'_core',
35
'coremodule.c',
6+
include_directories: 'submodule',
7+
dependencies: shlib_dep,
48
install: true,
5-
subdir: 'example_pkg'
9+
subdir: 'example_pkg',
10+
install_rpath: '$ORIGIN/submodule',
611
)
712

813
python_sources = [
@@ -15,5 +20,4 @@ py.install_sources(
1520
subdir: 'example_pkg'
1621
)
1722

18-
install_subdir('submodule', install_dir: py.get_install_dir() / 'example_pkg')
1923
install_subdir('tests', install_dir: py.get_install_dir() / 'example_pkg')
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,29 @@
1+
# Add a basic shared library, used from the Python extension module
2+
# in ../coremodule.c
3+
if meson.get_compiler('c').get_id() in ['msvc', 'clang-cl', 'intel-cl']
4+
export_dll_args = ['-DSHLIB_DLL_EXPORTS']
5+
import_dll_args = ['-DSHLIB_DLL_IMPORTS']
6+
else
7+
export_dll_args = []
8+
import_dll_args = []
9+
endif
10+
11+
shlib = shared_library(
12+
'shlib',
13+
'shlib.c',
14+
c_args: export_dll_args,
15+
install: true,
16+
install_dir: py.get_install_dir() / 'example_pkg/submodule',
17+
)
18+
19+
shlib_dep = declare_dependency(
20+
compile_args: import_dll_args,
21+
link_with: shlib,
22+
)
23+
24+
py.install_sources(
25+
['__init__.py'],
26+
subdir: 'example_pkg/submodule',
27+
)
28+
129
install_subdir('tests', install_dir: py.get_install_dir() / 'example_pkg/submodule')
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "shlib_dll.h"
2+
3+
SHLIB_DLL int sum(int a, int b) {
4+
return a + b;
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "shlib_dll.h"
2+
3+
SHLIB_DLL int sum(int a, int b);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
// Windows support. When building the `shlib` DLL, this macro expands to
4+
// `__declspec(dllexport)` so we can annotate symbols appropriately as being
5+
// exported. When used in headers consuming a DLL, this macro expands to
6+
// `__declspec(dllimport)` so that consumers know the symbol is defined inside
7+
// the DLL. In all other cases, the macro expands to nothing.
8+
#if defined(SHLIB_DLL_EXPORTS)
9+
#define SHLIB_DLL __declspec(dllexport)
10+
#elif defined(SHLIB_DLL_IMPORTS)
11+
#define SHLIB_DLL __declspec(dllimport)
12+
#else
13+
#define SHLIB_DLL
14+
#endif

example_pkg/meson.build

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ project(
1313

1414
cc = meson.get_compiler('c')
1515

16-
py_mod = import('python')
17-
py = py_mod.find_installation(pure: false)
16+
py = import('python').find_installation(pure: false)
1817
py_dep = py.dependency()
1918

2019
subdir('example_pkg')

0 commit comments

Comments
 (0)