Skip to content

Commit ce23ffd

Browse files
committed
gh-138750: Increase coverage of inspect.getcoroutinelocals
1 parent f5fa336 commit ce23ffd

File tree

1 file changed

+54
-4
lines changed

1 file changed

+54
-4
lines changed

Lib/test/test_inspect/test_inspect.py

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2859,21 +2859,71 @@ def test_easy_debugging(self):
28592859
self.assertIn(name, str(state))
28602860

28612861
def test_getcoroutinelocals(self):
2862+
from collections.abc import Coroutine
2863+
28622864
@types.coroutine
28632865
def gencoro():
28642866
yield
28652867

2866-
gencoro = gencoro()
2868+
genc = gencoro()
28672869
async def func(a=None):
28682870
b = 'spam'
2869-
await gencoro
2871+
await genc
28702872

28712873
coro = func()
28722874
self.assertEqual(inspect.getcoroutinelocals(coro),
2873-
{'a': None, 'gencoro': gencoro})
2875+
{'a': None, 'genc': genc})
28742876
coro.send(None)
28752877
self.assertEqual(inspect.getcoroutinelocals(coro),
2876-
{'a': None, 'gencoro': gencoro, 'b': 'spam'})
2878+
{'a': None, 'genc': genc, 'b': 'spam'})
2879+
2880+
def make_coro():
2881+
genc = gencoro()
2882+
async def coro():
2883+
local1 = 1
2884+
await genc
2885+
local2 = 2
2886+
return genc, coro()
2887+
2888+
# Does not have `cr_frame`:
2889+
2890+
class CustomCoro(Coroutine):
2891+
def __init__(self, coro):
2892+
self._coro = coro
2893+
2894+
def __await__(self):
2895+
return self._coro.__await__
2896+
2897+
def send(self, value):
2898+
return self._coro.send(value)
2899+
2900+
def throw(self, *args):
2901+
return super().throw(*args)
2902+
2903+
genc, cr = make_coro()
2904+
self.assertEqual(inspect.getcoroutinelocals(CustomCoro(cr)), {})
2905+
2906+
cr.send(None)
2907+
self.assertEqual(inspect.getcoroutinelocals(CustomCoro(cr)), {})
2908+
2909+
# Has custom `cr_frame` implementation:
2910+
2911+
class CustomCoroWithCRFrame(CustomCoro):
2912+
@property
2913+
def cr_frame(self):
2914+
return self._coro.cr_frame
2915+
2916+
genc, cr = make_coro()
2917+
self.assertEqual(
2918+
inspect.getcoroutinelocals(CustomCoroWithCRFrame(cr)),
2919+
{'genc': genc},
2920+
)
2921+
2922+
cr.send(None)
2923+
self.assertEqual(
2924+
inspect.getcoroutinelocals(CustomCoroWithCRFrame(cr)),
2925+
{'genc': genc, 'local1': 1},
2926+
)
28772927

28782928

28792929
@support.requires_working_socket()

0 commit comments

Comments
 (0)