Skip to content

Commit 7c0ca08

Browse files
committed
tests: add test for the Python build config
Signed-off-by: Filipe Laíns <[email protected]>
1 parent a211c3f commit 7c0ca08

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#define PY_SSIZE_T_CLEAN
2+
#include <Python.h>
3+
4+
5+
static PyObject *
6+
bar_impl(PyObject *self)
7+
{
8+
return Py_None;
9+
}
10+
11+
12+
static PyMethodDef foo_methods[] = {
13+
{"bar", bar_impl, METH_NOARGS, NULL},
14+
{NULL, NULL, 0, NULL} /* sentinel */
15+
};
16+
17+
18+
static struct PyModuleDef foo_module = {
19+
PyModuleDef_HEAD_INIT,
20+
"foo", /* m_name */
21+
NULL, /* m_doc */
22+
-1, /* m_size */
23+
foo_methods, /* m_methods */
24+
};
25+
26+
27+
PyMODINIT_FUNC
28+
PyInit_foo(void)
29+
{
30+
return PyModule_Create(&foo_module);
31+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
project('python extension', 'c')
2+
3+
py = import('python').find_installation('')
4+
5+
py.extension_module(
6+
'foo', 'foo.c',
7+
install: true,
8+
)
9+
10+
py.extension_module(
11+
'foo_stable', 'foo.c',
12+
install: true,
13+
limited_api: '3.2',
14+
)
15+

unittests/allplatformstests.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import pickle
1313
import zipfile, tarfile
1414
import sys
15+
import sysconfig
1516
from unittest import mock, SkipTest, skipIf, skipUnless
1617
from contextlib import contextmanager
1718
from glob import glob
@@ -2948,6 +2949,88 @@ def test_pkg_config_libdir(self):
29482949
self.wipe()
29492950
self.init(testdir, extra_args=['-Dstart_native=true'], override_envvars=env)
29502951

2952+
@skipIf(is_windows(), 'POSIX only')
2953+
def test_python_build_config_extensions(self):
2954+
testdir = os.path.join(self.unit_test_dir,
2955+
'125 python extension')
2956+
2957+
VERSION_INFO_KEYS = ('major', 'minor', 'micro', 'releaselevel', 'serial')
2958+
EXTENSION_SUFFIX = '.extension-suffix.so'
2959+
STABLE_ABI_SUFFIX = '.stable-abi-suffix.so'
2960+
2961+
python_build_config = {
2962+
'schema_version': '1.0',
2963+
'base_interpreter': sys.executable,
2964+
'base_prefix': '/usr',
2965+
'platform': sysconfig.get_platform(),
2966+
'language': {
2967+
'version': sysconfig.get_python_version(),
2968+
'version_info': {key: getattr(sys.version_info, key) for key in VERSION_INFO_KEYS}
2969+
},
2970+
'implementation': {
2971+
attr: (
2972+
getattr(sys.implementation, attr)
2973+
if attr != 'version' else
2974+
{key: getattr(sys.implementation.version, key) for key in VERSION_INFO_KEYS}
2975+
)
2976+
for attr in dir(sys.implementation)
2977+
if not attr.startswith('__')
2978+
},
2979+
'abi': {
2980+
'flags': [],
2981+
'extension_suffix': EXTENSION_SUFFIX,
2982+
'stable_abi_suffix': STABLE_ABI_SUFFIX,
2983+
},
2984+
'suffixes': {
2985+
'source': ['.py'],
2986+
'bytecode': ['.pyc'],
2987+
'optimized_bytecode': ['.pyc'],
2988+
'debug_bytecode': ['.pyc'],
2989+
'extensions': [EXTENSION_SUFFIX, STABLE_ABI_SUFFIX, '.so'],
2990+
},
2991+
'libpython': {
2992+
'dynamic': sysconfig.get_config_var('LDLIBRARY'),
2993+
'dynamic_stableabi': sysconfig.get_config_var('PY3LIBRARY'),
2994+
'static': sysconfig.get_config_var('LIBRARY'),
2995+
'link_extensions': True,
2996+
},
2997+
'c_api': {
2998+
'headers': sysconfig.get_path('include'),
2999+
'pkgconfig_path': sysconfig.get_config_var('LIBPC'),
3000+
}
3001+
}
3002+
with tempfile.NamedTemporaryFile(mode='w', delete=False, encoding='utf-8') as python_build_config_file:
3003+
json.dump(python_build_config, fp=python_build_config_file)
3004+
3005+
with tempfile.NamedTemporaryFile(mode='w', delete=False, encoding='utf-8') as cross_file:
3006+
cross_file.write(
3007+
textwrap.dedent(f'''
3008+
[binaries]
3009+
pkg-config = 'pkg-config'
3010+
3011+
[built-in options]
3012+
python.build_config = '{python_build_config_file.name}'
3013+
'''.strip())
3014+
)
3015+
cross_file.flush()
3016+
3017+
intro_installed_file = os.path.join(self.builddir, 'meson-info', 'intro-installed.json')
3018+
expected_files = [
3019+
os.path.join(self.builddir, 'foo' + EXTENSION_SUFFIX),
3020+
os.path.join(self.builddir, 'foo_stable' + STABLE_ABI_SUFFIX),
3021+
]
3022+
3023+
for extra_args in (
3024+
['--python.build-config', python_build_config_file.name],
3025+
['--cross-file', cross_file.name],
3026+
):
3027+
with self.subTest(extra_args=extra_args):
3028+
self.init(testdir, extra_args=extra_args)
3029+
with open(intro_installed_file) as f:
3030+
intro_installed = json.load(f)
3031+
self.assertEqual(expected_files, list(intro_installed))
3032+
self.wipe()
3033+
29513034
def __reconfigure(self):
29523035
# Set an older version to force a reconfigure from scratch
29533036
filename = os.path.join(self.privatedir, 'coredata.dat')

0 commit comments

Comments
 (0)