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