Skip to content

Commit 2d12c83

Browse files
Amertz08bhautikpip
andauthored
asyncio.Task.current_task PendingDeprecation fix (#217)
* Deprecation fix * try sys.version_info check * missing invocation * major version check Co-authored-by: Bhautik Pipaliya <[email protected]>
1 parent 3973f9c commit 2d12c83

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

aws_xray_sdk/core/async_context.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import asyncio
2+
import sys
23

34
from .context import Context as _Context
45

6+
_GTE_PY37 = sys.version_info.major == 3 and sys.version_info.minor >= 7
7+
58

69
class AsyncContext(_Context):
710
"""
@@ -47,7 +50,10 @@ def __setattr__(self, name, value):
4750

4851
else:
4952
# 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)
5157
if task is None:
5258
return None
5359

@@ -61,7 +67,10 @@ def __getattribute__(self, item):
6167
# Return references to local objects
6268
return object.__getattribute__(self, item)
6369

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)
6574
if task is None:
6675
return None
6776

@@ -72,7 +81,10 @@ def __getattribute__(self, item):
7281

7382
def clear(self):
7483
# 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)
7688
if task is not None and hasattr(task, 'context'):
7789
task.context.clear()
7890

@@ -91,7 +103,10 @@ def task_factory(loop, coro):
91103
del task._source_traceback[-1] # flake8: noqa
92104

93105
# 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)
95110
if current_task is not None and hasattr(current_task, 'context'):
96111
setattr(task, 'context', current_task.context)
97112

0 commit comments

Comments
 (0)