| 1 |
|
| 2 |
|
| 3 |
import os.path |
| 4 |
import sys |
| 5 |
import unittest |
| 6 |
import subprocess |
| 7 |
import sysconfig |
| 8 |
from test import support |
| 9 |
from test.support import os_helper |
| 10 |
|
| 11 |
|
| 12 |
SETUP = os.path.join(os.path.dirname(__file__), 'setup.py') |
| 13 |
|
| 14 |
|
| 15 |
@support.requires_subprocess() |
| 16 |
class TestCPPExt(unittest.TestCase): |
| 17 |
@support.requires_resource('cpu') |
| 18 |
def test_build_cpp11(self): |
| 19 |
self.check_build(False, '_testcpp11ext') |
| 20 |
|
| 21 |
@support.requires_resource('cpu') |
| 22 |
def test_build_cpp03(self): |
| 23 |
self.check_build(True, '_testcpp03ext') |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
@unittest.skipIf(support.MS_WINDOWS, 'test fails on Windows') |
| 28 |
|
| 29 |
|
| 30 |
@unittest.skipIf( |
| 31 |
'-fsanitize' in (sysconfig.get_config_var('PY_CFLAGS') or ''), |
| 32 |
'test does not work with analyzing builds') |
| 33 |
|
| 34 |
@support.requires_venv_with_pip() |
| 35 |
def check_build(self, std_cpp03, extension_name): |
| 36 |
|
| 37 |
with os_helper.temp_cwd(): |
| 38 |
self._check_build(std_cpp03, extension_name) |
| 39 |
|
| 40 |
def _check_build(self, std_cpp03, extension_name): |
| 41 |
venv_dir = 'env' |
| 42 |
verbose = support.verbose |
| 43 |
|
| 44 |
|
| 45 |
cmd = [sys.executable, '-X', 'dev', '-m', 'venv', venv_dir] |
| 46 |
if verbose: |
| 47 |
print() |
| 48 |
print('Run:', ' '.join(cmd)) |
| 49 |
subprocess.run(cmd, check=True) |
| 50 |
|
| 51 |
|
| 52 |
python_exe = 'python' |
| 53 |
if sys.executable.endswith('.exe'): |
| 54 |
python_exe += '.exe' |
| 55 |
if support.MS_WINDOWS: |
| 56 |
python = os.path.join(venv_dir, 'Scripts', python_exe) |
| 57 |
else: |
| 58 |
python = os.path.join(venv_dir, 'bin', python_exe) |
| 59 |
|
| 60 |
def run_cmd(operation, cmd): |
| 61 |
if verbose: |
| 62 |
print('Run:', ' '.join(cmd)) |
| 63 |
subprocess.run(cmd, check=True) |
| 64 |
else: |
| 65 |
proc = subprocess.run(cmd, |
| 66 |
stdout=subprocess.PIPE, |
| 67 |
stderr=subprocess.STDOUT, |
| 68 |
text=True) |
| 69 |
if proc.returncode: |
| 70 |
print(proc.stdout, end='') |
| 71 |
self.fail( |
| 72 |
f"{operation} failed with exit code {proc.returncode}") |
| 73 |
|
| 74 |
|
| 75 |
cmd = [python, '-X', 'dev', |
| 76 |
SETUP, 'build_ext', '--verbose'] |
| 77 |
if std_cpp03: |
| 78 |
cmd.append('-std=c++03') |
| 79 |
run_cmd('Build', cmd) |
| 80 |
|
| 81 |
|
| 82 |
cmd = [python, '-X', 'dev', |
| 83 |
SETUP, 'install'] |
| 84 |
run_cmd('Install', cmd) |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
cmd = [python, |
| 90 |
'-X', 'dev', |
| 91 |
'-X', 'showrefcount', |
| 92 |
'-c', 'pass'] |
| 93 |
run_cmd('Reference run', cmd) |
| 94 |
|
| 95 |
|
| 96 |
cmd = [python, |
| 97 |
'-X', 'dev', |
| 98 |
'-X', 'showrefcount', |
| 99 |
'-c', f"import {extension_name}"] |
| 100 |
run_cmd('Import', cmd) |
| 101 |
|
| 102 |
|
| 103 |
if __name__ == "__main__": |
| 104 |
unittest.main() |