lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From va...@apache.org
Subject svn commit: r755707 - in /lucene/pylucene/trunk: ./ jcc/ jcc/_jcc/ jcc/jcc/ jcc/jcc/sources/ test/
Date Wed, 18 Mar 2009 19:15:48 GMT
Author: vajda
Date: Wed Mar 18 19:15:48 2009
New Revision: 755707

URL: http://svn.apache.org/viewvc?rev=755707&view=rev
Log:
 - failure to call initVM() now reported with error instead of crash

Modified:
    lucene/pylucene/trunk/Makefile
    lucene/pylucene/trunk/jcc/CHANGES
    lucene/pylucene/trunk/jcc/_jcc/boot.cpp
    lucene/pylucene/trunk/jcc/jcc/cpp.py
    lucene/pylucene/trunk/jcc/jcc/python.py
    lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.cpp
    lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.h
    lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp
    lucene/pylucene/trunk/jcc/jcc/sources/functions.h
    lucene/pylucene/trunk/jcc/jcc/sources/jcc.cpp
    lucene/pylucene/trunk/test/test_PythonDirectory.py

Modified: lucene/pylucene/trunk/Makefile
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/Makefile?rev=755707&r1=755706&r2=755707&view=diff
==============================================================================
--- lucene/pylucene/trunk/Makefile (original)
+++ lucene/pylucene/trunk/Makefile Wed Mar 18 19:15:48 2009
@@ -14,7 +14,7 @@
 # site-packages directory.
 #
 
-VERSION=2.4.1-rc4
+VERSION=2.4.1-rc5
 LUCENE_SVN_VER=HEAD
 LUCENE_VER=2.4.1
 LUCENE_SVN=http://svn.apache.org/repos/asf/lucene/java/tags/lucene_2_4_1

Modified: lucene/pylucene/trunk/jcc/CHANGES
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/CHANGES?rev=755707&r1=755706&r2=755707&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/CHANGES (original)
+++ lucene/pylucene/trunk/jcc/CHANGES Wed Mar 18 19:15:48 2009
@@ -16,6 +16,7 @@
  - added isCurrentThreadAttached() method to VMEnv
  - added MANIFEST.in to exclude generated jcc/config.py (Christian Heimes)
  - passing strings for byte[] or char[] is no longer supported, use JArray
+ - failure to call initVM() now reported with error instead of crash
 
 Version 2.0 -> 2.1
 ------------------

Modified: lucene/pylucene/trunk/jcc/_jcc/boot.cpp
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/_jcc/boot.cpp?rev=755707&r1=755706&r2=755707&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/_jcc/boot.cpp (original)
+++ lucene/pylucene/trunk/jcc/_jcc/boot.cpp Wed Mar 18 19:15:48 2009
@@ -18,8 +18,9 @@
 #include "java/lang/RuntimeException.h"
 #include "macros.h"
 
-extern PyTypeObject JObject$$Type, JCCEnv$$Type, ConstVariableDescriptor$$Type;
+extern PyTypeObject JObject$$Type, ConstVariableDescriptor$$Type;
 
