Skip to content

Commit affb1fe

Browse files
authored
Merge branch 'main' into gh-161081-copydata-shape
2 parents 65dc847 + f871449 commit affb1fe

4 files changed

Lines changed: 247 additions & 111 deletions

File tree

‎Include/internal/pycore_interp_structs.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ struct _warnings_runtime_state {
654654
// Whether the thread calling fork() owns the warnings lock.
655655
bool lock_held_at_fork;
656656
#endif
657-
long filters_version;
657+
Py_ssize_t filters_version;
658658
PyObject *context;
659659
};
660660

‎Lib/test/test_warnings/__init__.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,6 +2020,7 @@ class ThreadTests(BaseTest):
20202020

20212021
def setUp(self):
20222022
super().setUp()
2023+
self.enterContext(self.module.catch_warnings())
20232024
self.module.resetwarnings()
20242025

20252026
@unittest.skipIf(not ENABLE_THREAD_TESTS,
@@ -2069,6 +2070,29 @@ def run_threads():
20692070

20702071
run_threads()
20712072

2073+
@threading_helper.requires_working_threading()
2074+
def test_lock_not_held_while_showing(self):
2075+
# showwarning() must not run with the warnings lock held, otherwise
2076+
# another thread that needs the lock would block.
2077+
import threading
2078+
2079+
def other_thread():
2080+
self.module.filterwarnings("always", category=TestWarning)
2081+
2082+
def showwarning(*args, **kwargs):
2083+
t = threading.Thread(target=other_thread)
2084+
t.start()
2085+
t.join(timeout=support.SHORT_TIMEOUT)
2086+
alive.append(t.is_alive())
2087+
2088+
alive = []
2089+
with self.module.catch_warnings():
2090+
self.module.simplefilter("always")
2091+
self.module.showwarning = showwarning
2092+
self.module.warn("test", TestWarning)
2093+
self.module.warn_explicit("test", TestWarning, "file.py", 1)
2094+
self.assertEqual(alive, [False, False])
2095+
20722096

20732097
class CThreadTests(ThreadTests, unittest.TestCase):
20742098
module = c_warnings
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The C implementation of :mod:`warnings` no longer takes the warnings lock
2+
when emitting a warning, so user code such as a custom
3+
:func:`~warnings.showwarning` no longer runs with the lock held. On the
4+
:term:`free-threaded build`, the filter search and the registry updates use
5+
per-object critical sections instead.

0 commit comments

Comments
 (0)