Skip to content

Commit f885c7d

Browse files
[3.14] gh-135429: Fix the argument mismatch in lsprof throw event (GH-135442) (#135446)
gh-135429: Fix the argument mismatch in lsprof throw event (GH-135442) (cherry picked from commit b03309f) Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
1 parent 04273ad commit f885c7d

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

Lib/test/test_cprofile.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,22 @@ def test_throw(self):
125125
"""
126126
gh-106152
127127
generator.throw() should trigger a call in cProfile
128-
In the any() call below, there should be two entries for the generator:
129-
* one for the call to __next__ which gets a True and terminates any
130-
* one when the generator is garbage collected which will effectively
131-
do a throw.
132128
"""
129+
130+
def gen():
131+
yield
132+
133133
pr = self.profilerclass()
134134
pr.enable()
135-
any(a == 1 for a in (1, 2))
135+
g = gen()
136+
try:
137+
g.throw(SyntaxError)
138+
except SyntaxError:
139+
pass
136140
pr.disable()
137141
pr.create_stats()
138142

139-
for func, (cc, nc, _, _, _) in pr.stats.items():
140-
if func[2] == "<genexpr>":
141-
self.assertEqual(cc, 1)
142-
self.assertEqual(nc, 1)
143+
self.assertTrue(any("throw" in func[2] for func in pr.stats.keys())),
143144

144145
def test_bad_descriptor(self):
145146
# gh-132250
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix the argument mismatch in ``_lsprof`` for ``PY_THROW`` event.

Modules/_lsprof.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,27 @@ _lsprof_Profiler__pystart_callback_impl(ProfilerObject *self, PyObject *code,
631631
Py_RETURN_NONE;
632632
}
633633

634+
/*[clinic input]
635+
_lsprof.Profiler._pythrow_callback
636+
637+
code: object
638+
instruction_offset: object
639+
exception: object
640+
/
641+
642+
[clinic start generated code]*/
643+
644+
static PyObject *
645+
_lsprof_Profiler__pythrow_callback_impl(ProfilerObject *self, PyObject *code,
646+
PyObject *instruction_offset,
647+
PyObject *exception)
648+
/*[clinic end generated code: output=0a32988919dfb94c input=fd728fc2c074f5e6]*/
649+
{
650+
ptrace_enter_call((PyObject*)self, (void *)code, code);
651+
652+
Py_RETURN_NONE;
653+
}
654+
634655
/*[clinic input]
635656
_lsprof.Profiler._pyreturn_callback
636657
@@ -747,7 +768,7 @@ static const struct {
747768
} callback_table[] = {
748769
{PY_MONITORING_EVENT_PY_START, "_pystart_callback"},
749770
{PY_MONITORING_EVENT_PY_RESUME, "_pystart_callback"},
750-
{PY_MONITORING_EVENT_PY_THROW, "_pystart_callback"},
771+
{PY_MONITORING_EVENT_PY_THROW, "_pythrow_callback"},
751772
{PY_MONITORING_EVENT_PY_RETURN, "_pyreturn_callback"},
752773
{PY_MONITORING_EVENT_PY_YIELD, "_pyreturn_callback"},
753774
{PY_MONITORING_EVENT_PY_UNWIND, "_pyreturn_callback"},
@@ -1002,6 +1023,7 @@ static PyMethodDef profiler_methods[] = {
10021023
_LSPROF_PROFILER_DISABLE_METHODDEF
10031024
_LSPROF_PROFILER_CLEAR_METHODDEF
10041025
_LSPROF_PROFILER__PYSTART_CALLBACK_METHODDEF
1026+
_LSPROF_PROFILER__PYTHROW_CALLBACK_METHODDEF
10051027
_LSPROF_PROFILER__PYRETURN_CALLBACK_METHODDEF
10061028
_LSPROF_PROFILER__CCALL_CALLBACK_METHODDEF
10071029
_LSPROF_PROFILER__CRETURN_CALLBACK_METHODDEF

Modules/clinic/_lsprof.c.h

Lines changed: 34 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)