示例#1
0
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;
}
示例#2
0
文件: pyjmap.c 项目: ndjensen/jep
/*
 * Method for iterating over the keys of the dictionary.  For example,
 * for key in o:
 */
static PyObject* pyjmap_getiter(PyObject* obj)
{
    jobject       set      = NULL;
    jobject       iter     = NULL;
    PyJObject    *pyjob    = (PyJObject*) obj;
    PyObject     *result   = NULL;
    JNIEnv       *env      = pyembed_get_env();

    if ((*env)->PushLocalFrame(env, JLOCAL_REFS) != 0) {
        process_java_exception(env);
        return NULL;
    }
    set = java_util_Map_keySet(env, pyjob->object);
    if (process_java_exception(env) || !set) {
        goto FINALLY;
    }


    iter = java_lang_Iterable_iterator(env, set);
    if (process_java_exception(env) || !iter) {
        goto FINALLY;
    }

    result = PyJObject_New(env, iter);
FINALLY:
    (*env)->PopLocalFrame(env, NULL);
    return result;
}
示例#3
0
文件: pyjmap.c 项目: ndjensen/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     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;
}
示例#4
0
文件: pyjlist.c 项目: mrj0/jep
/*
 * Method for the getting items with the [int] operator on pyjlist.  For
 * example, result = o[i]
 */
static PyObject* pyjlist_getitem(PyObject *o, Py_ssize_t i)
{
    jobject       val  = NULL;
    Py_ssize_t    size = 0;
    PyJObject    *obj  = (PyJObject*) o;
    JNIEnv       *env  = pyembed_get_env();

    size = PyObject_Size(o);
    if ((i > size - 1) || (i < 0)) {
        PyErr_Format(PyExc_IndexError, "list index %i out of range, size %i", (int) i,
                     (int) size);
        return NULL;
    }

    if ((*env)->PushLocalFrame(env, JLOCAL_REFS) != 0) {
        process_java_exception(env);
        return NULL;
    }

    val = java_util_List_get(env, obj->object, (jint) i);
    if (process_java_exception(env)) {
        (*env)->PopLocalFrame(env, NULL);
        return NULL;
    }

    if (val == NULL) {
        (*env)->PopLocalFrame(env, NULL);
        Py_RETURN_NONE;
    } else {
        PyObject *result = jobject_As_PyObject(env, val);
        (*env)->PopLocalFrame(env, NULL);
        return result;
    }
}
示例#5
0
static PyObject* java_number_to_pythonintlong(JNIEnv *env, PyObject* n)
{
    jlong      value;
    PyJObject *jnumber  = (PyJObject*) n;

    if (longValue == 0) {
        longValue = (*env)->GetMethodID(env, JNUMBER_TYPE, "longValue", "()J");

        if (process_java_exception(env)) {
            return NULL;
        }
    }

#if PY_MAJOR_VERSION < 3
    if (intValue == 0) {
        intValue = (*env)->GetMethodID(env, JNUMBER_TYPE, "intValue", "()I");

        if (process_java_exception(env)) {
            return NULL;
        }
    }

    if ((*env)->IsInstanceOf(env, jnumber->object, JBYTE_OBJ_TYPE) ||
            (*env)->IsInstanceOf(env, jnumber->object, JSHORT_OBJ_TYPE) ||
            (*env)->IsInstanceOf(env, jnumber->object, JINT_OBJ_TYPE)) {
        jint result = (*env)->CallIntMethod(env, jnumber->object, intValue);
        return PyInt_FromSsize_t(result);
    }
#endif

    value = (*env)->CallLongMethod(env, jnumber->object, longValue);
    return PyLong_FromLongLong(value);
}
示例#6
0
文件: pyjlist.c 项目: mrj0/jep
/*
 * Convenience method to copy a list's items into a new java.util.List of the
 * same type.
 */
