Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
GH-106152: Add PY_THROW event to cProfile (GH-106161)
(cherry picked from commit cea9d4e)

Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
  • Loading branch information
gaogaotiantian authored and miss-islington committed Jun 29, 2023
commit f80e76bfeca227e357a590f0f9783403148c66be
20 changes: 20 additions & 0 deletions Lib/test/test_cprofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ def test_second_profiler(self):
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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added PY_THROW event hook for :mod:`cProfile` for generators
1 change: 1 addition & 0 deletions Modules/_lsprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down