Skip to content

Commit e875c0a

Browse files
authored
[3.14] gh-161233: Fix TypeError when timer=None was passed to cProfile.Profile (GH-161234) (#161239)
* [3.14] gh-161233: Fix `TypeError` when `timer=None` was passed to `cProfile.Profile` (GH-161234) (cherry picked from commit e9c9a33) Co-authored-by: sobolevn <mail@sobolevn.me>
1 parent 79c5044 commit e875c0a

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

‎Lib/test/test_cprofile.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,20 @@ def timer():
105105
profiler.disable()
106106
profiler.clear()
107107

108+
def test_external_timer_is_none(self):
109+
# gh-161233
110+
def work():
111+
pass
112+
113+
for kwargs in ({}, {'timer': None}):
114+
with self.subTest(kwargs=kwargs):
115+
profiler = cProfile.Profile(**kwargs)
116+
profiler.enable()
117+
with support.catch_unraisable_exception() as cm:
118+
work()
119+
self.assertIsNone(cm.unraisable)
120+
profiler.disable()
121+
108122
def test_profile_enable_disable(self):
109123
prof = self.profilerclass()
110124
# 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 = NULL;
9697

9798
// External timer can do arbitrary things so we need a flag to prevent
@@ -1021,6 +1022,9 @@ profiler_init_impl(ProfilerObject *self, PyObject *timer, double timeunit,
10211022
if (setSubcalls(self, subcalls) < 0 || setBuiltins(self, builtins) < 0) {
10221023
return -1;
10231024
}
1025+
if (timer == Py_None) {
1026+
timer = NULL;
1027+
}
10241028
self->externalTimerUnit = timeunit;
10251029
Py_XSETREF(self->externalTimer, Py_XNewRef(timer));
10261030
self->tool_id = PY_MONITORING_PROFILER_ID;

0 commit comments

Comments
 (0)