diff options
-rw-r--r-- | ChangeLog.rst | 5 | ||||
-rw-r--r-- | pymemcache/client/hash.py | 5 | ||||
-rw-r--r-- | pymemcache/test/test_client_hash.py | 17 |
3 files changed, 22 insertions, 5 deletions
diff --git a/ChangeLog.rst b/ChangeLog.rst index a73b219..cc448d9 100644 --- a/ChangeLog.rst +++ b/ChangeLog.rst @@ -1,6 +1,11 @@ Changelog ========= +Unreleased +---------- +* ``Client.get`` returns the default when using ``ignore_exc`` and if memcached + is unavailable + New in version 3.5.0 -------------------- * Sockets are now closed on ``MemcacheUnexpectedCloseError``. diff --git a/pymemcache/client/hash.py b/pymemcache/client/hash.py index 789f111..72cb9d8 100644 --- a/pymemcache/client/hash.py +++ b/pymemcache/client/hash.py @@ -357,9 +357,8 @@ class HashClient(object): def set(self, key, *args, **kwargs): return self._run_cmd('set', key, False, *args, **kwargs) - def get(self, key, *args, **kwargs): - default = kwargs.get('default', None) - return self._run_cmd('get', key, default, *args, **kwargs) + def get(self, key, default=None, **kwargs): + return self._run_cmd('get', key, default, default=default, **kwargs) def incr(self, key, *args, **kwargs): return self._run_cmd('incr', key, False, *args, **kwargs) diff --git a/pymemcache/test/test_client_hash.py b/pymemcache/test/test_client_hash.py index 514b019..aecc1bd 100644 --- a/pymemcache/test/test_client_hash.py +++ b/pymemcache/test/test_client_hash.py @@ -262,6 +262,19 @@ class TestHashClient(ClientTestMixin, unittest.TestCase): result = client.set('foo', 'bar') assert result is False + def test_no_servers_left_return_positional_default(self): + from pymemcache.client.hash import HashClient + client = HashClient( + [], use_pooling=True, + ignore_exc=True, + timeout=1, connect_timeout=1 + ) + + # Ensure compatibility with clients that pass the default as a + # positional argument + result = client.get('foo', 'default') + assert result == 'default' + def test_no_servers_left_with_set_many(self): from pymemcache.client.hash import HashClient client = HashClient( @@ -381,7 +394,7 @@ class TestHashClient(ClientTestMixin, unittest.TestCase): # Client gets removed because of socket timeout assert ("127.0.0.1", 11211) in client._dead_clients - test_client.get.side_effect = lambda *_: "Some value" + test_client.get.side_effect = lambda *_, **_kw: "Some value" # Client should be retried and brought back assert client.get(b"key") == "Some value" assert ("127.0.0.1", 11211) not in client._dead_clients @@ -400,7 +413,7 @@ class TestHashClient(ClientTestMixin, unittest.TestCase): with pytest.raises(socket.timeout): client.get(b"key", noreply=False) - test_client.get.side_effect = lambda *_: "Some value" + test_client.get.side_effect = lambda *_, **_kw: "Some value" assert client.get(b"key") == "Some value" assert client_patch.call_count == 1 |