You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by rh...@apache.org on 2015/01/29 13:23:47 UTC

[2/2] qpid-proton git commit: improved exception handling

improved exception handling


Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/2df06c75
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/2df06c75
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/2df06c75

Branch: refs/heads/master
Commit: 2df06c751ad5cbc3717bad9b73301702c03327b8
Parents: a515e19
Author: Rafael Schloming <rh...@alum.mit.edu>
Authored: Thu Jan 29 07:14:01 2015 -0500
Committer: Rafael Schloming <rh...@alum.mit.edu>
Committed: Thu Jan 29 07:19:11 2015 -0500

----------------------------------------------------------------------
 proton-c/bindings/python/cproton.i          | 28 ++++++++++++++++++++++--
 proton-c/bindings/python/proton/__init__.py | 25 +++++++++++++--------
 proton-c/bindings/python/proton/reactors.py |  6 ++++-
 3 files changed, 47 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/2df06c75/proton-c/bindings/python/cproton.i
----------------------------------------------------------------------
diff --git a/proton-c/bindings/python/cproton.i b/proton-c/bindings/python/cproton.i
index 7b08a99..3e22c33 100644
--- a/proton-c/bindings/python/cproton.i
+++ b/proton-c/bindings/python/cproton.i
@@ -290,6 +290,8 @@ int pn_ssl_get_peer_hostname(pn_ssl_t *ssl, char *OUTPUT, size_t *OUTPUT_SIZE);
 
   typedef struct {
     PyObject *handler;
+    PyObject *dispatch;
+    PyObject *exception;
   } pni_pyh_t;
 
   static pni_pyh_t *pni_pyh(pn_handler_t *handler) {
@@ -300,6 +302,8 @@ int pn_ssl_get_peer_hostname(pn_ssl_t *ssl, char *OUTPUT, size_t *OUTPUT_SIZE);
     pni_pyh_t *pyh = pni_pyh(handler);
     SWIG_PYTHON_THREAD_BEGIN_BLOCK;
     Py_DECREF(pyh->handler);
+    Py_DECREF(pyh->dispatch);
+    Py_DECREF(pyh->exception);
     SWIG_PYTHON_THREAD_END_BLOCK;
   }
 
@@ -307,9 +311,27 @@ int pn_ssl_get_peer_hostname(pn_ssl_t *ssl, char *OUTPUT, size_t *OUTPUT_SIZE);
     pni_pyh_t *pyh = pni_pyh(handler);
     SWIG_PYTHON_THREAD_BEGIN_BLOCK;
     PyObject *arg = SWIG_NewPointerObj(event, SWIGTYPE_p_pn_event_t, 0);
-    PyObject *result = PyObject_CallFunctionObjArgs(pyh->handler, arg, NULL);
+    PyObject *result = PyObject_CallMethodObjArgs(pyh->handler, pyh->dispatch, arg, NULL);
     if (!result) {
-      PyErr_PrintEx(true);
+      PyObject *exc, *val, *tb;
+      PyErr_Fetch(&exc, &val, &tb);
+      PyErr_NormalizeException(&exc, &val, &tb);
+      if (!val) {
+        val = Py_None;
+        Py_INCREF(val);
+      }
+      if (!tb) {
+        tb = Py_None;
+        Py_INCREF(tb);
+      }
+      PyObject *result2 = PyObject_CallMethodObjArgs(pyh->handler, pyh->exception, exc, val, tb, NULL);
+      if (!result2) {
+        PyErr_PrintEx(true);
+      }
+      Py_XDECREF(result2);
+      Py_XDECREF(exc);
+      Py_XDECREF(val);
+      Py_XDECREF(tb);
     }
     Py_XDECREF(arg);
     Py_XDECREF(result);
@@ -321,6 +343,8 @@ int pn_ssl_get_peer_hostname(pn_ssl_t *ssl, char *OUTPUT, size_t *OUTPUT_SIZE);
     pni_pyh_t *phy = pni_pyh(chandler);
     phy->handler = handler;
     SWIG_PYTHON_THREAD_BEGIN_BLOCK;
+    phy->dispatch = PyString_FromString("dispatch");
+    phy->exception = PyString_FromString("exception");
     Py_INCREF(phy->handler);
     SWIG_PYTHON_THREAD_END_BLOCK;
     return chandler;

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/2df06c75/proton-c/bindings/python/proton/__init__.py
----------------------------------------------------------------------
diff --git a/proton-c/bindings/python/proton/__init__.py b/proton-c/bindings/python/proton/__init__.py
index d01721d..f0a1b0c 100644
--- a/proton-c/bindings/python/proton/__init__.py
+++ b/proton-c/bindings/python/proton/__init__.py
@@ -3539,23 +3539,30 @@ class _cadapter:
     self.handler = handler
     self.on_error = on_error
 
-  def __call__(self, cevent):
+  def dispatch(self, cevent):
     ev = Event.wrap(cevent)
-    try:
-      ev.dispatch(self.handler)
-    except:
-      if self.on_error is None:
-        raise
-      else:
-        self.on_error(sys.exc_info())
+    ev.dispatch(self.handler)
+
+  def exception(self, exc, val, tb):
+    if self.on_error is None:
+      raise exc, val, tb
+    else:
+      self.on_error((exc, val, tb))
 
 class WrappedHandler(Wrapper):
 
   def __init__(self, impl_or_constructor):
     Wrapper.__init__(self, impl_or_constructor)
 
+  def _on_error(self, info):
+    on_error = getattr(self, "on_error", None)
+    if on_error is None:
+      raise info[0], info[1], info[2]
+    else:
+      on_error(info)
+
   def add(self, handler):
-    impl = _chandler(handler, getattr(self, "on_error", None))
+    impl = _chandler(handler, self._on_error)
     pn_handler_add(self._impl, impl)
     pn_decref(impl)
 

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/2df06c75/proton-c/bindings/python/proton/reactors.py
----------------------------------------------------------------------
diff --git a/proton-c/bindings/python/proton/reactors.py b/proton-c/bindings/python/proton/reactors.py
index ecf293d..1d3d644 100644
--- a/proton-c/bindings/python/proton/reactors.py
+++ b/proton-c/bindings/python/proton/reactors.py
@@ -857,9 +857,10 @@ class Acceptor(Wrapper):
 
 def _wrap_handler(reactor, impl):
     wrapped = WrappedHandler(impl)
-    wrapped.__dict__["on_error"] = reactor.on_error
+    reactor._mark_handler(wrapped)
     return wrapped
 
+
 class Reactor(Wrapper):
 
     @staticmethod
@@ -881,6 +882,9 @@ class Reactor(Wrapper):
         self.errors.append(info)
         self.yield_()
 
+    def _mark_handler(self, handler):
+        handler.__dict__["on_error"] = self.on_error
+
     def global_(self, handler):
         impl = _chandler(handler, self.on_error)
         pn_reactor_global(self._impl, impl)


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org