lucene-pylucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From va...@apache.org
Subject svn commit: r1599407 - in /lucene/pylucene/trunk/jcc: CHANGES jcc/sources/JArray.h jcc/sources/functions.cpp jcc/sources/functions.h
Date Tue, 03 Jun 2014 05:00:21 GMT
Author: vajda
Date: Tue Jun  3 05:00:21 2014
New Revision: 1599407

URL: http://svn.apache.org/r1599407
Log:
 - added support for java varargs by reconstructing array from last parameters

Modified:
    lucene/pylucene/trunk/jcc/CHANGES
    lucene/pylucene/trunk/jcc/jcc/sources/JArray.h
    lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp
    lucene/pylucene/trunk/jcc/jcc/sources/functions.h

Modified: lucene/pylucene/trunk/jcc/CHANGES
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/CHANGES?rev=1599407&r1=1599406&r2=1599407&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/CHANGES (original)
+++ lucene/pylucene/trunk/jcc/CHANGES Tue Jun  3 05:00:21 2014
@@ -1,3 +1,8 @@
+Version 2.19 -> 2.20
+--------------------
+ - added support for java varargs by reconstructing array from last parameters
+ - 
+
 Version 2.18 -> 2.19
 --------------------
  - fixed bug with missing class parameters when fixed by declaration

Modified: lucene/pylucene/trunk/jcc/jcc/sources/JArray.h
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/JArray.h?rev=1599407&r1=1599406&r2=1599407&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/JArray.h (original)
+++ lucene/pylucene/trunk/jcc/jcc/sources/JArray.h Tue Jun  3 05:00:21 2014
@@ -24,6 +24,7 @@ typedef int Py_ssize_t;
 #endif
 
 extern jobjectArray fromPySequence(jclass cls, PyObject *sequence);
+extern jobjectArray fromPySequence(jclass cls, PyObject **sequence, int length);
 extern PyObject *PyErr_SetJavaError();
 
 extern PyTypeObject *PY_TYPE(JArrayObject);
@@ -67,6 +68,10 @@ public:
         length = this$ ? env->getArrayLength((jobjectArray) this$) : 0;
     }
 
+    JArray<T>(jclass cls, PyObject **sequence, int length) : java::lang::Object(fromPySequence(cls,
sequence, length)) {
+        length = this$ ? env->getArrayLength((jobjectArray) this$) : 0;
+    }
+
     PyObject *toSequence(PyObject *(*wrapfn)(const T&))
     {
         if (this$ == NULL)
@@ -122,6 +127,10 @@ template<> class JArray<jobject> : publi
         length = this$ ? env->getArrayLength((jobjectArray) this$) : 0;
     }
 
+    JArray<jobject>(jclass cls, PyObject **sequence, int length) : java::lang::Object(fromPySequence(cls,
sequence, length)) {
+        length = this$ ? env->getArrayLength((jobjectArray) this$) : 0;
+    }
+
     PyObject *toSequence(PyObject *(*wrapfn)(const jobject&))
     {
         return toSequence(0, length, wrapfn);
@@ -412,6 +421,26 @@ template<> class JArray<jboolean> : publ
         }
     }
 
+    JArray<jboolean>(PyObject **sequence, int length) : java::lang::Object(env->get_vm_env()->NewBooleanArray(length))
{
+        arrayElements elts = elements();
+        jboolean *buf = (jboolean *) elts;
+
+        for (int i = 0; i < length; i++) {
+            PyObject *obj = sequence[i];
+
+            if (!obj)
+                break;
+
+            if (obj == Py_True || obj == Py_False)
+                buf[i] = (jboolean) (obj == Py_True);
+            else
+            {
+                PyErr_SetObject(PyExc_TypeError, obj);
+                break;
+            }
+        }
+    }
+
     PyObject *toSequence()
     {
         return toSequence(0, length);
@@ -566,6 +595,28 @@ template<> class JArray<jbyte> : public 
             }
     }
 
