Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 34 additions & 25 deletions Lib/test/test_cppext/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,12 @@
@support.requires_venv_with_pip()
@support.requires_subprocess()
@support.requires_resource('cpu')
class TestCPPExt(unittest.TestCase):
class BaseTests:
TEST_INTERNAL_C_API = False

def test_build(self):
self.check_build('_testcppext')

def test_build_cpp03(self):
# In public docs, we say C API is compatible with C++11. However,
# in practice we do maintain C++03 compatibility in public headers.
# Please ask the C API WG before adding a new C++11-only feature.
self.check_build('_testcpp03ext', std='c++03')

@support.requires_gil_enabled('incompatible with Free Threading')
def test_build_limited_cpp03(self):
self.check_build('_test_limited_cpp03ext', std='c++03', limited=True)

@unittest.skipIf(support.MS_WINDOWS, "MSVC doesn't support /std:c++11")
def test_build_cpp11(self):
self.check_build('_testcpp11ext', std='c++11')

# Only test C++14 on MSVC.
# On s390x RHEL7, GCC 4.8.5 doesn't support C++14.
@unittest.skipIf(not support.MS_WINDOWS, "need Windows")
def test_build_cpp14(self):
self.check_build('_testcpp14ext', std='c++14')

@support.requires_gil_enabled('incompatible with Free Threading')
def test_build_limited(self):
self.check_build('_testcppext_limited', limited=True)

def check_build(self, extension_name, std=None, limited=False):
venv_dir = 'env'
with support.setup_venv_with_pip_setuptools(venv_dir) as python_exe:
Expand All @@ -71,6 +49,7 @@ def run_cmd(operation, cmd):
if limited:
env['CPYTHON_TEST_LIMITED'] = '1'
env['CPYTHON_TEST_EXT_NAME'] = extension_name
env['TEST_INTERNAL_C_API'] = str(int(self.TEST_INTERNAL_C_API))
if support.verbose:
print('Run:', ' '.join(map(shlex.quote, cmd)))
subprocess.run(cmd, check=True, env=env)
Expand Down Expand Up @@ -111,5 +90,35 @@ def run_cmd(operation, cmd):
run_cmd('Import', cmd)


class TestPublicCAPI(BaseTests, unittest.TestCase):
@support.requires_gil_enabled('incompatible with Free Threading')
def test_build_limited_cpp03(self):
self.check_build('_test_limited_cpp03ext', std='c++03', limited=True)

@support.requires_gil_enabled('incompatible with Free Threading')
def test_build_limited(self):
self.check_build('_testcppext_limited', limited=True)

def test_build_cpp03(self):
# In public docs, we say C API is compatible with C++11. However,
# in practice we do maintain C++03 compatibility in public headers.
# Please ask the C API WG before adding a new C++11-only feature.
self.check_build('_testcpp03ext', std='c++03')

@unittest.skipIf(support.MS_WINDOWS, "MSVC doesn't support /std:c++11")
def test_build_cpp11(self):
self.check_build('_testcpp11ext', std='c++11')

# Only test C++14 on MSVC.
# On s390x RHEL7, GCC 4.8.5 doesn't support C++14.
@unittest.skipIf(not support.MS_WINDOWS, "need Windows")
def test_build_cpp14(self):
self.check_build('_testcpp14ext', std='c++14')


class TestInteralCAPI(BaseTests, unittest.TestCase):
TEST_INTERNAL_C_API = True


if __name__ == "__main__":
unittest.main()
17 changes: 17 additions & 0 deletions Lib/test/test_cppext/extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,25 @@
// Always enable assertions
#undef NDEBUG

#ifdef TEST_INTERNAL_C_API
# define Py_BUILD_CORE_MODULE 1
#endif

#include "Python.h"

#ifdef TEST_INTERNAL_C_API
// gh-135906: Check for compiler warnings in the internal C API
# include "internal/pycore_frame.h"
// mimalloc emits many compiler warnings when Python is built in debug
// mode (when MI_DEBUG is not zero)
// mimalloc emits compiler warnings when Python is built on Windows
// in free-threaded mode.
# if !defined(Py_DEBUG) && !(defined(MS_WINDOWS) && defined(Py_GIL_DISABLED))
# include "internal/pycore_backoff.h"
# include "internal/pycore_cell.h"
# endif
#endif

#ifndef MODULE_NAME
# error "MODULE_NAME macro must be defined"
#endif
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_cppext/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def main():
std = os.environ.get("CPYTHON_TEST_CPP_STD", "")
module_name = os.environ["CPYTHON_TEST_EXT_NAME"]
limited = bool(os.environ.get("CPYTHON_TEST_LIMITED", ""))
internal = bool(int(os.environ.get("TEST_INTERNAL_C_API", "0")))

cppflags = list(CPPFLAGS)
cppflags.append(f'-DMODULE_NAME={module_name}')
Expand Down Expand Up @@ -82,6 +83,9 @@ def main():
version = sys.hexversion
cppflags.append(f'-DPy_LIMITED_API={version:#x}')

if internal:
cppflags.append('-DTEST_INTERNAL_C_API=1')

# On Windows, add PCbuild\amd64\ to include and library directories
include_dirs = []
library_dirs = []
Expand Down
Loading