From 343286a34ba1f4c9f7e6d0962233493fe24bafe5 Mon Sep 17 00:00:00 2001 From: lericson Date: Sat, 31 Oct 2009 11:22:18 +0100 Subject: [PATCH] Fix empty memcached value exception bug. pylibmc would produce non-error exceptions because the memcached value pointer would be NULL, while the return code would be zero. --- _pylibmcmodule.c | 7 ++++--- tests.py | 6 ++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/_pylibmcmodule.c b/_pylibmcmodule.c index 1f812e4..674d97f 100644 --- a/_pylibmcmodule.c +++ b/_pylibmcmodule.c @@ -195,9 +195,10 @@ static PyObject *PylibMC_Client_get(PylibMC_Client *self, PyObject *arg) { PyObject *r = _PylibMC_parse_memcached_value(mc_val, val_size, flags); free(mc_val); return r; - } - - if (error == MEMCACHED_NOTFOUND) { + } else if (error == MEMCACHED_SUCCESS) { + /* This happens for empty values, and so we fake an empty string. */ + return PyString_FromStringAndSize("", 0); + } else if (error == MEMCACHED_NOTFOUND) { /* Since python-memcache returns None when the key doesn't exist, * so shall we. */ Py_RETURN_NONE; diff --git a/tests.py b/tests.py index 96e60ca..a545900 100644 --- a/tests.py +++ b/tests.py @@ -14,6 +14,12 @@ True >>> c.get("test_key") >>> +We should handle empty values just nicely. +>>> c.set("foo", "") +True +>>> c.get("foo") +'' + Now this section is because most other implementations ignore zero-keys. >>> c.get("") >>> c.set("", "hi")