From c3215866c57e1bf26d87d59744d89535c0027548 Mon Sep 17 00:00:00 2001 From: lericson Date: Mon, 22 Feb 2010 16:36:56 +0100 Subject: [PATCH] Fix get_multi with an 0-sequence. Used to give weird errors like NULL return w/o exception etc. Fixes #7. --- _pylibmcmodule.c | 9 +++++++-- tests.py | 4 ++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/_pylibmcmodule.c b/_pylibmcmodule.c index 3b9570c..1cd5b45 100644 --- a/_pylibmcmodule.c +++ b/_pylibmcmodule.c @@ -643,10 +643,13 @@ static PyObject *PylibMC_Client_get_multi(PylibMC_Client *self, PyObject *args, } Py_XDECREF(key_it); - if (i == 0) { - /* No usable keys to fetch. */ + if (nkeys != 0 && i != nkeys) { + /* There were keys given, but some keys didn't pass validation. */ nkeys = 0; goto cleanup; + } else if (nkeys == 0) { + retval = PyDict_New(); + goto earlybird; } else if (PyErr_Occurred()) { nkeys--; goto cleanup; @@ -700,6 +703,7 @@ static PyObject *PylibMC_Client_get_multi(PylibMC_Client *self, PyObject *args, free(curr_val); } +earlybird: PyMem_Free(key_lens); PyMem_Free(keys); for (i = 0; i < nkeys; i++) { @@ -710,6 +714,7 @@ static PyObject *PylibMC_Client_get_multi(PylibMC_Client *self, PyObject *args, /* Not INCREFing because the only two outcomes are NULL and a new dict. * We're the owner of that dict already, so. */ return retval; + cleanup: Py_XDECREF(retval); PyMem_Free(key_lens); diff --git a/tests.py b/tests.py index 283e78b..9aa8285 100644 --- a/tests.py +++ b/tests.py @@ -98,6 +98,10 @@ Traceback (most recent call last): ... TypeError: key must be an instance of str +This didn't use to work, but now it does. +>>> c.get_multi([]) +{} + Getting stats is fun! >>> for (svr, stats) in c.get_stats(): ... print svr