Skip to content

Commit

Permalink
Return default from hash client when using positional argument
Browse files Browse the repository at this point in the history
When using `HashClient` with `ignore_exc`, `get` would always return
`None` if no server is available and the default is passed as a
positional argument. The other clients return the default
value in this case. An earlier fix only had the desired effect when
passing `default` as a keyword argument.

Return the default value so `HashClient` behaves like the other
clients even when using

Fixes another variation of issue pinterest#350
  • Loading branch information
Pankrat committed Oct 7, 2021
1 parent 25e38df commit 73c0fbb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
6 changes: 3 additions & 3 deletions pymemcache/client/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,9 @@ def close(self):
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):
kwargs['default'] = default
return self._run_cmd('get', key, default, **kwargs)

def incr(self, key, *args, **kwargs):
return self._run_cmd('incr', key, False, *args, **kwargs)
Expand Down
6 changes: 4 additions & 2 deletions pymemcache/test/test_client_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ def test_no_servers_left_with_commands_return_default_value(self):
assert result is None
result = client.get('foo', default='default')
assert result == 'default'
result = client.get('foo', 'default')
assert result == 'default'
result = client.set('foo', 'bar')
assert result is False

Expand Down Expand Up @@ -381,7 +383,7 @@ def test_dead_server_comes_back(self, client_patch):
# 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
Expand All @@ -400,7 +402,7 @@ def test_failed_is_retried(self, client_patch):
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
Expand Down

0 comments on commit 73c0fbb

Please sign in to comment.