[omniORB] exceptions during up-call to python servant

Luke Deller ldeller at xplantechnology.com
Tue Apr 20 19:30:21 BST 2004


Luke Deller wrote:
...
> Would it be feasible to add logging of tracebacks for *all* unexpected 
> exceptions in up-calls?
> 
> [ It seems to me that it could be implemented in omniORBpy by modifying 
> the five pieces of code which call omniPy::produceSystemException so 
> that they pass the unexpected exception+traceback to that function, 
> which could then log the traceback before producing the SystemException ]

Here's a cvs diff which incorporates these changes to print tracebacks 
for unexpected CORBA exceptions in up-calls.

Also, I noticed that the existing omniORBpy code would leak python 
references in the case where a python exception occurs in an up-call, 
but the exception is not logged (because the trace level is zero).  The 
attached diff also fixes this problem.

Regards,
Luke.
-------------- next part --------------
Index: omnipy.h
===================================================================
RCS file: /cvsroot/omniorb/omniORBpy/modules/Attic/omnipy.h,v
retrieving revision 1.2.4.22
diff -u -r1.2.4.22 omnipy.h
--- omnipy.h	15 Dec 2003 12:10:52 -0000	1.2.4.22
+++ omnipy.h	20 Apr 2004 07:50:00 -0000
@@ -260,7 +260,8 @@
 
   // Throw a C++ system exception equivalent to the given Python exception
   static
-  void produceSystemException(PyObject* eobj, PyObject* erepoId);
+  void produceSystemException(PyObject* eobj, PyObject* erepoId,
+    PyObject* etype, PyObject* etraceback);
 
   // Handle the current Python exception. An exception must have
   // occurred. Deals with system exceptions and
Index: pyServant.cc
===================================================================
RCS file: /cvsroot/omniorb/omniORBpy/modules/Attic/pyServant.cc,v
retrieving revision 1.1.2.16
diff -u -r1.1.2.16 pyServant.cc
--- pyServant.cc	15 Dec 2003 12:10:52 -0000	1.1.2.16
+++ pyServant.cc	20 Apr 2004 07:50:00 -0000
@@ -545,10 +545,11 @@
 	PyErr_Restore(etype, evalue, etraceback);
 	PyErr_Print();
       }
+      else {
+        Py_DECREF(etype); Py_XDECREF(evalue); Py_XDECREF(etraceback);
+      }
       OMNIORB_THROW(UNKNOWN, UNKNOWN_PythonException, CORBA::COMPLETED_MAYBE);
     }
-    Py_DECREF(etype);
-    Py_XDECREF(etraceback);
 
     PyObject* exc_d = pycd->exc_d_;
 
@@ -559,7 +560,7 @@
       PyObject* edesc = PyDict_GetItem(exc_d, erepoId);
 
       if (edesc) {
-	Py_DECREF(erepoId);
+	Py_DECREF(erepoId); Py_DECREF(etype); Py_XDECREF(etraceback);
 	PyUserException ex(edesc, evalue, CORBA::COMPLETED_MAYBE);
 	ex._raise();
       }
@@ -568,12 +569,12 @@
     // Is it a LOCATION_FORWARD?
     if (omni::strMatch(PyString_AS_STRING(erepoId),
 		       "omniORB.LOCATION_FORWARD")) {
-      Py_DECREF(erepoId);
+      Py_DECREF(erepoId); Py_DECREF(etype); Py_XDECREF(etraceback);
       omniPy::handleLocationForward(evalue);
     }
 
     // System exception or unknown user exception
-    omniPy::produceSystemException(evalue, erepoId);
+    omniPy::produceSystemException(evalue, erepoId, etype, etraceback);
   }
 }
 
@@ -704,10 +705,11 @@
 	PyErr_Restore(etype, evalue, etraceback);
 	PyErr_Print();
       }
+      else {
+        Py_DECREF(etype); Py_XDECREF(evalue); Py_XDECREF(etraceback);
+      }
       OMNIORB_THROW(UNKNOWN, UNKNOWN_PythonException, CORBA::COMPLETED_MAYBE);
     }
