Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d05a4e6

Browse filesBrowse files
authored
gh-127906: Test the limited C API in test_cppext (#127916)
1 parent 6ff38fc commit d05a4e6
Copy full SHA for d05a4e6

File tree

Expand file treeCollapse file tree

4 files changed

+26
-3
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+26
-3
lines changed

‎Lib/test/test_cppext/__init__.py

Copy file name to clipboardExpand all lines: Lib/test/test_cppext/__init__.py
+10-3Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,17 @@ 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+
@support.requires_gil_enabled('incompatible with Free Threading')
45+
def test_build_limited(self):
46+
self.check_build('_testcppext_limited', limited=True)
47+
48+
def check_build(self, extension_name, std=None, limited=False):
4549
venv_dir = 'env'
4650
with support.setup_venv_with_pip_setuptools_wheel(venv_dir) as python_exe:
47-
self._check_build(extension_name, python_exe, std=std)
51+
self._check_build(extension_name, python_exe,
52+
std=std, limited=limited)
4853

49-
def _check_build(self, extension_name, python_exe, std):
54+
def _check_build(self, extension_name, python_exe, std, limited):
5055
pkg_dir = 'pkg'
5156
os.mkdir(pkg_dir)
5257
shutil.copy(SETUP, os.path.join(pkg_dir, os.path.basename(SETUP)))
@@ -56,6 +61,8 @@ def run_cmd(operation, cmd):
5661
env = os.environ.copy()
5762
if std:
5863
env['CPYTHON_TEST_CPP_STD'] = std
64+
if limited:
65+
env['CPYTHON_TEST_LIMITED'] = '1'
5966
env['CPYTHON_TEST_EXT_NAME'] = extension_name
6067
if support.verbose:
6168
print('Run:', ' '.join(map(shlex.quote, cmd)))

‎Lib/test/test_cppext/extension.cpp

Copy file name to clipboardExpand all lines: Lib/test/test_cppext/extension.cpp
+9Lines 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

Copy file name to clipboardExpand all lines: Lib/test/test_cppext/setup.py
+6Lines 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 = []
+1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.