コード例 #1
0
ファイル: pyjmap.c プロジェクト: codeApeFromChina/jep
/*
 * Method for the setting items with the [key] operator on pyjmap.  For example,
 * o[key] = v
 */
static int pyjmap_setitem(PyObject *o, PyObject *key, PyObject *v)
{
    jobject       jkey     = NULL;
    jobject       value    = NULL;
    PyJObject    *obj      = (PyJObject*) o;
    JNIEnv       *env      = pyembed_get_env();

    if (v == Py_None) {
        value = NULL;
    } else {
        value = pyembed_box_py(env, v);
        if (process_java_exception(env)) {
            return -1;
        } else if (!value) {
            /*
             * with the way pyembed_box_py is currently implemented, shouldn't
             * be able to get here
             */
            PyObject *pystring = PyObject_Str((PyObject*) Py_TYPE(v));
            PyErr_Format(PyExc_TypeError,
                         "__setitem__ received an incompatible type: %s",
                         PyString_AsString(pystring));
            Py_XDECREF(pystring);
            return -1;
        }
    }

    if (pyjobject_check(key)) {
        jkey = ((PyJObject*) key)->object;
    } else {
        jvalue jvkey = convert_pyarg_jvalue(env, key, JOBJECT_TYPE, JOBJECT_ID, 1);
        jkey = jvkey.l;
        if (process_java_exception(env) || !jkey) {
            return -1;
        }
    }

    if (mapPut == 0) {
        mapPut = (*env)->GetMethodID(env, JMAP_TYPE, "put",
                                     "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
        if (process_java_exception(env) || !mapPut) {
            return -1;
        }
    }

    (*env)->CallObjectMethod(env, obj->object, mapPut, jkey, value);
    if (process_java_exception(env)) {
        return -1;
    }

    // have to return 0 on success even though it's not documented
    return 0;
}
コード例 #2
0
ファイル: pyjlist.c プロジェクト: behdad84/jep
/*
 * Method for the += operator on pyjlist.  For example, o1 += o2, where
 * o1 is a pyjlist.
 */
static PyObject* pyjlist_inplace_add(PyObject *o1, PyObject *o2) {
    jobject               value   = NULL;
    jclass           collection   = NULL;
    JNIEnv               *env     = pyembed_get_env();
    PyJobject_Object     *self    = (PyJobject_Object*) o1;

    if(pyjlist_check(o2)) {
        value                     = ((PyJobject_Object*) o2)->object;
    } else {
        value                     = pyembed_box_py(env, o2);
    }

    collection = (*env)->FindClass(env, "java/util/Collection");
    if(process_java_exception(env) || !collection) {
        return NULL;
    }

    if((*env)->IsInstanceOf(env, value, collection)) {
        /*
         * it's a Collection so we need to simulate a python + and combine the
         * two collections
         */
        jmethodID addAll = (*env)->GetMethodID(env, self->clazz, "addAll", "(Ljava/util/Collection;)Z");
        if(process_java_exception(env) || !addAll) {
            return NULL;
        }

        (*env)->CallBooleanMethod(env, self->object, addAll, value);
        if(process_java_exception(env)) {
            return NULL;
        }
    } else {
        // not a collection, add it as a single object
        jmethodID add = (*env)->GetMethodID(env, self->clazz, "add", "(Ljava/lang/Object;)Z");
        if(process_java_exception(env) || !add) {
            return NULL;
        }

        (*env)->CallBooleanMethod(env, self->object, add, value);
        if(process_java_exception(env)) {
            return NULL;
        }
    }

    Py_INCREF(o1);

    return o1;
}
コード例 #3
0
ファイル: pyjmap.c プロジェクト: codeApeFromChina/jep
/*
 * Method for checking if a key is in the dictionary.  For example,
 * if key in o:
 */
static int pyjmap_contains_key(PyObject *self, PyObject *key)
{
    jboolean      result      = JNI_FALSE;
    PyJObject    *obj         = (PyJObject*) self;
    JNIEnv       *env         = pyembed_get_env();
    jobject       jkey        = NULL;

    if (key == Py_None) {
        jkey = NULL;
    } else {
        jkey = pyembed_box_py(env, key);
        if (process_java_exception(env)) {
            return -1;
        } else if (!jkey) {
            /*
             * with the way pyembed_box_py is currently implemented, shouldn't
             * be able to get here
             */
            PyObject *pystring = PyObject_Str((PyObject*) Py_TYPE(key));
            PyErr_Format(PyExc_TypeError,
                         "__contains__ received an incompatible type: %s",
                         PyString_AsString(pystring));
            Py_XDECREF(pystring);
            return -1;
        }
    }

    if (mapContainsKey == 0) {
        mapContainsKey = (*env)->GetMethodID(env, JMAP_TYPE, "containsKey",
                                             "(Ljava/lang/Object;)Z");
        if (process_java_exception(env) || !mapContainsKey) {
            return -1;
        }
    }

    result = (*env)->CallBooleanMethod(env, obj->object, mapContainsKey, jkey);
    if (process_java_exception(env)) {
        return -1;
    }

    if (result) {
        return 1;
    } else {
        return 0;
    }
}
コード例 #4
0
ファイル: pyjlist.c プロジェクト: behdad84/jep
/*
 * Method for the setting items with the [int] operator on pyjlist.  For example,
 * o[i] = v
 */
static int pyjlist_setitem(PyObject *o, Py_ssize_t i, PyObject *v) {
    jmethodID         set      = NULL;
    PyJobject_Object *obj      = (PyJobject_Object*) o;
    JNIEnv           *env      = pyembed_get_env();
    jobject           value    = NULL;

    if(v == Py_None) {
        value = NULL;
    } else {
        value = pyembed_box_py(env, v);
        if(process_java_exception(env)) {
            return -1;
        } else if(!value) {
            /*
             * with the way pyembed_box_py is currently implemented, shouldn't
             * be able to get here
             */
            PyErr_Format(PyExc_TypeError,
                        "__setitem__ received an incompatible type: %s",
                        PyString_AsString(PyObject_Str((PyObject*) Py_TYPE(v))));
            return -1;
        }
    }

    set = (*env)->GetMethodID(env, obj->clazz, "set", "(ILjava/lang/Object;)Ljava/lang/Object;");
    if(process_java_exception(env) || !set) {
        return -1;
    }

    (*env)->CallObjectMethod(env, obj->object, set, (jint) i, value);
    if(process_java_exception(env)) {
        return -1;
    }

    // have to return 0 on success even though it's not documented
    return 0;
}