gh-91321: Fix compatibility with C++ older than C++11 (#93784)

Fix the compatibility of the Python C API with C++ older than C++11.

_Py_NULL is only defined as nullptr on C++11 and newer.
This commit is contained in:
Victor Stinner 2022-06-14 11:43:08 +02:00 committed by GitHub
parent 3597c12941
commit 4caf5c2753
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 36 deletions

View file

@ -16,22 +16,29 @@ SETUP_TESTCPPEXT = support.findfile('setup_testcppext.py')
@support.requires_subprocess()
class TestCPPExt(unittest.TestCase):
def test_build_cpp11(self):
self.check_build(False)
def test_build_cpp03(self):
self.check_build(True)
# With MSVC, the linker fails with: cannot open file 'python311.lib'
# https://github.com/python/cpython/pull/32175#issuecomment-1111175897
@unittest.skipIf(MS_WINDOWS, 'test fails on Windows')
# the test uses venv+pip: skip if it's not available
@support.requires_venv_with_pip()
def test_build(self):
def check_build(self, std_cpp03):
# Build in a temporary directory
with os_helper.temp_cwd():
self._test_build()
self._check_build(std_cpp03)
def _test_build(self):
def _check_build(self, std_cpp03):
venv_dir = 'env'
verbose = support.verbose
# Create virtual environment to get setuptools
cmd = [sys.executable, '-X', 'dev', '-m', 'venv', venv_dir]
if support.verbose:
if verbose:
print()
print('Run:', ' '.join(cmd))
subprocess.run(cmd, check=True)
@ -46,16 +53,21 @@ class TestCPPExt(unittest.TestCase):
python = os.path.join(venv_dir, 'bin', python_exe)
# Build the C++ extension
cmd = [python, '-X', 'dev', SETUP_TESTCPPEXT, 'build_ext', '--verbose']
if support.verbose:
cmd = [python, '-X', 'dev',
SETUP_TESTCPPEXT, 'build_ext', '--verbose']
if std_cpp03:
cmd.append('-std=c++03')
if verbose:
print('Run:', ' '.join(cmd))
proc = subprocess.run(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True)
if proc.returncode:
print(proc.stdout, end='')
self.fail(f"Build failed with exit code {proc.returncode}")
subprocess.run(cmd, check=True)
else:
proc = subprocess.run(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True)
if proc.returncode:
print(proc.stdout, end='')
self.fail(f"Build failed with exit code {proc.returncode}")
if __name__ == "__main__":