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.
33import os .path
4+ import shlex
45import shutil
5- import sys
6- import unittest
76import subprocess
8- import sysconfig
7+ import unittest
98from test import support
10- from test .support import os_helper
119
1210
1311SOURCE = os .path .join (os .path .dirname (__file__ ), 'extension.cpp' )
1412SETUP = os .path .join (os .path .dirname (__file__ ), 'setup.py' )
1513
1614
15+ # With MSVC on a debug build, the linker fails with: cannot open file
16+ # 'python311.lib', it should look 'python311_d.lib'.
17+ @unittest .skipIf (support .MS_WINDOWS and support .Py_DEBUG ,
18+ 'test fails on Windows debug build' )
19+ # Building and running an extension in clang sanitizing mode is not
20+ # straightforward
21+ @support .skip_if_sanitizer ('test does not work with analyzing builds' ,
22+ address = True , memory = True , ub = True , thread = True )
23+ # the test uses venv+pip: skip if it's not available
24+ @support .requires_venv_with_pip ()
1725@support .requires_subprocess ()
26+ @support .requires_resource ('cpu' )
1827class TestCPPExt (unittest .TestCase ):
19- @support .requires_resource ('cpu' )
20- def test_build_cpp11 (self ):
21- self .check_build (False , '_testcpp11ext' )
28+ def test_build (self ):
29+ self .check_build ('_testcppext' )
2230
23- @support .requires_resource ('cpu' )
2431 def test_build_cpp03 (self ):
25- self .check_build (True , '_testcpp03ext' )
32+ self .check_build ('_testcpp03ext' , std = 'c++03' )
33+
34+ @unittest .skipIf (support .MS_WINDOWS , "MSVC doesn't support /std:c++11" )
35+ def test_build_cpp11 (self ):
36+ self .check_build ('_testcpp11ext' , std = 'c++11' )
37+
38+ # Only test C++14 on MSVC.
39+ # On s390x RHEL7, GCC 4.8.5 doesn't support C++14.
40+ @unittest .skipIf (not support .MS_WINDOWS , "need Windows" )
41+ def test_build_cpp14 (self ):
42+ self .check_build ('_testcpp14ext' , std = 'c++14' )
2643
27- # With MSVC, the linker fails with: cannot open file 'python311.lib'
28- # https://github.com/python/cpython/pull/32175#issuecomment-1111175897
29- @unittest .skipIf (support .MS_WINDOWS , 'test fails on Windows' )
30- # Building and running an extension in clang sanitizing mode is not
31- # straightforward
32- @unittest .skipIf (
33- '-fsanitize' in (sysconfig .get_config_var ('PY_CFLAGS' ) or '' ),
34- 'test does not work with analyzing builds' )
35- # the test uses venv+pip: skip if it's not available
36- @support .requires_venv_with_pip ()
37- def check_build (self , std_cpp03 , extension_name ):
44+ def check_build (self , extension_name , std = None ):
3845 venv_dir = 'env'
3946 with support .setup_venv_with_pip_setuptools_wheel (venv_dir ) as python_exe :
40- self ._check_build (std_cpp03 , extension_name , python_exe )
47+ self ._check_build (extension_name , python_exe , std = std )
4148
42- def _check_build (self , std_cpp03 , extension_name , python_exe ):
49+ def _check_build (self , extension_name , python_exe , std ):
4350 pkg_dir = 'pkg'
4451 os .mkdir (pkg_dir )
4552 shutil .copy (SETUP , os .path .join (pkg_dir , os .path .basename (SETUP )))
4653 shutil .copy (SOURCE , os .path .join (pkg_dir , os .path .basename (SOURCE )))
4754
4855 def run_cmd (operation , cmd ):
4956 env = os .environ .copy ()
50- env ['CPYTHON_TEST_CPP_STD' ] = 'c++03' if std_cpp03 else 'c++11'
57+ if std :
58+ env ['CPYTHON_TEST_CPP_STD' ] = std
5159 env ['CPYTHON_TEST_EXT_NAME' ] = extension_name
5260 if support .verbose :
53- print ('Run:' , ' ' .join (cmd ))
61+ print ('Run:' , ' ' .join (map ( shlex . quote , cmd ) ))
5462 subprocess .run (cmd , check = True , env = env )
5563 else :
5664 proc = subprocess .run (cmd ,
@@ -59,6 +67,7 @@ def run_cmd(operation, cmd):
5967 stderr = subprocess .STDOUT ,
6068 text = True )
6169 if proc .returncode :
70+ print ('Run:' , ' ' .join (map (shlex .quote , cmd )))
6271 print (proc .stdout , end = '' )
6372 self .fail (
6473 f"{ operation } failed with exit code { proc .returncode } " )
@@ -67,6 +76,8 @@ def run_cmd(operation, cmd):
6776 cmd = [python_exe , '-X' , 'dev' ,
6877 '-m' , 'pip' , 'install' , '--no-build-isolation' ,
6978 os .path .abspath (pkg_dir )]
79+ if support .verbose :
80+ cmd .append ('-v' )
7081 run_cmd ('Install' , cmd )
7182
7283 # Do a reference run. Until we test that running python
0 commit comments