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 unittest
66import subprocess
7+ import unittest
78from test import support
89
910
1011SOURCE = os .path .join (os .path .dirname (__file__ ), 'extension.cpp' )
1112SETUP = os .path .join (os .path .dirname (__file__ ), 'setup.py' )
1213
1314
14- # With MSVC, the linker fails with: cannot open file 'python311.lib'
15- # https://github.com/python/cpython/pull/32175#issuecomment-1111175897
16- @unittest .skipIf (support .MS_WINDOWS , 'test fails on Windows' )
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' )
1719# Building and running an extension in clang sanitizing mode is not
1820# straightforward
1921@support .skip_if_sanitizer ('test does not work with analyzing builds' ,
2325@support .requires_subprocess ()
2426@support .requires_resource ('cpu' )
2527class TestCPPExt (unittest .TestCase ):
26- def test_build_cpp11 (self ):
27- self .check_build (False , '_testcpp11ext ' )
28+ def test_build (self ):
29+ self .check_build ('_testcppext ' )
2830
2931 def test_build_cpp03 (self ):
30- 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+ def test_build_cpp14 (self ):
39+ self .check_build ('_testcpp14ext' , std = 'c++14' )
3140
32- def check_build (self , std_cpp03 , extension_name ):
41+ def check_build (self , extension_name , std = None ):
3342 venv_dir = 'env'
3443 with support .setup_venv_with_pip_setuptools_wheel (venv_dir ) as python_exe :
35- self ._check_build (std_cpp03 , extension_name , python_exe )
44+ self ._check_build (extension_name , python_exe , std = std )
3645
37- def _check_build (self , std_cpp03 , extension_name , python_exe ):
46+ def _check_build (self , extension_name , python_exe , std ):
3847 pkg_dir = 'pkg'
3948 os .mkdir (pkg_dir )
4049 shutil .copy (SETUP , os .path .join (pkg_dir , os .path .basename (SETUP )))
4150 shutil .copy (SOURCE , os .path .join (pkg_dir , os .path .basename (SOURCE )))
4251
4352 def run_cmd (operation , cmd ):
4453 env = os .environ .copy ()
45- env ['CPYTHON_TEST_CPP_STD' ] = 'c++03' if std_cpp03 else 'c++11'
54+ if std :
55+ env ['CPYTHON_TEST_CPP_STD' ] = std
4656 env ['CPYTHON_TEST_EXT_NAME' ] = extension_name
4757 if support .verbose :
48- print ('Run:' , ' ' .join (cmd ))
58+ print ('Run:' , ' ' .join (map ( shlex . quote , cmd ) ))
4959 subprocess .run (cmd , check = True , env = env )
5060 else :
5161 proc = subprocess .run (cmd ,
@@ -54,6 +64,7 @@ def run_cmd(operation, cmd):
5464 stderr = subprocess .STDOUT ,
5565 text = True )
5666 if proc .returncode :
67+ print ('Run:' , ' ' .join (map (shlex .quote , cmd )))
5768 print (proc .stdout , end = '' )
5869 self .fail (
5970 f"{ operation } failed with exit code { proc .returncode } " )
0 commit comments