Skip to content

Commit 04a165f

Browse files
[3.12] GH-106152: Add PY_THROW event to cProfile (GH-106256)
GH-106152: Add PY_THROW event to cProfile (GH-106161) (cherry picked from commit cea9d4e) Co-authored-by: Tian Gao <[email protected]>
1 parent 2405929 commit 04a165f

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

Lib/test/test_cprofile.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,26 @@ def test_second_profiler(self):
6666
self.assertRaises(ValueError, pr2.enable)
6767
pr.disable()
6868

69+
def test_throw(self):
70+
"""
71+
gh-106152
72+
generator.throw() should trigger a call in cProfile
73+
In the any() call below, there should be two entries for the generator:
74+
* one for the call to __next__ which gets a True and terminates any
75+
* one when the generator is garbage collected which will effectively
76+
do a throw.
77+
"""
78+
pr = self.profilerclass()
79+
pr.enable()
80+
any(a == 1 for a in (1, 2))
81+
pr.disable()
82+
pr.create_stats()
83+
84+
for func, (cc, nc, _, _, _) in pr.stats.items():
85+
if func[2] == "<genexpr>":
86+
self.assertEqual(cc, 2)
87+
self.assertEqual(nc, 2)
88+
6989

7090
class TestCommandLine(unittest.TestCase):
7191
def test_sort(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added PY_THROW event hook for :mod:`cProfile` for generators

Modules/_lsprof.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ static const struct {
678678
} callback_table[] = {
679679
{PY_MONITORING_EVENT_PY_START, "_pystart_callback"},
680680
{PY_MONITORING_EVENT_PY_RESUME, "_pystart_callback"},
681+
{PY_MONITORING_EVENT_PY_THROW, "_pystart_callback"},
681682
{PY_MONITORING_EVENT_PY_RETURN, "_pyreturn_callback"},
682683
{PY_MONITORING_EVENT_PY_YIELD, "_pyreturn_callback"},
683684
{PY_MONITORING_EVENT_PY_UNWIND, "_pyreturn_callback"},

0 commit comments

Comments
 (0)