First pass of syntax & indent nitpick

This commit is contained in:
lericson 2010-05-10 15:53:44 +02:00
parent e8593ba7c7
commit 53879eb8a9

View File

@ -419,15 +419,12 @@ static PyObject *_PylibMC_RunSetCommandSingle(PylibMC_Client *self,
}
#endif
pylibmc_mset serialized = { NULL, 0,
NULL, 0,
0, PYLIBMC_FLAG_NONE,
NULL, NULL, NULL,
false };
pylibmc_mset serialized = { NULL };
success = _PylibMC_SerializeValue(key, NULL, value, time, &serialized);
if(!success) goto cleanup;
if (!success)
goto cleanup;
success = _PylibMC_RunSetCommand(self, f, fname,
&serialized, 1,
@ -449,7 +446,6 @@ static PyObject *_PylibMC_RunSetCommandMulti(PylibMC_Client* self,
_PylibMC_SetCommand f, char *fname, PyObject* args,
PyObject* kwds) {
/* function called by the set/add/incr/etc commands */
static char *kws[] = { "keys", "key_prefix", "time", "min_compress_len", NULL };
PyObject* keys = NULL;
PyObject* key_prefix = NULL;
unsigned int time = 0;
@ -457,6 +453,8 @@ static PyObject *_PylibMC_RunSetCommandMulti(PylibMC_Client* self,
PyObject * retval = NULL;
size_t idx = 0;
static char *kws[] = { "keys", "key_prefix", "time", "min_compress_len", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|O!II", kws,
&PyDict_Type, &keys,
&PyString_Type, &key_prefix,
@ -475,69 +473,69 @@ static PyObject *_PylibMC_RunSetCommandMulti(PylibMC_Client* self,
size_t nkeys = (size_t)PyDict_Size(keys);
pylibmc_mset* serialized = PyMem_New(pylibmc_mset, nkeys);
if(serialized == NULL) {
if (serialized == NULL) {
goto cleanup;
}
memset((void *)serialized, 0x0, nkeys * sizeof(pylibmc_mset));
/* we're pointing into existing Python memory with the 'key' members
of pylibmc_mset (extracted using PyDict_Next) and during
_PylibMC_RunSetCommand (which uses those same 'key' params, and
potentially points into value string objects too), so we don't
want to go around decrementing any references that risk
destroying the pointed objects until we're done, especially since
we're going to release the GIL while we do the I/O that accesses
that memory. We're assuming that this is safe because Python
strings are immutable */
/**
* We're pointing into existing Python memory with the 'key' members of
* pylibmc_mset (extracted using PyDict_Next) and during
* _PylibMC_RunSetCommand (which uses those same 'key' params, and
* potentially points into value string objects too), so we don't want to
* go around decrementing any references that risk destroying the pointed
* objects until we're done, especially since we're going to release the
* GIL while we do the I/O that accesses that memory. We're assuming that
* this is safe because Python strings are immutable
*/
Py_ssize_t pos = 0; /* PyDict_Next's 'pos' isn't an incrementing index */
idx = 0;
while(PyDict_Next(keys, &pos, &curr_key, &curr_value)) {
for (idx = 0; PyDict_Next(keys, &pos, &curr_key, &curr_value); idx++) {
int success = _PylibMC_SerializeValue(curr_key, key_prefix,
curr_value, time,
&serialized[idx]);
if(!success || PyErr_Occurred() != NULL) {
if (!success || PyErr_Occurred() != NULL) {
/* exception should already be on the stack */
goto cleanup;
}
idx++;
}
if(PyErr_Occurred() != NULL) {
if (PyErr_Occurred() != NULL) {
/* an iteration error of some sort */
goto cleanup;
}
bool allsuccess = _PylibMC_RunSetCommand(self, f, fname, serialized, nkeys, time);
bool allsuccess = _PylibMC_RunSetCommand(self, f, fname,
serialized, nkeys,
time);
if(PyErr_Occurred() != NULL) {
if (PyErr_Occurred() != NULL) {
goto cleanup;
}
/* We're building the return value for set_multi, which is the list
of keys that were not successfully set */
retval = PyList_New(0);
if(retval == NULL || PyErr_Occurred() != NULL) {
goto cleanup;
}
if(!allsuccess) {
for(idx = 0; idx < nkeys; idx++) {
if(!serialized[idx].success) {
if(PyList_Append(retval, serialized[idx].key_obj) != 0) {
/* Return value for set_multi, which is a list
of keys which failed to be set */
if ((retval = PyList_New(0)) == NULL)
return PyErr_NoMemory();
for (idx = 0; !allsuccess && idx < nkeys; idx++) {
if (serialized[idx].success)
continue;
if (PyList_Append(retval, serialized[idx].key_obj) != 0) {
/* Ugh */
Py_DECREF(retval);
retval = NULL;
retval = PyErr_NoMemory();
goto cleanup;
}
}
}
}
cleanup:
if(serialized != NULL) {
for(pos = 0; pos < nkeys; pos++) {
if (serialized != NULL) {
for (pos = 0; pos < nkeys; pos++) {
_PylibMC_FreeMset(&serialized[pos]);
}
PyMem_Free(serialized);
@ -546,23 +544,14 @@ cleanup:
return retval;
}
static void _PylibMC_FreeMset(pylibmc_mset* mset) {
mset->key = NULL;
mset->key_len = 0;
mset->value = NULL;
mset->value_len = 0;
/* if this isn't NULL then we incred it */
static void _PylibMC_FreeMset(pylibmc_mset *mset) {
Py_XDECREF(mset->key_obj);
mset->key_obj = NULL;
/* if this isn't NULL then we built it */
Py_XDECREF(mset->prefixed_key_obj);
mset->prefixed_key_obj = NULL;
/* this is either a string that we created, or a string that we
passed to us. in the latter case, we incred it ourselves, so this
should be safe */
/* Either a ref we own, or a ref passed to us which we borrowed. */
Py_XDECREF(mset->value_obj);
mset->value_obj = NULL;
}
@ -572,7 +561,6 @@ static int _PylibMC_SerializeValue(PyObject* key_obj,
PyObject* value_obj,
time_t time,
pylibmc_mset* serialized) {
/* do the easy bits first */
serialized->time = time;
serialized->success = false;
serialized->flags = PYLIBMC_FLAG_NONE;
@ -583,44 +571,53 @@ static int _PylibMC_SerializeValue(PyObject* key_obj,
return false;
}
/* we need to incr our reference here so that it's guaranteed to
/* We need to incr our reference here so that it's guaranteed to
exist while we release the GIL. Even if we fail after this it
should be decremeneted by pylib_mset_free */
serialized->key_obj = key_obj;
Py_INCREF(key_obj);
serialized->key_obj = key_obj;
/* make the prefixed key if appropriate */
if(key_prefix != NULL) {
if(!_PylibMC_CheckKey(key_prefix)) {
/* Check the key_prefix */
if (key_prefix != NULL) {
if (!_PylibMC_CheckKey(key_prefix)) {
return false;
}
/* we can safely ignore an empty prefix */
if(PyString_Size(key_prefix) > 0) {
/* Ignore empty prefixes */
if (!PyString_Size(key_prefix)) {
key_prefix = NULL;
}
}
/* Make the prefixed key if appropriate */
if (key_prefix != NULL) {
PyObject* prefixed_key_obj = NULL; /* freed by _PylibMC_FreeMset */
prefixed_key_obj = PyString_FromFormat("%s%s",
PyString_AS_STRING(key_prefix),
PyString_AS_STRING(key_obj));
if(prefixed_key_obj == NULL) {
return false;
}
/* check the key and overwrite the C string */
if(!_PylibMC_CheckKey(prefixed_key_obj)
|| PyString_AsStringAndSize(prefixed_key_obj, &serialized->key,
|| PyString_AsStringAndSize(prefixed_key_obj,
&serialized->key,
&serialized->key_len) == -1) {
Py_DECREF(prefixed_key_obj);
return false;
}
serialized->prefixed_key_obj = prefixed_key_obj;
}
}
/* key/key_size should be copasetic, now onto the value */
/* Key/key_size should be harmonized, now onto the value */
PyObject* store_val = NULL;
/* first, build store_val, a Python String object, out of the object
/* First build store_val, a Python String object, out of the object
we were passed */
if (PyString_Check(value_obj)) {
store_val = value_obj;
@ -656,7 +653,7 @@ static int _PylibMC_SerializeValue(PyObject* key_obj,
if(PyString_AsStringAndSize(store_val, &serialized->value,
&serialized->value_len) == -1) {
if(serialized->flags == PYLIBMC_FLAG_NONE) {
/* for some reason we weren't able to extract the value/size
/* For some reason we weren't able to extract the value/size
from a string that we were explicitly passed, that we
INCREF'd above */
Py_DECREF(store_val);
@ -664,7 +661,7 @@ static int _PylibMC_SerializeValue(PyObject* key_obj,
return false;
}
/* so now we have a reference to a string that we may have
/* So now we have a reference to a string that we may have
created. we need that to keep existing while we release the HIL,
so we need to hold the reference, but we need to free it up when
we're done */
@ -686,7 +683,7 @@ static bool _PylibMC_RunSetCommand(PylibMC_Client* self,
Py_BEGIN_ALLOW_THREADS;
for(pos=0; pos < nkeys && !error; pos++) {
for (pos=0; pos < nkeys && !error; pos++) {
pylibmc_mset* mset = &msets[pos];
char* value = mset->value;
@ -698,28 +695,27 @@ static bool _PylibMC_RunSetCommand(PylibMC_Client* self,
size_t compressed_len = 0;
if(min_compress && value_len >= min_compress) {
_PylibMC_Deflate(value, value_len, &compressed_value, &compressed_len);
_PylibMC_Deflate(value, value_len,
&compressed_value, &compressed_len);
}
if(compressed_value != NULL) {
/* will want to change this if this function needs to get back
at the old *value at some point */
/* Will want to change this if this function
* needs to get back at the old *value at some point */
value = compressed_value;
value_len = compressed_len;
flags |= PYLIBMC_FLAG_ZLIB;
}
#endif
/* finally go and call the actual libmemcached function */
/* Finally go and call the actual libmemcached function */
if(mset->key_len == 0) {
/* most other implementations ignore zero-length keys, so
/* Most other implementations ignore zero-length keys, so
we'll just do that */
rc = MEMCACHED_NOTSTORED;
} else {
rc = f(mc,
mset->key, mset->key_len,
value, value_len,
mset->time, flags);
rc = f(mc, mset->key, mset->key_len,
value, value_len, mset->time, flags);
}
#ifdef USE_ZLIB
@ -747,13 +743,13 @@ static bool _PylibMC_RunSetCommand(PylibMC_Client* self,
error = true; /* will break the for loop */
} /* switch */
} /* for */
} /* end for each mset */
Py_END_ALLOW_THREADS;
/* we only return the last return value, even for a _multi
/* We only return the last return value, even for a _multi
operation, but we do set the success on the mset */
if(error) {
if (error) {
PylibMC_ErrFromMemcached(self, fname, rc);
return false;
} else {
@ -857,15 +853,18 @@ static PyObject *_PylibMC_IncrSingle(PylibMC_Client *self,
static PyObject *_PylibMC_IncrMulti(PylibMC_Client *self,
_PylibMC_IncrCommand incr_func,
PyObject *args, PyObject *kwds) {
/* takes an iterable of keys and a single delta (that defaults to 1)
to be applied to all of them. Consider the return value and
exception behaviour to be undocumented: for now it returns None
and throws an exception on an error incrementing any key */
PyObject* keys = NULL;
PyObject* key_prefix = NULL;
PyObject* prefixed_keys = NULL;
PyObject* retval = NULL;
PyObject* iterator = NULL;
/**
* Takes an iterable of keys and a single delta (that defaults to 1)
* to be applied to all keys. Consider the return value and exception
* behaviour to be undocumented, for now it returns None and throws an
* exception on an error incrementing any key
*/
PyObject *keys = NULL;
PyObject *key_prefix = NULL;
PyObject *prefixed_keys = NULL;
PyObject *retval = NULL;
PyObject *iterator = NULL;
unsigned int delta = 1;
static char *kws[] = { "keys", "key_prefix", "delta", NULL };
@ -904,24 +903,27 @@ static PyObject *_PylibMC_IncrMulti(PylibMC_Client *self,
goto cleanup;
}
PyObject* key = NULL;
PyObject *key = NULL;
size_t idx = 0;
/* build our list of keys, prefixed as appropriate, and turn that
into a list of pylibmc_incr objects that can be incred in one
go. We're not going to own references to the prefixed keys: so
that we can free them all at once, we'll give ownership to a list
of them (prefixed_keys) which we'll DECR once at the end */
/**
* Build our list of keys, prefixed as appropriate, and turn that
* into a list of pylibmc_incr objects that can be incred in one
* go. We're not going to own references to the prefixed keys: so
* that we can free them all at once, we'll give ownership to a list
* of them (prefixed_keys) which we'll DECR once at the end
*/
while((key = PyIter_Next(iterator)) != NULL) {
if(!_PylibMC_CheckKey(key)) goto loopcleanup;
if(key_prefix != NULL) {
PyObject* newkey = NULL;
newkey = PyString_FromFormat("%s%s",
PyString_AS_STRING(key_prefix),
PyString_AS_STRING(key));
if(!_PylibMC_CheckKey(newkey)) {
Py_XDECREF(newkey);
goto loopcleanup;
@ -943,17 +945,16 @@ static PyObject *_PylibMC_IncrMulti(PylibMC_Client *self,
key = newkey;
}
if(PyString_AsStringAndSize(key, &incrs[idx].key, &incrs[idx].key_len) == -1)
if(PyString_AsStringAndSize(key, &incrs[idx].key,
&incrs[idx].key_len) == -1)
goto loopcleanup;
incrs[idx].delta = delta;
incrs[idx].incr_func = incr_func;
incrs[idx].result = 0; /* after incring we have no way of knowing
whether the real result is 0 or if the
incr wasn't successful (or if noreply is
set), but since we're not actually
returning the result that's okay for
now */
/* After incring we have no way of knowing whether the real result is 0
* or if the incr wasn't successful (or if noreply is set), but since
* we're not actually returning the result that's okay for now */
incrs[idx].result = 0;
loopcleanup:
Py_DECREF(key);
@ -962,7 +963,7 @@ loopcleanup:
break;
idx++;
}
} /* end each key */
/* iteration error */
if (PyErr_Occurred()) goto cleanup;
@ -976,10 +977,7 @@ loopcleanup:
Py_INCREF(retval);
cleanup:
if(incrs != NULL) {
PyMem_Free(incrs);
}
Py_XDECREF(prefixed_keys);
Py_XDECREF(iterator);
@ -1001,18 +999,18 @@ static PyObject *PylibMC_Client_incr_multi(PylibMC_Client *self, PyObject *args,
return _PylibMC_IncrMulti(self, memcached_increment, args, kwds);
}
static bool _PylibMC_IncrDecr(PylibMC_Client *self, pylibmc_incr *incrs,
size_t nkeys) {
static bool _PylibMC_IncrDecr(PylibMC_Client *self,
pylibmc_incr *incrs, size_t nkeys) {
bool error = false;
memcached_return rc = MEMCACHED_SUCCESS;
_PylibMC_IncrCommand f = NULL;
size_t i;
Py_BEGIN_ALLOW_THREADS;
for(i = 0; i < nkeys && !error; i++) {
for (i = 0; i < nkeys && !error; i++) {
pylibmc_incr *incr = &incrs[i];
uint64_t result = 0;
f = incr->incr_func;
rc = f(self->mc, incr->key, incr->key_len, incr->delta, &result);
if (rc == MEMCACHED_SUCCESS) {
@ -1023,7 +1021,7 @@ static bool _PylibMC_IncrDecr(PylibMC_Client *self, pylibmc_incr *incrs,
}
Py_END_ALLOW_THREADS;
if(error) {
if (error) {
char *fname = (f == memcached_decrement) ? "memcached_decrement"
: "memcached_increment";
PylibMC_ErrFromMemcached(self, fname, rc);