Skip to content

Commit d413403

Browse files
committed
Update test_cppext to stop invoking setuptools-based script as a CLI
This allows the "package" to be built using regular Python packaging workflows instead of relying on `setuptools` being present in the virtual environment.
1 parent 6aef7f9 commit d413403

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

Lib/test/cppextdata/setup.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# gh-91321: Build a basic C++ test extension to check that the Python C API is
2+
# compatible with C++ and does not emit C++ compiler warnings.
3+
import os
4+
import sys
5+
from test import support
6+
7+
from setuptools import setup, Extension
8+
9+
10+
MS_WINDOWS = (sys.platform == 'win32')
11+
12+
13+
SOURCE = support.findfile('_testcppext.cpp')
14+
if not MS_WINDOWS:
15+
# C++ compiler flags for GCC and clang
16+
CPPFLAGS = [
17+
# gh-91321: The purpose of _testcppext extension is to check that building
18+
# a C++ extension using the Python C API does not emit C++ compiler
19+
# warnings
20+
'-Werror',
21+
]
22+
else:
23+
# Don't pass any compiler flag to MSVC
24+
CPPFLAGS = []
25+
26+
27+
def main():
28+
cppflags = list(CPPFLAGS)
29+
std = os.environ["CPYTHON_TEST_CPP_STD"]
30+
name = os.environ["CPYTHON_TEST_EXT_NAME"]
31+
32+
cppflags = [*CPPFLAGS, f'-std={std}']
33+
34+
cpp_ext = Extension(
35+
name,
36+
sources=[SOURCE],
37+
language='c++',
38+
extra_compile_args=cppflags)
39+
setup(name='internal' + name, version='0.0', ext_modules=[cpp_ext])
40+
41+
42+
if __name__ == "__main__":
43+
main()

Lib/test/test_cppext.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# gh-91321: Build a basic C++ test extension to check that the Python C API is
22
# compatible with C++ and does not emit C++ compiler warnings.
33
import os.path
4+
try:
5+
import ssl
6+
except ImportError:
7+
ssl = None
48
import sys
59
import unittest
610
import subprocess
@@ -12,7 +16,7 @@
1216
MS_WINDOWS = (sys.platform == 'win32')
1317

1418

15-
SETUP_TESTCPPEXT = support.findfile('setup_testcppext.py')
19+
PKG_CPPEXTDATA = os.path.dirname(support.findfile('setup.py', subdir="cppextdata"))
1620

1721

1822
@support.requires_subprocess()
@@ -31,6 +35,8 @@ def test_build_cpp03(self):
3135
@unittest.skipIf(
3236
'-fsanitize' in (sysconfig.get_config_var('PY_CFLAGS') or ''),
3337
'test does not work with analyzing builds')
38+
# the test uses pip which needs a TLS connection to PyPI
39+
@unittest.skipIf(ssl is None, 'No ssl module')
3440
# the test uses venv+pip: skip if it's not available
3541
@support.requires_venv_with_pip()
3642
def check_build(self, std_cpp03, extension_name):
@@ -76,14 +82,9 @@ def run_cmd(operation, cmd):
7682
self.fail(
7783
f"{operation} failed with exit code {proc.returncode}")
7884

79-
# Build the C++ extension
85+
# Build and install the C++ extension
8086
cmd = [python, '-X', 'dev',
81-
SETUP_TESTCPPEXT, 'build_ext', '--verbose']
82-
run_cmd('Build', cmd)
83-
84-
# Install the C++ extension
85-
cmd = [python, '-X', 'dev',
86-
SETUP_TESTCPPEXT, 'install']
87+
'-m', 'pip', 'install', PKG_CPPEXTDATA]
8788
run_cmd('Install', cmd)
8889

8990
# Do a reference run. Until we test that running python

0 commit comments

Comments
 (0)