Skip to content

Commit

Permalink
Fix conversion of numpy ndarrays to java primitive arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
bsteffensmeier committed Oct 10, 2016
1 parent 5fb3f5a commit 4e0a1fe
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/jep/convert_p2j.c
Expand Up @@ -490,7 +490,11 @@ jobject PyObject_As_jobject(JNIEnv *env, PyObject *pyobject,
return pydict_as_jobject(env, pyobject, expectedType);
#if JEP_NUMPY_ENABLED
} else if (npy_array_check(pyobject)) {
return convert_pyndarray_jndarray(env, pyobject);
if ((*env)->IsAssignableFrom(env, JEP_NDARRAY_TYPE, expectedType)) {
return convert_pyndarray_jndarray(env, pyobject);
} else {
return convert_pyndarray_jprimitivearray(env, pyobject, expectedType);
}
#endif
} else if ((*env)->IsAssignableFrom(env, JSTRING_TYPE, expectedType)) {
return (jobject) PyObject_As_jstring(env, pyobject);
Expand Down
14 changes: 7 additions & 7 deletions src/jep/test/numpy/TestNumpy.java
Expand Up @@ -179,7 +179,7 @@ public NDArray<int[]> testArgAndReturn(NDArray<int[]> array) {
* @return true on success
*/
public boolean callBooleanMethod(boolean[] array) {
return array != null;
return array != null && array.getClass().isArray();
}

/**
Expand All @@ -190,7 +190,7 @@ public boolean callBooleanMethod(boolean[] array) {
* @return true on success
*/
public boolean callByteMethod(byte[] array) {
return array != null;
return array != null && array.getClass().isArray();
}

/**
Expand All @@ -201,7 +201,7 @@ public boolean callByteMethod(byte[] array) {
* @return true on success
*/
public boolean callShortMethod(short[] array) {
return array != null;
return array != null && array.getClass().isArray();
}

/**
Expand All @@ -212,7 +212,7 @@ public boolean callShortMethod(short[] array) {
* @return true on success
*/
public boolean callIntMethod(int[] array) {
return array != null;
return array != null && array.getClass().isArray();
}

/**
Expand All @@ -223,7 +223,7 @@ public boolean callIntMethod(int[] array) {
* @return true on success
*/
public boolean callLongMethod(long[] array) {
return array != null;
return array != null && array.getClass().isArray();
}

/**
Expand All @@ -234,7 +234,7 @@ public boolean callLongMethod(long[] array) {
* @return true on success
*/
public boolean callFloatMethod(float[] array) {
return array != null;
return array != null && array.getClass().isArray();
}

/**
Expand All @@ -245,7 +245,7 @@ public boolean callFloatMethod(float[] array) {
* @return true on success
*/
public boolean callDoubleMethod(double[] array) {
return array != null;
return array != null && array.getClass().isArray();
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/test_numpy.py
Expand Up @@ -98,6 +98,12 @@ def testArrayParams(self):
da.fill(True)
self.assertTrue(self.test.callDoubleMethod(da))

def testIncompatibleConversion(self):
import numpy
fa = numpy.zeros((15, 5), numpy.float32)
with self.assertRaises(TypeError):
self.test.callDoubleMethod(fa)

def testCharArrayCreation(self):
NDArray = jep.findClass("jep.NDArray")
with self.assertRaises(ValueError):
Expand Down

0 comments on commit 4e0a1fe

Please sign in to comment.