-    Py_DECREF(etype);
-    Py_XDECREF(etraceback);
 
     // Is it a user exception?
     if (exc_d != Py_None) {
@@ -716,7 +718,7 @@
       PyObject* edesc = PyDict_GetItem(exc_d, erepoId);
 
       if (edesc) {
-	Py_DECREF(erepoId);
+	Py_DECREF(erepoId); Py_DECREF(etype); Py_XDECREF(etraceback);
 	PyUserException ex(edesc, evalue, CORBA::COMPLETED_MAYBE);
 	ex._raise();
       }
@@ -725,12 +727,12 @@
     // Is it a LOCATION_FORWARD?
     if (omni::strMatch(PyString_AS_STRING(erepoId),
 		       "omniORB.LOCATION_FORWARD")) {
-      Py_DECREF(erepoId);
+      Py_DECREF(erepoId); Py_DECREF(etype); Py_XDECREF(etraceback);
       omniPy::handleLocationForward(evalue);
     }
 
     // System exception or unknown user exception
-    omniPy::produceSystemException(evalue, erepoId);
+    omniPy::produceSystemException(evalue, erepoId, etype, etraceback);
   }
 }
 
@@ -802,6 +804,9 @@
 	PyErr_Restore(etype, evalue, etraceback);
 	PyErr_Print();
       }
+      else {
+        Py_DECREF(etype); Py_XDECREF(evalue); Py_XDECREF(etraceback);
+      }
       OMNIORB_THROW(UNKNOWN, UNKNOWN_PythonException, CORBA::COMPLETED_MAYBE);
     }
     Py_DECREF(etype);
@@ -809,7 +814,7 @@
 
     if (omni::strMatch(PyString_AS_STRING(erepoId),
 		       PortableServer::ForwardRequest::_PD_repoId)) {
-      Py_DECREF(erepoId);
+      Py_DECREF(erepoId); Py_DECREF(etype); Py_XDECREF(etraceback);
       PyObject* pyfr = PyObject_GetAttrString(evalue,
 					      (char*)"forward_reference");
       Py_DECREF(evalue);
@@ -832,12 +837,12 @@
     // Is it a LOCATION_FORWARD?
     if (omni::strMatch(PyString_AS_STRING(erepoId),
 		       "omniORB.LOCATION_FORWARD")) {
-      Py_DECREF(erepoId);
+      Py_DECREF(erepoId); Py_DECREF(etype); Py_XDECREF(etraceback);
       omniPy::handleLocationForward(evalue);
     }
 
     // System exception or unknown user exception
-    omniPy::produceSystemException(evalue, erepoId);
+    omniPy::produceSystemException(evalue, erepoId, etype, etraceback);
   }
   OMNIORB_ASSERT(0); // Never reach here
   return 0;
@@ -998,14 +1003,15 @@
 	PyErr_Restore(etype, evalue, etraceback);
 	PyErr_Print();
       }
+      else {
+	Py_DECREF(etype); Py_XDECREF(evalue); Py_XDECREF(etraceback);
+      }
       OMNIORB_THROW(UNKNOWN, UNKNOWN_PythonException, CORBA::COMPLETED_MAYBE);
     }
-    Py_DECREF(etype);
-    Py_XDECREF(etraceback);
 
     if (omni::strMatch(PyString_AS_STRING(erepoId),
 		       PortableServer::ForwardRequest::_PD_repoId)) {
-      Py_DECREF(erepoId);
+      Py_DECREF(erepoId); Py_DECREF(etype); Py_XDECREF(etraceback);
       PyObject* pyfr = PyObject_GetAttrString(evalue,
 					      (char*)"forward_reference");
       Py_DECREF(evalue);
@@ -1027,12 +1033,12 @@
     // Is it a LOCATION_FORWARD?
     if (omni::strMatch(PyString_AS_STRING(erepoId),
 		       "omniORB.LOCATION_FORWARD")) {
-      Py_DECREF(erepoId);
+      Py_DECREF(erepoId); Py_DECREF(etype); Py_XDECREF(etraceback);
       omniPy::handleLocationForward(evalue);
     }
 
     // System exception or unknown user exception
-    omniPy::produceSystemException(evalue, erepoId);
+    omniPy::produceSystemException(evalue, erepoId, etype, etraceback);
   }
   OMNIORB_ASSERT(0); // Never reach here
   return 0;
