From 1312094b398e6153da2b8068bbfeca0360012964 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 19 Mar 2024 13:19:45 +0100 Subject: [PATCH] gh-116869: Test the limited C API in test_cext (#116995) --- Lib/test/test_cext/__init__.py | 22 ++++++++++++----- Lib/test/test_cext/extension.c | 16 +++++------- Lib/test/test_cext/setup.py | 45 ++++++++++++++++++++++++---------- 3 files changed, 54 insertions(+), 29 deletions(-) diff --git a/Lib/test/test_cext/__init__.py b/Lib/test/test_cext/__init__.py index 77929495336..992939241c5 100644 --- a/Lib/test/test_cext/__init__.py +++ b/Lib/test/test_cext/__init__.py @@ -27,17 +27,24 @@ SETUP = os.path.join(os.path.dirname(__file__), 'setup.py') @support.requires_resource('cpu') class TestExt(unittest.TestCase): def test_build_c99(self): - self.check_build('c99', '_test_c99_ext') + self.check_build('_test_c99_ext', std='c99') def test_build_c11(self): - self.check_build('c11', '_test_c11_ext') + self.check_build('_test_c11_ext', std='c11') - def check_build(self, clang_std, extension_name): + def test_build_limited(self): + self.check_build('_test_limited_ext', limited=True) + + def test_build_limited_c11(self): + self.check_build('_test_limited_c11_ext', limited=True, std='c11') + + def check_build(self, extension_name, std=None, limited=False): venv_dir = 'env' with support.setup_venv_with_pip_setuptools_wheel(venv_dir) as python_exe: - self._check_build(clang_std, extension_name, python_exe) + self._check_build(extension_name, python_exe, + std=std, limited=limited) - def _check_build(self, clang_std, extension_name, python_exe): + def _check_build(self, extension_name, python_exe, std, limited): pkg_dir = 'pkg' os.mkdir(pkg_dir) shutil.copy(SETUP, os.path.join(pkg_dir, os.path.basename(SETUP))) @@ -45,7 +52,10 @@ class TestExt(unittest.TestCase): def run_cmd(operation, cmd): env = os.environ.copy() - env['CPYTHON_TEST_STD'] = clang_std + if std: + env['CPYTHON_TEST_STD'] = std + if limited: + env['CPYTHON_TEST_LIMITED'] = '1' env['CPYTHON_TEST_EXT_NAME'] = extension_name if support.verbose: print('Run:', ' '.join(cmd)) diff --git a/Lib/test/test_cext/extension.c b/Lib/test/test_cext/extension.c index cfecad39f8a..a2433f2663c 100644 --- a/Lib/test/test_cext/extension.c +++ b/Lib/test/test_cext/extension.c @@ -6,12 +6,8 @@ #include "Python.h" -#if defined (__STDC_VERSION__) && __STDC_VERSION__ > 201710L -# define NAME _test_c2x_ext -#elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L -# define NAME _test_c11_ext -#else -# define NAME _test_c99_ext +#ifndef MODULE_NAME +# error "MODULE_NAME macro must be defined" #endif #define _STR(NAME) #NAME @@ -25,11 +21,11 @@ PyDoc_STRVAR(_testcext_add_doc, static PyObject * _testcext_add(PyObject *Py_UNUSED(module), PyObject *args) { - long i, j; + long i, j, res; if (!PyArg_ParseTuple(args, "ll:foo", &i, &j)) { return NULL; } - long res = i + j; + res = i + j; return PyLong_FromLong(res); } @@ -59,7 +55,7 @@ PyDoc_STRVAR(_testcext_doc, "C test extension."); static struct PyModuleDef _testcext_module = { PyModuleDef_HEAD_INIT, // m_base - STR(NAME), // m_name + STR(MODULE_NAME), // m_name _testcext_doc, // m_doc 0, // m_size _testcext_methods, // m_methods @@ -74,7 +70,7 @@ static struct PyModuleDef _testcext_module = { #define FUNC_NAME(NAME) _FUNC_NAME(NAME) PyMODINIT_FUNC -FUNC_NAME(NAME)(void) +FUNC_NAME(MODULE_NAME)(void) { return PyModuleDef_Init(&_testcext_module); } diff --git a/Lib/test/test_cext/setup.py b/Lib/test/test_cext/setup.py index aed7e6d46dd..5ca080a790a 100644 --- a/Lib/test/test_cext/setup.py +++ b/Lib/test/test_cext/setup.py @@ -27,24 +27,43 @@ else: def main(): - std = os.environ["CPYTHON_TEST_STD"] - name = os.environ["CPYTHON_TEST_EXT_NAME"] - cflags = [*CFLAGS, f'-std={std}'] + std = os.environ.get("CPYTHON_TEST_STD", "") + module_name = os.environ["CPYTHON_TEST_EXT_NAME"] + limited = bool(os.environ.get("CPYTHON_TEST_LIMITED", "")) - # Remove existing -std options to only test ours - cmd = (sysconfig.get_config_var('CC') or '') - if cmd is not None: - cmd = shlex.split(cmd) - cmd = [arg for arg in cmd if not arg.startswith('-std=')] - cmd = shlex.join(cmd) - # CC env var overrides sysconfig CC variable in setuptools - os.environ['CC'] = cmd + cflags = list(CFLAGS) + cflags.append(f'-DMODULE_NAME={module_name}') + + if std: + cflags.append(f'-std={std}') + + # Remove existing -std options to only test ours + cmd = (sysconfig.get_config_var('CC') or '') + if cmd is not None: + cmd = shlex.split(cmd) + cmd = [arg for arg in cmd if not arg.startswith('-std=')] + cmd = shlex.join(cmd) + # CC env var overrides sysconfig CC variable in setuptools + os.environ['CC'] = cmd + + if limited: + version = sys.hexversion + cflags.append(f'-DPy_LIMITED_API={version:#x}') + + for env_name in ('CC', 'CFLAGS'): + if env_name in os.environ: + print(f"{env_name} env var: {os.environ[env_name]!r}") + else: + print(f"{env_name} env var: ") + print(f"extra_compile_args: {cflags!r}") ext = Extension( - name, + module_name, sources=[SOURCE], extra_compile_args=cflags) - setup(name='internal' + name, version='0.0', ext_modules=[ext]) + setup(name=f'internal_{module_name}', + version='0.0', + ext_modules=[ext]) if __name__ == "__main__":