/* * Method for the setting items with the [key] operator on pyjmap. For example, * o[key] = v. Also supports del o[key] */ 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(); int result = -1; if ((*env)->PushLocalFrame(env, JLOCAL_REFS) != 0) { process_java_exception(env); return -1; } if (v == NULL) { // this is a del PyJMap[key] statement if (!pyjmap_contains_key(o, key)) { PyObject *pystr = PyObject_Str(key); PyErr_Format(PyExc_KeyError, "KeyError: %s", PyString_AsString(pystr)); Py_XDECREF(pystr); goto FINALLY; } jkey = PyObject_As_jobject(env, key, JOBJECT_TYPE); if (!jkey && PyErr_Occurred()) { goto FINALLY; } java_util_Map_remove(env, obj->object, jkey); if (process_java_exception(env)) { goto FINALLY; } } else { value = PyObject_As_jobject(env, v, JOBJECT_TYPE); if (!value && PyErr_Occurred()) { goto FINALLY; } jkey = PyObject_As_jobject(env, key, JOBJECT_TYPE); if (!jkey && PyErr_Occurred()) { return -1; } java_util_Map_put(env, obj->object, jkey, value); if (process_java_exception(env)) { goto FINALLY; } } // have to return 0 on success even though it's not documented result = 0; FINALLY: (*env)->PopLocalFrame(env, NULL); return result; }
/* * 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 jresult = JNI_FALSE; PyJObject *obj = (PyJObject*) self; JNIEnv *env = pyembed_get_env(); jobject jkey = NULL; int result = -1; if ((*env)->PushLocalFrame(env, JLOCAL_REFS) != 0) { process_java_exception(env); return -1; } jkey = PyObject_As_jobject(env, key, JOBJECT_TYPE); if (!jkey && PyErr_Occurred()) { goto FINALLY; } jresult = java_util_Map_containsKey(env, obj->object, jkey); if (process_java_exception(env)) { goto FINALLY; } if (jresult) { result = 1; } else { result = 0; } FINALLY: (*env)->PopLocalFrame(env, NULL); return result; }
/* * 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; JNIEnv *env = pyembed_get_env(); PyJObject *self = (PyJObject*) o1; PyObject *result = NULL; if ((*env)->PushLocalFrame(env, JLOCAL_REFS) != 0) { process_java_exception(env); return NULL; } /* * TODO: To match Python behavior of += operator, we should really be * using JITERABLE_TYPE and ensuring its an instance of Iterable, not * Collection. */ value = PyObject_As_jobject(env, o2, JCOLLECTION_TYPE); if (!value && PyErr_Occurred()) { return NULL; } if (!value) { PyErr_Format(PyExc_TypeError, "Expected java.util.Collection but received null."); return NULL; } /* * it's a Collection so we need to simulate a python + and combine the * two collections */ java_util_List_addAll(env, self->object, value); if (process_java_exception(env)) { goto FINALLY; } result = o1; Py_INCREF(o1); FINALLY: (*env)->PopLocalFrame(env, NULL); return result; }
/* * Method for the getting items with the [key] operator on pyjmap. For * example, result = o[key] */ static PyObject* pyjmap_getitem(PyObject *o, PyObject *key) { jobject jkey = NULL; jobject val = NULL; PyJObject *obj = (PyJObject*) o; JNIEnv *env = pyembed_get_env(); PyObject *result = NULL; if ((*env)->PushLocalFrame(env, JLOCAL_REFS) != 0) { process_java_exception(env); return NULL; } jkey = PyObject_As_jobject(env, key, JOBJECT_TYPE); if (!jkey && PyErr_Occurred()) { goto FINALLY; } val = java_util_Map_get(env, obj->object, jkey); if (process_java_exception(env)) { goto FINALLY; } if (!val) { /* * Python docs indicate KeyError should be set if the key is not in the * container, but some Maps allow null values. So we have to check. */ if (!pyjmap_contains_key(o, key)) { PyObject *pystr = PyObject_Str(key); PyErr_Format(PyExc_KeyError, "KeyError: %s", PyString_AsString(pystr)); Py_XDECREF(pystr); goto FINALLY; } } result = convert_jobject_pyobject(env, val); FINALLY: (*env)->PopLocalFrame(env, NULL); return result; }
/* * Method for the setting items with the [int] operator on pyjlist. For example, * o[i] = v. Also supports del o[i] */ static int pyjlist_setitem(PyObject *o, Py_ssize_t i, PyObject *v) { PyJObject *obj = (PyJObject*) o; JNIEnv *env = pyembed_get_env(); jobject value = NULL; int result = -1; if (v == NULL) { // this is a del PyJList[index] statement java_util_List_remove(env, obj->object, (jint) i); if (process_java_exception(env)) { return -1; } // have to return 0 on success even though it's not documented return 0; } if ((*env)->PushLocalFrame(env, JLOCAL_REFS) != 0) { process_java_exception(env); return -1; } value = PyObject_As_jobject(env, v, JOBJECT_TYPE); if (!value && PyErr_Occurred()) { goto FINALLY; } java_util_List_set(env, obj->object, (jint) i, value); if (process_java_exception(env)) { goto FINALLY; } // have to return 0 on success even though it's not documented result = 0; FINALLY: (*env)->PopLocalFrame(env, NULL); return result; }