+    JArray<jbyte>(PyObject **sequence, int length) : java::lang::Object(env->get_vm_env()->NewByteArray(length))
{
+        arrayElements elts = elements();
+        jbyte *buf = (jbyte *) elts;
+
+        for (int i = 0; i < length; i++) {
+            PyObject *obj = sequence[i];
+
+            if (!obj)
+                break;
+
+            if (PyString_Check(obj) && (PyString_GET_SIZE(obj) == 1))
+                buf[i] = (jbyte) PyString_AS_STRING(obj)[0];
+            else if (PyInt_CheckExact(obj))
+                buf[i] = (jbyte) PyInt_AS_LONG(obj);
+            else
+            {
+                PyErr_SetObject(PyExc_TypeError, obj);
+                break;
+            }
+        }
+    }
+
     char getType()
     {
         return 'Z';
@@ -745,6 +796,26 @@ template<> class JArray<jchar> : public 
             }
     }
 
+    JArray<jchar>(PyObject **sequence, int length) : java::lang::Object(env->get_vm_env()->NewCharArray(length))
{
+        arrayElements elts = elements();
+        jchar *buf = (jchar *) elts;
+
+        for (int i = 0; i < length; i++) {
+            PyObject *obj = sequence[i];
+
+            if (!obj)
+                break;
+
+            if (PyUnicode_Check(obj) && (PyUnicode_GET_SIZE(obj) == 1))
+                buf[i] = (jchar) PyUnicode_AS_UNICODE(obj)[0];
+            else
+            {
+                PyErr_SetObject(PyExc_TypeError, obj);
+                break;
+            }
+        }
+    }
+
     PyObject *toSequence()
     {
         return toSequence(0, length);
@@ -919,6 +990,26 @@ template<> class JArray<jdouble> : publi
         }
     }
 
+    JArray<jdouble>(PyObject **sequence, int length) : java::lang::Object(env->get_vm_env()->NewDoubleArray(length))
{
+        arrayElements elts = elements();
+        jdouble *buf = (jdouble *) elts;
+
+        for (int i = 0; i < length; i++) {
+            PyObject *obj = sequence[i];
+
+            if (!obj)
+                break;
+
+            if (PyFloat_Check(obj))
+                buf[i] = (jdouble) PyFloat_AS_DOUBLE(obj);
+            else
+            {
+                PyErr_SetObject(PyExc_TypeError, obj);
+                break;
+            }
+        }
+    }
+
     PyObject *toSequence()
     {
         return toSequence(0, length);
@@ -1066,6 +1157,26 @@ template<> class JArray<jfloat> : public
         }
     }
 
+    JArray<jfloat>(PyObject **sequence, int length) : java::lang::Object(env->get_vm_env()->NewFloatArray(length))
{
+        arrayElements elts = elements();
+        jfloat *buf = (jfloat *) elts;
+
+        for (int i = 0; i < length; i++) {
+            PyObject *obj = sequence[i];
+
+            if (!obj)
+                break;
+
+            if (PyFloat_Check(obj))
+                buf[i] = (jfloat) PyFloat_AS_DOUBLE(obj);
+            else
+            {
+                PyErr_SetObject(PyExc_TypeError, obj);
+                break;
+            }
+        }
+    }
+
     PyObject *toSequence()
     {
         return toSequence(0, length);
@@ -1213,6 +1324,26 @@ template<> class JArray<jint> : public j
         }
     }
 
+    JArray<jint>(PyObject **sequence, int length) : java::lang::Object(env->get_vm_env()->NewIntArray(length))
{
+        arrayElements elts = elements();
+        jint *buf = (jint *) elts;
+
+        for (int i = 0; i < length; i++) {
+            PyObject *obj = sequence[i];
+
+            if (!obj)
+                break;
+
+            if (PyInt_Check(obj))
+                buf[i] = (jint) PyInt_AS_LONG(obj);
+            else
+            {
+                PyErr_SetObject(PyExc_TypeError, obj);
+                break;
+            }
+        }
+    }
+
     PyObject *toSequence()
     {
         return toSequence(0, length);
@@ -1360,6 +1491,26 @@ template<> class JArray<jlong> : public 
         }
     }
 
