mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	Issue #23571: _Py_CheckFunctionResult() now gives the name of the function
which returned an invalid result (result+error or no result without error) in the exception message. Add also unit test to check that the exception contains the name of the function. Special case: the final _PyEval_EvalFrameEx() check doesn't mention the function since it didn't execute a single function but a whole frame.
This commit is contained in:
		
							parent
							
								
									6921c13bbb
								
							
						
					
					
						commit
						efde146b0c
					
				
					 6 changed files with 93 additions and 12 deletions
				
			
		|  | @ -6,10 +6,12 @@ | |||
| import random | ||||
| import subprocess | ||||
| import sys | ||||
| import textwrap | ||||
| import time | ||||
| import unittest | ||||
| from test import support | ||||
| from test.support import MISSING_C_DOCSTRINGS | ||||
| from test.script_helper import assert_python_failure | ||||
| try: | ||||
|     import _posixsubprocess | ||||
| except ImportError: | ||||
|  | @ -21,6 +23,9 @@ | |||
| # Skip this test if the _testcapi module isn't available. | ||||
| _testcapi = support.import_module('_testcapi') | ||||
| 
 | ||||
| # Were we compiled --with-pydebug or with #define Py_DEBUG? | ||||
| Py_DEBUG = hasattr(sys, 'gettotalrefcount') | ||||
| 
 | ||||
| 
 | ||||
| def testfunction(self): | ||||
|     """some doc""" | ||||
|  | @ -167,6 +172,45 @@ def test_c_type_with_matrix_multiplication(self): | |||
|         o @= m1 | ||||
|         self.assertEqual(o, ("matmul", 42, m1)) | ||||
| 
 | ||||
|     def test_return_null_without_error(self): | ||||
|         # Issue #23571: A function must not return NULL without setting an | ||||
|         # error | ||||
|         if Py_DEBUG: | ||||
|             code = textwrap.dedent(""" | ||||
|                 import _testcapi | ||||
|                 from test import support | ||||
| 
 | ||||
|                 with support.SuppressCrashReport(): | ||||
|                     _testcapi.return_null_without_error() | ||||
|             """) | ||||
|             rc, out, err = assert_python_failure('-c', code) | ||||
|             self.assertIn(b'_Py_CheckFunctionResult: Assertion', err) | ||||
|         else: | ||||
|             with self.assertRaises(SystemError) as cm: | ||||
|                 _testcapi.return_null_without_error() | ||||
|             self.assertRegex(str(cm.exception), | ||||
|                              'return_null_without_error.* ' | ||||
|                              'returned NULL without setting an error') | ||||
| 
 | ||||
|     def test_return_result_with_error(self): | ||||
|         # Issue #23571: A function must not return a result with an error set | ||||
|         if Py_DEBUG: | ||||
|             code = textwrap.dedent(""" | ||||
|                 import _testcapi | ||||
|                 from test import support | ||||
| 
 | ||||
|                 with support.SuppressCrashReport(): | ||||
|                     _testcapi.return_result_with_error() | ||||
|             """) | ||||
|             rc, out, err = assert_python_failure('-c', code) | ||||
|             self.assertIn(b'_Py_CheckFunctionResult: Assertion', err) | ||||
|         else: | ||||
|             with self.assertRaises(SystemError) as cm: | ||||
|                 _testcapi.return_result_with_error() | ||||
|             self.assertRegex(str(cm.exception), | ||||
|                              'return_result_with_error.* ' | ||||
|                              'returned a result with an error set') | ||||
| 
 | ||||
| 
 | ||||
| @unittest.skipUnless(threading, 'Threading required for this test.') | ||||
| class TestPendingCalls(unittest.TestCase): | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Victor Stinner
						Victor Stinner