Skip to content

Commit c9a1626

Browse files
committed
Fixed return value from delete command.
The `delete()` command currently returns `1` if the response from the server is either `'DELETED'` or `'NOT_FOUND'`. This differs from other implementations, e.g. `pymemcache` and `pylibmc`, and doesn't seem to make sense given that these are the only two documented return types from the protocol for the delete command. It seems that `delete()` was changed to explicitly consider both responses as successful way back in 2010, but that change only seemed to double down on the behavior that was being reported in the issue. See https://bugs.launchpad.net/python-memcached/+bug/471727. The concern was avoiding breaking backward compatibility. It was possible to work around this by calling `_deletetouch()` and passing in the `expected` responses, but that was broken by the change to remove `time` in ab668ed. Making the change to interpret `'NOT_FOUND'` as `0` doesn't cause any test failures, is consistent with the docstring which states that that the command returns non-zero on success, and also brings consistency with other implementations. Fixes #170.
1 parent deac889 commit c9a1626

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

memcache.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,9 @@ def delete(self, key, noreply=False):
552552
if noreply:
553553
return 1
554554
line = server.readline()
555-
if line and line.strip() in [b'DELETED', b'NOT_FOUND']:
555+
if line and line.strip() == b'DELETED':
556556
return 1
557-
self.debuglog('delete expected DELETED or NOT_FOUND, got: {!r}'.format(line))
557+
self.debuglog('delete expected DELETED, got: {!r}'.format(line))
558558
except OSError as msg:
559559
if isinstance(msg, tuple):
560560
msg = msg[1]

tests/test_memcache.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ def test_delete(self):
5656
result = self.mc.delete("long")
5757
self.assertEqual(result, True)
5858
self.assertEqual(self.mc.get("long"), None)
59+
result = self.mc.delete("<missing>")
60+
self.assertEqual(result, False)
5961

6062
def test_default(self):
6163
key = "default"

0 commit comments

Comments
 (0)