Kaydet (Commit) 691d8053 authored tarafından Raymond Hettinger's avatar Raymond Hettinger

Make sets and deques weak referencable.

üst d70ad8a9
...@@ -15,6 +15,7 @@ typedef struct { ...@@ -15,6 +15,7 @@ typedef struct {
PyObject_HEAD PyObject_HEAD
PyObject *data; PyObject *data;
long hash; /* only used by frozenset objects */ long hash; /* only used by frozenset objects */
PyObject *weakreflist; /* List of weak references */
} PySetObject; } PySetObject;
PyAPI_DATA(PyTypeObject) PySet_Type; PyAPI_DATA(PyTypeObject) PySet_Type;
......
from collections import deque from collections import deque
import unittest import unittest
from test import test_support from test import test_support
from weakref import proxy
import copy import copy
import cPickle as pickle import cPickle as pickle
from cStringIO import StringIO from cStringIO import StringIO
...@@ -435,6 +436,12 @@ class TestSubclass(unittest.TestCase): ...@@ -435,6 +436,12 @@ class TestSubclass(unittest.TestCase):
self.assertEqual(type(d), type(e)) self.assertEqual(type(d), type(e))
self.assertEqual(list(d), list(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)
#============================================================================== #==============================================================================
......
import unittest import unittest
from test import test_support from test import test_support
from weakref import proxy
import operator import operator
import copy import copy
import pickle import pickle
...@@ -346,6 +347,13 @@ class TestSet(TestJointOps): ...@@ -346,6 +347,13 @@ class TestSet(TestJointOps):
else: else:
self.assert_(c not in self.s) 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): class SetSubclass(set):
pass pass
......
#include "Python.h" #include "Python.h"
#include "structmember.h"
/* collections module implementation of a deque() datatype /* collections module implementation of a deque() datatype
Written and maintained by Raymond D. Hettinger <python@rcn.com> Written and maintained by Raymond D. Hettinger <python@rcn.com>
...@@ -32,6 +33,7 @@ typedef struct { ...@@ -32,6 +33,7 @@ typedef struct {
int leftindex; int leftindex;
int rightindex; int rightindex;
int len; int len;
PyObject *weakreflist; /* List of weak references */
} dequeobject; } dequeobject;
static PyTypeObject deque_type; static PyTypeObject deque_type;
...@@ -58,6 +60,7 @@ deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds) ...@@ -58,6 +60,7 @@ deque_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
deque->leftindex = BLOCKLEN / 2 + 1; deque->leftindex = BLOCKLEN / 2 + 1;
deque->rightindex = BLOCKLEN / 2; deque->rightindex = BLOCKLEN / 2;
deque->len = 0; deque->len = 0;
deque->weakreflist = NULL;
return (PyObject *)deque; return (PyObject *)deque;
} }
...@@ -439,6 +442,8 @@ static void ...@@ -439,6 +442,8 @@ static void
deque_dealloc(dequeobject *deque) deque_dealloc(dequeobject *deque)
{ {
PyObject_GC_UnTrack(deque); PyObject_GC_UnTrack(deque);
if (deque->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) deque);
if (deque->leftblock != NULL) { if (deque->leftblock != NULL) {
int err = deque_clear(deque); int err = deque_clear(deque);
assert(err == 0); assert(err == 0);
...@@ -744,12 +749,13 @@ static PyTypeObject deque_type = { ...@@ -744,12 +749,13 @@ static PyTypeObject deque_type = {
PyObject_GenericGetAttr, /* tp_getattro */ PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */ 0, /* tp_setattro */
0, /* tp_as_buffer */ 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 */ deque_doc, /* tp_doc */
(traverseproc)deque_traverse, /* tp_traverse */ (traverseproc)deque_traverse, /* tp_traverse */
(inquiry)deque_clear, /* tp_clear */ (inquiry)deque_clear, /* tp_clear */
(richcmpfunc)deque_richcompare, /* tp_richcompare */ (richcmpfunc)deque_richcompare, /* tp_richcompare */
0, /* tp_weaklistoffset*/ offsetof(dequeobject, weakreflist), /* tp_weaklistoffset*/
(getiterfunc)deque_iter, /* tp_iter */ (getiterfunc)deque_iter, /* tp_iter */
0, /* tp_iternext */ 0, /* tp_iternext */
deque_methods, /* tp_methods */ deque_methods, /* tp_methods */
......
#include "Python.h" #include "Python.h"
#include "structmember.h"
/* set object implementation /* set object implementation
written and maintained by Raymond D. Hettinger <python@rcn.com> written and maintained by Raymond D. Hettinger <python@rcn.com>
...@@ -61,6 +62,7 @@ make_new_set(PyTypeObject *type, PyObject *iterable) ...@@ -61,6 +62,7 @@ make_new_set(PyTypeObject *type, PyObject *iterable)
} }
so->data = data; so->data = data;
so->hash = -1; so->hash = -1;
so->weakreflist = NULL;
if (iterable != NULL) { if (iterable != NULL) {
tmp = set_update(so, iterable); tmp = set_update(so, iterable);
...@@ -113,6 +115,8 @@ static void ...@@ -113,6 +115,8 @@ static void
set_dealloc(PySetObject *so) set_dealloc(PySetObject *so)
{ {
PyObject_GC_UnTrack(so); PyObject_GC_UnTrack(so);
if (so->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) so);
Py_XDECREF(so->data); Py_XDECREF(so->data);
so->ob_type->tp_free(so); so->ob_type->tp_free(so);
} }
...@@ -1009,12 +1013,12 @@ PyTypeObject PySet_Type = { ...@@ -1009,12 +1013,12 @@ PyTypeObject PySet_Type = {
0, /* tp_setattro */ 0, /* tp_setattro */
0, /* tp_as_buffer */ 0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES | 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 */ set_doc, /* tp_doc */
(traverseproc)set_traverse, /* tp_traverse */ (traverseproc)set_traverse, /* tp_traverse */
(inquiry)set_tp_clear, /* tp_clear */ (inquiry)set_tp_clear, /* tp_clear */
(richcmpfunc)set_richcompare, /* tp_richcompare */ (richcmpfunc)set_richcompare, /* tp_richcompare */
0, /* tp_weaklistoffset */ offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
(getiterfunc)set_iter, /* tp_iter */ (getiterfunc)set_iter, /* tp_iter */
0, /* tp_iternext */ 0, /* tp_iternext */
set_methods, /* tp_methods */ set_methods, /* tp_methods */
...@@ -1104,12 +1108,12 @@ PyTypeObject PyFrozenSet_Type = { ...@@ -1104,12 +1108,12 @@ PyTypeObject PyFrozenSet_Type = {
0, /* tp_setattro */ 0, /* tp_setattro */
0, /* tp_as_buffer */ 0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES | 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 */ frozenset_doc, /* tp_doc */
(traverseproc)set_traverse, /* tp_traverse */ (traverseproc)set_traverse, /* tp_traverse */
0, /* tp_clear */ 0, /* tp_clear */
(richcmpfunc)set_richcompare, /* tp_richcompare */ (richcmpfunc)set_richcompare, /* tp_richcompare */
0, /* tp_weaklistoffset */ offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
(getiterfunc)set_iter, /* tp_iter */ (getiterfunc)set_iter, /* tp_iter */
0, /* tp_iternext */ 0, /* tp_iternext */
frozenset_methods, /* tp_methods */ frozenset_methods, /* tp_methods */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment