From 8bd78fe80234a9aba4d22391937f22ea712cbb4d Mon Sep 17 00:00:00 2001 From: Eduardo Schettino Date: Tue, 12 Apr 2022 23:30:42 +0800 Subject: [PATCH 1/2] Fix for Werkzeug 2.1. --- CHANGELOG.rst | 6 ++++++ flask_sqlalchemy_session/__init__.py | 13 +------------ 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 627c122..9a3e0c1 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,12 @@ Changelog ========= +Version 2.0 +----------- + +- Add support for Werkzeug 2.1. + + Version 1.1 ----------- diff --git a/flask_sqlalchemy_session/__init__.py b/flask_sqlalchemy_session/__init__.py index d19865f..0b6914b 100644 --- a/flask_sqlalchemy_session/__init__.py +++ b/flask_sqlalchemy_session/__init__.py @@ -48,18 +48,7 @@ def __init__(self, session_factory, app=None): :class:`~sqlalchemy.orm.session.Session` :param app: a :class:`~flask.Flask` application """ - super(flask_scoped_session, self).__init__( - session_factory, - scopefunc=_app_ctx_stack.__ident_func__) - # the _app_ctx_stack.__ident_func__ is the greenlet.get_current, or - # thread.get_ident if no greenlets are used. - # each Flask request is launched in a seperate greenlet/thread, so our - # session is unique per request - # _app_ctx_stack looks like internal API but is the only way to get to - # the active application context without adding logic to figure out - # whether threads, greenlets, or something else is used to create new - # application contexts. Keep in mind to refactor if Flask changes its - # public/private API towards this. + super(flask_scoped_session, self).__init__(session_factory) if app is not None: self.init_app(app) From 57bd6547f5c07ff1dd4ab6c9ff8d1d40107485f9 Mon Sep 17 00:00:00 2001 From: Eduardo Schettino Date: Mon, 25 Apr 2022 21:38:22 +0800 Subject: [PATCH 2/2] scopedsession supports greenlet as well. --- flask_sqlalchemy_session/__init__.py | 6 +++++- setup.py | 15 +++++++-------- tests/test_flask_sqlalchemy_session.py | 6 ++---- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/flask_sqlalchemy_session/__init__.py b/flask_sqlalchemy_session/__init__.py index 0b6914b..5bb7a56 100644 --- a/flask_sqlalchemy_session/__init__.py +++ b/flask_sqlalchemy_session/__init__.py @@ -48,7 +48,11 @@ def __init__(self, session_factory, app=None): :class:`~sqlalchemy.orm.session.Session` :param app: a :class:`~flask.Flask` application """ - super(flask_scoped_session, self).__init__(session_factory) + try: + from greenlet import getcurrent as scopefunc + except ImportError: + scopefunc = None # let sqlalchemy used default Threading + super(flask_scoped_session, self).__init__(session_factory, scopefunc=scopefunc) if app is not None: self.init_app(app) diff --git a/setup.py b/setup.py index ef5b31c..2bbfef9 100644 --- a/setup.py +++ b/setup.py @@ -10,9 +10,6 @@ import os from setuptools import setup -if sys.version_info < (2, 6): - raise Exception("Flask-SQLAlchemy-Session requires Python 2.6 or higher.") - # Hard linking doesn't work inside VirtualBox shared folders. This means that # you can't use tox in a directory that is being shared with Vagrant, # since tox relies on `python setup.py sdist` which uses hard links. As a @@ -38,14 +35,16 @@ 'Intended Audience :: Developers', 'Environment :: Web Environment', 'License :: OSI Approved :: MIT License', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: 3.9', + 'Programming Language :: Python :: 3.10', 'Topic :: Internet :: WWW/HTTP :: Dynamic Content', 'Topic :: Software Development :: Libraries :: Python Modules' ], - install_requires=["sqlalchemy>=0.9", "Flask>=0.9", "Werkzeug>=0.6.1"], - tests_require=["pytest>=2.6", "mock>=1.0"], + install_requires=["sqlalchemy", "Flask", "Werkzeug"], + tests_require=["pytest"], extras_require={ - 'docs': ["Sphinx>=1.2.3", "alabaster>=0.6.3"] + 'docs': ["Sphinx", "alabaster"] } ) diff --git a/tests/test_flask_sqlalchemy_session.py b/tests/test_flask_sqlalchemy_session.py index 6b7af52..119a1a3 100644 --- a/tests/test_flask_sqlalchemy_session.py +++ b/tests/test_flask_sqlalchemy_session.py @@ -1,9 +1,7 @@ # -*- coding: utf-8 -*- +from unittest import mock + import pytest -try: - from unittest import mock -except ImportError: - import mock from flask import Flask from sqlalchemy import create_engine from sqlalchemy.orm import Session, sessionmaker