static PyObject* pyjlist_new_copy(PyObject *toCopy)
{
    jobject       newList     = NULL;
    PyJObject    *obj         = (PyJObject*) toCopy;
    JNIEnv       *env         = pyembed_get_env();
    PyObject     *result      = NULL;


    if (!PyJList_Check(toCopy)) {
        PyErr_Format(PyExc_RuntimeError, "pyjlist_new_copy() must receive a PyJList");
        return NULL;
    }

    if ((*env)->PushLocalFrame(env, JLOCAL_REFS) != 0) {
        process_java_exception(env);
        return NULL;
    }

    newList = java_lang_Class_newInstance(env, obj->clazz);
    if (process_java_exception(env) || !newList) {
        goto FINALLY;
    }

    java_util_List_addAll(env, newList, obj->object);
    if (process_java_exception(env)) {
        goto FINALLY;
    }

    result = PyJList_Wrap(env, newList, obj->clazz);
FINALLY:
    (*env)->PopLocalFrame(env, NULL);
    return result;
}
示例#7
0
文件: pyjlist.c 项目: behdad84/jep
/*
 * Convenience method to copy a list's items into a new java.util.List of the
 * same type.
 */
PyObject* pyjlist_new_copy(PyObject *toCopy) {
    jmethodID         newInstance = NULL;
    jobject           newList     = NULL;
    jmethodID         addAll      = NULL;
    PyJobject_Object *obj         = (PyJobject_Object*) toCopy;
    JNIEnv           *env         = pyembed_get_env();


    if(!pyjlist_check(toCopy)) {
        PyErr_Format(PyExc_RuntimeError, "pyjlist_new_copy() must receive a pyjlist");
        return NULL;
    }

    newInstance = (*env)->GetMethodID(env, JCLASS_TYPE, "newInstance", "()Ljava/lang/Object;");
    if(process_java_exception(env) || !newInstance) {
        return NULL;
    }

    newList = (*env)->CallObjectMethod(env, obj->clazz, newInstance);
    if(process_java_exception(env) || !newList) {
        return NULL;
    }

    addAll = (*env)->GetMethodID(env, obj->clazz, "addAll", "(Ljava/util/Collection;)Z");
    if(process_java_exception(env) || !addAll) {
        return NULL;
    }

    (*env)->CallBooleanMethod(env, newList, addAll, obj->object);
    if(process_java_exception(env)) {
        return NULL;
    }

    return pyjobject_new(env, newList);
}
示例#8
0
static long pyjobject_hash(PyJObject *self)
{
    JNIEnv *env = pyembed_get_env();
    int   hash = -1;

    if (objectHashCode == 0) {
        objectHashCode = (*env)->GetMethodID(env,
                                             self->clazz,
                                             "hashCode",
                                             "()I");
        if (process_java_exception(env) || !objectHashCode) {
            return -1;
        }
    }

    if (self->object) {
        hash = (*env)->CallIntMethod(env, self->object, objectHashCode);
    } else {
        hash = (*env)->CallIntMethod(env, self->clazz, objectHashCode);
    }
    if (process_java_exception(env)) {
        return -1;
    }

    /*
     * this seems odd but python expects -1 for error occurred and other
     * built-in types then return -2 if the actual hash is -1
     */
    if (hash == -1) {
        hash = -2;
    }

    return hash;
}
示例#9
0
/*
 * Method for iterating over the keys of the dictionary.  For example,
 * for key in o:
 */
PyObject* pyjmap_getiter(PyObject* obj)
{
    jobject       set      = NULL;
    jobject       iter     = NULL;
    PyJObject    *pyjob    = (PyJObject*) obj;
    JNIEnv       *env      = pyembed_get_env();

    if (mapKeySet == 0) {
        mapKeySet = (*env)->GetMethodID(env, JMAP_TYPE, "keySet", "()Ljava/util/Set;");
        if (process_java_exception(env) || !mapKeySet) {
            return NULL;
        }
    }

    set = (*env)->CallObjectMethod(env, pyjob->object, mapKeySet);
    if (process_java_exception(env) || !set) {
        return NULL;
    }

    if (mapKeyItr == 0) {
        mapKeyItr = (*env)->GetMethodID(env, JCOLLECTION_TYPE, "iterator",
                                        "()Ljava/util/Iterator;");
        if (process_java_exception(env) || !mapKeyItr) {
            return NULL;
        }
    }

    iter = (*env)->CallObjectMethod(env, set, mapKeyItr);
    if (process_java_exception(env) || !iter) {
        return NULL;
    }

    return pyjobject_new(env, iter);
}
示例#10
0
文件: pyjlist.c 项目: behdad84/jep
/*
 * Method for the getting items with the [int] operator on pyjlist.  For
 * example, result = o[i]
 */
