PyObject* pyjiterator_next(PyObject* self) { jmethodID hasNext = NULL; jboolean nextAvail = JNI_FALSE; PyJobject_Object *pyjob = (PyJobject_Object*) self; JNIEnv *env = pyembed_get_env(); hasNext = (*env)->GetMethodID(env, pyjob->clazz, "hasNext", "()Z"); if(process_java_exception(env)) { return NULL; } nextAvail = (*env)->CallBooleanMethod(env, pyjob->object, hasNext); if(process_java_exception(env)) { return NULL; } if(nextAvail) { jobject nextItem; jmethodID next; next = (*env)->GetMethodID(env, pyjob->clazz, "next", "()Ljava/lang/Object;"); if(process_java_exception(env) || !next) { return NULL; } nextItem = (*env)->CallObjectMethod(env, pyjob->object, next); if(process_java_exception(env)) { return NULL; } return convert_jobject_pyobject(env, nextItem); } return NULL; }
/* * 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(); if (mapGet == 0) { mapGet = (*env)->GetMethodID(env, JMAP_TYPE, "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); if (process_java_exception(env) || !mapGet) { return NULL; } } if (pyjobject_check(key)) { jkey = ((PyJObject*) key)->object; } else { /* * convert_pyarg_jvalue will leave jkey as NULL and set PyExc_TypeError * if we can't handle the key type, which matches python's guidelines */ jvalue jvkey = convert_pyarg_jvalue(env, key, JOBJECT_TYPE, JOBJECT_ID, 1); jkey = jvkey.l; if (process_java_exception(env) || !jkey) { return NULL; } } val = (*env)->CallObjectMethod(env, obj->object, mapGet, jkey); if (process_java_exception(env)) { return NULL; } 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); return NULL; } } return convert_jobject_pyobject(env, val); }
/* * 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; }