1
1
import asyncio
2
+ import sys
2
3
3
4
from .context import Context as _Context
4
5
6
+ _GTE_PY37 = sys .version_info .major == 3 and sys .version_info .minor >= 7
7
+
5
8
6
9
class AsyncContext (_Context ):
7
10
"""
@@ -47,7 +50,10 @@ def __setattr__(self, name, value):
47
50
48
51
else :
49
52
# Set task local attributes
50
- task = asyncio .Task .current_task (loop = self ._loop )
53
+ if _GTE_PY37 :
54
+ task = asyncio .current_task (loop = self ._loop )
55
+ else :
56
+ task = asyncio .Task .current_task (loop = self ._loop )
51
57
if task is None :
52
58
return None
53
59
@@ -61,7 +67,10 @@ def __getattribute__(self, item):
61
67
# Return references to local objects
62
68
return object .__getattribute__ (self , item )
63
69
64
- task = asyncio .Task .current_task (loop = self ._loop )
70
+ if _GTE_PY37 :
71
+ task = asyncio .current_task (loop = self ._loop )
72
+ else :
73
+ task = asyncio .Task .current_task (loop = self ._loop )
65
74
if task is None :
66
75
return None
67
76
@@ -72,7 +81,10 @@ def __getattribute__(self, item):
72
81
73
82
def clear (self ):
74
83
# If were in a task, clear the context dictionary
75
- task = asyncio .Task .current_task (loop = self ._loop )
84
+ if _GTE_PY37 :
85
+ task = asyncio .current_task (loop = self ._loop )
86
+ else :
87
+ task = asyncio .Task .current_task (loop = self ._loop )
76
88
if task is not None and hasattr (task , 'context' ):
77
89
task .context .clear ()
78
90
@@ -91,7 +103,10 @@ def task_factory(loop, coro):
91
103
del task ._source_traceback [- 1 ] # flake8: noqa
92
104
93
105
# Share context with new task if possible
94
- current_task = asyncio .Task .current_task (loop = loop )
106
+ if _GTE_PY37 :
107
+ current_task = asyncio .current_task (loop = loop )
108
+ else :
109
+ current_task = asyncio .Task .current_task (loop = loop )
95
110
if current_task is not None and hasattr (current_task , 'context' ):
96
111
setattr (task , 'context' , current_task .context )
97
112
0 commit comments