1
1
# gh-91321: Build a basic C++ test extension to check that the Python C API is
2
2
# compatible with C++ and does not emit C++ compiler warnings.
3
3
import os .path
4
+ import shlex
4
5
import shutil
5
- import sys
6
- import unittest
7
6
import subprocess
8
- import sysconfig
7
+ import unittest
9
8
from test import support
10
- from test .support import os_helper
11
9
12
10
13
11
SOURCE = os .path .join (os .path .dirname (__file__ ), 'extension.cpp' )
14
12
SETUP = os .path .join (os .path .dirname (__file__ ), 'setup.py' )
15
13
16
14
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 ()
17
25
@support .requires_subprocess ()
26
+ @support .requires_resource ('cpu' )
18
27
class 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' )
22
30
23
- @support .requires_resource ('cpu' )
24
31
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' )
26
43
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 ):
38
45
venv_dir = 'env'
39
46
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 )
41
48
42
- def _check_build (self , std_cpp03 , extension_name , python_exe ):
49
+ def _check_build (self , extension_name , python_exe , std ):
43
50
pkg_dir = 'pkg'
44
51
os .mkdir (pkg_dir )
45
52
shutil .copy (SETUP , os .path .join (pkg_dir , os .path .basename (SETUP )))
46
53
shutil .copy (SOURCE , os .path .join (pkg_dir , os .path .basename (SOURCE )))
47
54
48
55
def run_cmd (operation , cmd ):
49
56
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
51
59
env ['CPYTHON_TEST_EXT_NAME' ] = extension_name
52
60
if support .verbose :
53
- print ('Run:' , ' ' .join (cmd ))
61
+ print ('Run:' , ' ' .join (map ( shlex . quote , cmd ) ))
54
62
subprocess .run (cmd , check = True , env = env )
55
63
else :
56
64
proc = subprocess .run (cmd ,
@@ -59,6 +67,7 @@ def run_cmd(operation, cmd):
59
67
stderr = subprocess .STDOUT ,
60
68
text = True )
61
69
if proc .returncode :
70
+ print ('Run:' , ' ' .join (map (shlex .quote , cmd )))
62
71
print (proc .stdout , end = '' )
63
72
self .fail (
64
73
f"{ operation } failed with exit code { proc .returncode } " )
@@ -67,6 +76,8 @@ def run_cmd(operation, cmd):
67
76
cmd = [python_exe , '-X' , 'dev' ,
68
77
'-m' , 'pip' , 'install' , '--no-build-isolation' ,
69
78
os .path .abspath (pkg_dir )]
79
+ if support .verbose :
80
+ cmd .append ('-v' )
70
81
run_cmd ('Install' , cmd )
71
82
72
83
# Do a reference run. Until we test that running python
0 commit comments