Permalink
Browse files

Fix conversion of numpy ndarrays to java primitive arrays.

  • Loading branch information...
1 parent 5fb3f5a commit 4e0a1fe71c27082370802ea91323422565d77824 @bsteffensmeier bsteffensmeier committed Oct 10, 2016
Showing with 18 additions and 8 deletions.
  1. +5 −1 src/jep/convert_p2j.c
  2. +7 −7 src/jep/test/numpy/TestNumpy.java
  3. +6 −0 tests/test_numpy.py
@@ -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);
@@ -179,7 +179,7 @@ public void testSetAndGet(Jep jep) throws JepException {
* @return true on success
*/
public boolean callBooleanMethod(boolean[] array) {
- return array != null;
+ return array != null && array.getClass().isArray();
}
/**
@@ -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();
}
/**
@@ -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();
}
/**
@@ -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();
}
/**
@@ -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();
}
/**
@@ -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();
}
/**
@@ -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();
}
/**
View
@@ -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):

0 comments on commit 4e0a1fe

Please sign in to comment.