Skip to content

asyncio.Task.current_task PendingDeprecation fix #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions aws_xray_sdk/core/async_context.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import asyncio
import sys

from .context import Context as _Context

_GTE_PY37 = sys.version_info.major == 3 and sys.version_info.minor >= 7

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also add condition which checks sys.version_info.major is 2 and 3 because in the case of major version 4.0 above condition would break.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a guard against 4? I think we do need to make sure its python3. I forgot python 2.7 had asyncio support. So I assumed no python2 code would call this code and if it did it would error for other reasons.

So basically just this.

_GTE_PY37 = sys.version_info.major == 3 and  sys.version_info.minor >= 7

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. I think this makes sense.


class AsyncContext(_Context):
"""
Expand Down Expand Up @@ -47,7 +50,10 @@ def __setattr__(self, name, value):

else:
# Set task local attributes
task = asyncio.Task.current_task(loop=self._loop)
if _GTE_PY37:
task = asyncio.current_task(loop=self._loop)
else:
task = asyncio.Task.current_task(loop=self._loop)
if task is None:
return None

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

task = asyncio.Task.current_task(loop=self._loop)
if _GTE_PY37:
task = asyncio.current_task(loop=self._loop)
else:
task = asyncio.Task.current_task(loop=self._loop)
if task is None:
return None

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

def clear(self):
# If were in a task, clear the context dictionary
task = asyncio.Task.current_task(loop=self._loop)
if _GTE_PY37:
task = asyncio.current_task(loop=self._loop)
else:
task = asyncio.Task.current_task(loop=self._loop)
if task is not None and hasattr(task, 'context'):
task.context.clear()

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

# Share context with new task if possible
current_task = asyncio.Task.current_task(loop=loop)
if _GTE_PY37:
current_task = asyncio.current_task(loop=loop)
else:
current_task = asyncio.Task.current_task(loop=loop)
if current_task is not None and hasattr(current_task, 'context'):
setattr(task, 'context', current_task.context)

Expand Down