Rework flow of set, replace et al.

This should make it easier to read as usual, but should also fix a
potential memory leak.
This commit is contained in:
lericson 2009-09-07 02:19:31 +02:00
parent e0af127fe6
commit 3c7fc65e38

View File

@ -187,21 +187,26 @@ static PyObject *_PylibMC_RunSetCommand(PylibMC_Client *self,
_PylibMC_SetCommand f, char *fname, PyObject *args, _PylibMC_SetCommand f, char *fname, PyObject *args,
PyObject *kwds) { PyObject *kwds) {
char *key; char *key;
int key_len; size_t key_sz;
PyObject *val, *retval; memcached_return rc;
unsigned int time; PyObject *val;
static char *kws[] = { "key", "val", "time", NULL }; PyObject *retval = NULL;
retval = NULL;
time = 0;
if (PyArg_ParseTupleAndKeywords(args, kwds, "s#O|I", kws,
&key, &key_len, &val, &time)) {
PyObject *store_val = NULL; PyObject *store_val = NULL;
unsigned int time = 0;
uint32_t store_flags = 0; uint32_t store_flags = 0;
if (!_PylibMC_CheckKeyStringAndSize(key, key_len)) { static char *kws[] = { "key", "val", "time", NULL };
/* Let store_val be NULL, thus triggering an error. */
} else if (PyString_Check(val)) { if (!PyArg_ParseTupleAndKeywords(args, kwds, "s#O|I", kws,
&key, &key_sz, &val, &time)) {
return NULL;
}
if (!_PylibMC_CheckKeyStringAndSize(key, key_sz)) {
return NULL;
}
/* Adapt val to a str. */
if (PyString_Check(val)) {
store_val = val; store_val = val;
Py_INCREF(store_val); Py_INCREF(store_val);
} else if (PyBool_Check(val)) { } else if (PyBool_Check(val)) {
@ -219,17 +224,15 @@ static PyObject *_PylibMC_RunSetCommand(PylibMC_Client *self,
store_val = _PylibMC_Pickle(val); store_val = _PylibMC_Pickle(val);
Py_DECREF(val); Py_DECREF(val);
} }
if (store_val == NULL) {
return NULL;
}
if (store_val != NULL) { rc = f(self->mc, key, key_sz,
memcached_return rc; PyString_AS_STRING(store_val), PyString_GET_SIZE(store_val),
const char *raw_val; time, store_flags);
size_t raw_val_len; Py_DECREF(store_val);
raw_val = PyString_AS_STRING(store_val);
raw_val_len = PyString_GET_SIZE(store_val);
rc = f(self->mc, key, key_len, raw_val, raw_val_len, time,
store_flags);
switch (rc) { switch (rc) {
case MEMCACHED_SUCCESS: case MEMCACHED_SUCCESS:
retval = Py_True; retval = Py_True;
@ -244,9 +247,6 @@ static PyObject *_PylibMC_RunSetCommand(PylibMC_Client *self,
default: default:
PylibMC_ErrFromMemcached(self, fname, rc); PylibMC_ErrFromMemcached(self, fname, rc);
} }
Py_DECREF(store_val);
}
}
Py_XINCREF(retval); Py_XINCREF(retval);
return retval; return retval;