static PyObject* pyjlist_getitem(PyObject *o, Py_ssize_t i) {
    jmethodID         get  = NULL;
    jobject           val  = NULL;
    Py_ssize_t        size = 0;
    PyJobject_Object *obj  = (PyJobject_Object*) o;
    JNIEnv           *env  = pyembed_get_env();

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

    size = PyObject_Size(o);
    if((i > size-1) || (i < 0)) {
        PyErr_Format(PyExc_IndexError, "list index %i out of range, size %i", (int) i, (int) size);
        return NULL;
    }

    val = (*env)->CallObjectMethod(env, obj->object, get, (jint) i);
    if(process_java_exception(env)) {
        return NULL;
    }

    if(val == NULL) {
        Py_RETURN_NONE;
    } else {
        return pyjobject_new(env, val);
    }
}
示例#11
0
// called internally to make new PyJmethod_Object instances.
// throws java exception and returns NULL on error.
PyJmethod_Object* pyjmethod_new(JNIEnv *env,
                                jobject rmethod,
                                PyJobject_Object *pyjobject) {
    PyJmethod_Object *pym          = NULL;
    jclass            rmethodClass = NULL;
    const char       *methodName   = NULL;
    jstring           jstr         = NULL;

    if(PyType_Ready(&PyJmethod_Type) < 0)
        return NULL;

    pym                = PyObject_NEW(PyJmethod_Object, &PyJmethod_Type);
    pym->rmethod       = (*env)->NewGlobalRef(env, rmethod);
    pym->parameters    = NULL;
    pym->lenParameters = 0;
    pym->pyMethodName  = NULL;
    pym->isStatic      = -1;
    pym->returnTypeId  = -1;
    
    // ------------------------------ get method name
    
    rmethodClass = (*env)->GetObjectClass(env, rmethod);
    if(process_java_exception(env) || !rmethodClass)
        goto EXIT_ERROR;
    
    if(classGetName == 0) {
        classGetName = (*env)->GetMethodID(env,
                                           rmethodClass,
                                           "getName",
                                           "()Ljava/lang/String;");
        if(process_java_exception(env) || !classGetName)
            goto EXIT_ERROR;
    }
    
    jstr = (jstring) (*env)->CallObjectMethod(env,
                                              rmethod,
                                              classGetName);
    if(process_java_exception(env) || !jstr)
        goto EXIT_ERROR;
    
    methodName        = (*env)->GetStringUTFChars(env, jstr, 0);
    pym->pyMethodName = PyString_FromString(methodName);
    (*env)->ReleaseStringUTFChars(env, jstr, methodName);
    (*env)->DeleteLocalRef(env, jstr);
    
    (*env)->DeleteLocalRef(env, rmethodClass);
    return pym;

    
EXIT_ERROR:
    if(rmethodClass)
        (*env)->DeleteLocalRef(env, rmethodClass);
    
    if(pym)
        pyjmethod_dealloc(pym);
    return NULL;
}
示例#12
0
/*
 * Convert a numpy ndarray to a jep.NDArray.
 *
 * @param env    the JNI environment
 * @param pyobj  the numpy ndarray to convert
 *
 * @return a new jep.NDArray or NULL if errors are encountered
 */
