]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-132250: Clear error in lsprof callback when method descriptor raises an...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 8 Apr 2025 21:19:40 +0000 (23:19 +0200)
committerGitHub <noreply@github.com>
Tue, 8 Apr 2025 21:19:40 +0000 (17:19 -0400)
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 <gaogaotiantian@hotmail.com>
Lib/test/test_cprofile.py
Misc/NEWS.d/next/Library/2025-04-08-01-55-11.gh-issue-132250.APBFCw.rst [new file with mode: 0644]
Modules/_lsprof.c

index 65720871d5c5f0d4a4dbea66b8c78a462d433357..b46edf66bf09f8ae4d71a26112cf17cf1d66f3dc 100644 (file)
@@ -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 (file)
index 0000000..b495288
--- /dev/null
@@ -0,0 +1 @@
+Fixed the :exc:`SystemError` in :mod:`cProfile` when locating the actual C function of a method raises an exception.
index 084f8b162270b8d696d914c48a192ea0cf443b3e..f23f254c0aa0db3da17e605f0968c6b3cceda85b 100644 (file)
@@ -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)) {