From ab64130b572424695bf072f7608a536997dce14f Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Tue, 8 Apr 2025 10:36:47 -0700 Subject: =?UTF-8?q?gh-132250:=20Clear=20error=20in=20lsprof=20callback=20w?= =?UTF-8?q?hen=20method=20descriptor=20raises=20an=20excep=E2=80=A6=20(#13?= =?UTF-8?q?2251)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lib/test/test_cprofile.py | 8 ++++++++ .../next/Library/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 6572087..b46edf6 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 0000000..b495288 --- /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 379f986..626c176 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)) { -- cgit v0.12