Make sets and deques weak referencable.
diff --git a/Include/setobject.h b/Include/setobject.h
index 267e3b0..abbd847 100644
--- a/Include/setobject.h
+++ b/Include/setobject.h
@@ -15,6 +15,7 @@
 	PyObject_HEAD
 	PyObject *data;
 	long hash;	/* only used by frozenset objects */
+	PyObject *weakreflist; /* List of weak references */
 } PySetObject;
 
 PyAPI_DATA(PyTypeObject) PySet_Type;
diff --git a/Lib/test/test_deque.py b/Lib/test/test_deque.py
index bad8857..13800ea 100644
--- a/Lib/test/test_deque.py
+++ b/Lib/test/test_deque.py
@@ -1,6 +1,7 @@
 from collections import deque
 import unittest
 from test import test_support
+from weakref import proxy
 import copy
 import cPickle as pickle
 from cStringIO import StringIO
@@ -435,6 +436,12 @@
         self.assertEqual(type(d), type(e))
         self.assertEqual(list(d), list(e))
 
+    def test_weakref(self):
+        d = deque('gallahad')
+        p = proxy(d)
+        self.assertEqual(str(p), str(d))
+        d = None
+        self.assertRaises(ReferenceError, str, p)
 
 #==============================================================================
 
diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py
index 3a85c76..514b75c 100644
--- a/Lib/test/test_set.py
+++ b/Lib/test/test_set.py
@@ -1,5 +1,6 @@
 import unittest
 from test import test_support
+from weakref import proxy
 import operator
 import copy
 import pickle
@@ -346,6 +347,13 @@
             else:
                 self.assert_(c not in self.s)
 
+    def test_weakref(self):
+        s = self.thetype('gallahad')
+        p = proxy(s)
+        self.assertEqual(str(p), str(s))
+        s = None
+        self.assertRaises(ReferenceError, str, p)
+
 class SetSubclass(set):
     pass
 
diff --git a/Modules/collectionsmodule.c b/Modules/collectionsmodule.c
index 368f0b6..62b43aa 100644
--- a/Modules/collectionsmodule.c
+++ b/Modules/collectionsmodule.c
@@ -1,4 +1,5 @@
 #include "Python.h"
+#include "structmember.h"
 
 /* collections module implementation of a deque() datatype
    Written and maintained by Raymond D. Hettinger <python@rcn.com>
@@ -32,6 +33,7 @@
 	int leftindex;
 	int rightindex;
 	int len;
+	PyObject *weakreflist; /* List of weak references */
 } dequeobject;
 
 static PyTypeObject deque_type;
@@ -58,6 +60,7 @@
 	deque->leftindex = BLOCKLEN / 2 + 1;
 	deque->rightindex = BLOCKLEN / 2;
 	deque->len = 0;
+	deque->weakreflist = NULL;
 
 	return (PyObject *)deque;
 }
@@ -439,6 +442,8 @@
 deque_dealloc(dequeobject *deque)
 {
 	PyObject_GC_UnTrack(deque);
+	if (deque->weakreflist != NULL)
+		PyObject_ClearWeakRefs((PyObject *) deque);
 	if (deque->leftblock != NULL) {
 		int err = deque_clear(deque);
 		assert(err == 0);
@@ -744,12 +749,13 @@
 	PyObject_GenericGetAttr,	/* tp_getattro */
 	0,				/* tp_setattro */
 	0,				/* tp_as_buffer */
-	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,	/* tp_flags */
+	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
+		Py_TPFLAGS_HAVE_WEAKREFS,	/* tp_flags */
 	deque_doc,			/* tp_doc */
 	(traverseproc)deque_traverse,	/* tp_traverse */
 	(inquiry)deque_clear,		/* tp_clear */
 	(richcmpfunc)deque_richcompare,	/* tp_richcompare */
-	0,				/* tp_weaklistoffset*/
+	offsetof(dequeobject, weakreflist),	/* tp_weaklistoffset*/
 	(getiterfunc)deque_iter,	/* tp_iter */
 	0,				/* tp_iternext */
 	deque_methods,			/* tp_methods */
diff --git a/Objects/setobject.c b/Objects/setobject.c
index 9aedc2f..fbff077 100644
--- a/Objects/setobject.c
+++ b/Objects/setobject.c
@@ -1,4 +1,5 @@
 #include "Python.h"
+#include "structmember.h"
 
 /* set object implementation 
    written and maintained by Raymond D. Hettinger <python@rcn.com>
@@ -61,6 +62,7 @@
 	}
 	so->data = data;
 	so->hash = -1;
+	so->weakreflist = NULL;
 
 	if (iterable != NULL) {
 		tmp = set_update(so, iterable);
@@ -113,6 +115,8 @@
 set_dealloc(PySetObject *so)
 {
 	PyObject_GC_UnTrack(so);
+	if (so->weakreflist != NULL)
+		PyObject_ClearWeakRefs((PyObject *) so);
 	Py_XDECREF(so->data);
 	so->ob_type->tp_free(so);
 }
@@ -1009,12 +1013,12 @@
 	0,				/* tp_setattro */
 	0,				/* tp_as_buffer */
 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
-		Py_TPFLAGS_BASETYPE,	/* tp_flags */
+		Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS,	/* tp_flags */
 	set_doc,			/* tp_doc */
 	(traverseproc)set_traverse,	/* tp_traverse */
 	(inquiry)set_tp_clear,		/* tp_clear */
 	(richcmpfunc)set_richcompare,	/* tp_richcompare */
-	0,				/* tp_weaklistoffset */
+	offsetof(PySetObject, weakreflist),	/* tp_weaklistoffset */
 	(getiterfunc)set_iter,		/* tp_iter */
 	0,				/* tp_iternext */
 	set_methods,			/* tp_methods */
@@ -1104,12 +1108,12 @@
 	0,				/* tp_setattro */
 	0,				/* tp_as_buffer */
 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
-		Py_TPFLAGS_BASETYPE,	/* tp_flags */
+		Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS,	/* tp_flags */
 	frozenset_doc,			/* tp_doc */
 	(traverseproc)set_traverse,	/* tp_traverse */
 	0,				/* tp_clear */
 	(richcmpfunc)set_richcompare,	/* tp_richcompare */
-	0,				/* tp_weaklistoffset */
+	offsetof(PySetObject, weakreflist),	/* tp_weaklistoffset */
 	(getiterfunc)set_iter,		/* tp_iter */
 	0,				/* tp_iternext */
 	frozenset_methods,		/* tp_methods */