mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 21:51:50 +00:00 
			
		
		
		
	gh-122728: Fix SystemError in PyEval_GetLocals() (#122735)
Fix PyEval_GetLocals() to avoid SystemError ("bad argument to
internal function"). Don't redefine the 'ret' variable in the if
block.
Add an unit test on PyEval_GetLocals().
			
			
This commit is contained in:
		
							parent
							
								
									5b8a6c5186
								
							
						
					
					
						commit
						4767a6e31c
					
				
					 4 changed files with 23 additions and 1 deletions
				
			
		|  | @ -1157,6 +1157,19 @@ def genf(): yield | ||||||
|         gen = genf() |         gen = genf() | ||||||
|         self.assertEqual(_testcapi.gen_get_code(gen), gen.gi_code) |         self.assertEqual(_testcapi.gen_get_code(gen), gen.gi_code) | ||||||
| 
 | 
 | ||||||
|  |     def test_pyeval_getlocals(self): | ||||||
|  |         # Test PyEval_GetLocals() | ||||||
|  |         x = 1 | ||||||
|  |         self.assertEqual(_testcapi.pyeval_getlocals(), | ||||||
|  |             {'self': self, | ||||||
|  |              'x': 1}) | ||||||
|  | 
 | ||||||
|  |         y = 2 | ||||||
|  |         self.assertEqual(_testcapi.pyeval_getlocals(), | ||||||
|  |             {'self': self, | ||||||
|  |              'x': 1, | ||||||
|  |              'y': 2}) | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| @requires_limited_api | @requires_limited_api | ||||||
| class TestHeapTypeRelative(unittest.TestCase): | class TestHeapTypeRelative(unittest.TestCase): | ||||||
|  |  | ||||||
|  | @ -0,0 +1,2 @@ | ||||||
|  | Fix :c:func:`PyEval_GetLocals` to avoid :exc:`SystemError` ("bad argument to | ||||||
|  | internal function"). Patch by Victor Stinner. | ||||||
|  | @ -3341,6 +3341,12 @@ test_critical_sections(PyObject *module, PyObject *Py_UNUSED(args)) | ||||||
|     Py_RETURN_NONE; |     Py_RETURN_NONE; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | static PyObject * | ||||||
|  | pyeval_getlocals(PyObject *module, PyObject *Py_UNUSED(args)) | ||||||
|  | { | ||||||
|  |     return Py_XNewRef(PyEval_GetLocals()); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| static PyMethodDef TestMethods[] = { | static PyMethodDef TestMethods[] = { | ||||||
|     {"set_errno",               set_errno,                       METH_VARARGS}, |     {"set_errno",               set_errno,                       METH_VARARGS}, | ||||||
|     {"test_config",             test_config,                     METH_NOARGS}, |     {"test_config",             test_config,                     METH_NOARGS}, | ||||||
|  | @ -3483,6 +3489,7 @@ static PyMethodDef TestMethods[] = { | ||||||
|     {"test_weakref_capi", test_weakref_capi, METH_NOARGS}, |     {"test_weakref_capi", test_weakref_capi, METH_NOARGS}, | ||||||
|     {"function_set_warning", function_set_warning, METH_NOARGS}, |     {"function_set_warning", function_set_warning, METH_NOARGS}, | ||||||
|     {"test_critical_sections", test_critical_sections, METH_NOARGS}, |     {"test_critical_sections", test_critical_sections, METH_NOARGS}, | ||||||
|  |     {"pyeval_getlocals", pyeval_getlocals, METH_NOARGS}, | ||||||
|     {NULL, NULL} /* sentinel */ |     {NULL, NULL} /* sentinel */ | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -2499,7 +2499,7 @@ PyEval_GetLocals(void) | ||||||
|         PyFrameObject *f = _PyFrame_GetFrameObject(current_frame); |         PyFrameObject *f = _PyFrame_GetFrameObject(current_frame); | ||||||
|         PyObject *ret = f->f_locals_cache; |         PyObject *ret = f->f_locals_cache; | ||||||
|         if (ret == NULL) { |         if (ret == NULL) { | ||||||
|             PyObject *ret = PyDict_New(); |             ret = PyDict_New(); | ||||||
|             if (ret == NULL) { |             if (ret == NULL) { | ||||||
|                 Py_DECREF(locals); |                 Py_DECREF(locals); | ||||||
|                 return NULL; |                 return NULL; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Victor Stinner
						Victor Stinner