Skip to content

Commit 143a816

Browse files
FFY00mgorny
authored andcommitted
tests: add test for the Python build config
Signed-off-by: Filipe Laíns <[email protected]>
1 parent 170f77b commit 143a816

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
@@ -13,6 +13,7 @@
1313
import pickle
1414
import zipfile, tarfile
1515
import sys
16+
import sysconfig
1617
from unittest import mock, SkipTest, skipIf, skipUnless, expectedFailure
1718
from contextlib import contextmanager
1819
from glob import glob
@@ -2981,6 +2982,88 @@ def test_pkg_config_libdir(self):
29812982
self.wipe()
29822983
self.init(testdir, extra_args=['-Dstart_native=true'], override_envvars=env)
29832984

2985+
@skipIf(is_windows(), 'POSIX only')
2986+
def test_python_build_config_extensions(self):
2987+
testdir = os.path.join(self.unit_test_dir,
2988+
'125 python extension')
2989+
2990+
VERSION_INFO_KEYS = ('major', 'minor', 'micro', 'releaselevel', 'serial')
2991+
EXTENSION_SUFFIX = '.extension-suffix.so'
2992+
STABLE_ABI_SUFFIX = '.stable-abi-suffix.so'
2993+
2994+
python_build_config = {
2995+
'schema_version': '1.0',
2996+
'base_interpreter': sys.executable,
2997+
'base_prefix': '/usr',
2998+
'platform': sysconfig.get_platform(),
2999+
'language': {
3000+
'version': sysconfig.get_python_version(),
3001+
'version_info': {key: getattr(sys.version_info, key) for key in VERSION_INFO_KEYS}
3002+
},
3003+
'implementation': {
3004+
attr: (
3005+
getattr(sys.implementation, attr)
3006+
if attr != 'version' else
3007+
{key: getattr(sys.implementation.version, key) for key in VERSION_INFO_KEYS}
3008+
)
3009+
for attr in dir(sys.implementation)
3010+
if not attr.startswith('__')
3011+
},
3012+
'abi': {
3013+
'flags': [],
3014+
'extension_suffix': EXTENSION_SUFFIX,
3015+
'stable_abi_suffix': STABLE_ABI_SUFFIX,
3016+
},
3017+
'suffixes': {
3018+
'source': ['.py'],
3019+
'bytecode': ['.pyc'],
3020+
'optimized_bytecode': ['.pyc'],
3021+
'debug_bytecode': ['.pyc'],
3022+
'extensions': [EXTENSION_SUFFIX, STABLE_ABI_SUFFIX, '.so'],
3023+
},
3024+
'libpython': {
3025+
'dynamic': sysconfig.get_config_var('LDLIBRARY'),
3026+
'dynamic_stableabi': sysconfig.get_config_var('PY3LIBRARY'),
3027+
'static': sysconfig.get_config_var('LIBRARY'),
3028+
'link_extensions': True,
3029+
},
3030+
'c_api': {
3031+
'headers': sysconfig.get_path('include'),
3032+
'pkgconfig_path': sysconfig.get_config_var('LIBPC'),
3033+
}
3034+
}
3035+
with tempfile.NamedTemporaryFile(mode='w', delete=False, encoding='utf-8') as python_build_config_file:
3036+
json.dump(python_build_config, fp=python_build_config_file)
3037+
3038+
with tempfile.NamedTemporaryFile(mode='w', delete=False, encoding='utf-8') as cross_file:
3039+
cross_file.write(
3040+
textwrap.dedent(f'''
3041+
[binaries]
3042+
pkg-config = 'pkg-config'
3043+
3044+
[built-in options]
3045+
python.build_config = '{python_build_config_file.name}'
3046+
'''.strip())
3047+
)
3048+
cross_file.flush()
3049+
3050+
intro_installed_file = os.path.join(self.builddir, 'meson-info', 'intro-installed.json')
3051+
expected_files = [
3052+
os.path.join(self.builddir, 'foo' + EXTENSION_SUFFIX),
3053+
os.path.join(self.builddir, 'foo_stable' + STABLE_ABI_SUFFIX),
3054+
]
3055+
3056+
for extra_args in (
3057+
['--python.build-config', python_build_config_file.name],
3058+
['--cross-file', cross_file.name],
3059+
):
3060+
with self.subTest(extra_args=extra_args):
3061+
self.init(testdir, extra_args=extra_args)
3062+
with open(intro_installed_file) as f:
3063+
intro_installed = json.load(f)
3064+
self.assertEqual(expected_files, list(intro_installed))
3065+
self.wipe()
3066+
29843067
def __reconfigure(self):
29853068
# Set an older version to force a reconfigure from scratch
29863069
filename = os.path.join(self.privatedir, 'coredata.dat')

0 commit comments

Comments
 (0)