aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_logging.py
diff options
context:
space:
mode:
authorDale Collison <92315623+dcollison@users.noreply.github.com>2023-09-27 17:26:41 +0100
committerGitHub <noreply@github.com>2023-09-27 18:26:41 +0200
commit74723e11109a320e628898817ab449b3dad9ee96 (patch)
tree4d50d43744e116f339b4fd752f8928d022feca23 /Lib/test/test_logging.py
parentcc54bcf17b5b5f7681f52baf3acef75b995fa1fd (diff)
downloadcpython-74723e11109a320e628898817ab449b3dad9ee96.tar.gz
cpython-74723e11109a320e628898817ab449b3dad9ee96.zip
gh-109461: Update logging module lock to use context manager (#109462)
Co-authored-by: Victor Stinner <vstinner@python.org>
Diffstat (limited to 'Lib/test/test_logging.py')
-rw-r--r--Lib/test/test_logging.py54
1 files changed, 19 insertions, 35 deletions
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
index 375f65f9d16..cca02a010b8 100644
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -90,8 +90,7 @@ class BaseTest(unittest.TestCase):
self._threading_key = threading_helper.threading_setup()
logger_dict = logging.getLogger().manager.loggerDict
- logging._acquireLock()
- try:
+ with logging._lock:
self.saved_handlers = logging._handlers.copy()
self.saved_handler_list = logging._handlerList[:]
self.saved_loggers = saved_loggers = logger_dict.copy()
@@ -101,8 +100,6 @@ class BaseTest(unittest.TestCase):
for name in saved_loggers:
logger_states[name] = getattr(saved_loggers[name],
'disabled', None)
- finally:
- logging._releaseLock()
# Set two unused loggers
self.logger1 = logging.getLogger("\xab\xd7\xbb")
@@ -136,8 +133,7 @@ class BaseTest(unittest.TestCase):
self.root_logger.removeHandler(h)
h.close()
self.root_logger.setLevel(self.original_logging_level)
- logging._acquireLock()
- try:
+ with logging._lock:
logging._levelToName.clear()
logging._levelToName.update(self.saved_level_to_name)
logging._nameToLevel.clear()
@@ -154,8 +150,6 @@ class BaseTest(unittest.TestCase):
for name in self.logger_states:
if logger_states[name] is not None:
self.saved_loggers[name].disabled = logger_states[name]
- finally:
- logging._releaseLock()
self.doCleanups()
threading_helper.threading_cleanup(*self._threading_key)
@@ -739,11 +733,8 @@ class HandlerTest(BaseTest):
stream=open('/dev/null', 'wt', encoding='utf-8'))
def emit(self, record):
- self.sub_handler.acquire()
- try:
+ with self.sub_handler.lock:
self.sub_handler.emit(record)
- finally:
- self.sub_handler.release()
self.assertEqual(len(logging._handlers), 0)
refed_h = _OurHandler()
@@ -759,29 +750,22 @@ class HandlerTest(BaseTest):
fork_happened__release_locks_and_end_thread = threading.Event()
def lock_holder_thread_fn():
- logging._acquireLock()
- try:
- refed_h.acquire()
- try:
- # Tell the main thread to do the fork.
- locks_held__ready_to_fork.set()
-
- # If the deadlock bug exists, the fork will happen
- # without dealing with the locks we hold, deadlocking
- # the child.
-
- # Wait for a successful fork or an unreasonable amount of
- # time before releasing our locks. To avoid a timing based
- # test we'd need communication from os.fork() as to when it
- # has actually happened. Given this is a regression test
- # for a fixed issue, potentially less reliably detecting
- # regression via timing is acceptable for simplicity.
- # The test will always take at least this long. :(
- fork_happened__release_locks_and_end_thread.wait(0.5)
- finally:
- refed_h.release()
- finally:
- logging._releaseLock()
+ with logging._lock, refed_h.lock:
+ # Tell the main thread to do the fork.
+ locks_held__ready_to_fork.set()
+
+ # If the deadlock bug exists, the fork will happen
+ # without dealing with the locks we hold, deadlocking
+ # the child.
+
+ # Wait for a successful fork or an unreasonable amount of
+ # time before releasing our locks. To avoid a timing based
+ # test we'd need communication from os.fork() as to when it
+ # has actually happened. Given this is a regression test
+ # for a fixed issue, potentially less reliably detecting
+ # regression via timing is acceptable for simplicity.
+ # The test will always take at least this long. :(
+ fork_happened__release_locks_and_end_thread.wait(0.5)
lock_holder_thread = threading.Thread(
target=lock_holder_thread_fn,