jobject convert_pyndarray_jndarray(JNIEnv *env, PyObject *pyobj)
{
    npy_intp      *dims      = NULL;
    jint          *jdims     = NULL;
    jobject        jdimObj   = NULL;
    jobject        primitive = NULL;
    jobject        result    = NULL;
    PyArrayObject *pyarray   = (PyArrayObject*) pyobj;
    int            ndims     = 0;
    int            i;

    init_numpy();
    if (ndarrayInit == 0) {
        ndarrayInit = (*env)->GetMethodID(env,
                                          JEP_NDARRAY_TYPE,
                                          "<init>",
                                          "(Ljava/lang/Object;[I)V");
        if (process_java_exception(env) || !ndarrayInit) {
            return NULL;
        }
    }

    // setup the int[] constructor arg
    ndims = PyArray_NDIM(pyarray);
    dims = PyArray_DIMS(pyarray);
    jdims = malloc(((int) ndims) * sizeof(jint));
    for (i = 0; i < ndims; i++) {
        jdims[i] = (jint) dims[i];
    }

    jdimObj = (*env)->NewIntArray(env, ndims);
    if (process_java_exception(env) || !jdimObj) {
        free(jdims);
        return NULL;
    }

    (*env)->SetIntArrayRegion(env, jdimObj, 0, ndims, jdims);
    free(jdims);
    if (process_java_exception(env)) {
        return NULL;
    }

    // setup the primitive array arg
    primitive = convert_pyndarray_jprimitivearray(env, pyobj, NULL);
    if (!primitive) {
        return NULL;
    }

    result = (*env)->NewObject(env, JEP_NDARRAY_TYPE, ndarrayInit, primitive,
                               jdimObj);
    if (process_java_exception(env) || !result) {
        return NULL;
    }

    return result;
}
示例#13
0
文件: pyjmap.c 项目: ndjensen/jep
/*
 * 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;
}
示例#14
0
PyJfield_Object* pyjfield_new(JNIEnv *env,
                              jobject rfield,
                              PyJobject_Object *pyjobject) {
    PyJfield_Object *pyf;
    jclass           rfieldClass = NULL;
    jstring          jstr        = NULL;
    const char      *fieldName   = NULL;

    if(PyType_Ready(&PyJfield_Type) < 0)
        return NULL;
    
    pyf              = PyObject_NEW(PyJfield_Object, &PyJfield_Type);
    pyf->rfield      = (*env)->NewGlobalRef(env, rfield);
    pyf->pyjobject   = pyjobject;
    pyf->pyFieldName = NULL;
    pyf->fieldTypeId = -1;
    pyf->isStatic    = -1;
    pyf->init        = 0;
    
    // ------------------------------ get field name
    
    rfieldClass = (*env)->GetObjectClass(env, rfield);
    if(process_java_exception(env) || !rfieldClass)
        goto EXIT_ERROR;
    
    if(classGetName == 0) {
        classGetName = (*env)->GetMethodID(env,
                                           rfieldClass,
                                           "getName",
                                           "()Ljava/lang/String;");
        if(process_java_exception(env) || !classGetName)
            goto EXIT_ERROR;
    }
    
    jstr = (jstring) (*env)->CallObjectMethod(env,
                                              rfield,
                                              classGetName);
    if(process_java_exception(env) || !jstr)
        goto EXIT_ERROR;
    
    fieldName        = (*env)->GetStringUTFChars(env, jstr, 0);
    pyf->pyFieldName = PyString_FromString(fieldName);
    
    (*env)->ReleaseStringUTFChars(env, jstr, fieldName);
    (*env)->DeleteLocalRef(env, jstr);
    
    
    return pyf;
    
EXIT_ERROR:
    if(pyf)
        pyjfield_dealloc(pyf);
    return NULL;
}
示例#15
0
/*
 * 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;
}
示例#16
0
/*
 * 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);
}
示例#17
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;
}
示例#18
0
文件: jbox.c 项目: ndjensen/jep
jobject JBox_Double(JNIEnv* env, jdouble d)
{
    if (!JNI_METHOD(doubleConstructor, env, JDOUBLE_OBJ_TYPE, "<init>", "(D)V")) {
        process_java_exception(env);
        return NULL;
    }
    return (*env)->NewObject(env, JDOUBLE_OBJ_TYPE, doubleConstructor, d);
}
示例#19
0
文件: jbox.c 项目: ndjensen/jep
jobject JBox_Float(JNIEnv* env, jfloat f)
{
    if (!JNI_METHOD(floatConstructor, env, JFLOAT_OBJ_TYPE, "<init>", "(F)V")) {
        process_java_exception(env);
        return NULL;
    }
    return (*env)->NewObject(env, JFLOAT_OBJ_TYPE, floatConstructor, f);
}
示例#20
0
文件: jbox.c 项目: ndjensen/jep
jobject JBox_Int(JNIEnv* env, jint i)
{
    if (!JNI_METHOD(intConstructor, env, JINT_OBJ_TYPE, "<init>", "(I)V")) {
        process_java_exception(env);
        return NULL;
    }
    return (*env)->NewObject(env, JINT_OBJ_TYPE, intConstructor, i);
}
示例#21
0
文件: jbox.c 项目: ndjensen/jep
jobject JBox_Short(JNIEnv* env, jshort s)
{
    if (!JNI_METHOD(shortConstructor, env, JSHORT_OBJ_TYPE, "<init>", "(S)V")) {
        process_java_exception(env);
        return NULL;
    }
    return (*env)->NewObject(env, JSHORT_OBJ_TYPE, shortConstructor, s);
}
示例#22
0
文件: jbox.c 项目: ndjensen/jep
jobject JBox_Byte(JNIEnv* env, jbyte b)
{
    if (!JNI_METHOD(byteConstructor, env, JBYTE_OBJ_TYPE, "<init>", "(B)V")) {
        process_java_exception(env);
        return NULL;
    }
    return (*env)->NewObject(env, JBYTE_OBJ_TYPE, byteConstructor, b);
}
示例#23
0
文件: jbox.c 项目: ndjensen/jep
jobject JBox_Boolean(JNIEnv* env, jboolean z)
{
    if (!JNI_METHOD(booleanConstructor, env, JBOOL_OBJ_TYPE, "<init>", "(Z)V")) {
        process_java_exception(env);
        return NULL;
    }
    return (*env)->NewObject(env, JBOOL_OBJ_TYPE, booleanConstructor, z);
}
示例#24
0
/*
 * 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;
    }
}
示例#25
0
文件: jbox.c 项目: ndjensen/jep
jobject JBox_Char(JNIEnv* env, jchar c)
{
    if (!JNI_METHOD(charConstructor, env, JCHAR_OBJ_TYPE, "<init>", "(C)V")) {
        process_java_exception(env);
        return NULL;
    }
    return (*env)->NewObject(env, JCHAR_OBJ_TYPE, charConstructor, c);
}
示例#26
0
文件: jbox.c 项目: ndjensen/jep
jobject JBox_Long(JNIEnv* env, jlong j)
{
    if (!JNI_METHOD(longConstructor, env, JLONG_OBJ_TYPE, "<init>", "(J)V")) {
        process_java_exception(env);
        return NULL;
    }
    return (*env)->NewObject(env, JLONG_OBJ_TYPE, longConstructor, j);
}
示例#27
0
/*
 * Checks if a jobject is an instance of a jep.NDArray
 *
 * @param env   the JNI environment
 * @param obj   the jobject to check
 *
 * @return true if it is an NDArray and jep was compiled with numpy support,
 *          otherwise false
 */
