Skip to content
Open
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
16 changes: 16 additions & 0 deletions Lib/test/test_profiling/test_tracing_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ def test_profile_enable_disable(self):
prof.disable()
self.assertIs(sys.monitoring.get_tool(sys.monitoring.PROFILER_ID), None)

def test_dealloc_does_not_disable_sys_profile(self):
self.addCleanup(sys.setprofile, sys.getprofile())
self.addCleanup(sys.monitoring.free_tool_id, sys.monitoring.PROFILER_ID)

def callback(frame, event, arg):
pass

prof = self.profilerclass()
prof.enable()
sys.setprofile(callback)
# Release the monitoring callbacks so the enabled profiler can be
# destroyed without an explicit disable().
sys.monitoring.free_tool_id(sys.monitoring.PROFILER_ID)
del prof
self.assertIs(sys.getprofile(), callback)

def test_profile_as_context_manager(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 @@
Prevent deallocation of a :mod:`profiling.tracing` profiler from disabling an
unrelated :func:`sys.setprofile` callback.
9 changes: 0 additions & 9 deletions Modules/_lsprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#include "Python.h"
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_ceval.h" // _PyEval_SetProfile()
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_time.h" // _PyTime_FromSecondsObject()
#include "pycore_typeobject.h" // _PyType_GetModuleState()
#include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()
Expand Down Expand Up @@ -982,13 +980,6 @@ profiler_dealloc(PyObject *op)
{
ProfilerObject *self = ProfilerObject_CAST(op);
PyObject_GC_UnTrack(self);
if (self->flags & POF_ENABLED) {
PyThreadState *tstate = _PyThreadState_GET();
if (_PyEval_SetProfile(tstate, NULL, NULL) < 0) {
PyErr_FormatUnraisable("Exception ignored while "
"destroying _lsprof profiler");
}
}

/* Drop the external timer before flushing: it is Python code, and the
profiler can be deallocated by the garbage collector. */
Expand Down
Loading