+    JArray<jlong>(PyObject **sequence, int length) : java::lang::Object(env->get_vm_env()->NewLongArray(length))
{
+        arrayElements elts = elements();
+        jlong *buf = (jlong *) elts;
+
+        for (int i = 0; i < length; i++) {
+            PyObject *obj = sequence[i];
+
+            if (!obj)
+                break;
+
+            if (PyLong_Check(obj))
+                buf[i] = (jlong) PyLong_AsLongLong(obj);
+            else
+            {
+                PyErr_SetObject(PyExc_TypeError, obj);
+                break;
+            }
+        }
+    }
+
     PyObject *toSequence()
     {
         return toSequence(0, length);
@@ -1507,6 +1658,26 @@ template<> class JArray<jshort> : public
         }
     }
 
+    JArray<jshort>(PyObject **sequence, int length) : java::lang::Object(env->get_vm_env()->NewShortArray(length))
{
+        arrayElements elts = elements();
+        jshort *buf = (jshort *) elts;
+
+        for (int i = 0; i < length; i++) {
+            PyObject *obj = sequence[i];
+
+            if (!obj)
+                break;
+
+            if (PyInt_Check(obj))
+                buf[i] = (jshort) PyInt_AS_LONG(obj);
+            else
+            {
+                PyErr_SetObject(PyExc_TypeError, obj);
+                break;
+            }
+        }
+    }
+
     PyObject *toSequence()
     {
         return toSequence(0, length);

Modified: lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp?rev=1599407&r1=1599406&r2=1599407&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp (original)
+++ lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp Tue Jun  3 05:00:21 2014
@@ -334,9 +334,6 @@ int _parseArgs(PyObject **args, unsigned
     unsigned int typeCount = strlen(types);
     va_list list, check;
 
-    if (count > typeCount)
-        return -1;
-
     va_start(list, types);
     va_start(check, types);
 #endif
@@ -355,6 +352,9 @@ int _parseArgs(PyObject **args, unsigned
         return -1;
     }
 
+    bool last = false;
+    bool varargs = false;
+    bool empty = false;
     unsigned int pos = 0;
     int array = 0;
 
@@ -362,6 +362,8 @@ int _parseArgs(PyObject **args, unsigned
         PyObject *arg = args[a];
         char tc = types[pos];
 
+        last = last || types[pos + 1] == '\0';
+
         if (array > 1 && tc != '[')
           tc = 'o';
 
@@ -448,6 +450,30 @@ int _parseArgs(PyObject **args, unsigned
                       else
                           break;
                   }
+
+                  if (last)
+                  {
+                      int ok = 0;
+
+                      if (arg == Py_None)
+                          ok = 1;
+                      else if (PyObject_TypeCheck(arg, &PY_TYPE(Object)) &&
+                               vm_env->IsInstanceOf(((t_Object *) arg)->object.this$,
cls))
+                          ok = 1;
+                      else if (PyObject_TypeCheck(arg, &PY_TYPE(FinalizerProxy)))
+                      {
+                          PyObject *o = ((t_fp *) arg)->object;
+
+                          if (PyObject_TypeCheck(o, &PY_TYPE(Object)) &&
+                              vm_env->IsInstanceOf(((t_Object *) o)->object.this$,
cls))
+                              ok = 1;
+                      }
+                      if (ok)
+                      {
+                          varargs = true;
+                          break;
+                      }
+                  }
               }
               else if (PyObject_TypeCheck(arg, &PY_TYPE(Object)) &&
                        vm_env->IsInstanceOf(((t_Object *) arg)->object.this$, cls))
@@ -487,6 +513,12 @@ int _parseArgs(PyObject **args, unsigned
                       else
                           break;
                   }
+
+                  if (last && (arg == Py_True || arg == Py_False))
+                  {
+                      varargs = true;
+                      break;
+                  }
               }
               else if (arg == Py_True || arg == Py_False)
                   break;
@@ -502,6 +534,16 @@ int _parseArgs(PyObject **args, unsigned
                       break;
                   if (PyObject_TypeCheck(arg, PY_TYPE(JArrayByte)))
                       break;
+
+                  if (last)
+                  {
+                      if ((PyString_Check(arg) && (PyString_Size(arg) == 1)) ||
+                          PyInt_CheckExact(arg))
+                      {
+                          varargs = true;
+                          break;
+                      }
+                  }
               }
               else if (PyString_Check(arg) && (PyString_Size(arg) == 1))
                   break;
@@ -519,6 +561,15 @@ int _parseArgs(PyObject **args, unsigned
                       break;
                   if (PyObject_TypeCheck(arg, PY_TYPE(JArrayChar)))
                       break;
+
+                  if (last)
+                  {
+                      if (PyUnicode_Check(arg) && (PyUnicode_GET_SIZE(arg) == 1))
+                      {
+                          varargs = true;
+                          break;
+                      }
+                  }
               }
               else if (PyUnicode_Check(arg) && PyUnicode_GET_SIZE(arg) == 1)
                   break;
@@ -549,6 +600,12 @@ int _parseArgs(PyObject **args, unsigned
                       else
                           break;
                   }
+
+                  if (last && PyInt_CheckExact(arg))
+                  {
+                      varargs = true;
+                      break;
+                  }
               }
               else if (PyInt_CheckExact(arg))
                   break;
