Author: sayer
Date: 2009-10-22 21:23:53 +0200 (Thu, 22 Oct 2009)
New Revision: 1562

Modified:
   trunk/apps/dsm/mods/mod_py/PyDSMSession.cpp
   trunk/apps/dsm/mods/mod_py/Readme.mod_py.txt
Log:
python exception handling 


Modified: trunk/apps/dsm/mods/mod_py/PyDSMSession.cpp
===================================================================
--- trunk/apps/dsm/mods/mod_py/PyDSMSession.cpp 2009-10-22 17:11:11 UTC (rev 
1561)
+++ trunk/apps/dsm/mods/mod_py/PyDSMSession.cpp 2009-10-22 19:23:53 UTC (rev 
1562)
@@ -25,6 +25,7 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#include "DSMStateEngine.h"
 #include "PyDSMSession.h"
 #include "log.h"
 #include "DSMSession.h"
@@ -100,7 +101,13 @@
     GET_SESS_PTR;
     
     DBG("playPrompt('%s', loop=%s)\n", name, loop?"true":"false");
-    sess->playPrompt(name, loop);
+    try {
+      sess->playPrompt(name, loop);
+    } catch (DSMException& e) {
+      PyErr_SetString(PyExc_RuntimeError, e.params["type"].c_str());
+      return NULL;
+    }
+
     Py_INCREF(Py_None);
     return Py_None;
   }
@@ -116,9 +123,15 @@
 
     GET_SESS_PTR;
     
-    DBG("playPrompt('%s', loop=%s, front=%s)\n", name, 
+    DBG("playFile('%s', loop=%s, front=%s)\n", name, 
        loop?"true":"false", front?"true":"false");
-    sess->playFile(name, loop, front);
+    try {
+      sess->playFile(name, loop, front);
+    } catch (DSMException& e) {
+      PyErr_SetString(PyExc_RuntimeError, e.params["type"].c_str());
+      return NULL;
+    }
+
     Py_INCREF(Py_None);
     return Py_None;
   }
@@ -132,7 +145,13 @@
     GET_SESS_PTR;
     
     DBG("recordFile('%s')\n", name);
-    sess->recordFile(name);
+    try {
+      sess->recordFile(name);
+    } catch (DSMException& e) {
+      PyErr_SetString(PyExc_RuntimeError, e.params["type"].c_str());
+      return NULL;
+    }
+
     Py_INCREF(Py_None);
     return Py_None;
   }
@@ -186,7 +205,13 @@
     GET_SESS_PTR;
     
     DBG("setPromptSet('%s')\n", name);
-    sess->setPromptSet(name);
+    try {
+      sess->setPromptSet(name);
+    } catch (DSMException& e) {
+      PyErr_SetString(PyExc_RuntimeError, e.params["type"].c_str());
+      return NULL;
+    }
+
     Py_INCREF(Py_None);
     return Py_None;
   }

Modified: trunk/apps/dsm/mods/mod_py/Readme.mod_py.txt
===================================================================
--- trunk/apps/dsm/mods/mod_py/Readme.mod_py.txt        2009-10-22 17:11:11 UTC 
(rev 1561)
+++ trunk/apps/dsm/mods/mod_py/Readme.mod_py.txt        2009-10-22 19:23:53 UTC 
(rev 1562)
@@ -36,6 +36,35 @@
 dsm     - module to access dsm functions (see below)
 session - module to access session functions (see below)
 
+exceptions
+==========
+dsm.playPrompt, dsm.playFile, dsm.recordFile and dsm.setPromptSet
+may throw an exception. This is a Python RuntimeError exception,
+not the DSM exception handling (exceptions in interpreted code
+can not be catched through outside of the interpreter). 
+The application needs to catch the exceptions in the python code
+itself, and can then handle them or signal the DSM code to throw
+an exception. example:
+
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+initial state begin 
+ enter {
+  py(dsm.INFO("hello dsm-world\n"))
+  py(#
+try:
+       session.playFile('doesnotexist.wav')
+except RuntimeError, e:
+       dsm.ERROR('thats a runtime error: ' + e.message + '!\n')
+       session.setvar('exception', e.message)
+)
+  log(2, huhu, still there);
+  repost();
+};
+transition "catch py exception" begin - test($exception!="") / 
throw($exception) -> begin;
+transition "normal exception handling" begin - exception; test(#type=="file") 
-> exception_state;
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+
 example
 =======
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -44,7 +73,7 @@
 initial state begin 
  enter {
   py(dsm.INFO("hello dsm-world"))
-  py(
+  py(#
 session.setvar('some_variable','some val')
 print session.var('some_variable')
 print "dsm.Timer = ", dsm.Timer

_______________________________________________
Semsdev mailing list
[email protected]
http://lists.iptel.org/mailman/listinfo/semsdev

Reply via email to