コード例 #1
0
ファイル: pongo_dict.c プロジェクト: cfrantz/pongo
static int
PongoDict_SetItem(PongoDict *self, PyObject *key, PyObject *value)
{
    dbtype_t k;
    dbtype_t v;
    int ret = -1;

    dblock(self->ctx);
    k = from_python(self->ctx, key);
    if (!PyErr_Occurred()) {
        if (value == NULL) {
            if (dbobject_delitem(SELF_CTX_AND_DBPTR, k, &v, self->ctx->sync) == 0) {
                ret = 0;
            } else {
                PyErr_SetObject(PyExc_KeyError, key);
            }
        } else {
            v = from_python(self->ctx, value);
            if (!PyErr_Occurred() && dbobject_setitem(SELF_CTX_AND_DBPTR, k, v, self->ctx->sync) == 0)
                ret = 0;
        }
    }
    dbunlock(self->ctx);
    return ret;
}
コード例 #2
0
ファイル: pongo_iter.c プロジェクト: cfrantz/pongo
static PyObject *
PongoIter_expr(PyObject *ob, PyObject *args, PyObject *kwargs)
{
    PongoIter *self = (PongoIter*)ob;
    PyObject *lhs = Py_None, *rhs = Py_None;
    int lhex = 0, rhex = 0;
    char *kwlist[] = {"lhs", "rhs", "lhex", "rhex", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii:expr", kwlist,
                &lhs, &rhs, &lhex, &rhex))
        return NULL;

    dblock(self->ctx);
    // Force lhex to be 0 or 1, and rhex to be 0 or -1
    self->lhex = !!lhex;
    self->rhex = -(!!rhex);

    // Convert the lhs and rhs to pongo objects and reference them in the pidcache
    self->lhdata = from_python(self->ctx, lhs);
    if (self->lhdata.all)
        pidcache_put(self->ctx, &self->lhdata, self->lhdata);

    self->rhdata = from_python(self->ctx, rhs);
    if (self->rhdata.all)
        pidcache_put(self->ctx, &self->rhdata, self->rhdata);
    dbunlock(self->ctx);
    Py_INCREF(self);
    return (PyObject*)self;
}
コード例 #3
0
ファイル: pongo_dict.c プロジェクト: cfrantz/pongo
static PyObject *
PongoDict_search(PongoDict *self, PyObject *args)
{
    PyObject *path, *value, *ret=NULL;
    dbtype_t dbpath, dbvalue, dbrslt;
    char *rel;
    char *sep = ".";
    int decpath = 0;
    relop_t relop;

    if (!PyArg_ParseTuple(args, "OsO:search", &path, &rel, &value))
        return NULL;

    if (!strcmp(rel, "==") || !strcmp(rel, "eq")) {
        relop = db_EQ;
    } else if (!strcmp(rel, "!=") || !strcmp(rel, "ne")) {
        relop = db_NE;
    } else if (!strcmp(rel, "<") || !strcmp(rel, "lt")) {
        relop = db_LT;
    } else if (!strcmp(rel, "<=") || !strcmp(rel, "le")) {
        relop = db_LE;
    } else if (!strcmp(rel, ">") || !strcmp(rel, "gt")) {
        relop = db_GT;
    } else if (!strcmp(rel, ">=") || !strcmp(rel, "ge")) {
        relop = db_GE;
    } else {
        PyErr_Format(PyExc_ValueError, "Unknown relop '%s'", rel);
        return NULL;
    }

    if (PyString_Check(path)) {
        path = PyObject_CallMethod(path, "split", "s", sep);
        decpath = 1;
    }

    if (!PySequence_Check(path)) {
        PyErr_Format(PyExc_TypeError, "path must be a sequence");
        return NULL;
    }
    dblock(self->ctx);
    dbpath = from_python(self->ctx, path);
    if (decpath)
        Py_DECREF(path);
    if (dbtype(self->ctx, dbpath) == List) {
        dbvalue = from_python(self->ctx, value);
        if (!PyErr_Occurred()) {
            dbrslt = dbcollection_new(self->ctx, 0);
            db_search(SELF_CTX_AND_DBPTR, dbpath, -1, relop, dbvalue, dbrslt);
            // PROXYCHLD means turn the root object into a real dict, but
            // create proxy objects for all children.
            ret = to_python(self->ctx, dbrslt, TP_PROXYCHLD);
        }
    } else {
        PyErr_Format(PyExc_Exception, "path type isn't List (%d)", dbtype(self->ctx, dbpath));
    }
    dbunlock(self->ctx);
    return ret;
}
コード例 #4
0
ファイル: functions.hpp プロジェクト: metashell/headers
	PyObject* do_call(PyObject* args, PyObject* keywords) const
    { 
        ref dict(keywords ? 
                 ref(keywords, ref::increment_count) :
                 ref(PyDict_New()));
            
        return to_python(
            (*m_pf)(from_python(args, boost::python::type<Args>()),
                    from_python(dict.get(), boost::python::type<Keywords>()))); 
    }
