diff --git a/CHANGES.rst b/CHANGES.rst index 34c965df..fd71d35f 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -7,6 +7,7 @@ Released 2023-00-00 - Permanent session otherwise empty will not be saved. - Use `secrets` module to generate session identifiers, with 256 bits of entropy (was previously 122). +- Explicitly name support for python-memcached, pylibmc and pymemcache. - Introduce SESSION_KEY_LENGTH to control the length of the session key in bytes, default is 32. - Fix expiry is None bug in SQLAlchemy. - Drop support for Redis < 2.6.12. diff --git a/docs/interfaces.rst b/docs/interfaces.rst index f45d66a4..ac4e3811 100644 --- a/docs/interfaces.rst +++ b/docs/interfaces.rst @@ -25,7 +25,7 @@ Relevant configuration values: :class:`MemcachedSessionInterface` ---------------------------------- -Uses the Memcached as a session backend. (`pylibmc`_ or `memcache`_ required) +Uses the Memcached as a session backend. (`pylibmc`_ or `python-memcached`_ or `pymemcache` required) - SESSION_MEMCACHED diff --git a/requirements/pytest.txt b/requirements/pytest.txt index 74f9ef06..25311c8e 100644 --- a/requirements/pytest.txt +++ b/requirements/pytest.txt @@ -11,7 +11,7 @@ pytest-cov # Requirements for interfaces redis -python3-memcached +python-memcached Flask-SQLAlchemy pymongo diff --git a/src/flask_session/sessions.py b/src/flask_session/sessions.py index 787738ab..3d5efe9e 100644 --- a/src/flask_session/sessions.py +++ b/src/flask_session/sessions.py @@ -226,26 +226,25 @@ class MemcachedSessionInterface(ServerSideSessionInterface): def __init__(self, client, key_prefix, use_signer, permanent, sid_length): if client is None: client = self._get_preferred_memcache_client() - if client is None: - raise RuntimeError("no memcache module found") self.client = client super().__init__(client, key_prefix, use_signer, permanent, sid_length) def _get_preferred_memcache_client(self): - servers = ["127.0.0.1:11211"] - try: - import pylibmc - except ImportError: - pass - else: - return pylibmc.Client(servers) + clients = [ + ("pylibmc", ["127.0.0.1:11211"]), + ("memcache", ["127.0.0.1:11211"]), + ("pymemcache.client.base", "127.0.0.1:11211"), + ] - try: - import memcache - except ImportError: - pass - else: - return memcache.Client(servers) + for module_name, server in clients: + try: + module = __import__(module_name) + ClientClass = getattr(module, "Client") + return ClientClass(server) + except ImportError: + continue + + raise ImportError("No memcache module found") def _get_memcache_timeout(self, timeout): """