changeset 86:16a3e99e9b77

JS-wrapped Python functions that have had their Python functions cleared by context.clear_object_private() now throw appropriate JS exceptions when called.
author Atul Varma <varmaa@toolness.com>
date Sun, 09 Aug 2009 15:46:40 -0700
parents e9f450d30c0e
children 345d4c0e3dd3
files function.c test_pymonkey.py
diffstat 2 files changed, 24 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/function.c	Sun Aug 09 15:37:29 2009 -0700
+++ b/function.c	Sun Aug 09 15:46:40 2009 -0700
@@ -61,6 +61,11 @@
                                  &callable))
     return JS_FALSE;
 
+  if (callable == Py_None) {
+    JS_ReportError(cx, "Wrapped Python function no longer exists");
+    return JS_FALSE;
+  }
+
   PYM_JSContextObject *context = (PYM_JSContextObject *)
     JS_GetContextPrivate(cx);
 
--- a/test_pymonkey.py	Sun Aug 09 15:37:29 2009 -0700
+++ b/test_pymonkey.py	Sun Aug 09 15:46:40 2009 -0700
@@ -153,6 +153,25 @@
         del obj
         self.assertEqual(ref(), None)
 
+    def testJsWrappedPythonFuncThrowsExcIfPrivateCleared(self):
+        def func(cx, this, args):
+            return True
+
+        code = "func()"
+        cx = pymonkey.Runtime().new_context()
+        obj = cx.new_object()
+        cx.init_standard_classes(obj)
+        jsfunc = cx.new_function(func, func.__name__)
+        cx.define_property(obj, func.__name__, jsfunc)
+        cx.clear_object_private(jsfunc)
+        self.assertRaises(pymonkey.error,
+                          cx.evaluate_script,
+                          obj, code, '<string>', 1)
+        self.assertEqual(
+            self._tostring(cx, self.last_exception.message),
+            "Error: Wrapped Python function no longer exists"
+            )
+
     def testJsWrappedPythonFuncPassesContext(self):
         contexts = []