From 8f706d1cdb38637e84c7ba12e87e90e91be02011 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Mon, 7 Apr 2025 21:46:34 -0400 Subject: [PATCH 1/5] Clear error in lsprof callback when method descriptor raises an exception --- Lib/test/test_cprofile.py | 10 ++++++++++ Modules/_lsprof.c | 1 + 2 files changed, 11 insertions(+) diff --git a/Lib/test/test_cprofile.py b/Lib/test/test_cprofile.py index 65720871d5c5f0..d4ce8c5caf9c51 100644 --- a/Lib/test/test_cprofile.py +++ b/Lib/test/test_cprofile.py @@ -139,6 +139,16 @@ def test_throw(self): self.assertEqual(cc, 1) self.assertEqual(nc, 1) + def test_bad_descriptor(self): + """ + gh-132250 + cProfile should not crash when profiling a function with a descriptor + that raises an exception. + """ + with self.profilerclass() as prof: + with self.assertRaises(TypeError): + bytes.find(str()) + class TestCommandLine(unittest.TestCase): def test_sort(self): diff --git a/Modules/_lsprof.c b/Modules/_lsprof.c index 379f9869f2fe09..626c176715bdac 100644 --- a/Modules/_lsprof.c +++ b/Modules/_lsprof.c @@ -671,6 +671,7 @@ PyObject* get_cfunc_from_callable(PyObject* callable, PyObject* self_arg, PyObje PyObject *meth = Py_TYPE(callable)->tp_descr_get( callable, self_arg, (PyObject*)Py_TYPE(self_arg)); if (meth == NULL) { + PyErr_Clear(); return NULL; } if (PyCFunction_Check(meth)) { From 36d5bff770c35cb245dfb013678da4d09ba13088 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Mon, 7 Apr 2025 21:52:44 -0400 Subject: [PATCH 2/5] Update comments --- Lib/test/test_cprofile.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_cprofile.py b/Lib/test/test_cprofile.py index d4ce8c5caf9c51..9c96c652b68c51 100644 --- a/Lib/test/test_cprofile.py +++ b/Lib/test/test_cprofile.py @@ -142,8 +142,8 @@ def test_throw(self): def test_bad_descriptor(self): """ gh-132250 - cProfile should not crash when profiling a function with a descriptor - that raises an exception. + cProfile should not crash when the profiler callback fails to locate + the actual function of a method. """ with self.profilerclass() as prof: with self.assertRaises(TypeError): From d23b23f43b4179569c190e6e07ca6be0c5fb4972 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 8 Apr 2025 01:55:14 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2025-04-08-01-55-11.gh-issue-132250.APBFCw.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-04-08-01-55-11.gh-issue-132250.APBFCw.rst diff --git a/Misc/NEWS.d/next/Library/2025-04-08-01-55-11.gh-issue-132250.APBFCw.rst b/Misc/NEWS.d/next/Library/2025-04-08-01-55-11.gh-issue-132250.APBFCw.rst new file mode 100644 index 00000000000000..1c869c1f55df46 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-04-08-01-55-11.gh-issue-132250.APBFCw.rst @@ -0,0 +1 @@ +Fixed the :exc:`SystemError` in :mod:`cProfile` when locating the actual c function of a method raises an exception. From 07c0a060c04e0adcf4723ab69327d004b808c7e1 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Tue, 8 Apr 2025 11:09:31 -0400 Subject: [PATCH 4/5] Update Misc/NEWS.d/next/Library/2025-04-08-01-55-11.gh-issue-132250.APBFCw.rst Co-authored-by: sobolevn --- .../next/Library/2025-04-08-01-55-11.gh-issue-132250.APBFCw.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-04-08-01-55-11.gh-issue-132250.APBFCw.rst b/Misc/NEWS.d/next/Library/2025-04-08-01-55-11.gh-issue-132250.APBFCw.rst index 1c869c1f55df46..b49528867c2ef3 100644 --- a/Misc/NEWS.d/next/Library/2025-04-08-01-55-11.gh-issue-132250.APBFCw.rst +++ b/Misc/NEWS.d/next/Library/2025-04-08-01-55-11.gh-issue-132250.APBFCw.rst @@ -1 +1 @@ -Fixed the :exc:`SystemError` in :mod:`cProfile` when locating the actual c function of a method raises an exception. +Fixed the :exc:`SystemError` in :mod:`cProfile` when locating the actual C function of a method raises an exception. From a22b4a426e6da936cce1554e901c63f0f2e2dc66 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Tue, 8 Apr 2025 12:08:30 -0400 Subject: [PATCH 5/5] Use comments instead of docstring --- Lib/test/test_cprofile.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_cprofile.py b/Lib/test/test_cprofile.py index 9c96c652b68c51..b46edf66bf09f8 100644 --- a/Lib/test/test_cprofile.py +++ b/Lib/test/test_cprofile.py @@ -140,11 +140,9 @@ def test_throw(self): self.assertEqual(nc, 1) def test_bad_descriptor(self): - """ - gh-132250 - cProfile should not crash when the profiler callback fails to locate - the actual function of a method. - """ + # gh-132250 + # cProfile should not crash when the profiler callback fails to locate + # the actual function of a method. with self.profilerclass() as prof: with self.assertRaises(TypeError): bytes.find(str())