mirror of
				https://github.com/python/cpython.git
				synced 2025-10-25 10:44:55 +00:00 
			
		
		
		
	Accept optional lock object in Condition ctor (tulip issue #198)
This commit is contained in:
		
							parent
							
								
									e254e53c83
								
							
						
					
					
						commit
						f21fcd09c5
					
				
					 2 changed files with 18 additions and 3 deletions
				
			
		|  | @ -255,14 +255,17 @@ class Condition: | ||||||
|     A new Lock object is created and used as the underlying lock. |     A new Lock object is created and used as the underlying lock. | ||||||
|     """ |     """ | ||||||
| 
 | 
 | ||||||
|     def __init__(self, *, loop=None): |     def __init__(self, lock=None, *, loop=None): | ||||||
|         if loop is not None: |         if loop is not None: | ||||||
|             self._loop = loop |             self._loop = loop | ||||||
|         else: |         else: | ||||||
|             self._loop = events.get_event_loop() |             self._loop = events.get_event_loop() | ||||||
| 
 | 
 | ||||||
|         # Lock as an attribute as in threading.Condition. |         if lock is None: | ||||||
|         lock = Lock(loop=self._loop) |             lock = Lock(loop=self._loop) | ||||||
|  |         elif lock._loop is not self._loop: | ||||||
|  |             raise ValueError("loop argument must agree with lock") | ||||||
|  | 
 | ||||||
|         self._lock = lock |         self._lock = lock | ||||||
|         # Export the lock's locked(), acquire() and release() methods. |         # Export the lock's locked(), acquire() and release() methods. | ||||||
|         self.locked = lock.locked |         self.locked = lock.locked | ||||||
|  |  | ||||||
|  | @ -656,6 +656,18 @@ def test_context_manager_no_yield(self): | ||||||
| 
 | 
 | ||||||
|         self.assertFalse(cond.locked()) |         self.assertFalse(cond.locked()) | ||||||
| 
 | 
 | ||||||
|  |     def test_explicit_lock(self): | ||||||
|  |         lock = asyncio.Lock(loop=self.loop) | ||||||
|  |         cond = asyncio.Condition(lock, loop=self.loop) | ||||||
|  | 
 | ||||||
|  |         self.assertIs(lock._loop, cond._loop) | ||||||
|  | 
 | ||||||
|  |     def test_ambiguous_loops(self): | ||||||
|  |         loop = self.new_test_loop() | ||||||
|  |         lock = asyncio.Lock(loop=self.loop) | ||||||
|  |         with self.assertRaises(ValueError): | ||||||
|  |             asyncio.Condition(lock, loop=loop) | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| class SemaphoreTests(test_utils.TestCase): | class SemaphoreTests(test_utils.TestCase): | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andrew Svetlov
						Andrew Svetlov