From 508aee203d9ad6beb7bdb83a41b08deae6df0146 Mon Sep 17 00:00:00 2001 From: Federico Caselli Date: Thu, 1 Apr 2021 20:33:53 +0200 Subject: [PATCH] Prevent loading contextvars in python 3.6 Fixes: #6166 Change-Id: I1355e9a8b6455ca377892214e9426c8f70441f98 --- doc/build/changelog/unreleased_14/6166.rst | 6 ++++++ lib/sqlalchemy/util/_concurrency_py3k.py | 19 ++++++++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 doc/build/changelog/unreleased_14/6166.rst diff --git a/doc/build/changelog/unreleased_14/6166.rst b/doc/build/changelog/unreleased_14/6166.rst new file mode 100644 index 0000000000..5070304956 --- /dev/null +++ b/doc/build/changelog/unreleased_14/6166.rst @@ -0,0 +1,6 @@ +.. change:: + :tags: bug, asyncio + :tickets: 6166 + + Prevent loading contextvars in python 3.6 when the backport + library is installed. diff --git a/lib/sqlalchemy/util/_concurrency_py3k.py b/lib/sqlalchemy/util/_concurrency_py3k.py index 94f4705d88..b905f903b8 100644 --- a/lib/sqlalchemy/util/_concurrency_py3k.py +++ b/lib/sqlalchemy/util/_concurrency_py3k.py @@ -6,16 +6,21 @@ from typing import Coroutine import greenlet +from . import compat from .. import exc -try: - from contextvars import copy_context as _copy_context - # If greenlet.gr_context is present in current version of greenlet, - # it will be set with a copy of the current context on creation. - # Refs: https://github.com/python-greenlet/greenlet/pull/198 - getattr(greenlet.greenlet, "gr_context") -except (ImportError, AttributeError): +if compat.py37: + try: + from contextvars import copy_context as _copy_context + + # If greenlet.gr_context is present in current version of greenlet, + # it will be set with a copy of the current context on creation. + # Refs: https://github.com/python-greenlet/greenlet/pull/198 + getattr(greenlet.greenlet, "gr_context") + except (ImportError, AttributeError): + _copy_context = None +else: _copy_context = None -- 2.47.2