Index: pyExceptions.cc
===================================================================
RCS file: /cvsroot/omniorb/omniORBpy/modules/Attic/pyExceptions.cc,v
retrieving revision 1.1.2.13
diff -u -r1.1.2.13 pyExceptions.cc
--- pyExceptions.cc	16 Feb 2004 13:56:41 -0000	1.1.2.13
+++ pyExceptions.cc	20 Apr 2004 07:50:00 -0000
@@ -111,7 +111,8 @@
 
 
 void
-omniPy::produceSystemException(PyObject* eobj, PyObject* erepoId)
+omniPy::produceSystemException(PyObject* eobj, PyObject* erepoId,
+  PyObject* etype, PyObject* etraceback)
 {
   CORBA::ULong            minor  = 0;
   CORBA::CompletionStatus status = CORBA::COMPLETED_MAYBE;
@@ -143,11 +144,22 @@
 
   char* repoId = PyString_AS_STRING(erepoId);
 
-  Py_DECREF(eobj);
-
 #define THROW_SYSTEM_EXCEPTION_IF_MATCH(ex) \
   if (!strcmp(repoId, "IDL:omg.org/CORBA/" #ex ":1.0")) { \
-    Py_DECREF(erepoId); OMNIORB_THROW(ex, minor, status); \
+    if (omniORB::trace(2)) { \
+      { \
+        omniORB::logger l; \
+        l << "Caught a recognised CORBA SystemException during up-call: " \
+          << PyString_AS_STRING(erepoId) << "\n"; \
+      } \
+      PyErr_Restore(etype, eobj, etraceback); \
+      PyErr_Print(); \
+    } \
+    else { \
+      Py_XDECREF(eobj); Py_DECREF(etype); Py_XDECREF(etraceback); \
+    } \
+    Py_DECREF(erepoId); \
+    OMNIORB_THROW(ex, minor, status); \
   }
 
   OMNIORB_FOR_EACH_SYS_EXCEPTION(THROW_SYSTEM_EXCEPTION_IF_MATCH)
@@ -155,9 +167,16 @@
 #undef THROW_SYSTEM_EXCEPTION_IF_MATCH
 
   if (omniORB::trace(1)) {
-    omniORB::logger l;
-    l << "Caught an unexpected CORBA exception during up-call: "
-      << PyString_AS_STRING(erepoId) << "\n";
+    {
+      omniORB::logger l;
+      l << "Caught an unexpected CORBA exception during up-call: "
+        << PyString_AS_STRING(erepoId) << "\n";
+    }
+    PyErr_Restore(etype, eobj, etraceback);
+    PyErr_Print();
+  }
+  else {
+    Py_XDECREF(eobj); Py_DECREF(etype); Py_XDECREF(etraceback);
   }
   Py_DECREF(erepoId);
   if (m && c && v)
@@ -194,18 +213,15 @@
 		  CORBA::COMPLETED_MAYBE);
   }
 
-  Py_DECREF(etype);
-  Py_XDECREF(etraceback);
-
   // Is it a LOCATION_FORWARD?
   if (omni::strMatch(PyString_AS_STRING(erepoId),
 		     "omniORB.LOCATION_FORWARD")) {
-    Py_DECREF(erepoId);
+    Py_DECREF(erepoId); Py_DECREF(etype); Py_XDECREF(etraceback);
     omniPy::handleLocationForward(evalue);
   }
 
   // System exception
-  omniPy::produceSystemException(evalue, erepoId);
+  omniPy::produceSystemException(evalue, erepoId, etype, etraceback);
 }
 
 


More information about the omniORB-list mailing list