@@ -580,6 +637,12 @@ int _parseArgs(PyObject **args, unsigned
                       else
                           break;
                   }
+
+                  if (last && PyInt_CheckExact(arg))
+                  {
+                      varargs = true;
+                      break;
+                  }
               }
               else if (PyInt_CheckExact(arg))
                   break;
@@ -611,6 +674,12 @@ int _parseArgs(PyObject **args, unsigned
                       else
                           break;
                   }
+
+                  if (last && PyFloat_CheckExact(arg))
+                  {
+                      varargs = true;
+                      break;
+                  }
               }
               else if (PyFloat_CheckExact(arg))
                   break;
@@ -642,6 +711,12 @@ int _parseArgs(PyObject **args, unsigned
                       else
                           break;
                   }
+
+                  if (last && PyFloat_CheckExact(arg))
+                  {
+                      varargs = true;
+                      break;
+                  }
               }
               else if (PyFloat_CheckExact(arg))
                   break;
@@ -673,6 +748,12 @@ int _parseArgs(PyObject **args, unsigned
                       else
                           break;
                   }
+
+                  if (last && PyLong_CheckExact(arg))
+                  {
+                      varargs = true;
+                      break;
+                  }
               }
               else if (PyLong_CheckExact(arg))
                   break;
@@ -706,6 +787,13 @@ int _parseArgs(PyObject **args, unsigned
                       }
                       else
                           break;
