Fix SystemError in PyEval_GetLocals()

- Fixes: rhbz#2303107
This commit is contained in:
Miro Hrončok 2024-08-06 23:21:44 +02:00
commit 396df33f84
2 changed files with 105 additions and 1 deletions

View file

@ -0,0 +1,90 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Victor Stinner <vstinner@python.org>
Date: Tue, 6 Aug 2024 23:01:44 +0200
Subject: [PATCH] 00434: gh-122728: Fix SystemError in PyEval_GetLocals()
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().
(cherry picked from commit 4767a6e31c0550836b2af45d27e374e721f0c4e6)
---
Lib/test/test_capi/test_misc.py | 13 +++++++++++++
.../2024-08-06-14-23-11.gh-issue-122728.l-fQ-v.rst | 2 ++
Modules/_testcapimodule.c | 7 +++++++
Python/ceval.c | 2 +-
4 files changed, 23 insertions(+), 1 deletion(-)
create mode 100644 Misc/NEWS.d/next/C_API/2024-08-06-14-23-11.gh-issue-122728.l-fQ-v.rst
diff --git a/Lib/test/test_capi/test_misc.py b/Lib/test/test_capi/test_misc.py
index f3d16e4a2f..080b3e6533 100644
--- a/Lib/test/test_capi/test_misc.py
+++ b/Lib/test/test_capi/test_misc.py
@@ -1180,6 +1180,19 @@ def genf(): yield
gen = genf()
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
class TestHeapTypeRelative(unittest.TestCase):
diff --git a/Misc/NEWS.d/next/C_API/2024-08-06-14-23-11.gh-issue-122728.l-fQ-v.rst b/Misc/NEWS.d/next/C_API/2024-08-06-14-23-11.gh-issue-122728.l-fQ-v.rst
new file mode 100644
index 0000000000..a128d6aef3
--- /dev/null
+++ b/Misc/NEWS.d/next/C_API/2024-08-06-14-23-11.gh-issue-122728.l-fQ-v.rst
@@ -0,0 +1,2 @@
+Fix :c:func:`PyEval_GetLocals` to avoid :exc:`SystemError` ("bad argument to
+internal function"). Patch by Victor Stinner.
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index 1fa7c37841..01b6bd89d1 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -3332,6 +3332,12 @@ test_critical_sections(PyObject *module, PyObject *Py_UNUSED(args))
Py_RETURN_NONE;
}
+static PyObject *
+pyeval_getlocals(PyObject *module, PyObject *Py_UNUSED(args))
+{
+ return Py_XNewRef(PyEval_GetLocals());
+}
+
static PyMethodDef TestMethods[] = {
{"set_errno", set_errno, METH_VARARGS},
{"test_config", test_config, METH_NOARGS},
@@ -3476,6 +3482,7 @@ static PyMethodDef TestMethods[] = {
{"test_weakref_capi", test_weakref_capi, METH_NOARGS},
{"function_set_warning", function_set_warning, METH_NOARGS},
{"test_critical_sections", test_critical_sections, METH_NOARGS},
+ {"pyeval_getlocals", pyeval_getlocals, METH_NOARGS},
{NULL, NULL} /* sentinel */
};
diff --git a/Python/ceval.c b/Python/ceval.c
index 866328e85b..351ddd2666 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -2482,7 +2482,7 @@ PyEval_GetLocals(void)
PyFrameObject *f = _PyFrame_GetFrameObject(current_frame);
PyObject *ret = f->f_locals_cache;
if (ret == NULL) {
- PyObject *ret = PyDict_New();
+ ret = PyDict_New();
if (ret == NULL) {
Py_DECREF(locals);
return NULL;

View file

@ -17,7 +17,7 @@ URL: https://www.python.org/
%global prerel rc1
%global upstream_version %{general_version}%{?prerel}
Version: %{general_version}%{?prerel:~%{prerel}}
Release: 1%{?dist}
Release: 2%{?dist}
License: Python-2.0.1
@ -397,6 +397,16 @@ Patch251: 00251-change-user-install-location.patch
# Another process might have already moved jit_stencils.h.new
Patch429: 00429-gh-118943-fix-another-race-condition-when-generating-jit_stencils-h.patch
# 00434 # 62b8e745105f68cec7016eccec1406911da01d66
# gh-122728: Fix SystemError in PyEval_GetLocals()
#
# 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().
Patch434: 00434-gh-122728-fix-systemerror-in-pyeval_getlocals.patch
# (New patches go here ^^^)
#
# When adding new patches to "python" and "python3" in Fedora, EL, etc.,
@ -1738,6 +1748,10 @@ CheckPython freethreading
# ======================================================
%changelog
* Tue Aug 06 2024 Miro Hrončok <mhroncok@redhat.com> - 3.13.0~rc1-2
- Fix SystemError in PyEval_GetLocals()
- Fixes: rhbz#2303107
* Thu Aug 01 2024 Karolina Surma <ksurma@redhat.com> - 3.13.0~rc1-1
- Update to Python 3.13.0rc1