+PyObject *initJCC(PyObject *module);
 PyObject *initVM(PyObject *self, PyObject *args, PyObject *kwds);
 
 namespace java {
@@ -49,8 +50,9 @@
     {
         PyObject *m = Py_InitModule3("_jcc", jcc_funcs, "_jcc");
 
+        initJCC(m);
+
         INSTALL_TYPE(JObject, m);
-        INSTALL_TYPE(JCCEnv, m);
         INSTALL_TYPE(ConstVariableDescriptor, m);
         java::lang::__install__(m);
     }

Modified: lucene/pylucene/trunk/jcc/jcc/cpp.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/cpp.py?rev=755707&r1=755706&r2=755707&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/cpp.py (original)
+++ lucene/pylucene/trunk/jcc/jcc/cpp.py Wed Mar 18 19:15:48 2009
@@ -637,16 +637,11 @@
         line(out, indent, 'static jfieldID *fids$;');
     line(out, indent, 'static jclass initializeClass();');
     line(out)
-    line(out, indent, 'static inline java::lang::Class *getclass$() {')
-    line(out, indent + 1, 'initializeClass();')
-    line(out, indent + 1, 'return class$;');
-    line(out, indent, '}')
-
-    line(out)
 
     line(out, indent, 'explicit %s(jobject obj) : %s(obj) {',
          cppname(names[-1]), '::'.join(cppnames(superNames)))
-    line(out, indent + 1, 'initializeClass();')
+    line(out, indent + 1, 'if (obj != NULL)');
+    line(out, indent + 2, 'initializeClass();')
     line(out, indent, '}')
     line(out, indent, '%s(const %s& obj) : %s(obj) {}',
          cppname(names[-1]), cppname(names[-1]),

Modified: lucene/pylucene/trunk/jcc/jcc/python.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/python.py?rev=755707&r1=755706&r2=755707&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/python.py (original)
+++ lucene/pylucene/trunk/jcc/jcc/python.py Wed Mar 18 19:15:48 2009
@@ -61,7 +61,7 @@
             return array + 's'
         if clsName == 'java.lang.Object':
             return array + 'o'
-        return array + 'j'
+        return array + 'k'
 
     def checkarg(cls):
         while cls.isArray():
@@ -69,7 +69,7 @@
         if (cls.isPrimitive() or
             cls.getName() in ('java.lang.String', 'java.lang.Object')):
             return ''
-        return ', %s::getclass$()' %(typename(cls, current, False))
+        return ', %s::initializeClass' %(typename(cls, current, False))
 
     def callarg(cls, i):
         return ', &a%d' %(i)
@@ -858,14 +858,14 @@
         line(out)
         line(out, indent, 'static PyObject *t_%s_cast_(PyTypeObject *type, PyObject *arg)',
names[-1])
         line(out, indent, '{')
-        line(out, indent + 1, 'if (!(arg = castCheck(arg, %s::initializeClass(), 1)))', cppname(names[-1]))
+        line(out, indent + 1, 'if (!(arg = castCheck(arg, %s::initializeClass, 1)))', cppname(names[-1]))
         line(out, indent + 2, 'return NULL;')
         line(out, indent + 1, 'return t_%s::wrap_Object(%s(((t_%s *) arg)->object.this$));',
names[-1], cppname(names[-1]), names[-1])
         line(out, indent, '}')
 
         line(out, indent, 'static PyObject *t_%s_instance_(PyTypeObject *type, PyObject *arg)',
names[-1])
         line(out, indent, '{')
-        line(out, indent + 1, 'if (!castCheck(arg, %s::initializeClass(), 0))', cppname(names[-1]))
+        line(out, indent + 1, 'if (!castCheck(arg, %s::initializeClass, 0))', cppname(names[-1]))
         line(out, indent + 2, 'Py_RETURN_FALSE;')
         line(out, indent + 1, 'Py_RETURN_TRUE;')
         line(out, indent, '}')
@@ -1243,18 +1243,11 @@
         package(None, False, cppdir, namespaces, ())
 
     line(out)
+    line(out, 0, 'PyObject *initJCC(PyObject *module);')
     line(out, 0, 'void __install__(PyObject *module);')
     line(out, 0, 'extern PyTypeObject JObject$$Type, ConstVariableDescriptor$$Type, FinalizerClass$$Type,
FinalizerProxy$$Type;')
     line(out, 0, 'extern void _install_jarray(PyObject *);')
     line(out)
-    line(out, 0, '#if defined(_jcc_shared) && (defined(_MSC_VER) || defined(__WIN32))')
-    line(out, 0, '#define _DLL_IMPORT __declspec(dllimport)')
-    line(out, 0, '#else')
-    line(out, 0, '#define _DLL_IMPORT')
-    line(out, 0, '#endif')
-    line(out, 0, '_DLL_IMPORT extern PyTypeObject JCCEnv$$Type;')
-
-    line(out)
     line(out, 0, 'extern "C" {')
 
     line(out)
@@ -1263,10 +1256,9 @@
     line(out, 2, 'PyObject *module = Py_InitModule3("%s", jcc_funcs, "");',
          extname);
     line(out)
-    line(out, 2, 'PyEval_InitThreads();')
+    line(out, 2, 'initJCC(module);')
     line(out)
     line(out, 2, 'INSTALL_TYPE(JObject, module);')
-    line(out, 2, 'INSTALL_TYPE(JCCEnv, module);')
     line(out, 2, 'INSTALL_TYPE(ConstVariableDescriptor, module);')
     line(out, 2, 'INSTALL_TYPE(FinalizerClass, module);')
     line(out, 2, 'INSTALL_TYPE(FinalizerProxy, module);')

Modified: lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.cpp
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.cpp?rev=755707&r1=755706&r2=755707&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.cpp (original)
+++ lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.cpp Wed Mar 18 19:15:48 2009
@@ -18,7 +18,6 @@
 
 #include "JCCEnv.h"
 
-extern JCCEnv *env;
 
 #if defined(_MSC_VER) || defined(__WIN32)
 _DLL_EXPORT DWORD VM_ENV = 0;
@@ -72,9 +71,16 @@
     }
 #endif
 
+    if (vm != NULL)
+        set_vm(vm, vm_env);
+    else
+        this->vm = NULL;
+}
+
+void JCCEnv::set_vm(JavaVM *vm, JNIEnv *vm_env)
+{
     this->vm = vm;
     set_vm_env(vm_env);
-    env = this;
 
     _sys = (jclass) vm_env->NewGlobalRef(vm_env->FindClass("java/lang/System"));
     _obj = (jclass) vm_env->NewGlobalRef(vm_env->FindClass("java/lang/Object"));
@@ -126,8 +132,20 @@
 {
     jclass cls = NULL;
 
-    if (env)
+    if (env->vm)
         cls = get_vm_env()->FindClass(className);
+#ifdef PYTHON
+    else
+    {
+        PythonGIL gil;
+
+        PyErr_SetString(PyExc_RuntimeError, "initVM() must be called first");
+        throw pythonError(NULL);
+    }
+#else
+    else
+        throw exception(NULL);
+#endif
 
     reportException();
 

Modified: lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.h
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.h?rev=755707&r1=755706&r2=755707&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.h (original)
+++ lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.h Wed Mar 18 19:15:48 2009
@@ -127,6 +127,7 @@
         return (JNIEnv *) pthread_getspecific(VM_ENV);
     }
 #endif
+    virtual void set_vm(JavaVM *vm, JNIEnv *vm_env);
     virtual void set_vm_env(JNIEnv *vm_env);
 
     virtual jclass findClass(const char *className);

Modified: lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp?rev=755707&r1=755706&r2=755707&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp (original)
+++ lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp Wed Mar 18 19:15:48 2009
@@ -55,6 +55,8 @@
 
         if (cls)
             return t_Class::wrap_Object(Class(cls));
+    } catch (JCCEnv::pythonError e) {
+        return NULL;
     } catch (JCCEnv::exception e) {
         PyErr_SetJavaError(e.throwable);
         return NULL;
@@ -124,10 +126,25 @@
               break;
           }
 
-          case 'j':           /* Java object */
+          case 'j':           /* Java object, with class$    */
+          case 'k':           /* Java object, with initializeClass */
           {
-              Class *cls = va_arg(list, Class *);
-              
+              jclass cls = NULL;
+
+              switch (types[pos]) {
+                case 'j':
+                  cls = (jclass) va_arg(list, Class *)->this$;
+                  break;
+                case 'k':
+                  try {
+                      jclass (*initializeClass)() = va_arg(list, jclass (*)());
+                      cls = (*initializeClass)();
+                  } catch (JCCEnv::pythonError e) {
+                      return -1;
+                  }
+                  break;
+              }
+
               if (arg == Py_None)
                   break;
 
@@ -151,7 +168,7 @@
                           int ok =
                               (obj == Py_None ||
                                (PyObject_TypeCheck(obj, &Object$$Type) &&
-                                cls->isInstance(((t_Object *) obj)->object)));
+                                env->get_vm_env()->IsInstanceOf(((t_Object *) obj)->object.this$,
cls)));
 
                           Py_DECREF(obj);
                           if (ok)
