https://github.com/python/cpython/commit/78e9fdb78f61f99a0ee05f412b45292df2088cc2
commit: 78e9fdb78f61f99a0ee05f412b45292df2088cc2
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: pablogsal <[email protected]>
date: 2026-07-07T00:11:55Z
summary:

[3.15] gh-153068: Preserve cprofile enable errors (GH-153070) (#153241)

files:
A Misc/NEWS.d/next/Library/2026-07-05-16-09-31.gh-issue-153068.huY9Jh.rst
M Lib/test/test_profiling/test_tracing_profiler.py
M Modules/_lsprof.c

diff --git a/Lib/test/test_profiling/test_tracing_profiler.py 
b/Lib/test/test_profiling/test_tracing_profiler.py
index a4fabe9d3cc75b..8a7070142b60ee 100644
--- a/Lib/test/test_profiling/test_tracing_profiler.py
+++ b/Lib/test/test_profiling/test_tracing_profiler.py
@@ -117,8 +117,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 00000000000000..accf6e99cd6434
--- /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 3d341a336ab22e..4e50ca64f59af2 100644
--- a/Modules/_lsprof.c
+++ b/Modules/_lsprof.c
@@ -821,7 +821,6 @@ _lsprof_Profiler_enable_impl(ProfilerObject *self, int 
subcalls,
                                           "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);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to