]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-132250: Clear error in lsprof callback when method descriptor raises an excep...
authorTian Gao <gaogaotiantian@hotmail.com>
Tue, 8 Apr 2025 17:36:47 +0000 (10:36 -0700)
committerGitHub <noreply@github.com>
Tue, 8 Apr 2025 17:36:47 +0000 (13:36 -0400)
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 379f9869f2fe09b85fff73626681ee62e505f1a9..626c176715bdacd8b57a2f25aacb13b2679ae644 100644 (file)
@@ -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)) {