+                  } 
+
+                  if (last && (arg == Py_None ||
+                               PyString_Check(arg) || PyUnicode_Check(arg)))
+                  {
+                      varargs = true;
+                      break;
                   }
               }
               else if (arg == Py_None ||
@@ -761,19 +849,45 @@ int _parseArgs(PyObject **args, unsigned
 
         if (tc != '[')
             array = 0;
+
+        if (varargs)
+        {
+            pos = typeCount;
+            break;
+        }
     }
 
     if (array)
         return -1;
 
+    if (pos == typeCount - 2 && types[pos] == '[' && types[pos + 1] != '[')
+    {
+        varargs = true;
+        empty = true;
+        pos = typeCount;
+    }
+
     if (pos != typeCount)
       return -1;
 
     pos = 0;
+    last = false;
 
-    for (unsigned int a = 0; a < count; a++, pos++) {
-        PyObject *arg = args[a];
+    for (unsigned int a = 0; a <= count; a++, pos++) {
         char tc = types[pos];
+        PyObject *arg;
+
+        if (a == count)
+        {
+            if (empty)  /* empty varargs */
+                arg = NULL;
+            else
+                break;
+        }
+        else
+            arg = args[a];
+
+        last = last || types[pos + 1] == '\0';
 
         if (array > 1 && tc != '[')
             tc = 'o';
@@ -819,6 +933,8 @@ int _parseArgs(PyObject **args, unsigned
 #endif
                   if (arg == Py_None)
                       *array = JArray<jobject>((jobject) NULL);
+                  else if (last && varargs)
+                      *array = fromPySequence(cls, args + a, count - a);
                   else if (PyObject_TypeCheck(arg, PY_TYPE(JArrayObject)))
                       *array = ((t_JArray<jobject> *) arg)->array;
                   else 
@@ -863,6 +979,8 @@ int _parseArgs(PyObject **args, unsigned
 
                   if (arg == Py_None)
                       *array = JArray<jboolean>((jobject) NULL);
+                  else if (last && varargs)
+                      *array = JArray<jboolean>(args + a, count - a);
                   else if (PyObject_TypeCheck(arg, PY_TYPE(JArrayBool)))
                       *array = ((t_JArray<jboolean> *) arg)->array;
                   else
@@ -887,6 +1005,8 @@ int _parseArgs(PyObject **args, unsigned
 
                   if (arg == Py_None)
                       *array = JArray<jbyte>((jobject) NULL);
+                  else if (last && varargs)
+                      *array = JArray<jbyte>(args + a, count - a);
                   else if (PyObject_TypeCheck(arg, PY_TYPE(JArrayByte)))
                       *array = ((t_JArray<jbyte> *) arg)->array;
                   else 
@@ -916,6 +1036,8 @@ int _parseArgs(PyObject **args, unsigned
 
                   if (arg == Py_None)
                       *array = JArray<jchar>((jobject) NULL);
+                  else if (last && varargs)
+                      *array = JArray<jchar>(args + a, count - a);
                   else if (PyObject_TypeCheck(arg, PY_TYPE(JArrayChar)))
                       *array = ((t_JArray<jchar> *) arg)->array;
                   else 
@@ -940,6 +1062,8 @@ int _parseArgs(PyObject **args, unsigned
 
                   if (arg == Py_None)
                       *array = JArray<jint>((jobject) NULL);
+                  else if (last && varargs)
+                      *array = JArray<jint>(args + a, count - a);
                   else if (PyObject_TypeCheck(arg, PY_TYPE(JArrayInt)))
                       *array = ((t_JArray<jint> *) arg)->array;
                   else 
@@ -964,6 +1088,8 @@ int _parseArgs(PyObject **args, unsigned
 
                   if (arg == Py_None)
                       *array = JArray<jshort>((jobject) NULL);
+                  else if (last && varargs)
+                      *array = JArray<jshort>(args + a, count - a);
                   else if (PyObject_TypeCheck(arg, PY_TYPE(JArrayShort)))
                       *array = ((t_JArray<jshort> *) arg)->array;
                   else 
@@ -988,6 +1114,8 @@ int _parseArgs(PyObject **args, unsigned
 
                   if (arg == Py_None)
                       *array = JArray<jdouble>((jobject) NULL);
+                  else if (last && varargs)
+                      *array = JArray<jdouble>(args + a, count - a);
                   else if (PyObject_TypeCheck(arg, PY_TYPE(JArrayDouble)))
                       *array = ((t_JArray<jdouble> *) arg)->array;
                   else 
@@ -1012,6 +1140,8 @@ int _parseArgs(PyObject **args, unsigned
 
                   if (arg == Py_None)
                       *array = JArray<jfloat>((jobject) NULL);
+                  else if (last && varargs)
+                      *array = JArray<jfloat>(args + a, count - a);
                   else if (PyObject_TypeCheck(arg, PY_TYPE(JArrayFloat)))
                       *array = ((t_JArray<jfloat> *) arg)->array;
                   else 
@@ -1036,6 +1166,8 @@ int _parseArgs(PyObject **args, unsigned
 
                   if (arg == Py_None)
                       *array = JArray<jlong>((jobject) NULL);
+                  else if (last && varargs)
+                      *array = JArray<jlong>(args + a, count - a);
                   else if (PyObject_TypeCheck(arg, PY_TYPE(JArrayLong)))
                       *array = ((t_JArray<jlong> *) arg)->array;
                   else 
@@ -1060,9 +1192,13 @@ int _parseArgs(PyObject **args, unsigned
 
                   if (arg == Py_None)
                       *array = JArray<jstring>((jobject) NULL);
+                  else if (last && varargs)
+                      *array = fromPySequence(
+                          env->getClass(String::initializeClass),
+                          args + a, count - a);
                   else if (PyObject_TypeCheck(arg, PY_TYPE(JArrayString)))
                       *array = ((t_JArray<jstring> *) arg)->array;
-                  else 
+                  else
                       *array = JArray<jstring>(arg);
 
                   if (PyErr_Occurred())
@@ -1093,6 +1229,10 @@ int _parseArgs(PyObject **args, unsigned
 
                   if (arg == Py_None)
                       *array = JArray<Object>((jobject) NULL);
+                  else if (last && varargs)
+                      *array = JArray<Object>(
+                          env->getClass(Object::initializeClass),
+                          args + a, count - a);
                   else 
                       *array = JArray<Object>(arg);
 
@@ -1146,6 +1286,12 @@ int _parseArgs(PyObject **args, unsigned
 
         if (tc != '[')
             array = 0;
+
+        if (last && varargs)
+        {
+            pos = typeCount;
+            break;
+        }
     }
 
     if (pos == typeCount)
@@ -1407,6 +1553,69 @@ PyObject *get_extension_nextElement(PyOb
     return PyObject_CallMethod(self, "nextElement", "");
 }
 
+static bool setArrayObj(jobjectArray array, int index, PyObject *obj)
+{
+    bool deleteLocal = false;
+    jobject jobj;
+
+    if (obj == Py_None)
+      jobj = NULL;
+    else if (PyString_Check(obj) || PyUnicode_Check(obj))
+    {
+        jobj = env->fromPyString(obj);
+        deleteLocal = 1;
+    }
+    else if (PyObject_TypeCheck(obj, &PY_TYPE(JObject)))
+        jobj = ((t_JObject *) obj)->object.this$;
+    else if (PyObject_TypeCheck(obj, &PY_TYPE(FinalizerProxy)))
+        jobj = ((t_JObject *) ((t_fp *) obj)->object)->object.this$;
+    else if (obj == Py_True || obj == Py_False)
+    {
+        jobj = env->boxBoolean(obj == Py_True);
+        deleteLocal = 1;
+    }
+    else if (PyFloat_Check(obj))
+    {
+        jobj = env->boxDouble(PyFloat_AS_DOUBLE(obj));
+        deleteLocal = 1;
+    }
+    else if (PyInt_Check(obj))
+    {
+        jobj = env->boxInteger(PyInt_AS_LONG(obj));
+        deleteLocal = 1;
+    }
+    else if (PyLong_Check(obj))
+    {
+        jobj = env->boxLong(PyLong_AsLongLong(obj));
+        deleteLocal = 1;
+    }
+    else
+    {
+        PyErr_SetObject(PyExc_TypeError, obj);
+        Py_DECREF(obj);
+        return false;
+    }
+
+    try {
+        env->setObjectArrayElement(array, index, jobj);
+        if (deleteLocal)
+            env->get_vm_env()->DeleteLocalRef(jobj);
+        Py_DECREF(obj);
+    } catch (int e) {
+        Py_DECREF(obj);
+        switch (e) {
+          case _EXC_JAVA:
+            PyErr_SetJavaError();
+            return false;
+          default:
+            throw;
+        }
+    }
+
+    return true;
+}
+
+
 jobjectArray fromPySequence(jclass cls, PyObject *sequence)
 {
     if (sequence == Py_None)
@@ -1435,70 +1644,48 @@ jobjectArray fromPySequence(jclass cls, 
         }
     }
 
-    JNIEnv *vm_env = env->get_vm_env();
-
     for (int i = 0; i < length; i++) {
         PyObject *obj = PySequence_GetItem(sequence, i);
-        int deleteLocal = 0;
-        jobject jobj;
 
         if (!obj)
             break;
-        else if (obj == Py_None)
-            jobj = NULL;
-        else if (PyString_Check(obj) || PyUnicode_Check(obj))
-        {
-            jobj = env->fromPyString(obj);
-            deleteLocal = 1;
-        }
-        else if (PyObject_TypeCheck(obj, &PY_TYPE(JObject)))
-            jobj = ((t_JObject *) obj)->object.this$;
-        else if (PyObject_TypeCheck(obj, &PY_TYPE(FinalizerProxy)))
-            jobj = ((t_JObject *) ((t_fp *) obj)->object)->object.this$;
-        else if (obj == Py_True || obj == Py_False)
-        {
-            jobj = env->boxBoolean(obj == Py_True);
-            deleteLocal = 1;
-        }
-        else if (PyFloat_Check(obj))
-        {
-            jobj = env->boxDouble(PyFloat_AS_DOUBLE(obj));
-            deleteLocal = 1;
-        }
-        else if (PyInt_Check(obj))
-        {
-            jobj = env->boxInteger(PyInt_AS_LONG(obj));
-            deleteLocal = 1;
-        }
-        else if (PyLong_Check(obj))
-        {
-            jobj = env->boxLong(PyLong_AsLongLong(obj));
-            deleteLocal = 1;
-        }
-        else
-        {
-            PyErr_SetObject(PyExc_TypeError, obj);
-            Py_DECREF(obj);
+
+        if (!setArrayObj(array, i, obj))
             return NULL;
-        }
+    }
 
-        try {
-            env->setObjectArrayElement(array, i, jobj);
-            if (deleteLocal)
-                vm_env->DeleteLocalRef(jobj);
-            Py_DECREF(obj);
-        } catch (int e) {
-            Py_DECREF(obj);
-            switch (e) {
-              case _EXC_JAVA:
-                PyErr_SetJavaError();
-                return NULL;
-              default:
-                throw;
-            }
+    return array;
+}
+
+jobjectArray fromPySequence(jclass cls, PyObject **args, int length)
+{
+    jobjectArray array;
+
+    try {
+        array = env->newObjectArray(cls, length);
+    } catch (int e) {
+        switch (e) {
+          case _EXC_PYTHON:
+            return NULL;
+          case _EXC_JAVA:
+            PyErr_SetJavaError();
+            return NULL;
+          default:
+            throw;
         }
     }
 
+    for (int i = 0; i < length; i++) {
+        PyObject *obj = args[i];
+
+        if (!obj)
+            break;
+
+        Py_INCREF(obj);
+        if (!setArrayObj(array, i, obj))
+            return NULL;
+    }
+
     return array;
 }
 

Modified: lucene/pylucene/trunk/jcc/jcc/sources/functions.h
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/functions.h?rev=1599407&r1=1599406&r2=1599407&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/functions.h (original)
+++ lucene/pylucene/trunk/jcc/jcc/sources/functions.h Tue Jun  3 05:00:21 2014
@@ -276,6 +276,7 @@ PyObject *get_extension_next(PyObject *s
 PyObject *get_extension_nextElement(PyObject *self);
 
 jobjectArray fromPySequence(jclass cls, PyObject *sequence);
+jobjectArray fromPySequence(jclass cls, PyObject **args, int length);
 PyObject *castCheck(PyObject *obj, getclassfn initializeClass,
                     int reportError);
 void installType(PyTypeObject *type, PyObject *module, char *name,



Mime
View raw message