1616)
1717RGX_REPR = re .compile (STR_RGX_REPR )
1818
19+ if sys .version_info >= (3 , 7 ):
20+
21+ def deprecate (self ):
22+ return self .assertWarns (DeprecationWarning )
23+
24+ else :
25+
26+ class deprecate :
27+ def __init__ (self , tc ):
28+ pass
29+
30+ def __enter__ (self ):
31+ return self
32+
33+ def __exit__ (self , * tb ):
34+ pass
35+
1936
2037class LockTests (test_utils .TestCase ):
2138 def setUp (self ):
@@ -42,7 +59,8 @@ def test_repr(self):
4259
4360 @asyncio .coroutine
4461 def acquire_lock ():
45- yield from lock
62+ with deprecate (self ):
63+ yield from lock
4664
4765 self .loop .run_until_complete (acquire_lock ())
4866 self .assertTrue (repr (lock ).endswith ('[locked]>' ))
@@ -53,7 +71,8 @@ def test_lock(self):
5371
5472 @asyncio .coroutine
5573 def acquire_lock ():
56- return (yield from lock )
74+ with deprecate (self ):
75+ return (yield from lock )
5776
5877 res = self .loop .run_until_complete (acquire_lock ())
5978
@@ -69,21 +88,18 @@ def test_acquire(self):
6988
7089 self .assertTrue (self .loop .run_until_complete (lock .acquire ()))
7190
72- @asyncio .coroutine
73- def c1 (result ):
74- if (yield from lock .acquire ()):
91+ async def c1 (result ):
92+ if await lock .acquire ():
7593 result .append (1 )
7694 return True
7795
78- @asyncio .coroutine
79- def c2 (result ):
80- if (yield from lock .acquire ()):
96+ async def c2 (result ):
97+ if await lock .acquire ():
8198 result .append (2 )
8299 return True
83100
84- @asyncio .coroutine
85- def c3 (result ):
86- if (yield from lock .acquire ()):
101+ async def c3 (result ):
102+ if await lock .acquire ():
87103 result .append (3 )
88104 return True
89105
@@ -143,12 +159,11 @@ def test_cancel_race(self):
143159 # Setup: A has the lock, b and c are waiting.
144160 lock = asyncio .Lock (loop = self .loop )
145161
146- @asyncio .coroutine
147- def lockit (name , blocker ):
148- yield from lock .acquire ()
162+ async def lockit (name , blocker ):
163+ await lock .acquire ()
149164 try :
150165 if blocker is not None :
151- yield from blocker
166+ await blocker
152167 finally :
153168 lock .release ()
154169
@@ -215,7 +230,8 @@ def test_context_manager(self):
215230
216231 @asyncio .coroutine
217232 def acquire_lock ():
218- return (yield from lock )
233+ with deprecate (self ):
234+ return (yield from lock )
219235
220236 with self .loop .run_until_complete (acquire_lock ()):
221237 self .assertTrue (lock .locked ())
@@ -227,7 +243,8 @@ def test_context_manager_cant_reuse(self):
227243
228244 @asyncio .coroutine
229245 def acquire_lock ():
230- return (yield from lock )
246+ with deprecate (self ):
247+ return (yield from lock )
231248
232249 # This spells "yield from lock" outside a generator.
233250 cm = self .loop .run_until_complete (acquire_lock ())
@@ -290,19 +307,16 @@ def test_wait(self):
290307
291308 result = []
292309
293- @asyncio .coroutine
294- def c1 (result ):
295- if (yield from ev .wait ()):
310+ async def c1 (result ):
311+ if await ev .wait ():
296312 result .append (1 )
297313
298- @asyncio .coroutine
299- def c2 (result ):
300- if (yield from ev .wait ()):
314+ async def c2 (result ):
315+ if await ev .wait ():
301316 result .append (2 )
302317
303- @asyncio .coroutine
304- def c3 (result ):
305- if (yield from ev .wait ()):
318+ async def c3 (result ):
319+ if await ev .wait ():
306320 result .append (3 )
307321
308322 t1 = asyncio .Task (c1 (result ), loop = self .loop )
@@ -353,9 +367,8 @@ def test_clear_with_waiters(self):
353367 ev = asyncio .Event (loop = self .loop )
354368 result = []
355369
356- @asyncio .coroutine
357- def c1 (result ):
358- if (yield from ev .wait ()):
370+ async def c1 (result ):
371+ if await ev .wait ():
359372 result .append (1 )
360373 return True
361374
@@ -401,24 +414,21 @@ def test_wait(self):
401414 cond = asyncio .Condition (loop = self .loop )
402415 result = []
403416
404- @asyncio .coroutine
405- def c1 (result ):
406- yield from cond .acquire ()
407- if (yield from cond .wait ()):
417+ async def c1 (result ):
418+ await cond .acquire ()
419+ if await cond .wait ():
408420 result .append (1 )
409421 return True
410422
411- @asyncio .coroutine
412- def c2 (result ):
413- yield from cond .acquire ()
414- if (yield from cond .wait ()):
423+ async def c2 (result ):
424+ await cond .acquire ()
425+ if await cond .wait ():
415426 result .append (2 )
416427 return True
417428
418- @asyncio .coroutine
419- def c3 (result ):
420- yield from cond .acquire ()
421- if (yield from cond .wait ()):
429+ async def c3 (result ):
430+ await cond .acquire ()
431+ if await cond .wait ():
422432 result .append (3 )
423433 return True
424434
@@ -511,10 +521,9 @@ def predicate():
511521
512522 result = []
513523
514- @asyncio .coroutine
515- def c1 (result ):
516- yield from cond .acquire ()
517- if (yield from cond .wait_for (predicate )):
524+ async def c1 (result ):
525+ await cond .acquire ()
526+ if await cond .wait_for (predicate ):
518527 result .append (1 )
519528 cond .release ()
520529 return True
@@ -553,26 +562,23 @@ def test_notify(self):
553562 cond = asyncio .Condition (loop = self .loop )
554563 result = []
555564
556- @asyncio .coroutine
557- def c1 (result ):
558- yield from cond .acquire ()
559- if (yield from cond .wait ()):
565+ async def c1 (result ):
566+ await cond .acquire ()
567+ if await cond .wait ():
560568 result .append (1 )
561569 cond .release ()
562570 return True
563571
564- @asyncio .coroutine
565- def c2 (result ):
566- yield from cond .acquire ()
567- if (yield from cond .wait ()):
572+ async def c2 (result ):
573+ await cond .acquire ()
574+ if await cond .wait ():
568575 result .append (2 )
569576 cond .release ()
570577 return True
571578
572- @asyncio .coroutine
573- def c3 (result ):
574- yield from cond .acquire ()
575- if (yield from cond .wait ()):
579+ async def c3 (result ):
580+ await cond .acquire ()
581+ if await cond .wait ():
576582 result .append (3 )
577583 cond .release ()
578584 return True
@@ -609,18 +615,16 @@ def test_notify_all(self):
609615
610616 result = []
611617
612- @asyncio .coroutine
613- def c1 (result ):
614- yield from cond .acquire ()
615- if (yield from cond .wait ()):
618+ async def c1 (result ):
619+ await cond .acquire ()
620+ if await cond .wait ():
616621 result .append (1 )
617622 cond .release ()
618623 return True
619624
620- @asyncio .coroutine
621- def c2 (result ):
622- yield from cond .acquire ()
623- if (yield from cond .wait ()):
625+ async def c2 (result ):
626+ await cond .acquire ()
627+ if await cond .wait ():
624628 result .append (2 )
625629 cond .release ()
626630 return True
@@ -671,7 +675,8 @@ def test_context_manager(self):
671675
672676 @asyncio .coroutine
673677 def acquire_cond ():
674- return (yield from cond )
678+ with deprecate (self ):
679+ return (yield from cond )
675680
676681 with self .loop .run_until_complete (acquire_cond ()):
677682 self .assertTrue (cond .locked ())
@@ -754,7 +759,8 @@ def test_semaphore(self):
754759
755760 @asyncio .coroutine
756761 def acquire_lock ():
757- return (yield from sem )
762+ with deprecate (self ):
763+ return (yield from sem )
758764
759765 res = self .loop .run_until_complete (acquire_lock ())
760766
@@ -777,27 +783,23 @@ def test_acquire(self):
777783 self .assertTrue (self .loop .run_until_complete (sem .acquire ()))
778784 self .assertFalse (sem .locked ())
779785
780- @asyncio .coroutine
781- def c1 (result ):
782- yield from sem .acquire ()
786+ async def c1 (result ):
787+ await sem .acquire ()
783788 result .append (1 )
784789 return True
785790
786- @asyncio .coroutine
787- def c2 (result ):
788- yield from sem .acquire ()
791+ async def c2 (result ):
792+ await sem .acquire ()
789793 result .append (2 )
790794 return True
791795
792- @asyncio .coroutine
793- def c3 (result ):
794- yield from sem .acquire ()
796+ async def c3 (result ):
797+ await sem .acquire ()
795798 result .append (3 )
796799 return True
797800
798- @asyncio .coroutine
799- def c4 (result ):
800- yield from sem .acquire ()
801+ async def c4 (result ):
802+ await sem .acquire ()
801803 result .append (4 )
802804 return True
803805
@@ -897,7 +899,8 @@ def test_context_manager(self):
897899
898900 @asyncio .coroutine
899901 def acquire_lock ():
900- return (yield from sem )
902+ with deprecate (self ):
903+ return (yield from sem )
901904
902905 with self .loop .run_until_complete (acquire_lock ()):
903906 self .assertFalse (sem .locked ())
0 commit comments