diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2023-06-29 23:39:18 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-29 23:39:18 (GMT) |
commit | 04a165f4f96792e4c9409420ae12002cf6ce9701 (patch) | |
tree | 580d89a92af86584bd54dd7530a4b82c76193e73 | |
parent | 2405929c35239580aa0344f0e8fd31b87f9ccfe9 (diff) | |
download | cpython-04a165f4f96792e4c9409420ae12002cf6ce9701.zip cpython-04a165f4f96792e4c9409420ae12002cf6ce9701.tar.gz cpython-04a165f4f96792e4c9409420ae12002cf6ce9701.tar.bz2 |
[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 cea9d4ea82abcb2c6f1d83a2fe819859da4bbda4)
Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
-rw-r--r-- | Lib/test/test_cprofile.py | 20 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2023-06-27-23-22-37.gh-issue-106152.ya5jBT.rst | 1 | ||||
-rw-r--r-- | Modules/_lsprof.c | 1 |
3 files changed, 22 insertions, 0 deletions
diff --git a/Lib/test/test_cprofile.py b/Lib/test/test_cprofile.py index 484b8f8..3056fe8 100644 --- a/Lib/test/test_cprofile.py +++ b/Lib/test/test_cprofile.py @@ -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 index 0000000..da9d260 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-06-27-23-22-37.gh-issue-106152.ya5jBT.rst @@ -0,0 +1 @@ +Added PY_THROW event hook for :mod:`cProfile` for generators diff --git a/Modules/_lsprof.c b/Modules/_lsprof.c index 1c84f66..257de43 100644 --- a/Modules/_lsprof.c +++ b/Modules/_lsprof.c @@ -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"}, |