@@ -162,13 +179,13 @@
                   }
               }
               else if (PyObject_TypeCheck(arg, &Object$$Type) &&
-                       cls->isInstance(((t_Object *) arg)->object))
+                       env->get_vm_env()->IsInstanceOf(((t_Object *) arg)->object.this$,
cls))
                   break;
               else if (PyObject_TypeCheck(arg, &FinalizerProxy$$Type))
               {
                   arg = ((t_fp *) arg)->object;
                   if (PyObject_TypeCheck(arg, &Object$$Type) &&
-                      cls->isInstance(((t_Object *) arg)->object))
+                      env->get_vm_env()->IsInstanceOf(((t_Object *) arg)->object.this$,
cls))
                       break;
               }
 
@@ -454,8 +471,18 @@
           }
 
           case 'j':           /* Java object except String and Object */
+          case 'k':           /* Java object, with initializeClass    */
           {
-              Class *cls = va_arg(check, Class *);
+              jclass cls = NULL;
+
+              switch (types[pos]) {
+                case 'j':
+                  cls = (jclass) va_arg(check, Class *)->this$;
+                  break;
+                case 'k':
+                  jclass (*initializeClass)() = va_arg(check, jclass (*)());
+                  cls = (*initializeClass)();
+              }
 
               if (array)
               {
@@ -466,7 +493,7 @@
                   else if (PyObject_TypeCheck(arg, JArrayObject$$Type))
                       *array = ((t_jarray<jobject> *) arg)->array;
                   else 
-                      *array = JArray<jobject>((jclass) cls->this$, arg);
+                      *array = JArray<jobject>(cls, arg);
 
                   if (PyErr_Occurred())
                       return -1;
@@ -796,31 +823,40 @@
 
 PyObject *PyErr_SetArgsError(char *name, PyObject *args)
 {
-    PyObject *err = Py_BuildValue("(sO)", name, args);
+    if (!PyErr_Occurred())
+    {
+        PyObject *err = Py_BuildValue("(sO)", name, args);
 
-    PyErr_SetObject(PyExc_InvalidArgsError, err);
-    Py_DECREF(err);
+        PyErr_SetObject(PyExc_InvalidArgsError, err);
+        Py_DECREF(err);
+    }
 
     return NULL;
 }
 
 PyObject *PyErr_SetArgsError(PyObject *self, char *name, PyObject *args)
 {
-    PyObject *type = (PyObject *) self->ob_type;
-    PyObject *err = Py_BuildValue("(OsO)", type, name, args);
+    if (!PyErr_Occurred())
+    {
+        PyObject *type = (PyObject *) self->ob_type;
+        PyObject *err = Py_BuildValue("(OsO)", type, name, args);
 
-    PyErr_SetObject(PyExc_InvalidArgsError, err);
-    Py_DECREF(err);
+        PyErr_SetObject(PyExc_InvalidArgsError, err);
+        Py_DECREF(err);
+    }
 
     return NULL;
 }
 
 PyObject *PyErr_SetArgsError(PyTypeObject *type, char *name, PyObject *args)
 {
-    PyObject *err = Py_BuildValue("(OsO)", type, name, args);
+    if (!PyErr_Occurred())
+    {
+        PyObject *err = Py_BuildValue("(OsO)", type, name, args);
 
-    PyErr_SetObject(PyExc_InvalidArgsError, err);
-    Py_DECREF(err);
+        PyErr_SetObject(PyExc_InvalidArgsError, err);
+        Py_DECREF(err);
+    }
 
     return NULL;
 }
@@ -981,7 +1017,8 @@
     return value;
 }
 
-PyObject *castCheck(PyObject *obj, jclass cls, int reportError)
+PyObject *castCheck(PyObject *obj, jclass (*initializeClass)(),
+                    int reportError)
 {
     if (PyObject_TypeCheck(obj, &FinalizerProxy$$Type))
         obj = ((t_fp *) obj)->object;
@@ -994,12 +1031,24 @@
     }
 
     jobject jobj = ((t_Object *) obj)->object.this$;
-    
-    if (jobj && !env->get_vm_env()->IsInstanceOf(jobj, cls))
+
+    if (jobj)
     {
-        if (reportError)
-            PyErr_SetObject(PyExc_TypeError, obj);
-        return NULL;
+        jclass cls;
+
+        try {
+            cls = (*initializeClass)();
+        } catch (JCCEnv::pythonError e) {
+            return NULL;
+        }
+
+        if (!env->get_vm_env()->IsInstanceOf(jobj, cls))
+        {
+            if (reportError)
+                PyErr_SetObject(PyExc_TypeError, obj);
+
+            return NULL;
+        }
     }
 
     return obj;

Modified: lucene/pylucene/trunk/jcc/jcc/sources/functions.h
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/functions.h?rev=755707&r1=755706&r2=755707&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/functions.h (original)
+++ lucene/pylucene/trunk/jcc/jcc/sources/functions.h Wed Mar 18 19:15:48 2009
@@ -167,7 +167,8 @@
 PyObject *get_extension_nextElement(PyObject *self);
 
 jobjectArray fromPySequence(jclass cls, PyObject *sequence);
-PyObject *castCheck(PyObject *obj, jclass cls, int reportError);
+PyObject *castCheck(PyObject *obj, jclass (*initializeClass)(),
+                    int reportError);
 
 extern PyTypeObject FinalizerClass$$Type;
 extern PyTypeObject FinalizerProxy$$Type;

Modified: lucene/pylucene/trunk/jcc/jcc/sources/jcc.cpp
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/jcc.cpp?rev=755707&r1=755706&r2=755707&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/jcc.cpp (original)
+++ lucene/pylucene/trunk/jcc/jcc/sources/jcc.cpp Wed Mar 18 19:15:48 2009
@@ -25,7 +25,7 @@
 
 #include "JObject.h"
 #include "JCCEnv.h"
-
+#include "macros.h"
 
 _DLL_EXPORT JCCEnv *env;
 
@@ -64,7 +64,7 @@
     { NULL, NULL, 0, NULL }
 };
 