int jndarray_check(JNIEnv *env, jobject obj)
{
    int ret = (*env)->IsInstanceOf(env, obj, JEP_NDARRAY_TYPE);
    if (process_java_exception(env)) {
        return JNI_FALSE;
    }

    return ret;
}
示例#28
0
文件: pyjlist.c 项目: behdad84/jep
/*
 * Method for getting slices with the [int:int] operator on pyjlist.  For
 * example, result = o[i1:i2]
 */
static PyObject* pyjlist_getslice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2) {
    jmethodID         sublist = NULL;
    jobject           result  = NULL;
    PyJobject_Object *obj     = (PyJobject_Object*) o;
    JNIEnv           *env     = pyembed_get_env();

    sublist = (*env)->GetMethodID(env, obj->clazz, "subList", "(II)Ljava/util/List;");
    if(process_java_exception(env) || !sublist) {
        return NULL;
    }

    result = (*env)->CallObjectMethod(env, obj->object, sublist, (jint) i1, (jint) i2);
    if(process_java_exception(env)) {
        return NULL;
    }

    return pyjobject_new(env, result);
}
示例#29
0
/*
 * Gets the size of the map.
 */
static Py_ssize_t pyjmap_len(PyObject *self)
{
    Py_ssize_t    len   = 0;
    PyJObject    *pyjob = (PyJObject*) self;
    JNIEnv       *env   = pyembed_get_env();

    if (mapSize == 0) {
        mapSize = (*env)->GetMethodID(env, JMAP_TYPE, "size", "()I");
        if (process_java_exception(env) || !mapSize) {
            return -1;
        }
    }

    len = (*env)->CallIntMethod(env, pyjob->object, mapSize);
    if (process_java_exception(env)) {
        return -1;
    }
    return len;
}
示例#30
0
文件: pyjlist.c 项目: mrj0/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;
    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;
}