Mercurial > pymonkey
changeset 81:99138265d4b9
Added a context.clear_object_private() function.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Sun, 09 Aug 2009 14:22:17 -0700 |
parents | 4945e4073110 |
children | 9e8e56b4de6f |
files | context.c test_pymonkey.py |
diffstat | 2 files changed, 34 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/context.c Fri Aug 07 18:59:10 2009 -0700 +++ b/context.c Sun Aug 09 14:22:17 2009 -0700 @@ -137,6 +137,26 @@ } static PyObject * +PYM_clearObjectPrivate(PYM_JSContextObject *self, PyObject *args) +{ + PYM_JSObject *object; + + if (!PyArg_ParseTuple(args, "O!", &PYM_JSObjectType, &object)) + return NULL; + + JSClass *klass = JS_GET_CLASS(cx, object->obj); + if (klass != &PYM_JS_ObjectClass) + Py_RETURN_NONE; + + if (!PYM_JS_setPrivatePyObject(self->cx, object->obj, Py_None)) { + PYM_jsExceptionToPython(self); + return NULL; + } + + Py_RETURN_NONE; +} + +static PyObject * PYM_newObject(PYM_JSContextObject *self, PyObject *args) { PyObject *privateObj = NULL; @@ -395,6 +415,8 @@ "Triggers the operation callback for the context."}, {"get_object_private", (PyCFunction) PYM_getObjectPrivate, METH_VARARGS, "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."}, {NULL, NULL, 0, NULL} };
--- a/test_pymonkey.py Fri Aug 07 18:59:10 2009 -0700 +++ b/test_pymonkey.py Sun Aug 09 14:22:17 2009 -0700 @@ -31,6 +31,18 @@ was_raised = True self.assertTrue(was_raised) + def testClearObjectPrivateWorks(self): + class Foo(object): + pass + pyobj = Foo() + cx = pymonkey.Runtime().new_context() + obj = cx.new_object(pyobj) + pyobj = weakref.ref(pyobj) + self.assertEqual(pyobj(), cx.get_object_private(obj)) + cx.clear_object_private(obj) + self.assertEqual(cx.get_object_private(obj), None) + self.assertEqual(pyobj(), None) + def testGetObjectPrivateWorks(self): class Foo(object): pass