/**
*******************************************************************************************************
* This function will get a batch of records from the Aeropike DB.
*
* @param err as_error object
* @param self AerospikeClient object
* @param py_keys The list of keys
* @param batch_policy_p as_policy_batch object
*
* Returns the record if key exists otherwise NULL.
*******************************************************************************************************
*/
static PyObject * batch_select_aerospike_batch_get(as_error *err, AerospikeClient * self, PyObject *py_keys, as_policy_batch * batch_policy_p, char **filter_bins, Py_ssize_t bins_size)
{
PyObject * py_recs = NULL;
as_batch batch;
bool batch_initialised = false;
// Convert python keys list to as_key ** and add it to as_batch.keys
// keys can be specified in PyList or PyTuple
if ( py_keys != NULL && PyList_Check(py_keys) ) {
Py_ssize_t size = PyList_Size(py_keys);
py_recs = PyList_New(size);
as_batch_init(&batch, size);
// Batch object initialised
batch_initialised = true;
for ( int i = 0; i < size; i++ ) {
PyObject * py_key = PyList_GetItem(py_keys, i);
if ( !PyTuple_Check(py_key) ) {
as_error_update(err, AEROSPIKE_ERR_PARAM, "Key should be a tuple.");
goto CLEANUP;
}
pyobject_to_key(err, py_key, as_batch_keyat(&batch, i));
if ( err->code != AEROSPIKE_OK ) {
goto CLEANUP;
}
}
}
else if ( py_keys != NULL && PyTuple_Check(py_keys) ) {
Py_ssize_t size = PyTuple_Size(py_keys);
py_recs = PyList_New(size);
as_batch_init(&batch, size);
// Batch object initialised
batch_initialised = true;
for ( int i = 0; i < size; i++ ) {
PyObject * py_key = PyTuple_GetItem(py_keys, i);
if ( !PyTuple_Check(py_key) ) {
as_error_update(err, AEROSPIKE_ERR_PARAM, "Key should be a tuple.");
goto CLEANUP;
}
pyobject_to_key(err, py_key, as_batch_keyat(&batch, i));
if ( err->code != AEROSPIKE_OK ) {
goto CLEANUP;
}
}
}
else {
as_error_update(err, AEROSPIKE_ERR_PARAM, "Keys should be specified as a list or tuple.");
goto CLEANUP;
}
// Invoke C-client API
aerospike_batch_get_bins(self->as, err, batch_policy_p,
&batch, (const char **) filter_bins, bins_size,
(aerospike_batch_read_callback) batch_select_cb,
py_recs);
CLEANUP:
if (batch_initialised == true) {
// We should destroy batch object as we are using 'as_batch_init' for initialisation
// Also, pyobject_to_key is soing strdup() in case of Unicode. So, object destruction
// is necessary.
as_batch_destroy(&batch);
}
return py_recs;
}
/**
* pyg_flags_get_value:
* @flag_type: the GType of the flag.
* @obj: a Python object representing the flag value
* @val: a pointer to the location to store the integer representation of the flag.
*
* Converts a Python object to the integer equivalent. The conversion
* will depend on the type of the Python object. If the object is an
* integer, it is passed through directly. If it is a string, it will
* be treated as a full or short flag name as defined in the GType.
* If it is a tuple, then the items are treated as strings and ORed
* together.
*
* Returns: 0 on success or -1 on failure
*/
gint
pyg_flags_get_value(GType flag_type, PyObject *obj, gint *val)
{
GFlagsClass *fclass = NULL;
gint res = -1;
g_return_val_if_fail(val != NULL, -1);
if (!obj) {
*val = 0;
res = 0;
} else if (PYGLIB_PyLong_Check(obj)) {
*val = PYGLIB_PyLong_AsLong(obj);
res = 0;
} else if (PyLong_Check(obj)) {
*val = PyLong_AsLongLong(obj);
res = 0;
} else if (PYGLIB_PyUnicode_Check(obj)) {
GFlagsValue *info;
char *str = PYGLIB_PyUnicode_AsString(obj);
if (flag_type != G_TYPE_NONE)
fclass = G_FLAGS_CLASS(g_type_class_ref(flag_type));
else {
PyErr_SetString(PyExc_TypeError, "could not convert string to flag because there is no GType associated to look up the value");
res = -1;
}
info = g_flags_get_value_by_name(fclass, str);
g_type_class_unref(fclass);
if (!info)
info = g_flags_get_value_by_nick(fclass, str);
if (info) {
*val = info->value;
res = 0;
} else {
PyErr_SetString(PyExc_TypeError, "could not convert string");
res = -1;
}
} else if (PyTuple_Check(obj)) {
int i, len;
len = PyTuple_Size(obj);
*val = 0;
res = 0;
if (flag_type != G_TYPE_NONE)
fclass = G_FLAGS_CLASS(g_type_class_ref(flag_type));
else {
PyErr_SetString(PyExc_TypeError, "could not convert string to flag because there is no GType associated to look up the value");
res = -1;
}
for (i = 0; i < len; i++) {
PyObject *item = PyTuple_GetItem(obj, i);
char *str = PYGLIB_PyUnicode_AsString(item);
GFlagsValue *info = g_flags_get_value_by_name(fclass, str);
if (!info)
info = g_flags_get_value_by_nick(fclass, str);
if (info) {
*val |= info->value;
} else {
PyErr_SetString(PyExc_TypeError, "could not convert string");
res = -1;
break;
}
}
g_type_class_unref(fclass);
} else {
PyErr_SetString(PyExc_TypeError,
"flag values must be strings, ints, longs, or tuples");
res = -1;
}
return res;
}
开发者ID:nzjrs,项目名称:pygobject,代码行数:90,代码来源:pygtype.c
示例5: switch
static PyObject *swi_swi(PyObject *self,PyObject *args)
{ PyObject *name,*format,*result,*v;
int swino,carry,rno=0,j,n;
char *swiname,*fmt,*outfmt;
_kernel_swi_regs r;
PyBlockObject *ao;
if(args==NULL||!PyTuple_Check(args)||(n=PyTuple_Size(args))<2)
{ PyErr_BadArgument(); return NULL;}
name=PyTuple_GetItem(args,0);
if(!PyArg_Parse(name,"i",&swino))
{ PyErr_Clear();
if(!PyArg_Parse(name,"s",&swiname)) return NULL;
e=xos_swi_number_from_string(swiname,&swino);
if(e) return swi_oserror();
}
format=PyTuple_GetItem(args,1);
if(!PyArg_Parse(format,"s",&fmt)) return NULL;
j=2;
for(;;fmt++)
{ switch(*fmt)
{ case '.': rno++;continue;
case ';':case 0: goto swicall;
case '0':case '1':case '2':case '3':case '4':
case '5':case '6':case '7':case '8':case '9':
r.r[rno++]=*fmt-'0';continue;
case '-':r.r[rno++]=-1;continue;
}
if(j>=n) return swi_error("Too few arguments");
v=PyTuple_GetItem(args,j++);
switch(*fmt)
{ case 'i':if(!PyArg_Parse(v,"i",&r.r[rno])) return NULL;
break;
case 's':if(!PyArg_Parse(v,"s",(char**)(&r.r[rno]))) return NULL;
break;
case 'b':if(!PyArg_Parse(v,"O",(PyObject**)&ao)) return NULL;
if(!PyBlock_Check(v)) return swi_error("Not a block");
r.r[rno]=(int)(ao->block);
break;
case 'e':if(!PyArg_Parse(v,"O",(PyObject**)&ao)) return NULL;
if(!PyBlock_Check(v)) return swi_error("Not a block");
r.r[rno]=(int)(ao->block)+ao->length;
break;
default:return swi_error("Odd format character");
}
rno++;
}
swicall:e=(os_error*)_kernel_swi_c(swino,&r,&r,&carry);
if(e) return swi_oserror();
if(*fmt==0) { Py_INCREF(Py_None);return Py_None;}
n=0;
for(outfmt=++fmt;*outfmt;outfmt++) switch(*outfmt)
{ case 'i':case 's':case '*':n++;break;
case '.':break;
default:return swi_error("Odd format character");
}
if(n==0) { Py_INCREF(Py_None);return Py_None;}
if(n!=1)
{ result=PyTuple_New(n);
if(!result) return NULL;
}
rno=0;j=0;
for(;*fmt;fmt++)
{ switch(*fmt)
{ case 'i':v=PyInt_FromLong((long)r.r[rno++]); break;
case 's':v=PyString_FromString((char*)(r.r[rno++])); break;
case '.':rno++; continue;
case '*':v=PyInt_FromLong((long)carry); break;
}
if(!v) goto fail;
if(n==1) return v;
PyTuple_SetItem(result,j,v);
j++;
}
return result;
fail:Py_DECREF(result);return 0;
}
static PyObject *
tuplerichcompare(PyObject *v, PyObject *w, int op)
{
PyTupleObject *vt, *wt;
Py_ssize_t i;
Py_ssize_t vlen, wlen;
if (!PyTuple_Check(v) || !PyTuple_Check(w))
Py_RETURN_NOTIMPLEMENTED;
vt = (PyTupleObject *)v;
wt = (PyTupleObject *)w;
vlen = Py_SIZE(vt);
wlen = Py_SIZE(wt);
/* Note: the corresponding code for lists has an "early out" test
* here when op is EQ or NE and the lengths differ. That pays there,
* but Tim was unable to find any real code where EQ/NE tuple
* compares don't have the same length, so testing for it here would
* have cost without benefit.
*/
/* Search for the first index where items are different.
* Note that because tuples are immutable, it's safe to reuse
* vlen and wlen across the comparison calls.
*/
for (i = 0; i < vlen && i < wlen; i++) {
int k = PyObject_RichCompareBool(vt->ob_item[i],
wt->ob_item[i], Py_EQ);
if (k < 0)
return NULL;
if (!k)
break;
}
if (i >= vlen || i >= wlen) {
/* No more items to compare -- compare sizes */
int cmp;
PyObject *res;
switch (op) {
case Py_LT: cmp = vlen < wlen; break;
case Py_LE: cmp = vlen <= wlen; break;
case Py_EQ: cmp = vlen == wlen; break;
case Py_NE: cmp = vlen != wlen; break;
case Py_GT: cmp = vlen > wlen; break;
case Py_GE: cmp = vlen >= wlen; break;
default: return NULL; /* cannot happen */
}
if (cmp)
res = Py_True;
else
res = Py_False;
Py_INCREF(res);
return res;
}
/* We have an item that differs -- shortcuts for EQ/NE */
if (op == Py_EQ) {
Py_INCREF(Py_False);
return Py_False;
}
if (op == Py_NE) {
Py_INCREF(Py_True);
return Py_True;
}
/* Compare the final item again using the proper operator */
return PyObject_RichCompare(vt->ob_item[i], wt->ob_item[i], op);
}
// The type init slot.
static int pyqtSignal_init(PyObject *self, PyObject *args, PyObject *kwd_args)
{
qpycore_pyqtSignal *ps = (qpycore_pyqtSignal *)self;
// Get the keyword arguments.
PyObject *name_obj = 0;
const char *name = 0;
if (kwd_args)
{
SIP_SSIZE_T pos = 0;
PyObject *key, *value;
while (PyDict_Next(kwd_args, &pos, &key, &value))
{
#if PY_MAJOR_VERSION >= 3
if (PyUnicode_CompareWithASCIIString(key, "name") != 0)
{
PyErr_Format(PyExc_TypeError,
"pyqtSignal() got an unexpected keyword argument '%U'",
key);
Py_XDECREF(name_obj);
return -1;
}
#else
Q_ASSERT(PyString_Check(key));
if (qstrcmp(PyString_AS_STRING(key), "name") != 0)
{
PyErr_Format(PyExc_TypeError,
"pyqtSignal() got an unexpected keyword argument '%s'",
PyString_AS_STRING(key));
Py_XDECREF(name_obj);
return -1;
}
#endif
name_obj = value;
name = sipString_AsASCIIString(&name_obj);
if (!name)
return -1;
}
}
// If there is at least one argument and it is a sequence then assume all
// arguments are sequences. Unfortunately a string is also a sequence so
// check for tuples and lists explicitly.
if (PyTuple_GET_SIZE(args) > 0 && (PyTuple_Check(PyTuple_GET_ITEM(args, 0)) || PyList_Check(PyTuple_GET_ITEM(args, 0))))
{
for (SIP_SSIZE_T i = 0; i < PyTuple_GET_SIZE(args); ++i)
{
PyObject *types = PySequence_Tuple(PyTuple_GET_ITEM(args, i));
if (!types)
{
PyErr_SetString(PyExc_TypeError,
"pyqtSignal() argument expected to be sequence of types");
if (name)
{
Py_DECREF(name_obj);
}
return -1;
}
int rc;
if (i == 0)
{
// The first is the default.
rc = init_signal_from_types(ps, name, types);
}
else
{
qpycore_pyqtSignal *overload = (qpycore_pyqtSignal *)PyType_GenericNew(&qpycore_pyqtSignal_Type, 0, 0);
if (!overload)
{
rc = -1;
}
else if ((rc = init_signal_from_types(overload, name, types)) < 0)
{
Py_DECREF((PyObject *)overload);
}
else
{
overload->default_signal = ps;
append_overload(overload);
}
}
Py_DECREF(types);
if (rc < 0)
{
//.........这里部分代码省略.........
/**
*********************************************************************
* This function will invoke aerospike_batch_get_bins to get filtered
* bins from all the records in a batches.
*
* @param self AerospikeClient object
* @param py_keys List of keys passed on by user
* @param py_bins List of filter bins passed on by user
* @param py_policy User specified Policy dictionary
*
*********************************************************************
**/
static
PyObject * AerospikeClient_Select_Many_Invoke(
AerospikeClient * self,
PyObject * py_keys, PyObject * py_bins, PyObject * py_policy)
{
// Python Return Value
PyObject * py_recs = NULL;
// Aerospike Client Arguments
as_error err;
as_policy_batch policy;
as_policy_batch * batch_policy_p = NULL;
Py_ssize_t bins_size = 0;
char **filter_bins = NULL;
bool has_batch_index = false;
// Unicode object's pool
UnicodePyObjects u_objs;
u_objs.size = 0;
int i = 0;
// Initialize error
as_error_init(&err);
if (!self || !self->as) {
as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid aerospike object");
goto CLEANUP;
}
if (!self->is_conn_16) {
as_error_update(&err, AEROSPIKE_ERR_CLUSTER, "No connection to aerospike cluster");
goto CLEANUP;
}
// Check the type of bins and get it's size
// i.e. number of bins provided
if (py_bins != NULL && PyList_Check(py_bins)) {
bins_size = PyList_Size(py_bins);
}
else if (py_bins != NULL && PyTuple_Check(py_bins)) {
bins_size = PyTuple_Size(py_bins);
}
else {
as_error_update(&err, AEROSPIKE_ERR_PARAM, "Filter bins should be specified as a list or tuple.");
goto CLEANUP;
}
filter_bins = (char **)malloc(sizeof(long int) * bins_size);
for (i = 0; i < bins_size; i++) {
PyObject *py_bin = NULL;
if(PyList_Check(py_bins)) {
py_bin = PyList_GetItem(py_bins, i);
}
if(PyTuple_Check(py_bins)) {
py_bin = PyTuple_GetItem(py_bins, i);
}
if (PyUnicode_Check(py_bin)) {
// Store the unicode object into a pool
// It is DECREFed at later stages
// So, no need of DECREF here.
filter_bins[i] = PyStr_AsString(
store_unicode_bins(&u_objs, PyUnicode_AsUTF8String(py_bin)));
}
else if (PyStr_Check(py_bin)) {
filter_bins[i] = PyStr_AsString(py_bin);
}
else {
as_error_update(&err, AEROSPIKE_ERR_PARAM, "Bin name should be a string or unicode string.");
goto CLEANUP;
}
}
// Convert python policy object to as_policy_batch
pyobject_to_policy_batch(&err, py_policy, &policy, &batch_policy_p,
&self->as->config.policies.batch);
if ( err.code != AEROSPIKE_OK ) {
goto CLEANUP;
}
has_batch_index = aerospike_has_batch_index(self->as);
if (has_batch_index) {
py_recs = batch_select_aerospike_batch_read(&err, self, py_keys, batch_policy_p, filter_bins, bins_size);
} else {
py_recs = batch_select_aerospike_batch_get(&err, self, py_keys, batch_policy_p, filter_bins, bins_size);
}
CLEANUP:
//.........这里部分代码省略.........
/* Used in many places to normalize a raised exception, including in
eval_code2(), do_raise(), and PyErr_Print()
XXX: should PyErr_NormalizeException() also call
PyException_SetTraceback() with the resulting value and tb?
*/
void
PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
{
PyObject *type = *exc;
PyObject *value = *val;
PyObject *inclass = NULL;
PyObject *initial_tb = NULL;
PyThreadState *tstate = NULL;
if (type == NULL) {
/* There was no exception, so nothing to do. */
return;
}
/* If PyErr_SetNone() was used, the value will have been actually
set to NULL.
*/
if (!value) {
value = Py_None;
Py_INCREF(value);
}
if (PyExceptionInstance_Check(value))
inclass = PyExceptionInstance_Class(value);
/* Normalize the exception so that if the type is a class, the
value will be an instance.
*/
if (PyExceptionClass_Check(type)) {
int is_subclass;
if (inclass) {
is_subclass = PyObject_IsSubclass(inclass, type);
if (is_subclass < 0)
goto finally;
}
else
is_subclass = 0;
/* if the value was not an instance, or is not an instance
whose class is (or is derived from) type, then use the
value as an argument to instantiation of the type
class.
*/
if (!inclass || !is_subclass) {
PyObject *args, *res;
if (value == Py_None)
args = PyTuple_New(0);
else if (PyTuple_Check(value)) {
Py_INCREF(value);
args = value;
}
else
args = PyTuple_Pack(1, value);
if (args == NULL)
goto finally;
res = PyEval_CallObject(type, args);
Py_DECREF(args);
if (res == NULL)
goto finally;
Py_DECREF(value);
value = res;
}
/* if the class of the instance doesn't exactly match the
class of the type, believe the instance
*/
else if (inclass != type) {
Py_DECREF(type);
type = inclass;
Py_INCREF(type);
}
}
*exc = type;
*val = value;
return;
finally:
Py_DECREF(type);
Py_DECREF(value);
/* If the new exception doesn't set a traceback and the old
exception had a traceback, use the old traceback for the
new exception. It's better than nothing.
*/
initial_tb = *tb;
PyErr_Fetch(exc, val, tb);
if (initial_tb != NULL) {
if (*tb == NULL)
*tb = initial_tb;
else
Py_DECREF(initial_tb);
}
/* normalize recursively */
tstate = PyThreadState_GET();
if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
//.........这里部分代码省略.........
请发表评论