]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-106152: Add PY_THROW event to cProfile (GH-106161)
authorTian Gao <gaogaotiantian@hotmail.com>
Thu, 29 Jun 2023 23:14:09 +0000 (16:14 -0700)
committerGitHub <noreply@github.com>
Thu, 29 Jun 2023 23:14:09 +0000 (16:14 -0700)
Lib/test/test_cprofile.py
Misc/NEWS.d/next/Library/2023-06-27-23-22-37.gh-issue-106152.ya5jBT.rst [new file with mode: 0644]
Modules/_lsprof.c

index 484b8f8e3a365c40334fa60c6e4ec5eb7ab68156..3056fe84dac5dd37f9bb12c11e670449c3f62704 100644 (file)
@@ -66,6 +66,26 @@ class CProfileTest(ProfileTest):
         self.assertRaises(ValueError, pr2.enable)
         pr.disable()
 
+    def test_throw(self):
+        """
+        gh-106152
+        generator.throw() should trigger a call in cProfile
+        In the any() call below, there should be two entries for the generator:
+            * one for the call to __next__ which gets a True and terminates any
+            * one when the generator is garbage collected which will effectively
+              do a throw.
+        """
+        pr = self.profilerclass()
+        pr.enable()
+        any(a == 1 for a in (1, 2))
+        pr.disable()
+        pr.create_stats()
+
+        for func, (cc, nc, _, _, _) in pr.stats.items():
+            if func[2] == "<genexpr>":
+                self.assertEqual(cc, 2)
+                self.assertEqual(nc, 2)
+
 
 class TestCommandLine(unittest.TestCase):
     def test_sort(self):
diff --git a/Misc/NEWS.d/next/Library/2023-06-27-23-22-37.gh-issue-106152.ya5jBT.rst b/Misc/NEWS.d/next/Library/2023-06-27-23-22-37.gh-issue-106152.ya5jBT.rst
new file mode 100644 (file)
index 0000000..da9d260
--- /dev/null
@@ -0,0 +1 @@
+Added PY_THROW event hook for :mod:`cProfile` for generators
index 1c84f66ee6f579249d4d8d6075c043c9c9f2b906..257de4387c0ab9bbd01abad855161c3252679204 100644 (file)
@@ -678,6 +678,7 @@ static const struct {
 } callback_table[] = {
     {PY_MONITORING_EVENT_PY_START, "_pystart_callback"},
     {PY_MONITORING_EVENT_PY_RESUME, "_pystart_callback"},
+    {PY_MONITORING_EVENT_PY_THROW, "_pystart_callback"},
     {PY_MONITORING_EVENT_PY_RETURN, "_pyreturn_callback"},
     {PY_MONITORING_EVENT_PY_YIELD, "_pyreturn_callback"},
     {PY_MONITORING_EVENT_PY_UNWIND, "_pyreturn_callback"},