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,65 +187,65 @@ 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;
PyObject *retval = NULL;
PyObject *store_val = NULL;
unsigned int time = 0;
uint32_t store_flags = 0;
static char *kws[] = { "key", "val", "time", NULL }; static char *kws[] = { "key", "val", "time", NULL };
retval = NULL; if (!PyArg_ParseTupleAndKeywords(args, kwds, "s#O|I", kws,
time = 0; &key, &key_sz, &val, &time)) {
if (PyArg_ParseTupleAndKeywords(args, kwds, "s#O|I", kws, return NULL;
&key, &key_len, &val, &time)) { }
PyObject *store_val = NULL; if (!_PylibMC_CheckKeyStringAndSize(key, key_sz)) {
uint32_t store_flags = 0; return NULL;
}
if (!_PylibMC_CheckKeyStringAndSize(key, key_len)) { /* Adapt val to a str. */
/* Let store_val be NULL, thus triggering an error. */ if (PyString_Check(val)) {
} else 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)) { store_flags |= PYLIBMC_FLAG_BOOL;
store_flags |= PYLIBMC_FLAG_BOOL; store_val = PyObject_Str(PyNumber_Int(val));
store_val = PyObject_Str(PyNumber_Int(val)); } else if (PyInt_Check(val)) {
} else if (PyInt_Check(val)) { store_flags |= PYLIBMC_FLAG_INTEGER;
store_flags |= PYLIBMC_FLAG_INTEGER; store_val = PyObject_Str(PyNumber_Int(val));
store_val = PyObject_Str(PyNumber_Int(val)); } else if (PyLong_Check(val)) {
} else if (PyLong_Check(val)) { store_flags |= PYLIBMC_FLAG_LONG;
store_flags |= PYLIBMC_FLAG_LONG; store_val = PyObject_Str(PyNumber_Long(val));
store_val = PyObject_Str(PyNumber_Long(val)); } else {
} else { Py_INCREF(val);
Py_INCREF(val); store_flags |= PYLIBMC_FLAG_PICKLE;
store_flags |= PYLIBMC_FLAG_PICKLE; 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); switch (rc) {
raw_val_len = PyString_GET_SIZE(store_val); case MEMCACHED_SUCCESS:
retval = Py_True;
rc = f(self->mc, key, key_len, raw_val, raw_val_len, time, break;
store_flags); case MEMCACHED_FAILURE:
switch (rc) { case MEMCACHED_NO_KEY_PROVIDED:
case MEMCACHED_SUCCESS: case MEMCACHED_BAD_KEY_PROVIDED:
retval = Py_True; case MEMCACHED_MEMORY_ALLOCATION_FAILURE:
break; case MEMCACHED_NOTSTORED:
case MEMCACHED_FAILURE: retval = Py_False;
case MEMCACHED_NO_KEY_PROVIDED: break;
case MEMCACHED_BAD_KEY_PROVIDED: default:
case MEMCACHED_MEMORY_ALLOCATION_FAILURE: PylibMC_ErrFromMemcached(self, fname, rc);
case MEMCACHED_NOTSTORED:
retval = Py_False;
break;
default:
PylibMC_ErrFromMemcached(self, fname, rc);
}
Py_DECREF(store_val);
}
} }
Py_XINCREF(retval); Py_XINCREF(retval);