From 1ba5b87b3306acbf21dabcadaf2d4821b246e8dc Mon Sep 17 00:00:00 2001 From: Thiago Bellini Ribeiro Date: Fri, 4 Feb 2022 00:02:21 -0300 Subject: [PATCH 1/2] Support asyncio views Use contextvars instead of threading.local to support async views. --- reversion/revisions.py | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/reversion/revisions.py b/reversion/revisions.py index 6a8dafd2..79591939 100644 --- a/reversion/revisions.py +++ b/reversion/revisions.py @@ -1,7 +1,7 @@ +from contextvars import ContextVar from collections import namedtuple, defaultdict from contextlib import contextmanager from functools import wraps -from threading import local from django.apps import apps from django.core import serializers from django.core.exceptions import ObjectDoesNotExist @@ -34,23 +34,17 @@ )) -class _Local(local): - - def __init__(self): - self.stack = () - - -_local = _Local() +_stack = ContextVar("reversion-stack", default=[]) def is_active(): - return bool(_local.stack) + return bool(_stack.get()) def _current_frame(): if not is_active(): raise RevisionManagementError("There is no active revision for this thread") - return _local.stack[-1] + return _stack.get()[-1] def _copy_db_versions(db_versions): @@ -79,16 +73,17 @@ def _push_frame(manage_manually, using): db_versions={using: {}}, meta=(), ) - _local.stack += (stack_frame,) + _stack.set(_stack.get() + [stack_frame]) def _update_frame(**kwargs): - _local.stack = _local.stack[:-1] + (_current_frame()._replace(**kwargs),) + _stack.get()[-1] = _current_frame()._replace(**kwargs) def _pop_frame(): prev_frame = _current_frame() - _local.stack = _local.stack[:-1] + stack = _stack.get() + del stack[-1] if is_active(): current_frame = _current_frame() db_versions = { @@ -284,7 +279,7 @@ def _create_revision_context(manage_manually, using, atomic): try: yield # Only save for a db if that's the last stack frame for that db. - if not any(using in frame.db_versions for frame in _local.stack[:-1]): + if not any(using in frame.db_versions for frame in _stack.get()[:-1]): current_frame = _current_frame() _save_revision( versions=current_frame.db_versions[using].values(), From 3be3e51ed20bf3f3db07b069a03923ca889f69b2 Mon Sep 17 00:00:00 2001 From: Thiago Bellini Ribeiro Date: Sat, 12 Feb 2022 19:05:54 -0300 Subject: [PATCH 2/2] Drop support for Python 3.6 Python 3.6 has reached EOL on December 2021. It's time to upgrade! --- .github/workflows/python-package.yml | 4 +--- README.rst | 2 +- setup.py | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index c3008b86..f511f26f 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -41,7 +41,7 @@ jobs: DJANGO_DATABASE_PASSWORD_MYSQL: root strategy: matrix: - python-version: [3.6, 3.7, 3.8, 3.9, '3.10'] + python-version: [3.7, 3.8, 3.9, '3.10'] django-version: - '>=4.0a1,<4.1' - '>=3.2,<4.0' @@ -51,8 +51,6 @@ jobs: - '>=2.1,<2.2' - '>=2.0,<2.1' exclude: - - python-version: 3.6 - django-version: '>=4.0a1,<4.1' - python-version: 3.7 django-version: '>=4.0a1,<4.1' - python-version: '3.10' diff --git a/README.rst b/README.rst index da0ae653..a30ccb5e 100644 --- a/README.rst +++ b/README.rst @@ -11,7 +11,7 @@ version control for model instances. Requirements ============ -- Python 3.6 or later +- Python 3.7 or later - Django 2.0 or later Features diff --git a/setup.py b/setup.py index a0a1953a..8cab0222 100644 --- a/setup.py +++ b/setup.py @@ -37,7 +37,7 @@ def read(filepath): install_requires=[ "django>=2.0", ], - python_requires='>=3.6', + python_requires='>=3.7', classifiers=[ "Development Status :: 5 - Production/Stable", "Environment :: Web Environment", @@ -45,7 +45,6 @@ def read(filepath): "License :: OSI Approved :: BSD License", "Operating System :: OS Independent", "Programming Language :: Python", - 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: 3.9',