コード例 #5
0
ファイル: pongo_dict.c プロジェクト: cfrantz/pongo
static PyObject *
PongoDict_set(PongoDict *self, PyObject *args, PyObject *kwargs)
{
    PyObject *key, *value;
    PyObject *klist = NULL;
    PyObject *ret = NULL;
    dbtype_t k, v;
    int sync = self->ctx->sync;
    int fail = 0;
    multi_t op = multi_SET;
    char *kwlist[] = {"key", "value", "sep", "sync", "fail", NULL};
    char *sep = ".";

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OO|sii:set", kwlist,
                &key, &value, &sep, &sync, &fail))
        return NULL;

    k = DBNULL;
    dblock(self->ctx);
    if (PyString_Check(key) || PyUnicode_Check(key)) {
        klist = PyObject_CallMethod(key, "split", "s", sep);
        k = from_python(self->ctx, klist);
        Py_XDECREF(klist);
    } else {
        if (key == pongo_id) {
            sync |= PUT_ID;
        } else {
            k = from_python(self->ctx, key);
        }
    }
    v = from_python(self->ctx, value);
    if (!PyErr_Occurred()) {
        if (dbtype(self->ctx, k) == List) {
            if (fail) op = multi_SET_OR_FAIL;
            if (db_multi(SELF_CTX_AND_DBPTR, k, op, &v, sync) == 0) {
                ret = Py_None;
            } else {
                PyErr_SetObject(PyExc_KeyError, key);
            }
        } else if (db_multi(SELF_CTX_AND_DBPTR, k, op, &v, sync) == 0) {
            // db_mutli will tell us the newly created value of
            // "_id" when PUT_ID is enabled.
            ret = (sync & PUT_ID) ? to_python(self->ctx, v, TP_PROXY) : Py_None;
        } else {
            if (sync & PUT_ID) {
                PyErr_Format(PyExc_ValueError, "value must be a dictionary");
            } else {
                PyErr_SetObject(PyExc_KeyError, key);
            }
        }
    }
    dbunlock(self->ctx);
    Py_XINCREF(ret);
    return ret;
}
コード例 #6
0
ファイル: pongo.c プロジェクト: cfrantz/pongo
int _py_itermapping_cb(pgctx_t *ctx, int i, dbtype_t *key, dbtype_t *value, void *user)
{
    PyObject *iter = (PyObject*)user;
    PyObject *item = PyIter_Next(iter);
    PyObject *k = PySequence_GetItem(item, 0);
    PyObject *v = PySequence_GetItem(item, 1);
    *key = from_python(ctx, k);
    *value = from_python(ctx, v);
    Py_DECREF(k); Py_DECREF(v);
    Py_DECREF(item);
    if (PyErr_Occurred()) return -1;
    return 0;
}
コード例 #7
0
ファイル: functions.hpp プロジェクト: metashell/headers
PyObject* setter_function<ClassType, MemberType>::do_call(
    PyObject* args, PyObject* /* keywords */) const
{
    PyObject* self;
    PyObject* value;
    if (!PyArg_ParseTuple(args, const_cast<char*>("OO"), &self, &value))
        return 0;
    
    typedef typename boost::call_traits<MemberType>::const_reference extract_type;
    from_python(self, type<ClassType*>())->*m_pm
        = from_python(value, type<extract_type>());
    
    return none();
}
コード例 #8
0
ファイル: pongo_dict.c プロジェクト: cfrantz/pongo
static PyObject *
PongoDict_get(PongoDict *self, PyObject *args, PyObject *kwargs)
{
    PyObject *key, *dflt = Py_None;
    PyObject *klist = NULL;
    PyObject *ret = NULL;
    dbtype_t k, v;
    char *sep = ".";
    char *kwlist[] = {"key", "default", "sep", NULL};
    int r;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Os:get", kwlist,
                &key, &dflt, &sep))
        return NULL;

    dblock(self->ctx);
    if (PyString_Check(key) || PyUnicode_Check(key)) {
        klist = PyObject_CallMethod(key, "split", "s", sep);
        k = from_python(self->ctx, klist);
        Py_XDECREF(klist);
    } else {
        k = from_python(self->ctx, key);
    }

    if (!PyErr_Occurred()) {
        if (dbtype(self->ctx, k) == List) {
            r = db_multi(SELF_CTX_AND_DBPTR, k, multi_GET, &v, 0);
            if (r == 0) {
                ret = to_python(self->ctx, v, TP_PROXY);
            } else if (dflt) {
                Py_INCREF(dflt);
                ret = dflt;
            } else {
                PyErr_SetObject(PyExc_KeyError, key);
            }
        } else if (dbobject_getitem(SELF_CTX_AND_DBPTR, k, &v) == 0) {
            ret = to_python(self->ctx, v, TP_PROXY);
        } else {
            if (dflt) {
                ret = dflt; Py_INCREF(ret);
            } else {
                PyErr_SetObject(PyExc_KeyError, key);
            }
        }
    }
    dbunlock(self->ctx);
    return ret;
}
コード例 #9
0
ファイル: pongo_dict.c プロジェクト: cfrantz/pongo
static PyObject *
PongoDict_pop(PongoDict *self, PyObject *args, PyObject *kwargs)
{
    PyObject *key, *dflt = NULL;
    PyObject *ret = NULL;
    dbtype_t k, v;
    int sync = self->ctx->sync;
    char *kwlist[] = {"key", "default", "sync", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Oi:pop", kwlist,
                &key, &dflt, &sync))
        return NULL;

    dblock(self->ctx);
    k = from_python(self->ctx, key);
    if (!PyErr_Occurred()) {
        if (dbobject_delitem(SELF_CTX_AND_DBPTR, k, &v, sync) < 0) {
            if (dflt) {
                Py_INCREF(dflt);
                ret = dflt;
            } else {
                PyErr_SetObject(PyExc_KeyError, key);
            }
        } else {
            ret = to_python(self->ctx, v, TP_PROXY);
        }
    }
    dbunlock(self->ctx);
    return ret;
}
コード例 #10
0
ファイル: pongo.c プロジェクト: cfrantz/pongo
int _py_sequence_cb(pgctx_t *ctx, int i, dbtype_t *item, void *user)
{
    PyObject *seq = (PyObject*)user;
    PyObject *sqitem = PySequence_GetItem(seq, i);
    *item = from_python(ctx, sqitem);
    Py_DECREF(sqitem);
    if (PyErr_Occurred()) return -1;
    return 0;
}
コード例 #11
0
ファイル: functions.hpp プロジェクト: metashell/headers
PyObject* getter_function<ClassType, MemberType>::do_call(
    PyObject* args, PyObject* /* keywords */) const
{
    PyObject* self;
    if (!PyArg_ParseTuple(args, const_cast<char*>("O"), &self))
        return 0;

    return to_python(
        from_python(self, type<const ClassType*>())->*m_pm);
}
コード例 #12
0
ファイル: pongo.c プロジェクト: cfrantz/pongo
static PyObject *
pongo_meta(PyObject *self, PyObject *args)
{
    PongoCollection *data;
    pgctx_t *ctx;
    const char *key;
    PyObject *value = NULL, *ret = Py_None;

    if (!PyArg_ParseTuple(args, "Os|O:meta", &data, &key, &value))
        return NULL;
    if (pongo_check(data))
        return NULL;

    ctx = data->ctx;
    dblock(ctx);
    if (!strcmp(key, "chunksize")) {
        ret = PyLong_FromLongLong(ctx->root->meta.chunksize);
        if (value && value != Py_None) ctx->root->meta.chunksize = PyInt_AsLong(value);
    } else if (!strcmp(key, "id")) {
        ret = to_python(ctx, ctx->root->meta.id, 0);
        if (value) ctx->root->meta.id = from_python(ctx, value);
    } else if (!strcmp(key, ".sync")) {
        ret = PyInt_FromLong(ctx->sync);
        if (value && value != Py_None) ctx->sync = PyInt_AsLong(value);
#ifdef WANT_UUID_TYPE
    } else if (!strcmp(key, ".uuid_class")) {
        ret = (PyObject*)uuid_class;
        if (value) uuid_class = (PyTypeObject*)value;
    } else if (!strcmp(key, ".uuid_constructor")) {
        ret = (PyObject*)uuid_constructor;
        if (value) uuid_constructor = value;
#endif
    } else if (!strcmp(key, ".newkey")) {
        ret = pongo_newkey;
        if (value) {
            pongo_newkey = value;
            if (value == Py_None) {
#ifdef WANT_UUID_TYPE
                ctx->newkey = _newkey;
#else
                ctx->newkey = NULL;
#endif
            } else {
                ctx->newkey = pongo_newkey_helper;
            }
        }
    } else {
        PyErr_Format(PyExc_Exception, "Unknown meta key %s", key);
        ret = NULL;
    }
    dbunlock(ctx);

    return ret;
}
コード例 #13
0
ファイル: pongo.c プロジェクト: cfrantz/pongo
static dbtype_t
pongo_newkey_helper(pgctx_t *ctx, dbtype_t value)
{
    // FIXME: something wrong here.
    PyObject *ob;
    ob = PyObject_CallFunction(pongo_newkey, "(N)", to_python(ctx, value, 1));
    if (ob) {
        value = from_python(ctx, ob);
        Py_DECREF(ob);
    } else {
        value = DBNULL;
    }
    return value;
}
コード例 #14
0
ファイル: pongo_dict.c プロジェクト: cfrantz/pongo
static int
PongoDict_contains(PongoDict *self, PyObject *key)
{
    dbtype_t k;
    int ret = 0;

    dblock(self->ctx);
    k = from_python(self->ctx, key);
    if (!PyErr_Occurred()) {
        ret = dbobject_contains(SELF_CTX_AND_DBPTR, k);
    }
    dbunlock(self->ctx);
    return ret;
}
コード例 #15
0
ファイル: pongo_dict.c プロジェクト: cfrantz/pongo
static PyObject *
PongoDict_GetItem(PongoDict *self, PyObject *key)
{
    dbtype_t k, v;
    PyObject *ret = NULL;

    dblock(self->ctx);
    k = from_python(self->ctx, key);
    if (!PyErr_Occurred()) {
        if (dbobject_getitem(SELF_CTX_AND_DBPTR, k, &v) == 0) {
            ret = to_python(self->ctx, v, TP_PROXY);
        } else {
            PyErr_SetObject(PyExc_KeyError, key);
        }
    }
    dbunlock(self->ctx);
    return ret;
}
コード例 #16
0
ファイル: conversions.hpp プロジェクト: metashell/headers
inline bool from_python(PyObject* p, boost::python::type<const bool&>)
{
    return from_python(p, boost::python::type<bool>());
}
コード例 #17
0
ファイル: conversions.hpp プロジェクト: metashell/headers
inline unsigned short from_python(PyObject* p, boost::python::type<const unsigned short&>)
{
    return from_python(p, boost::python::type<unsigned short>());
}
コード例 #18
0
ファイル: conversions.hpp プロジェクト: metashell/headers
inline short from_python(PyObject* p, boost::python::type<const short&>)
{
    return from_python(p, boost::python::type<short>());
}
コード例 #19
0
ファイル: conversions.hpp プロジェクト: metashell/headers
inline long from_python(PyObject* p, boost::python::type<const long&>)
{
    return from_python(p, boost::python::type<long>());
}
コード例 #20
0
ファイル: conversions.hpp プロジェクト: metashell/headers
inline int from_python(PyObject* p, boost::python::type<const int&>)
{
    return from_python(p, boost::python::type<int>());
}
コード例 #21
0
ファイル: conversions.hpp プロジェクト: metashell/headers
inline float from_python(PyObject* p, boost::python::type<const float&>)
{
    return from_python(p, boost::python::type<float>());
}
コード例 #22
0
ファイル: conversions.hpp プロジェクト: metashell/headers
inline double from_python(PyObject* p, boost::python::type<const double&>)
{
    return from_python(p, boost::python::type<double>());
}
コード例 #23
0
ファイル: conversions.hpp プロジェクト: metashell/headers
inline char from_python(PyObject* p, boost::python::type<const char&>)
{
    return from_python(p, boost::python::type<char>());
}
コード例 #24
0
ファイル: conversions.hpp プロジェクト: metashell/headers
inline unsigned long from_python(PyObject* p, boost::python::type<const unsigned long&>)
{
    return from_python(p, boost::python::type<unsigned long>());
}
コード例 #25
0
ファイル: conversions.hpp プロジェクト: metashell/headers
inline unsigned char from_python(PyObject* p, boost::python::type<const unsigned char&>)
{
    return from_python(p, boost::python::type<unsigned char>());
}
コード例 #26
0
ファイル: conversions.hpp プロジェクト: metashell/headers
inline std::string from_python(PyObject* p, boost::python::type<const std::string&>)
{
    return from_python(p, boost::python::type<std::string>());
}
コード例 #27
0
ファイル: conversions.hpp プロジェクト: metashell/headers
 friend EnumType from_python(PyObject* x, boost::python::type<const EnumType&>)
 {
     return static_cast<EnumType>(
         from_python(x, boost::python::type<long>()));
 }
コード例 #28
0
ファイル: conversions.hpp プロジェクト: metashell/headers
inline const char* from_python(PyObject* p, boost::python::type<const char* const&>)
{
    return from_python(p, boost::python::type<const char*>());
}