You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pylucene-commits@lucene.apache.org by va...@apache.org on 2014/10/02 00:54:30 UTC

svn commit: r1628857 - in /lucene/pylucene/trunk/jcc/jcc/sources: JCCEnv.cpp JCCEnv.h functions.cpp jcc.cpp

Author: vajda
Date: Wed Oct  1 22:54:29 2014
New Revision: 1628857

URL: http://svn.apache.org/r1628857
Log:
fixed python exception reporting when not in shared mode

Modified:
    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/jcc.cpp

Modified: lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.cpp
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.cpp?rev=1628857&r1=1628856&r2=1628857&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.cpp (original)
+++ lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.cpp Wed Oct  1 22:54:29 2014
@@ -476,6 +476,48 @@ jclass JCCEnv::getPythonExceptionClass()
 {
     return _thr;
 }
+
+// returns true if Python exception instance was successfully restored
+bool JCCEnv::restorePythonException(jthrowable throwable) const
+{
+#ifdef _jcc_lib   // PythonException is only available in shared mode
+    jclass pycls = getPythonExceptionClass();
+    JNIEnv *vm_env = get_vm_env();
+
+    // Support through-layer exceptions by taking the active PythonException
+    // and making the enclosed exception visible to Python again.
+
+    if (vm_env->IsSameObject(vm_env->GetObjectClass(throwable), pycls))
+    {
+        jfieldID fid = vm_env->GetFieldID(pycls, "py_error_state", "J");
+        PyObject *state = (PyObject *) vm_env->GetLongField(throwable, fid);
+        
+        if (state != NULL)
+        {
+            PyObject *type = PyTuple_GET_ITEM(state, 0);
+            PyObject *value = PyTuple_GET_ITEM(state, 1);
+            PyObject *tb = PyTuple_GET_ITEM(state, 2);
+
+            Py_INCREF(type);
+            if (value == Py_None)
+                value = NULL;
+            else
+                Py_INCREF(value);
+            if (tb == Py_None)
+                tb = NULL;
+            else
+                Py_INCREF(tb);
+
+            PyErr_Restore(type, value, tb);
+
+            return true;
+        }
+    }
+#endif
+
+    return false;
+}
+
 #endif
 
 void JCCEnv::reportException() const

Modified: lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.h
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.h?rev=1628857&r1=1628856&r2=1628857&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.h (original)
+++ lucene/pylucene/trunk/jcc/jcc/sources/JCCEnv.h Wed Oct  1 22:54:29 2014
@@ -285,6 +285,7 @@ public:
     char *getClassName(jobject obj) const;
 #ifdef PYTHON
     jclass getPythonExceptionClass() const;
+    bool restorePythonException(jthrowable throwable) const;
     jstring fromPyString(PyObject *object) const;
     PyObject *fromJString(jstring js, int delete_local_ref) const;
     void finalizeObject(JNIEnv *jenv, PyObject *obj);

Modified: lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp?rev=1628857&r1=1628856&r2=1628857&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp (original)
+++ lucene/pylucene/trunk/jcc/jcc/sources/functions.cpp Wed Oct  1 22:54:29 2014
@@ -1358,37 +1358,8 @@ PyObject *PyErr_SetJavaError()
 
     vm_env->ExceptionClear();
 
-    jclass pycls = env->getPythonExceptionClass();
-
-    // Support through-layer exceptions by taking the active PythonException
-    // and making the enclosed exception visible to Python again.
-
-    if (vm_env->IsSameObject(vm_env->GetObjectClass(throwable), pycls))
-    {
-        jfieldID fid = vm_env->GetFieldID(pycls, "py_error_state", "J");
-        PyObject *state = (PyObject *) vm_env->GetLongField(throwable, fid);
-        
-        if (state != NULL)
-        {
-            PyObject *type = PyTuple_GET_ITEM(state, 0);
-            PyObject *value = PyTuple_GET_ITEM(state, 1);
-            PyObject *tb = PyTuple_GET_ITEM(state, 2);
-
-            Py_INCREF(type);
-            if (value == Py_None)
-                value = NULL;
-            else
-                Py_INCREF(value);
-            if (tb == Py_None)
-                tb = NULL;
-            else
-                Py_INCREF(tb);
-
-            PyErr_Restore(type, value, tb);
-
-            return NULL;
-        }
-    }
+    if (env->restorePythonException(throwable))
+        return NULL;
 
     PyObject *err = t_Throwable::wrap_Object(Throwable(throwable));
 

Modified: lucene/pylucene/trunk/jcc/jcc/sources/jcc.cpp
URL: http://svn.apache.org/viewvc/lucene/pylucene/trunk/jcc/jcc/sources/jcc.cpp?rev=1628857&r1=1628856&r2=1628857&view=diff
==============================================================================
--- lucene/pylucene/trunk/jcc/jcc/sources/jcc.cpp (original)
+++ lucene/pylucene/trunk/jcc/jcc/sources/jcc.cpp Wed Oct  1 22:54:29 2014
@@ -43,6 +43,7 @@ static void t_jccenv_dealloc(t_jccenv *s
 static PyObject *t_jccenv_attachCurrentThread(PyObject *self, PyObject *args);
 static PyObject *t_jccenv_detachCurrentThread(PyObject *self);
 static PyObject *t_jccenv_isCurrentThreadAttached(PyObject *self);
+static PyObject *t_jccenv_isShared(PyObject *self);
 static PyObject *t_jccenv_strhash(PyObject *self, PyObject *arg);
 static PyObject *t_jccenv__dumpRefs(PyObject *self,
                                     PyObject *args, PyObject *kwds);
@@ -70,6 +71,8 @@ static PyMethodDef t_jccenv_methods[] = 
       METH_NOARGS, NULL },
     { "isCurrentThreadAttached", (PyCFunction) t_jccenv_isCurrentThreadAttached,
       METH_NOARGS, NULL },
+    { "isShared", (PyCFunction) t_jccenv_isShared,
+      METH_NOARGS, NULL },
     { "strhash", (PyCFunction) t_jccenv_strhash,
       METH_O, NULL },
     { "_dumpRefs", (PyCFunction) t_jccenv__dumpRefs,
@@ -180,6 +183,15 @@ static PyObject *t_jccenv_isCurrentThrea
     Py_RETURN_FALSE;
 }
 
+static PyObject *t_jccenv_isShared(PyObject *self)
+{
+#ifdef _jcc_lib
+    Py_RETURN_TRUE;
+#else
+    Py_RETURN_FALSE;
+#endif
+}
+
 static PyObject *t_jccenv_strhash(PyObject *self, PyObject *arg)
 {
     int hash = PyObject_Hash(arg);