-_DLL_EXPORT PyTypeObject JCCEnv$$Type = {
+PyTypeObject JCCEnv$$Type = {
     PyObject_HEAD_INIT(NULL)
     0,                                   /* ob_size */
     "jcc.JCCEnv",                        /* tp_name */
@@ -261,7 +261,7 @@
 
 _DLL_EXPORT PyObject *getVMEnv(PyObject *self)
 {
-    if (env)
+    if (env->vm != NULL)
     {
         t_jccenv *jccenv = (t_jccenv *) JCCEnv$$Type.tp_alloc(&JCCEnv$$Type, 0);
         jccenv->env = env;
@@ -276,6 +276,21 @@
 static void registerNatives(JNIEnv *vm_env);
 #endif
 
+_DLL_EXPORT PyObject *initJCC(PyObject *module)
+{
+    if (env == NULL)
+    {
+        PyEval_InitThreads();
+        INSTALL_TYPE(JCCEnv, module);
+
+        env = new JCCEnv(NULL, NULL);
+
+        Py_RETURN_TRUE;
+    }
+
+    Py_RETURN_FALSE;
+}
+
 _DLL_EXPORT PyObject *initVM(PyObject *self, PyObject *args, PyObject *kwds)
 {
     static char *kwnames[] = {
@@ -292,7 +307,7 @@
                                      &vmargs))
         return NULL;
 
-    if (env)
+    if (env->vm)
     {
         if (initialheap || maxheap || maxstack || vmargs)
         {
@@ -382,11 +397,13 @@
             return NULL;
         }
 
+        env->set_vm(vm, vm_env);
+
         for (unsigned int i = 0; i < nOptions; i++)
             delete vm_options[i].optionString;
 
         t_jccenv *jccenv = (t_jccenv *) JCCEnv$$Type.tp_alloc(&JCCEnv$$Type, 0);
-        jccenv->env = new JCCEnv(vm, vm_env);
+        jccenv->env = env;
 
 #ifdef _jcc_lib
         registerNatives(vm_env);

Modified: lucene/pylucene/trunk/test/test_PythonDirectory.py
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/test/test_PythonDirectory.py?rev=755707&r1=755706&r2=755707&view=diff
==============================================================================
--- lucene/pylucene/trunk/test/test_PythonDirectory.py (original)
+++ lucene/pylucene/trunk/test/test_PythonDirectory.py Wed Mar 18 19:15:48 2009
@@ -147,7 +147,7 @@
 
     def createOutput(self, name):
         file_path = os.path.join(self.path, name)
-        fh = open(file_path, "w")
+        fh = open(file_path, "wb")
         stream = PythonFileStreamOutput(name, fh)
         self._streams.append(stream)
         return stream
@@ -180,7 +180,7 @@
     def openInput(self, name, bufferSize=0):
         file_path = os.path.join(self.path, name)
         try:
-            fh = open(file_path, 'r')
+            fh = open(file_path, "rb")
         except IOError:
             raise JavaError, IOException(name)
         stream = PythonFileStreamInput(name, fh, os.path.getsize(file_path))



Mime
View raw message