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
14 changes: 14 additions & 0 deletions Lib/test/test_profiling/test_tracing_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@ def timer():
profiler.disable()
profiler.clear()

def test_external_timer_is_none(self):
# gh-161233
def work():
pass

for kwargs in ({}, {'timer': None}):
with self.subTest(kwargs=kwargs):
profiler = cProfile.Profile(**kwargs)
profiler.enable()
with support.catch_unraisable_exception() as cm:
work()
self.assertIsNone(cm.unraisable)
profiler.disable()

def test_profile_enable_disable(self):
prof = self.profilerclass()
# Make sure we clean ourselves up if the test fails for some reason.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix unraisable :exc:`TypeError` when ``None`` was passed as a *timer* to
:class:`cProfile.Profile <profile.Profile>`.
4 changes: 4 additions & 0 deletions Modules/_lsprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ _lsprof_get_state(PyObject *module)

static PyTime_t CallExternalTimer(ProfilerObject *pObj)
{
assert(pObj->externalTimer != NULL && pObj->externalTimer != Py_None);
PyObject *o = _PyObject_CallNoArgs(pObj->externalTimer);

if (o == NULL) {
Expand Down Expand Up @@ -1031,6 +1032,9 @@ profiler_init_impl(ProfilerObject *self, PyObject *timer, double timeunit,
if (setSubcalls(self, subcalls) < 0 || setBuiltins(self, builtins) < 0) {
return -1;
}
if (timer == Py_None) {
timer = NULL;
}
self->externalTimerUnit = timeunit;
Py_XSETREF(self->externalTimer, Py_XNewRef(timer));
self->tool_id = PY_MONITORING_PROFILER_ID;
Expand Down
Loading