From e586d966698aed6fb281da805b2b409e344544e2 Mon Sep 17 00:00:00 2001 From: An Long Date: Tue, 7 Jul 2026 08:36:26 +0900 Subject: [PATCH] gh-153068: Preserve cprofile enable errors (GH-153070) (cherry picked from commit dea93ce2a203a9b3aedf091faa6610d13d2b5aea) Co-authored-by: An Long --- Lib/test/test_cprofile.py | 5 +++-- .../Library/2026-07-05-16-09-31.gh-issue-153068.huY9Jh.rst | 2 ++ Modules/_lsprof.c | 1 - 3 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-07-05-16-09-31.gh-issue-153068.huY9Jh.rst diff --git a/Lib/test/test_cprofile.py b/Lib/test/test_cprofile.py index acead07d507e4b8..c2d9dcab09ed9ec 100644 --- a/Lib/test/test_cprofile.py +++ b/Lib/test/test_cprofile.py @@ -116,8 +116,9 @@ def test_second_profiler(self): pr = self.profilerclass() pr2 = self.profilerclass() pr.enable() - self.assertRaises(ValueError, pr2.enable) - pr.disable() + self.addCleanup(pr.disable) + msg = f"tool {sys.monitoring.PROFILER_ID} is already in use" + self.assertRaisesRegex(ValueError, msg, pr2.enable) def test_throw(self): """ diff --git a/Misc/NEWS.d/next/Library/2026-07-05-16-09-31.gh-issue-153068.huY9Jh.rst b/Misc/NEWS.d/next/Library/2026-07-05-16-09-31.gh-issue-153068.huY9Jh.rst new file mode 100644 index 000000000000000..accf6e99cd6434d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-05-16-09-31.gh-issue-153068.huY9Jh.rst @@ -0,0 +1,2 @@ +Fix :meth:`!cProfile.Profile.enable` to no longer overwrite errors from +:mod:`sys.monitoring`. diff --git a/Modules/_lsprof.c b/Modules/_lsprof.c index f23f254c0aa0db3..fcd856f1035d6cd 100644 --- a/Modules/_lsprof.c +++ b/Modules/_lsprof.c @@ -759,7 +759,6 @@ profiler_enable(ProfilerObject *self, PyObject *args, PyObject *kwds) "use_tool_id", "is", self->tool_id, "cProfile"); if (check == NULL) { - PyErr_Format(PyExc_ValueError, "Another profiling tool is already active"); goto error; } Py_DECREF(check);