Skip to content

Commit e9c9a33

Browse files
authored
gh-161233: Fix TypeError when timer=None was passed to cProfile.Profile (#161234)
1 parent 97ff57f commit e9c9a33

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

‎Lib/test/test_profiling/test_tracing_profiler.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,20 @@ def timer():
139139
profiler.disable()
140140
profiler.clear()
141141

142+
def test_external_timer_is_none(self):
143+
# gh-161233
144+
def work():
145+
pass
146+
147+
for kwargs in ({}, {'timer': None}):
148+
with self.subTest(kwargs=kwargs):
149+
profiler = cProfile.Profile(**kwargs)
150+
profiler.enable()
151+
with support.catch_unraisable_exception() as cm:
152+
work()
153+
self.assertIsNone(cm.unraisable)
154+
profiler.disable()
155+
142156
def test_profile_enable_disable(self):
143157
prof = self.profilerclass()
144158
# Make sure we clean ourselves up if the test fails for some reason.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix unraisable :exc:`TypeError` when ``None`` was passed as a *timer* to
2+
:class:`cProfile.Profile <profile.Profile>`.

‎Modules/_lsprof.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ _lsprof_get_state(PyObject *module)
9292

9393
static PyTime_t CallExternalTimer(ProfilerObject *pObj)
9494
{
95+
assert(pObj->externalTimer != NULL && pObj->externalTimer != Py_None);
9596
PyObject *o = _PyObject_CallNoArgs(pObj->externalTimer);
9697

9798
if (o == NULL) {
@@ -1031,6 +1032,9 @@ profiler_init_impl(ProfilerObject *self, PyObject *timer, double timeunit,
10311032
if (setSubcalls(self, subcalls) < 0 || setBuiltins(self, builtins) < 0) {
10321033
return -1;
10331034
}
1035+
if (timer == Py_None) {
1036+
timer = NULL;
1037+
}
10341038
self->externalTimerUnit = timeunit;
10351039
Py_XSETREF(self->externalTimer, Py_XNewRef(timer));
10361040
self->tool_id = PY_MONITORING_PROFILER_ID;

0 commit comments

Comments
 (0)