From aaa08caca953bdc18d08e8a004596dedd720ca90 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Tue, 8 Apr 2025 10:36:47 -0700 Subject: [PATCH] =?UTF-8?q?gh-132250:=20Clear=20error=20in=20lsprof=20call?= =?UTF-8?q?back=20when=20method=20descriptor=20raises=20an=20excep?= =?UTF-8?q?=E2=80=A6=20(GH-132251)=20(cherry=20picked=20from=20commit=20ab?= =?UTF-8?q?64130b572424695bf072f7608a536997dce14f)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tian Gao --- Lib/test/test_cprofile.py | 8 ++++++++ .../2025-04-08-01-55-11.gh-issue-132250.APBFCw.rst | 1 + Modules/_lsprof.c | 1 + 3 files changed, 10 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-04-08-01-55-11.gh-issue-132250.APBFCw.rst diff --git a/Lib/test/test_cprofile.py b/Lib/test/test_cprofile.py index 65720871d5c5f0..b46edf66bf09f8 100644 --- a/Lib/test/test_cprofile.py +++ b/Lib/test/test_cprofile.py @@ -139,6 +139,14 @@ def test_throw(self): self.assertEqual(cc, 1) 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. + with self.profilerclass() as prof: + with self.assertRaises(TypeError): + bytes.find(str()) + class TestCommandLine(unittest.TestCase): def test_sort(self): 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..b49528867c2ef3 --- /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. diff --git a/Modules/_lsprof.c b/Modules/_lsprof.c index 084f8b162270b8..f23f254c0aa0db 100644 --- a/Modules/_lsprof.c +++ b/Modules/_lsprof.c @@ -652,6 +652,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)) {