Skip to content

Commit 58916eb

Browse files
[3.12] gh-127906: Test the limited C API in test_cppext (GH-127916) (#127920)
gh-127906: Test the limited C API in test_cppext (GH-127916) (cherry picked from commit d05a4e6) Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 5a658d8 commit 58916eb

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

Lib/test/test_cppext/__init__.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,16 @@ def test_build_cpp11(self):
4141
def test_build_cpp14(self):
4242
self.check_build('_testcpp14ext', std='c++14')
4343

44-
def check_build(self, extension_name, std=None):
44+
def test_build_limited(self):
45+
self.check_build('_testcppext_limited', limited=True)
46+
47+
def check_build(self, extension_name, std=None, limited=False):
4548
venv_dir = 'env'
4649
with support.setup_venv_with_pip_setuptools_wheel(venv_dir) as python_exe:
47-
self._check_build(extension_name, python_exe, std=std)
50+
self._check_build(extension_name, python_exe,
51+
std=std, limited=limited)
4852

49-
def _check_build(self, extension_name, python_exe, std):
53+
def _check_build(self, extension_name, python_exe, std, limited):
5054
pkg_dir = 'pkg'
5155
os.mkdir(pkg_dir)
5256
shutil.copy(SETUP, os.path.join(pkg_dir, os.path.basename(SETUP)))
@@ -56,6 +60,8 @@ def run_cmd(operation, cmd):
5660
env = os.environ.copy()
5761
if std:
5862
env['CPYTHON_TEST_CPP_STD'] = std
63+
if limited:
64+
env['CPYTHON_TEST_LIMITED'] = '1'
5965
env['CPYTHON_TEST_EXT_NAME'] = extension_name
6066
if support.verbose:
6167
print('Run:', ' '.join(map(shlex.quote, cmd)))

Lib/test/test_cppext/extension.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ test_api_casts(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
6262
Py_ssize_t refcnt = Py_REFCNT(obj);
6363
assert(refcnt >= 1);
6464

65+
#ifndef Py_LIMITED_API
6566
// gh-92138: For backward compatibility, functions of Python C API accepts
6667
// "const PyObject*". Check that using it does not emit C++ compiler
6768
// warnings.
@@ -74,6 +75,7 @@ test_api_casts(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
7475
assert(PyTuple_GET_SIZE(const_obj) == 2);
7576
PyObject *one = PyTuple_GET_ITEM(const_obj, 0);
7677
assert(PyLong_AsLong(one) == 1);
78+
#endif
7779

7880
// gh-92898: StrongRef doesn't inherit from PyObject but has an operator to
7981
// cast to PyObject*.
@@ -106,6 +108,12 @@ test_unicode(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
106108
}
107109

108110
assert(PyUnicode_Check(str));
111+
112+
assert(PyUnicode_GetLength(str) == 3);
113+
assert(PyUnicode_ReadChar(str, 0) == 'a');
114+
assert(PyUnicode_ReadChar(str, 1) == 'b');
115+
116+
#ifndef Py_LIMITED_API
109117
assert(PyUnicode_GET_LENGTH(str) == 3);
110118

111119
// gh-92800: test PyUnicode_READ()
@@ -121,6 +129,7 @@ test_unicode(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
121129
assert(PyUnicode_READ(ukind, const_data, 2) == 'c');
122130

123131
assert(PyUnicode_READ_CHAR(str, 1) == 'b');
132+
#endif
124133

125134
Py_DECREF(str);
126135
Py_RETURN_NONE;

Lib/test/test_cppext/setup.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def main():
3333
cppflags = list(CPPFLAGS)
3434
std = os.environ.get("CPYTHON_TEST_CPP_STD", "")
3535
module_name = os.environ["CPYTHON_TEST_EXT_NAME"]
36+
limited = bool(os.environ.get("CPYTHON_TEST_LIMITED", ""))
3637

3738
cppflags = list(CPPFLAGS)
3839
cppflags.append(f'-DMODULE_NAME={module_name}')
@@ -59,6 +60,11 @@ def main():
5960
# CC env var overrides sysconfig CC variable in setuptools
6061
os.environ['CC'] = cmd
6162

63+
# Define Py_LIMITED_API macro
64+
if limited:
65+
version = sys.hexversion
66+
cppflags.append(f'-DPy_LIMITED_API={version:#x}')
67+
6268
# On Windows, add PCbuild\amd64\ to include and library directories
6369
include_dirs = []
6470
library_dirs = []
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Test the limited C API in test_cppext. Patch by Victor Stinner.

0 commit comments

Comments
 (0)