]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-122029: Log call events in sys.setprofile when it's a method with c functio...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 23 Jul 2024 22:44:43 +0000 (00:44 +0200)
committerGitHub <noreply@github.com>
Tue, 23 Jul 2024 22:44:43 +0000 (22:44 +0000)
gh-122029: Log call events in sys.setprofile when it's a method with c function (GH-122072)

Log call events in sys.setprofile when it is a method with a C function.
(cherry picked from commit e91ef13861e88c27aed51a24e58d1dcc855a01dc)

Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
Lib/test/test_sys_setprofile.py
Misc/NEWS.d/next/Core and Builtins/2024-07-21-01-23-54.gh-issue-122029.gKv-e2.rst [new file with mode: 0644]
Python/legacy_tracing.c

index 9e8936630de920c186804f501e3ff1c20dcc5e30..f77036962cfe5d329a2e584027b2cfa274fc9928 100644 (file)
@@ -478,6 +478,20 @@ class TestEdgeCases(unittest.TestCase):
         sys.setprofile(lambda *args: None)
         f()
 
+    def test_method_with_c_function(self):
+        # gh-122029
+        # When we have a PyMethodObject whose im_func is a C function, we
+        # should record both the call and the return. f = classmethod(repr)
+        # is just a way to create a PyMethodObject with a C function.
+        class A:
+            f = classmethod(repr)
+        events = []
+        sys.setprofile(lambda frame, event, args: events.append(event))
+        A().f()
+        sys.setprofile(None)
+        # The last c_call is the call to sys.setprofile
+        self.assertEqual(events, ['c_call', 'c_return', 'c_call'])
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-07-21-01-23-54.gh-issue-122029.gKv-e2.rst b/Misc/NEWS.d/next/Core and Builtins/2024-07-21-01-23-54.gh-issue-122029.gKv-e2.rst
new file mode 100644 (file)
index 0000000..bddee3a
--- /dev/null
@@ -0,0 +1 @@
+Emit ``c_call`` events in :func:`sys.setprofile` when a ``PyMethodObject`` pointing to a ``PyCFunction`` is called.
index 43fa5910ef6dd179e1ad1d13e5f48e05fabb2eee..4a6565bebc1f14fafdc9bb76e7fb4ccf611bb18d 100644 (file)
@@ -103,6 +103,19 @@ sys_profile_call_or_return(
         Py_DECREF(meth);
         return res;
     }
+    else if (Py_TYPE(callable) == &PyMethod_Type) {
+        // CALL instruction will grab the function from the method,
+        // so if the function is a C function, the return event will
+        // be emitted. However, CALL event happens before CALL
+        // instruction, so we need to handle this case here.
+        PyObject* func = PyMethod_GET_FUNCTION(callable);
+        if (func == NULL) {
+            return NULL;
+        }
+        if (PyCFunction_Check(func)) {
+            return call_profile_func(self, func);
+        }
+    }
     Py_RETURN_NONE;
 }