From 036fc0f08a532e4d332bc541d00210fceb05b7f1 Mon Sep 17 00:00:00 2001 From: AN Long Date: Fri, 3 Nov 2023 01:06:20 +0800 Subject: [PATCH] remove redundant decref on the eval stack value in LOAD_FROM_DICT_OR_DEREF in error path --- Lib/test/test_exceptions.py | 7 +++++++ .../2023-11-03-01-04-55.gh-issue-111654.scUhDO.rst | 2 ++ Python/bytecodes.c | 3 +-- Python/executor_cases.c.h | 3 +-- Python/generated_cases.c.h | 3 +-- 5 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-11-03-01-04-55.gh-issue-111654.scUhDO.rst diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index 7f1d5ee9322b45..8ccf08703e5389 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -1844,6 +1844,13 @@ def f(): self.assertIn("nonsense", err.getvalue()) self.assertIn("ZeroDivisionError", err.getvalue()) + def test_gh_111654(self): + def f(): + class TestClass: + TestClass + + self.assertRaises(NameError, f) + # Note: name suggestion tests live in `test_traceback`. diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-11-03-01-04-55.gh-issue-111654.scUhDO.rst b/Misc/NEWS.d/next/Core and Builtins/2023-11-03-01-04-55.gh-issue-111654.scUhDO.rst new file mode 100644 index 00000000000000..e9a896e660916f --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-11-03-01-04-55.gh-issue-111654.scUhDO.rst @@ -0,0 +1,2 @@ +Fix runtime crash when some error happens in opcode +``LOAD_FROM_DICT_OR_DEREF``. diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 9aca82a80ce839..1af6a8c23489d9 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1537,10 +1537,8 @@ dummy_func( assert(oparg >= 0 && oparg < _PyFrame_GetCode(frame)->co_nlocalsplus); name = PyTuple_GET_ITEM(_PyFrame_GetCode(frame)->co_localsplusnames, oparg); if (PyMapping_GetOptionalItem(class_dict, name, &value) < 0) { - Py_DECREF(class_dict); GOTO_ERROR(error); } - Py_DECREF(class_dict); if (!value) { PyObject *cell = GETLOCAL(oparg); value = PyCell_GET(cell); @@ -1550,6 +1548,7 @@ dummy_func( } Py_INCREF(value); } + Py_DECREF(class_dict); } inst(LOAD_DEREF, ( -- value)) { diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index 903fc40595ceb7..eb56c34b432783 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -1241,10 +1241,8 @@ assert(oparg >= 0 && oparg < _PyFrame_GetCode(frame)->co_nlocalsplus); name = PyTuple_GET_ITEM(_PyFrame_GetCode(frame)->co_localsplusnames, oparg); if (PyMapping_GetOptionalItem(class_dict, name, &value) < 0) { - Py_DECREF(class_dict); GOTO_ERROR(error); } - Py_DECREF(class_dict); if (!value) { PyObject *cell = GETLOCAL(oparg); value = PyCell_GET(cell); @@ -1254,6 +1252,7 @@ } Py_INCREF(value); } + Py_DECREF(class_dict); stack_pointer[-1] = value; break; } diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index dc62d797ae7c3b..db1839bfab04fb 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -2206,10 +2206,8 @@ assert(oparg >= 0 && oparg < _PyFrame_GetCode(frame)->co_nlocalsplus); name = PyTuple_GET_ITEM(_PyFrame_GetCode(frame)->co_localsplusnames, oparg); if (PyMapping_GetOptionalItem(class_dict, name, &value) < 0) { - Py_DECREF(class_dict); GOTO_ERROR(error); } - Py_DECREF(class_dict); if (!value) { PyObject *cell = GETLOCAL(oparg); value = PyCell_GET(cell); @@ -2219,6 +2217,7 @@ } Py_INCREF(value); } + Py_DECREF(class_dict); stack_pointer[-1] = value; DISPATCH(); }