mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	gh-127906: Test the limited C API in test_cppext (#127916)
This commit is contained in:
		
							parent
							
								
									6ff38fc4e2
								
							
						
					
					
						commit
						d05a4e6a0d
					
				
					 4 changed files with 26 additions and 3 deletions
				
			
		|  | @ -41,12 +41,17 @@ def test_build_cpp11(self): | ||||||
|     def test_build_cpp14(self): |     def test_build_cpp14(self): | ||||||
|         self.check_build('_testcpp14ext', std='c++14') |         self.check_build('_testcpp14ext', std='c++14') | ||||||
| 
 | 
 | ||||||
|     def check_build(self, extension_name, std=None): |     @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' |         venv_dir = 'env' | ||||||
|         with support.setup_venv_with_pip_setuptools_wheel(venv_dir) as python_exe: |         with support.setup_venv_with_pip_setuptools_wheel(venv_dir) as python_exe: | ||||||
|             self._check_build(extension_name, python_exe, std=std) |             self._check_build(extension_name, python_exe, | ||||||
|  |                               std=std, limited=limited) | ||||||
| 
 | 
 | ||||||
|     def _check_build(self, extension_name, python_exe, std): |     def _check_build(self, extension_name, python_exe, std, limited): | ||||||
|         pkg_dir = 'pkg' |         pkg_dir = 'pkg' | ||||||
|         os.mkdir(pkg_dir) |         os.mkdir(pkg_dir) | ||||||
|         shutil.copy(SETUP, os.path.join(pkg_dir, os.path.basename(SETUP))) |         shutil.copy(SETUP, os.path.join(pkg_dir, os.path.basename(SETUP))) | ||||||
|  | @ -56,6 +61,8 @@ def run_cmd(operation, cmd): | ||||||
|             env = os.environ.copy() |             env = os.environ.copy() | ||||||
|             if std: |             if std: | ||||||
|                 env['CPYTHON_TEST_CPP_STD'] = std |                 env['CPYTHON_TEST_CPP_STD'] = std | ||||||
|  |             if limited: | ||||||
|  |                 env['CPYTHON_TEST_LIMITED'] = '1' | ||||||
|             env['CPYTHON_TEST_EXT_NAME'] = extension_name |             env['CPYTHON_TEST_EXT_NAME'] = extension_name | ||||||
|             if support.verbose: |             if support.verbose: | ||||||
|                 print('Run:', ' '.join(map(shlex.quote, cmd))) |                 print('Run:', ' '.join(map(shlex.quote, cmd))) | ||||||
|  |  | ||||||
|  | @ -62,6 +62,7 @@ test_api_casts(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) | ||||||
|     Py_ssize_t refcnt = Py_REFCNT(obj); |     Py_ssize_t refcnt = Py_REFCNT(obj); | ||||||
|     assert(refcnt >= 1); |     assert(refcnt >= 1); | ||||||
| 
 | 
 | ||||||
|  | #ifndef Py_LIMITED_API | ||||||
|     // gh-92138: For backward compatibility, functions of Python C API accepts
 |     // gh-92138: For backward compatibility, functions of Python C API accepts
 | ||||||
|     // "const PyObject*". Check that using it does not emit C++ compiler
 |     // "const PyObject*". Check that using it does not emit C++ compiler
 | ||||||
|     // warnings.
 |     // warnings.
 | ||||||
|  | @ -74,6 +75,7 @@ test_api_casts(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) | ||||||
|     assert(PyTuple_GET_SIZE(const_obj) == 2); |     assert(PyTuple_GET_SIZE(const_obj) == 2); | ||||||
|     PyObject *one = PyTuple_GET_ITEM(const_obj, 0); |     PyObject *one = PyTuple_GET_ITEM(const_obj, 0); | ||||||
|     assert(PyLong_AsLong(one) == 1); |     assert(PyLong_AsLong(one) == 1); | ||||||
|  | #endif | ||||||
| 
 | 
 | ||||||
|     // gh-92898: StrongRef doesn't inherit from PyObject but has an operator to
 |     // gh-92898: StrongRef doesn't inherit from PyObject but has an operator to
 | ||||||
|     // cast to PyObject*.
 |     // cast to PyObject*.
 | ||||||
|  | @ -106,6 +108,12 @@ test_unicode(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     assert(PyUnicode_Check(str)); |     assert(PyUnicode_Check(str)); | ||||||
|  | 
 | ||||||
|  |     assert(PyUnicode_GetLength(str) == 3); | ||||||
|  |     assert(PyUnicode_ReadChar(str, 0) == 'a'); | ||||||
|  |     assert(PyUnicode_ReadChar(str, 1) == 'b'); | ||||||
|  | 
 | ||||||
|  | #ifndef Py_LIMITED_API | ||||||
|     assert(PyUnicode_GET_LENGTH(str) == 3); |     assert(PyUnicode_GET_LENGTH(str) == 3); | ||||||
| 
 | 
 | ||||||
|     // gh-92800: test PyUnicode_READ()
 |     // gh-92800: test PyUnicode_READ()
 | ||||||
|  | @ -121,6 +129,7 @@ test_unicode(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) | ||||||
|     assert(PyUnicode_READ(ukind, const_data, 2) == 'c'); |     assert(PyUnicode_READ(ukind, const_data, 2) == 'c'); | ||||||
| 
 | 
 | ||||||
|     assert(PyUnicode_READ_CHAR(str, 1) == 'b'); |     assert(PyUnicode_READ_CHAR(str, 1) == 'b'); | ||||||
|  | #endif | ||||||
| 
 | 
 | ||||||
|     Py_DECREF(str); |     Py_DECREF(str); | ||||||
|     Py_RETURN_NONE; |     Py_RETURN_NONE; | ||||||
|  |  | ||||||
|  | @ -33,6 +33,7 @@ def main(): | ||||||
|     cppflags = list(CPPFLAGS) |     cppflags = list(CPPFLAGS) | ||||||
|     std = os.environ.get("CPYTHON_TEST_CPP_STD", "") |     std = os.environ.get("CPYTHON_TEST_CPP_STD", "") | ||||||
|     module_name = os.environ["CPYTHON_TEST_EXT_NAME"] |     module_name = os.environ["CPYTHON_TEST_EXT_NAME"] | ||||||
|  |     limited = bool(os.environ.get("CPYTHON_TEST_LIMITED", "")) | ||||||
| 
 | 
 | ||||||
|     cppflags = list(CPPFLAGS) |     cppflags = list(CPPFLAGS) | ||||||
|     cppflags.append(f'-DMODULE_NAME={module_name}') |     cppflags.append(f'-DMODULE_NAME={module_name}') | ||||||
|  | @ -59,6 +60,11 @@ def main(): | ||||||
|         # CC env var overrides sysconfig CC variable in setuptools |         # CC env var overrides sysconfig CC variable in setuptools | ||||||
|         os.environ['CC'] = cmd |         os.environ['CC'] = cmd | ||||||
| 
 | 
 | ||||||
|  |     # Define Py_LIMITED_API macro | ||||||
|  |     if limited: | ||||||
|  |         version = sys.hexversion | ||||||
|  |         cppflags.append(f'-DPy_LIMITED_API={version:#x}') | ||||||
|  | 
 | ||||||
|     # On Windows, add PCbuild\amd64\ to include and library directories |     # On Windows, add PCbuild\amd64\ to include and library directories | ||||||
|     include_dirs = [] |     include_dirs = [] | ||||||
|     library_dirs = [] |     library_dirs = [] | ||||||
|  |  | ||||||
|  | @ -0,0 +1 @@ | ||||||
|  | Test the limited C API in test_cppext. Patch by Victor Stinner. | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Victor Stinner
						Victor Stinner