changeset 166:2eaae00d5e30

Added is_exception_pending() and get_pending_exception().
author Atul Varma <varmaa@toolness.com>
date Sun, 30 Aug 2009 20:04:35 -0700
parents 1f9a5982db9c
children b745c9d8eef9
files src/context.cpp tests/test_pymonkey.py
diffstat 2 files changed, 45 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/context.cpp	Sun Aug 30 19:03:46 2009 -0700
+++ b/src/context.cpp	Sun Aug 30 20:04:35 2009 -0700
@@ -274,6 +274,34 @@
 }
 
 static PyObject *
+PYM_isExceptionPending(PYM_JSContextObject *self, PyObject *args)
+{
+  PYM_SANITY_CHECK(self->runtime);
+
+  if (JS_IsExceptionPending(self->cx))
+    Py_RETURN_TRUE;
+  Py_RETURN_FALSE;
+}
+
+static PyObject *
+PYM_getPendingException(PYM_JSContextObject *self, PyObject *args)
+{
+  PYM_SANITY_CHECK(self->runtime);
+
+  if (!JS_IsExceptionPending(self->cx))
+    Py_RETURN_NONE;
+
+  jsval exception;
+
+  if (!JS_GetPendingException(self->cx, &exception)) {
+    PyErr_SetString(PYM_error, "JS_GetPendingException() failed");
+    return NULL;
+  }
+
+  return PYM_jsvalToPyObject(self, exception);
+}
+
+static PyObject *
 PYM_getObjectPrivate(PYM_JSContextObject *self, PyObject *args)
 {
   PYM_SANITY_CHECK(self->runtime);
@@ -764,6 +792,12 @@
    "Returns the private Python object stored in the JavaScript object."},
   {"clear_object_private", (PyCFunction) PYM_clearObjectPrivate, METH_VARARGS,
    "Clears any private Python object stored in the JavaScript object."},
+  {"is_exception_pending", (PyCFunction) PYM_isExceptionPending,
+   METH_VARARGS,
+   "Returns whether an exception is currently being propagated."},
+  {"get_pending_exception", (PyCFunction) PYM_getPendingException,
+   METH_VARARGS,
+   "Returns the current exception being propagated."},
   {NULL, NULL, 0, NULL}
 };
 
--- a/tests/test_pymonkey.py	Sun Aug 30 19:03:46 2009 -0700
+++ b/tests/test_pymonkey.py	Sun Aug 30 20:04:35 2009 -0700
@@ -232,10 +232,16 @@
                                     '(function(){})', '<string>', 1)
         self.assertEqual(cx.get_object_private(jsfunc), None)
 
+    def testGetPendingExceptionReturnsNone(self):
+        cx = pymonkey.Runtime().new_context()
+        self.assertFalse(cx.is_exception_pending())
+        self.assertEqual(cx.get_pending_exception(), None)
+
     def testThrowHookWorks(self):
-        timesCalled = [0]
+        exceptions = []
         def throwhook(cx):
-            timesCalled[0] += 1
+            self.assertTrue(cx.is_exception_pending())
+            exceptions.append(cx.get_pending_exception())
 
         cx = pymonkey.Runtime().new_context()
         cx.set_throw_hook(throwhook)
@@ -246,7 +252,9 @@
             '(function() { throw "hi"; })()',
             '<string>', 1
             )
-        self.assertEqual(timesCalled[0], 2)
+        self.assertEqual(exceptions, ['hi', 'hi'])
+        self.assertFalse(cx.is_exception_pending())
+        self.assertEqual(cx.get_pending_exception(), None)
 
     def testJsWrappedPythonFuncHasPrivate(self):
         def foo(cx, this, args):