File tree Expand file tree Collapse file tree 2 files changed +40
-2
lines changed Expand file tree Collapse file tree 2 files changed +40
-2
lines changed Original file line number Diff line number Diff line change 3
3
import random
4
4
import sys
5
5
import unittest
6
+ import weakref
6
7
7
8
from uvloop import _testbase as tb
8
9
@@ -117,6 +118,29 @@ async def main():
117
118
118
119
self .assertEqual (cvar .get (), - 1 )
119
120
121
+ @unittest .skipUnless (PY37 , 'requires Python 3.7' )
122
+ def test_task_context_4 (self ):
123
+ import contextvars
124
+ cvar = contextvars .ContextVar ('cvar' , default = 'nope' )
125
+
126
+ class TrackMe :
127
+ pass
128
+ tracked = TrackMe ()
129
+ ref = weakref .ref (tracked )
130
+
131
+ async def sub ():
132
+ cvar .set (tracked )
133
+ self .loop .call_soon (lambda : None )
134
+
135
+ async def main ():
136
+ await self .loop .create_task (sub ())
137
+
138
+ task = self .loop .create_task (main ())
139
+ self .loop .run_until_complete (task )
140
+
141
+ del tracked
142
+ self .assertIsNone (ref ())
143
+
120
144
121
145
class Test_UV_Context (_ContextBaseTests , tb .UVTestCase ):
122
146
Original file line number Diff line number Diff line change @@ -15,9 +15,11 @@ cdef class Handle:
15
15
self ._source_traceback = extract_stack()
16
16
17
17
cdef inline _set_context(self , object context):
18
+ cdef PyContext* current_context
19
+
18
20
if PY37:
19
21
if context is None :
20
- context = < object > PyContext_CopyCurrent ()
22
+ context = copy_current_context ()
21
23
self .context = context
22
24
else :
23
25
if context is not None :
@@ -179,7 +181,7 @@ cdef class TimerHandle:
179
181
180
182
if PY37:
181
183
if context is None :
182
- context = < object > PyContext_CopyCurrent ()
184
+ context = copy_current_context ()
183
185
self .context = context
184
186
else :
185
187
if context is not None :
@@ -400,3 +402,15 @@ cdef extract_stack():
400
402
401
403
stack.reverse()
402
404
return stack
405
+
406
+
407
+ cdef copy_current_context():
408
+ cdef PyContext* current_context
409
+
410
+ if PY37:
411
+ current_context = PyContext_CopyCurrent()
412
+ py_context = < object > current_context
413
+ Py_XDECREF(< PyObject* > current_context)
414
+ return py_context
415
+
416
+ raise NotImplementedError (' "contextvars" support requires Python 3.7+' )
You can’t perform that action at this time.
0 commit comments