[Python-checkins] bpo-36688: Adding an implementation of RLock in _dummy_thread (GH-12943)

Miss Islington (bot) webhook-mailer at python.org
Mon Jun 17 04:28:50 EDT 2019


https://github.com/python/cpython/commit/351b0e793e35510e8cbbcbb455a1b9544e808cdd
commit: 351b0e793e35510e8cbbcbb455a1b9544e808cdd
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-06-17T01:28:43-07:00
summary:

bpo-36688: Adding an implementation of RLock in _dummy_thread (GH-12943)

(cherry picked from commit c5905f39bcf4ef895d42eede41bb5a2f071a501d)

Co-authored-by: Joost Lek <vlabakje at gmail.com>

files:
M Lib/_dummy_thread.py
M Lib/test/test_dummy_thread.py

diff --git a/Lib/_dummy_thread.py b/Lib/_dummy_thread.py
index a2cae54b0580..2e46a07603be 100644
--- a/Lib/_dummy_thread.py
+++ b/Lib/_dummy_thread.py
@@ -14,7 +14,7 @@
 # Exports only things specified by thread documentation;
 # skipping obsolete synonyms allocate(), start_new(), exit_thread().
 __all__ = ['error', 'start_new_thread', 'exit', 'get_ident', 'allocate_lock',
-           'interrupt_main', 'LockType']
+           'interrupt_main', 'LockType', 'RLock']
 
 # A dummy value
 TIMEOUT_MAX = 2**31
@@ -148,6 +148,36 @@ def __repr__(self):
             hex(id(self))
         )
 
+
+class RLock(LockType):
+    """Dummy implementation of threading._RLock.
+
+    Re-entrant lock can be aquired multiple times and needs to be released
+    just as many times. This dummy implemention does not check wheter the
+    current thread actually owns the lock, but does accounting on the call
+    counts.
+    """
+    def __init__(self):
+        super().__init__()
+        self._levels = 0
+
+    def acquire(self, waitflag=None, timeout=-1):
+        """Aquire the lock, can be called multiple times in succession.
+        """
+        locked = super().acquire(waitflag, timeout)
+        if locked:
+            self._levels += 1
+        return locked
+
+    def release(self):
+        """Release needs to be called once for every call to acquire().
+        """
+        if self._levels == 0:
+            raise error
+        if self._levels == 1:
+            super().release()
+        self._levels -= 1
+
 # Used to signal that interrupt_main was called in a "thread"
 _interrupt = False
 # True when not executing in a "thread"
diff --git a/Lib/test/test_dummy_thread.py b/Lib/test/test_dummy_thread.py
index da512167834f..0f56fcf97336 100644
--- a/Lib/test/test_dummy_thread.py
+++ b/Lib/test/test_dummy_thread.py
@@ -102,6 +102,24 @@ def test_lock_representation(self):
         self.assertIn("unlocked", repr(self.lock))
 
 
+class RLockTests(unittest.TestCase):
+    """Test dummy RLock objects."""
+
+    def setUp(self):
+        self.rlock = _thread.RLock()
+
+    def test_multiple_acquire(self):
+        self.assertIn("unlocked", repr(self.rlock))
+        self.rlock.acquire()
+        self.rlock.acquire()
+        self.assertIn("locked", repr(self.rlock))
+        self.rlock.release()
+        self.assertIn("locked", repr(self.rlock))
+        self.rlock.release()
+        self.assertIn("unlocked", repr(self.rlock))
+        self.assertRaises(RuntimeError, self.rlock.release)
+
+
 class MiscTests(unittest.TestCase):
     """Miscellaneous tests."""
 
@@ -253,3 +271,6 @@ def test_RaiseException(self, mock_print_exc):
         func = mock.Mock(side_effect=Exception)
         _thread.start_new_thread(func, tuple())
         self.assertTrue(mock_print_exc.called)
+
+if __name__ == '__main__':
+    unittest.main()



More information about the Python-checkins mailing list