# HG changeset patch # User Atul Varma # Date 1251072465 25200 # Node ID 8371983fee63bb6570f911f1a9e741a7410ffea9 # Parent 05e18059b5c477d55ebdb82106c0837feddb2bae Added support for weakrefs to contexts. diff -r 05e18059b5c4 -r 8371983fee63 src/context.cpp --- a/src/context.cpp Sun Aug 23 16:20:15 2009 -0700 +++ b/src/context.cpp Sun Aug 23 17:07:45 2009 -0700 @@ -103,6 +103,8 @@ static void PYM_JSContextDealloc(PYM_JSContextObject *self) { + if (self->weakrefs) + PyObject_ClearWeakRefs((PyObject *) self); if (self->cx) { JS_DestroyContext(self->cx); self->cx = NULL; @@ -522,13 +524,14 @@ 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ /*tp_flags*/ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_doc */ "JavaScript Context.", (traverseproc) PYM_traverse, /* tp_traverse */ (inquiry) PYM_clear, /* tp_clear */ 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ + /* tp_weaklistoffset */ + offsetof(PYM_JSContextObject, weakrefs), 0, /* tp_iter */ 0, /* tp_iternext */ PYM_JSContextMethods, /* tp_methods */ @@ -552,6 +555,7 @@ if (context == NULL) return NULL; + context->weakrefs = NULL; context->opCallback = NULL; context->runtime = runtime; Py_INCREF(runtime); diff -r 05e18059b5c4 -r 8371983fee63 src/context.h --- a/src/context.h Sun Aug 23 16:20:15 2009 -0700 +++ b/src/context.h Sun Aug 23 17:07:45 2009 -0700 @@ -47,6 +47,7 @@ PYM_JSRuntimeObject *runtime; JSContext *cx; PyObject *opCallback; + PyObject *weakrefs; } PYM_JSContextObject; extern PyTypeObject PYM_JSContextType; diff -r 05e18059b5c4 -r 8371983fee63 tests/test_pymonkey.py --- a/tests/test_pymonkey.py Sun Aug 23 16:20:15 2009 -0700 +++ b/tests/test_pymonkey.py Sun Aug 23 17:07:45 2009 -0700 @@ -438,6 +438,14 @@ cx = rt.new_context() self.assertEqual(cx.get_runtime(), rt) + def testContextsAreWeakReferencable(self): + rt = pymonkey.Runtime() + cx = rt.new_context() + wcx = weakref.ref(cx) + self.assertEqual(cx, wcx()) + del cx + self.assertEqual(wcx(), None) + def testUndefinedCannotBeInstantiated(self): self.assertRaises(TypeError, pymonkey.undefined)