Skip to content

Commit

Permalink
Simplify and improve error message for the object case of convert_pya…
Browse files Browse the repository at this point in the history
…rg_jvalue.
  • Loading branch information
bsteffensmeier committed Mar 24, 2016
1 parent 52ef441 commit c53e591
Showing 1 changed file with 27 additions and 49 deletions.
76 changes: 27 additions & 49 deletions src/jep/jep_util.c
Expand Up @@ -1172,59 +1172,37 @@ jvalue convert_pyarg_jvalue(JNIEnv *env,
}

case JOBJECT_ID: {
jobject obj = NULL;
// none is okay, we'll translate to null
if (param == Py_None) {
;
} else if (PyString_Check(param)) {
char *val;

// strings count as objects here
if (!(*env)->IsAssignableFrom(env,
JSTRING_TYPE,
paramType)) {
PyErr_Format(
PyExc_TypeError,
"Tried to set a string on an incomparable parameter %i.",
pos + 1);
return ret;
}

val = PyString_AsString(param);
obj = (*env)->NewStringUTF(env, (const char *) val);
}
#if JEP_NUMPY_ENABLED
else if (npy_array_check(param)) {
ret.l = convert_pyndarray_jndarray(env, param);
return ret;
}
#endif
else {
if (!pyjobject_check(param)) {
obj = pyembed_box_py(env, param);
if ((*env)->IsInstanceOf(env, obj, paramType)){
ret.l = obj;
}else{
PyErr_Format(PyExc_TypeError,
"Expected object parameter at %i.",
pos + 1);
}
return ret;
jobject obj = pyembed_box_py(env, param);
if (obj != NULL && !(*env)->IsInstanceOf(env, obj, paramType)) {
jmethodID getName;
jstring expTypeJavaName, actTypeJavaName = NULL;
const char *expTypeName, *actTypeName;

This comment has been minimized.

Copy link
@ndjensen

ndjensen Mar 24, 2016

Member

Two blank lines is weird.



getName = (*env)->GetMethodID(env, JCLASS_TYPE, "getName",
"()Ljava/lang/String;");

This comment has been minimized.

Copy link
@ndjensen

ndjensen Mar 24, 2016

Member

I've got Class.getName() cached over in pyjobject.c but it's static and not accessible outside that file. Candidate for moving that over here to the top of this file and being non-static so other files can get at it. Not really worth it at this time though since this is only triggered on exceptions when it can't match the type.

expTypeJavaName = (*env)->CallObjectMethod(env, paramType, getName);
expTypeName = (*env)->GetStringUTFChars(env, expTypeJavaName, 0);
if (pyjclass_check(param)) {
actTypeJavaName = (*env)->CallObjectMethod(env, JCLASS_TYPE, getName);
actTypeName = (*env)->GetStringUTFChars(env, actTypeJavaName, 0);
} else if (pyjobject_check(param)) {
actTypeJavaName = (*env)->CallObjectMethod(env, ((PyJObject *) param)->clazz,
getName);
actTypeName = (*env)->GetStringUTFChars(env, actTypeJavaName, 0);
} else {
actTypeName = param->ob_type->tp_name;
}

// check object itself is assignable to that type.
if (!(*env)->IsAssignableFrom(env,
((PyJObject *) param)->clazz,
paramType)) {
PyErr_Format(PyExc_TypeError,
"Incorrect object type at %i.",
pos + 1);
return ret;
PyErr_Format(PyExc_TypeError,
"Expected %s at parameter %i but received a %s.",
expTypeName, pos + 1, actTypeName);
(*env)->ReleaseStringUTFChars(env, expTypeJavaName, expTypeName);
if (actTypeJavaName) {
(*env)->ReleaseStringUTFChars(env, actTypeJavaName, actTypeName);
}

obj = ((PyJObject *) param)->object;
obj = NULL;
}

ret.l = obj;
return ret;
}
Expand Down

0 comments on commit c53e591

Please sign in to comment.