From 10fe658f34a7b4d1bd07882fe5f76204d69977e9 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Tue, 8 Apr 2025 23:19:40 +0200 Subject: [PATCH] =?utf8?q?[3.13]=20gh-132250:=20Clear=20error=20in=20lspro?= =?utf8?q?f=20callback=20when=20method=20descriptor=20raises=20an=20excep?= =?utf8?q?=E2=80=A6=20(GH-132251)=20(#132281)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit gh-132250: Clear error in lsprof callback when method descriptor raises an excep… (GH-132251) (cherry picked from commit ab64130b572424695bf072f7608a536997dce14f) 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 65720871d5c5..b46edf66bf09 100644 --- a/Lib/test/test_cprofile.py +++ b/Lib/test/test_cprofile.py @@ -139,6 +139,14 @@ class CProfileTest(ProfileTest): 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 000000000000..b49528867c2e --- /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 084f8b162270..f23f254c0aa0 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)) { -- 2.47.3