Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 2 | #include "structmember.h" |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 3 | |
| 4 | /* set object implementation |
| 5 | written and maintained by Raymond D. Hettinger <python@rcn.com> |
| 6 | derived from sets.py written by Greg V. Wilson, Alex Martelli, |
| 7 | Guido van Rossum, Raymond Hettinger, and Tim Peters. |
| 8 | |
| 9 | Copyright (c) 2003 Python Software Foundation. |
| 10 | All rights reserved. |
| 11 | */ |
| 12 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 13 | static PyObject * |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 14 | set_update(PySetObject *so, PyObject *other) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 15 | { |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 16 | PyObject *item, *data, *it; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 17 | |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 18 | if (PyAnySet_Check(other)) { |
Raymond Hettinger | 81ad32e | 2003-12-15 21:16:06 +0000 | [diff] [blame] | 19 | if (PyDict_Merge(so->data, ((PySetObject *)other)->data, 1) == -1) |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 20 | return NULL; |
| 21 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 22 | } |
| 23 | |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 24 | it = PyObject_GetIter(other); |
| 25 | if (it == NULL) |
| 26 | return NULL; |
| 27 | data = so->data; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 28 | |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 29 | while ((item = PyIter_Next(it)) != NULL) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 30 | if (PyDict_SetItem(data, item, Py_True) == -1) { |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 31 | Py_DECREF(it); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 32 | Py_DECREF(item); |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 33 | return NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 34 | } |
| 35 | Py_DECREF(item); |
| 36 | } |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 37 | Py_DECREF(it); |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 38 | if (PyErr_Occurred()) |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 39 | return NULL; |
| 40 | Py_RETURN_NONE; |
| 41 | } |
| 42 | |
| 43 | PyDoc_STRVAR(update_doc, |
| 44 | "Update a set with the union of itself and another."); |
| 45 | |
| 46 | static PyObject * |
| 47 | make_new_set(PyTypeObject *type, PyObject *iterable) |
| 48 | { |
| 49 | PyObject *data = NULL; |
| 50 | PyObject *tmp; |
| 51 | PySetObject *so = NULL; |
| 52 | |
| 53 | data = PyDict_New(); |
| 54 | if (data == NULL) |
| 55 | return NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 56 | |
| 57 | /* create PySetObject structure */ |
| 58 | so = (PySetObject *)type->tp_alloc(type, 0); |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 59 | if (so == NULL) { |
| 60 | Py_DECREF(data); |
| 61 | return NULL; |
| 62 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 63 | so->data = data; |
| 64 | so->hash = -1; |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 65 | so->weakreflist = NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 66 | |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 67 | if (iterable != NULL) { |
| 68 | tmp = set_update(so, iterable); |
| 69 | if (tmp == NULL) { |
| 70 | Py_DECREF(so); |
| 71 | return NULL; |
| 72 | } |
| 73 | Py_DECREF(tmp); |
| 74 | } |
| 75 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 76 | return (PyObject *)so; |
| 77 | } |
| 78 | |
| 79 | static PyObject * |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 80 | frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 81 | { |
| 82 | PyObject *iterable = NULL; |
| 83 | |
| 84 | if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable)) |
| 85 | return NULL; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 86 | if (iterable != NULL && PyFrozenSet_CheckExact(iterable)) { |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 87 | Py_INCREF(iterable); |
| 88 | return iterable; |
| 89 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 90 | return make_new_set(type, iterable); |
| 91 | } |
| 92 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 93 | static PyObject * |
| 94 | set_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 95 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 96 | return make_new_set(type, NULL); |
| 97 | } |
| 98 | |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 99 | static PyObject * |
| 100 | frozenset_dict_wrapper(PyObject *d) |
| 101 | { |
| 102 | PySetObject *w; |
| 103 | |
| 104 | assert(PyDict_Check(d)); |
| 105 | w = (PySetObject *)make_new_set(&PyFrozenSet_Type, NULL); |
| 106 | if (w == NULL) |
| 107 | return NULL; |
Raymond Hettinger | 6429a47 | 2004-09-28 01:51:35 +0000 | [diff] [blame] | 108 | Py_CLEAR(w->data); |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 109 | Py_INCREF(d); |
| 110 | w->data = d; |
| 111 | return (PyObject *)w; |
| 112 | } |
| 113 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 114 | static void |
| 115 | set_dealloc(PySetObject *so) |
| 116 | { |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 117 | PyObject_GC_UnTrack(so); |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 118 | if (so->weakreflist != NULL) |
| 119 | PyObject_ClearWeakRefs((PyObject *) so); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 120 | Py_XDECREF(so->data); |
| 121 | so->ob_type->tp_free(so); |
| 122 | } |
| 123 | |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 124 | static int |
| 125 | set_traverse(PySetObject *so, visitproc visit, void *arg) |
| 126 | { |
| 127 | if (so->data) |
| 128 | return visit(so->data, arg); |
| 129 | return 0; |
| 130 | } |
| 131 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 132 | static PyObject * |
| 133 | set_iter(PySetObject *so) |
| 134 | { |
| 135 | return PyObject_GetIter(so->data); |
| 136 | } |
| 137 | |
| 138 | static int |
| 139 | set_len(PySetObject *so) |
| 140 | { |
| 141 | return PyDict_Size(so->data); |
| 142 | } |
| 143 | |
| 144 | static int |
| 145 | set_contains(PySetObject *so, PyObject *key) |
| 146 | { |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 147 | PyObject *tmp; |
Raymond Hettinger | 19c2d77 | 2003-11-21 18:36:54 +0000 | [diff] [blame] | 148 | int result; |
| 149 | |
Raymond Hettinger | bc0f2ab | 2003-11-25 21:12:14 +0000 | [diff] [blame] | 150 | result = PyDict_Contains(so->data, key); |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 151 | if (result == -1 && PyAnySet_Check(key)) { |
Raymond Hettinger | 19c2d77 | 2003-11-21 18:36:54 +0000 | [diff] [blame] | 152 | PyErr_Clear(); |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 153 | tmp = frozenset_dict_wrapper(((PySetObject *)(key))->data); |
Raymond Hettinger | 19c2d77 | 2003-11-21 18:36:54 +0000 | [diff] [blame] | 154 | if (tmp == NULL) |
| 155 | return -1; |
Raymond Hettinger | bc0f2ab | 2003-11-25 21:12:14 +0000 | [diff] [blame] | 156 | result = PyDict_Contains(so->data, tmp); |
Raymond Hettinger | 19c2d77 | 2003-11-21 18:36:54 +0000 | [diff] [blame] | 157 | Py_DECREF(tmp); |
| 158 | } |
| 159 | return result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static PyObject * |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 163 | set_direct_contains(PySetObject *so, PyObject *key) |
| 164 | { |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 165 | long result; |
| 166 | |
Raymond Hettinger | 438e02d | 2003-12-13 19:38:47 +0000 | [diff] [blame] | 167 | result = set_contains(so, key); |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 168 | if (result == -1) |
| 169 | return NULL; |
| 170 | return PyBool_FromLong(result); |
| 171 | } |
| 172 | |
| 173 | PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x."); |
| 174 | |
| 175 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 176 | set_copy(PySetObject *so) |
| 177 | { |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 178 | return make_new_set(so->ob_type, (PyObject *)so); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 181 | static PyObject * |
| 182 | frozenset_copy(PySetObject *so) |
| 183 | { |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 184 | if (PyFrozenSet_CheckExact(so)) { |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 185 | Py_INCREF(so); |
| 186 | return (PyObject *)so; |
| 187 | } |
| 188 | return set_copy(so); |
| 189 | } |
| 190 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 191 | PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set."); |
| 192 | |
| 193 | static PyObject * |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 194 | set_union(PySetObject *so, PyObject *other) |
| 195 | { |
| 196 | PySetObject *result; |
| 197 | PyObject *rv; |
| 198 | |
| 199 | result = (PySetObject *)set_copy(so); |
| 200 | if (result == NULL) |
| 201 | return NULL; |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 202 | rv = set_update(result, other); |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 203 | if (rv == NULL) { |
| 204 | Py_DECREF(result); |
| 205 | return NULL; |
| 206 | } |
| 207 | Py_DECREF(rv); |
| 208 | return (PyObject *)result; |
| 209 | } |
| 210 | |
| 211 | PyDoc_STRVAR(union_doc, |
| 212 | "Return the union of two sets as a new set.\n\ |
| 213 | \n\ |
| 214 | (i.e. all elements that are in either set.)"); |
| 215 | |
| 216 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 217 | set_or(PySetObject *so, PyObject *other) |
| 218 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 219 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 220 | Py_INCREF(Py_NotImplemented); |
| 221 | return Py_NotImplemented; |
| 222 | } |
| 223 | return set_union(so, other); |
| 224 | } |
| 225 | |
| 226 | static PyObject * |
| 227 | set_ior(PySetObject *so, PyObject *other) |
| 228 | { |
| 229 | PyObject *result; |
| 230 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 231 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 232 | Py_INCREF(Py_NotImplemented); |
| 233 | return Py_NotImplemented; |
| 234 | } |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 235 | result = set_update(so, other); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 236 | if (result == NULL) |
| 237 | return NULL; |
| 238 | Py_DECREF(result); |
| 239 | Py_INCREF(so); |
| 240 | return (PyObject *)so; |
| 241 | } |
| 242 | |
| 243 | static PyObject * |
| 244 | set_intersection(PySetObject *so, PyObject *other) |
| 245 | { |
| 246 | PySetObject *result; |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 247 | PyObject *item, *selfdata, *tgtdata, *it, *tmp; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 248 | |
| 249 | result = (PySetObject *)make_new_set(so->ob_type, NULL); |
| 250 | if (result == NULL) |
| 251 | return NULL; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 252 | tgtdata = result->data; |
| 253 | selfdata = so->data; |
| 254 | |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 255 | if (PyAnySet_Check(other)) |
| 256 | other = ((PySetObject *)other)->data; |
| 257 | |
| 258 | if (PyDict_Check(other) && PyDict_Size(other) > PyDict_Size(selfdata)) { |
| 259 | tmp = selfdata; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 260 | selfdata = other; |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 261 | other = tmp; |
| 262 | } |
| 263 | |
| 264 | if (PyDict_CheckExact(other)) { |
| 265 | PyObject *value; |
| 266 | int pos = 0; |
| 267 | while (PyDict_Next(other, &pos, &item, &value)) { |
| 268 | if (PyDict_Contains(selfdata, item)) { |
| 269 | if (PyDict_SetItem(tgtdata, item, Py_True) == -1) { |
| 270 | Py_DECREF(result); |
| 271 | return NULL; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | return (PyObject *)result; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 278 | it = PyObject_GetIter(other); |
| 279 | if (it == NULL) { |
| 280 | Py_DECREF(result); |
| 281 | return NULL; |
| 282 | } |
| 283 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 284 | while ((item = PyIter_Next(it)) != NULL) { |
Raymond Hettinger | bc0f2ab | 2003-11-25 21:12:14 +0000 | [diff] [blame] | 285 | if (PyDict_Contains(selfdata, item)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 286 | if (PyDict_SetItem(tgtdata, item, Py_True) == -1) { |
| 287 | Py_DECREF(it); |
| 288 | Py_DECREF(result); |
| 289 | Py_DECREF(item); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 290 | return NULL; |
| 291 | } |
| 292 | } |
| 293 | Py_DECREF(item); |
| 294 | } |
| 295 | Py_DECREF(it); |
| 296 | if (PyErr_Occurred()) { |
| 297 | Py_DECREF(result); |
| 298 | return NULL; |
| 299 | } |
| 300 | return (PyObject *)result; |
| 301 | } |
| 302 | |
| 303 | PyDoc_STRVAR(intersection_doc, |
| 304 | "Return the intersection of two sets as a new set.\n\ |
| 305 | \n\ |
| 306 | (i.e. all elements that are in both sets.)"); |
| 307 | |
| 308 | static PyObject * |
| 309 | set_intersection_update(PySetObject *so, PyObject *other) |
| 310 | { |
| 311 | PyObject *item, *selfdata, *it, *newdict, *tmp; |
| 312 | |
| 313 | newdict = PyDict_New(); |
| 314 | if (newdict == NULL) |
| 315 | return newdict; |
| 316 | |
| 317 | it = PyObject_GetIter(other); |
| 318 | if (it == NULL) { |
| 319 | Py_DECREF(newdict); |
| 320 | return NULL; |
| 321 | } |
| 322 | |
| 323 | selfdata = so->data; |
| 324 | while ((item = PyIter_Next(it)) != NULL) { |
Raymond Hettinger | bc0f2ab | 2003-11-25 21:12:14 +0000 | [diff] [blame] | 325 | if (PyDict_Contains(selfdata, item)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 326 | if (PyDict_SetItem(newdict, item, Py_True) == -1) { |
| 327 | Py_DECREF(newdict); |
| 328 | Py_DECREF(it); |
| 329 | Py_DECREF(item); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 330 | return NULL; |
| 331 | } |
| 332 | } |
| 333 | Py_DECREF(item); |
| 334 | } |
| 335 | Py_DECREF(it); |
| 336 | if (PyErr_Occurred()) { |
| 337 | Py_DECREF(newdict); |
| 338 | return NULL; |
| 339 | } |
| 340 | tmp = so->data; |
| 341 | so->data = newdict; |
| 342 | Py_DECREF(tmp); |
| 343 | Py_RETURN_NONE; |
| 344 | } |
| 345 | |
| 346 | PyDoc_STRVAR(intersection_update_doc, |
| 347 | "Update a set with the intersection of itself and another."); |
| 348 | |
| 349 | static PyObject * |
| 350 | set_and(PySetObject *so, PyObject *other) |
| 351 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 352 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 353 | Py_INCREF(Py_NotImplemented); |
| 354 | return Py_NotImplemented; |
| 355 | } |
| 356 | return set_intersection(so, other); |
| 357 | } |
| 358 | |
| 359 | static PyObject * |
| 360 | set_iand(PySetObject *so, PyObject *other) |
| 361 | { |
| 362 | PyObject *result; |
| 363 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 364 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 365 | Py_INCREF(Py_NotImplemented); |
| 366 | return Py_NotImplemented; |
| 367 | } |
| 368 | result = set_intersection_update(so, other); |
| 369 | if (result == NULL) |
| 370 | return NULL; |
| 371 | Py_DECREF(result); |
| 372 | Py_INCREF(so); |
| 373 | return (PyObject *)so; |
| 374 | } |
| 375 | |
| 376 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 377 | set_difference_update(PySetObject *so, PyObject *other) |
| 378 | { |
| 379 | PyObject *item, *tgtdata, *it; |
| 380 | |
| 381 | it = PyObject_GetIter(other); |
| 382 | if (it == NULL) |
| 383 | return NULL; |
| 384 | |
| 385 | tgtdata = so->data; |
| 386 | while ((item = PyIter_Next(it)) != NULL) { |
| 387 | if (PyDict_DelItem(tgtdata, item) == -1) { |
| 388 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 389 | PyErr_Clear(); |
| 390 | else { |
| 391 | Py_DECREF(it); |
| 392 | Py_DECREF(item); |
| 393 | return NULL; |
| 394 | } |
| 395 | } |
| 396 | Py_DECREF(item); |
| 397 | } |
| 398 | Py_DECREF(it); |
| 399 | if (PyErr_Occurred()) |
| 400 | return NULL; |
| 401 | Py_RETURN_NONE; |
| 402 | } |
| 403 | |
| 404 | PyDoc_STRVAR(difference_update_doc, |
| 405 | "Remove all elements of another set from this set."); |
| 406 | |
| 407 | static PyObject * |
Raymond Hettinger | fb4e33a | 2003-12-15 13:23:55 +0000 | [diff] [blame] | 408 | set_difference(PySetObject *so, PyObject *other) |
| 409 | { |
| 410 | PyObject *result, *tmp; |
| 411 | PyObject *otherdata, *tgtdata; |
| 412 | PyObject *key, *value; |
| 413 | int pos = 0; |
| 414 | |
| 415 | if (PyDict_Check(other)) |
| 416 | otherdata = other; |
| 417 | else if (PyAnySet_Check(other)) |
| 418 | otherdata = ((PySetObject *)other)->data; |
| 419 | else { |
| 420 | result = set_copy(so); |
| 421 | if (result == NULL) |
| 422 | return result; |
| 423 | tmp = set_difference_update((PySetObject *)result, other); |
| 424 | if (tmp != NULL) { |
| 425 | Py_DECREF(tmp); |
| 426 | return result; |
| 427 | } |
| 428 | Py_DECREF(result); |
| 429 | return NULL; |
| 430 | } |
| 431 | |
| 432 | result = make_new_set(so->ob_type, NULL); |
| 433 | if (result == NULL) |
| 434 | return NULL; |
| 435 | tgtdata = ((PySetObject *)result)->data; |
| 436 | |
| 437 | while (PyDict_Next(so->data, &pos, &key, &value)) { |
| 438 | if (!PyDict_Contains(otherdata, key)) { |
| 439 | if (PyDict_SetItem(tgtdata, key, Py_True) == -1) |
| 440 | return NULL; |
| 441 | } |
| 442 | } |
| 443 | return result; |
| 444 | } |
| 445 | |
| 446 | PyDoc_STRVAR(difference_doc, |
| 447 | "Return the difference of two sets as a new set.\n\ |
| 448 | \n\ |
| 449 | (i.e. all elements that are in this set but not the other.)"); |
| 450 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 451 | set_sub(PySetObject *so, PyObject *other) |
| 452 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 453 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 454 | Py_INCREF(Py_NotImplemented); |
| 455 | return Py_NotImplemented; |
| 456 | } |
| 457 | return set_difference(so, other); |
| 458 | } |
| 459 | |
| 460 | static PyObject * |
| 461 | set_isub(PySetObject *so, PyObject *other) |
| 462 | { |
| 463 | PyObject *result; |
| 464 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 465 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 466 | Py_INCREF(Py_NotImplemented); |
| 467 | return Py_NotImplemented; |
| 468 | } |
| 469 | result = set_difference_update(so, other); |
| 470 | if (result == NULL) |
| 471 | return NULL; |
| 472 | Py_DECREF(result); |
| 473 | Py_INCREF(so); |
| 474 | return (PyObject *)so; |
| 475 | } |
| 476 | |
| 477 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 478 | set_symmetric_difference_update(PySetObject *so, PyObject *other) |
| 479 | { |
Raymond Hettinger | dc5ae11 | 2003-12-13 14:46:46 +0000 | [diff] [blame] | 480 | PyObject *selfdata, *otherdata; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 481 | PySetObject *otherset = NULL; |
Raymond Hettinger | dc5ae11 | 2003-12-13 14:46:46 +0000 | [diff] [blame] | 482 | PyObject *key, *value; |
| 483 | int pos = 0; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 484 | |
| 485 | selfdata = so->data; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 486 | if (PyDict_Check(other)) |
| 487 | otherdata = other; |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 488 | else if (PyAnySet_Check(other)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 489 | otherdata = ((PySetObject *)other)->data; |
| 490 | else { |
| 491 | otherset = (PySetObject *)make_new_set(so->ob_type, other); |
| 492 | if (otherset == NULL) |
| 493 | return NULL; |
| 494 | otherdata = otherset->data; |
| 495 | } |
| 496 | |
Raymond Hettinger | dc5ae11 | 2003-12-13 14:46:46 +0000 | [diff] [blame] | 497 | while (PyDict_Next(otherdata, &pos, &key, &value)) { |
| 498 | if (PyDict_Contains(selfdata, key)) { |
| 499 | if (PyDict_DelItem(selfdata, key) == -1) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 500 | Py_XDECREF(otherset); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 501 | return NULL; |
| 502 | } |
| 503 | } else { |
Raymond Hettinger | dc5ae11 | 2003-12-13 14:46:46 +0000 | [diff] [blame] | 504 | if (PyDict_SetItem(selfdata, key, Py_True) == -1) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 505 | Py_XDECREF(otherset); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 506 | return NULL; |
| 507 | } |
| 508 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 509 | } |
| 510 | Py_XDECREF(otherset); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 511 | Py_RETURN_NONE; |
| 512 | } |
| 513 | |
| 514 | PyDoc_STRVAR(symmetric_difference_update_doc, |
| 515 | "Update a set with the symmetric difference of itself and another."); |
| 516 | |
| 517 | static PyObject * |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 518 | set_symmetric_difference(PySetObject *so, PyObject *other) |
| 519 | { |
| 520 | PySetObject *result; |
Raymond Hettinger | dc5ae11 | 2003-12-13 14:46:46 +0000 | [diff] [blame] | 521 | PyObject *selfdata, *otherdata, *tgtdata, *rv, *otherset; |
| 522 | PyObject *key, *value; |
| 523 | int pos = 0; |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 524 | |
| 525 | if (PyDict_Check(other)) |
| 526 | otherdata = other; |
| 527 | else if (PyAnySet_Check(other)) |
| 528 | otherdata = ((PySetObject *)other)->data; |
| 529 | else { |
| 530 | otherset = make_new_set(so->ob_type, other); |
| 531 | if (otherset == NULL) |
| 532 | return NULL; |
| 533 | rv = set_symmetric_difference_update((PySetObject *)otherset, (PyObject *)so); |
| 534 | if (rv == NULL) |
| 535 | return NULL; |
| 536 | Py_DECREF(rv); |
| 537 | return otherset; |
| 538 | } |
| 539 | |
| 540 | result = (PySetObject *)make_new_set(so->ob_type, NULL); |
| 541 | if (result == NULL) |
| 542 | return NULL; |
| 543 | tgtdata = result->data; |
| 544 | selfdata = so->data; |
| 545 | |
Raymond Hettinger | dc5ae11 | 2003-12-13 14:46:46 +0000 | [diff] [blame] | 546 | while (PyDict_Next(otherdata, &pos, &key, &value)) { |
| 547 | if (!PyDict_Contains(selfdata, key)) { |
| 548 | if (PyDict_SetItem(tgtdata, key, Py_True) == -1) { |
| 549 | Py_DECREF(result); |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 550 | return NULL; |
| 551 | } |
| 552 | } |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Raymond Hettinger | dc5ae11 | 2003-12-13 14:46:46 +0000 | [diff] [blame] | 555 | pos = 0; |
| 556 | while (PyDict_Next(selfdata, &pos, &key, &value)) { |
| 557 | if (!PyDict_Contains(otherdata, key)) { |
| 558 | if (PyDict_SetItem(tgtdata, key, Py_True) == -1) { |
| 559 | Py_DECREF(result); |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 560 | return NULL; |
| 561 | } |
| 562 | } |
Raymond Hettinger | f5f41bf | 2003-11-24 02:57:33 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | return (PyObject *)result; |
| 566 | } |
| 567 | |
| 568 | PyDoc_STRVAR(symmetric_difference_doc, |
| 569 | "Return the symmetric difference of two sets as a new set.\n\ |
| 570 | \n\ |
| 571 | (i.e. all elements that are in exactly one of the sets.)"); |
| 572 | |
| 573 | static PyObject * |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 574 | set_xor(PySetObject *so, PyObject *other) |
| 575 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 576 | if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 577 | Py_INCREF(Py_NotImplemented); |
| 578 | return Py_NotImplemented; |
| 579 | } |
| 580 | return set_symmetric_difference(so, other); |
| 581 | } |
| 582 | |
| 583 | static PyObject * |
| 584 | set_ixor(PySetObject *so, PyObject *other) |
| 585 | { |
| 586 | PyObject *result; |
| 587 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 588 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 589 | Py_INCREF(Py_NotImplemented); |
| 590 | return Py_NotImplemented; |
| 591 | } |
| 592 | result = set_symmetric_difference_update(so, other); |
| 593 | if (result == NULL) |
| 594 | return NULL; |
| 595 | Py_DECREF(result); |
| 596 | Py_INCREF(so); |
| 597 | return (PyObject *)so; |
| 598 | } |
| 599 | |
| 600 | static PyObject * |
| 601 | set_issubset(PySetObject *so, PyObject *other) |
| 602 | { |
Raymond Hettinger | dc5ae11 | 2003-12-13 14:46:46 +0000 | [diff] [blame] | 603 | PyObject *otherdata, *tmp, *result; |
| 604 | PyObject *key, *value; |
| 605 | int pos = 0; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 606 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 607 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | 3fbec70 | 2003-11-21 07:56:36 +0000 | [diff] [blame] | 608 | tmp = make_new_set(&PySet_Type, other); |
| 609 | if (tmp == NULL) |
| 610 | return NULL; |
| 611 | result = set_issubset(so, tmp); |
| 612 | Py_DECREF(tmp); |
| 613 | return result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 614 | } |
| 615 | if (set_len(so) > set_len((PySetObject *)other)) |
| 616 | Py_RETURN_FALSE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 617 | |
| 618 | otherdata = ((PySetObject *)other)->data; |
Raymond Hettinger | dc5ae11 | 2003-12-13 14:46:46 +0000 | [diff] [blame] | 619 | while (PyDict_Next(((PySetObject *)so)->data, &pos, &key, &value)) { |
| 620 | if (!PyDict_Contains(otherdata, key)) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 621 | Py_RETURN_FALSE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 622 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 623 | Py_RETURN_TRUE; |
| 624 | } |
| 625 | |
| 626 | PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set."); |
| 627 | |
| 628 | static PyObject * |
| 629 | set_issuperset(PySetObject *so, PyObject *other) |
| 630 | { |
Raymond Hettinger | 3fbec70 | 2003-11-21 07:56:36 +0000 | [diff] [blame] | 631 | PyObject *tmp, *result; |
| 632 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 633 | if (!PyAnySet_Check(other)) { |
Raymond Hettinger | 3fbec70 | 2003-11-21 07:56:36 +0000 | [diff] [blame] | 634 | tmp = make_new_set(&PySet_Type, other); |
| 635 | if (tmp == NULL) |
| 636 | return NULL; |
| 637 | result = set_issuperset(so, tmp); |
| 638 | Py_DECREF(tmp); |
| 639 | return result; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 640 | } |
| 641 | return set_issubset((PySetObject *)other, (PyObject *)so); |
| 642 | } |
| 643 | |
| 644 | PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set."); |
| 645 | |
| 646 | static long |
| 647 | set_nohash(PyObject *self) |
| 648 | { |
| 649 | PyErr_SetString(PyExc_TypeError, "set objects are unhashable"); |
| 650 | return -1; |
| 651 | } |
| 652 | |
| 653 | static int |
| 654 | set_nocmp(PyObject *self) |
| 655 | { |
| 656 | PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()"); |
| 657 | return -1; |
| 658 | } |
| 659 | |
| 660 | static long |
| 661 | frozenset_hash(PyObject *self) |
| 662 | { |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 663 | PySetObject *so = (PySetObject *)self; |
Raymond Hettinger | dc5ae11 | 2003-12-13 14:46:46 +0000 | [diff] [blame] | 664 | PyObject *key, *value; |
| 665 | int pos = 0; |
Raymond Hettinger | c978633 | 2004-06-10 22:41:48 +0000 | [diff] [blame] | 666 | long hash = 1927868237L; |
Raymond Hettinger | 438e02d | 2003-12-13 19:38:47 +0000 | [diff] [blame] | 667 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 668 | if (so->hash != -1) |
| 669 | return so->hash; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 670 | |
Raymond Hettinger | c978633 | 2004-06-10 22:41:48 +0000 | [diff] [blame] | 671 | hash *= (PyDict_Size(so->data) + 1); |
Raymond Hettinger | dc5ae11 | 2003-12-13 14:46:46 +0000 | [diff] [blame] | 672 | while (PyDict_Next(so->data, &pos, &key, &value)) { |
Raymond Hettinger | c978633 | 2004-06-10 22:41:48 +0000 | [diff] [blame] | 673 | /* Work to increase the bit dispersion for closely spaced hash |
| 674 | values. The is important because some use cases have many |
| 675 | combinations of a small number of elements with nearby |
| 676 | hashes so that many distinct combinations collapse to only |
| 677 | a handful of distinct hash values. */ |
| 678 | long h = PyObject_Hash(key); |
| 679 | hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 680 | } |
Raymond Hettinger | c978633 | 2004-06-10 22:41:48 +0000 | [diff] [blame] | 681 | hash = hash * 69069L + 907133923L; |
Raymond Hettinger | 27e403e | 2004-06-10 21:38:41 +0000 | [diff] [blame] | 682 | if (hash == -1) |
| 683 | hash = 590923713L; |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 684 | so->hash = hash; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 685 | return hash; |
| 686 | } |
| 687 | |
| 688 | static PyObject * |
| 689 | set_richcompare(PySetObject *v, PyObject *w, int op) |
| 690 | { |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 691 | if(!PyAnySet_Check(w)) { |
| 692 | if (op == Py_EQ) |
| 693 | Py_RETURN_FALSE; |
| 694 | if (op == Py_NE) |
| 695 | Py_RETURN_TRUE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 696 | PyErr_SetString(PyExc_TypeError, "can only compare to a set"); |
| 697 | return NULL; |
| 698 | } |
| 699 | switch (op) { |
| 700 | case Py_EQ: |
| 701 | case Py_NE: |
| 702 | return PyObject_RichCompare(((PySetObject *)v)->data, |
| 703 | ((PySetObject *)w)->data, op); |
| 704 | case Py_LE: |
| 705 | return set_issubset((PySetObject *)v, w); |
| 706 | case Py_GE: |
| 707 | return set_issuperset((PySetObject *)v, w); |
| 708 | case Py_LT: |
| 709 | if (set_len(v) >= set_len((PySetObject *)w)) |
| 710 | Py_RETURN_FALSE; |
| 711 | return set_issubset((PySetObject *)v, w); |
| 712 | case Py_GT: |
| 713 | if (set_len(v) <= set_len((PySetObject *)w)) |
| 714 | Py_RETURN_FALSE; |
| 715 | return set_issuperset((PySetObject *)v, w); |
| 716 | } |
| 717 | Py_INCREF(Py_NotImplemented); |
| 718 | return Py_NotImplemented; |
| 719 | } |
| 720 | |
| 721 | static PyObject * |
| 722 | set_repr(PySetObject *so) |
| 723 | { |
| 724 | PyObject *keys, *result, *listrepr; |
| 725 | |
| 726 | keys = PyDict_Keys(so->data); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 727 | if (keys == NULL) |
| 728 | return NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 729 | listrepr = PyObject_Repr(keys); |
| 730 | Py_DECREF(keys); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 731 | if (listrepr == NULL) |
| 732 | return NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 733 | |
| 734 | result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name, |
| 735 | PyString_AS_STRING(listrepr)); |
| 736 | Py_DECREF(listrepr); |
| 737 | return result; |
| 738 | } |
| 739 | |
| 740 | static int |
| 741 | set_tp_print(PySetObject *so, FILE *fp, int flags) |
| 742 | { |
Raymond Hettinger | dc5ae11 | 2003-12-13 14:46:46 +0000 | [diff] [blame] | 743 | PyObject *key, *value; |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 744 | int pos=0; |
| 745 | char *emit = ""; /* No separator emitted on first pass */ |
| 746 | char *separator = ", "; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 747 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 748 | fprintf(fp, "%s([", so->ob_type->tp_name); |
Raymond Hettinger | dc5ae11 | 2003-12-13 14:46:46 +0000 | [diff] [blame] | 749 | while (PyDict_Next(so->data, &pos, &key, &value)) { |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 750 | fputs(emit, fp); |
| 751 | emit = separator; |
Raymond Hettinger | dc5ae11 | 2003-12-13 14:46:46 +0000 | [diff] [blame] | 752 | if (PyObject_Print(key, fp, 0) != 0) |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 753 | return -1; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 754 | } |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 755 | fputs("])", fp); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | static PyObject * |
| 760 | set_clear(PySetObject *so) |
| 761 | { |
| 762 | PyDict_Clear(so->data); |
| 763 | so->hash = -1; |
Raymond Hettinger | 438e02d | 2003-12-13 19:38:47 +0000 | [diff] [blame] | 764 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | PyDoc_STRVAR(clear_doc, "Remove all elements from this set."); |
| 768 | |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 769 | static int |
| 770 | set_tp_clear(PySetObject *so) |
| 771 | { |
| 772 | PyDict_Clear(so->data); |
| 773 | so->hash = -1; |
| 774 | return 0; |
| 775 | } |
| 776 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 777 | static PyObject * |
| 778 | set_add(PySetObject *so, PyObject *item) |
| 779 | { |
| 780 | if (PyDict_SetItem(so->data, item, Py_True) == -1) |
| 781 | return NULL; |
Raymond Hettinger | 438e02d | 2003-12-13 19:38:47 +0000 | [diff] [blame] | 782 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | PyDoc_STRVAR(add_doc, |
| 786 | "Add an element to a set.\n\ |
| 787 | \n\ |
| 788 | This has no effect if the element is already present."); |
| 789 | |
| 790 | static PyObject * |
| 791 | set_remove(PySetObject *so, PyObject *item) |
| 792 | { |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 793 | PyObject *tmp, *result; |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 794 | |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 795 | if (PyType_IsSubtype(item->ob_type, &PySet_Type)) { |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 796 | tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data); |
| 797 | if (tmp == NULL) |
| 798 | return NULL; |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 799 | result = set_remove(so, tmp); |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 800 | Py_DECREF(tmp); |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 801 | return result; |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 802 | } |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 803 | |
| 804 | if (PyDict_DelItem(so->data, item) == -1) |
| 805 | return NULL; |
Raymond Hettinger | 438e02d | 2003-12-13 19:38:47 +0000 | [diff] [blame] | 806 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | PyDoc_STRVAR(remove_doc, |
| 810 | "Remove an element from a set; it must be a member.\n\ |
| 811 | \n\ |
| 812 | If the element is not a member, raise a KeyError."); |
| 813 | |
| 814 | static PyObject * |
| 815 | set_discard(PySetObject *so, PyObject *item) |
| 816 | { |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 817 | PyObject *tmp, *result; |
| 818 | |
| 819 | if (PyType_IsSubtype(item->ob_type, &PySet_Type)) { |
| 820 | tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data); |
| 821 | if (tmp == NULL) |
| 822 | return NULL; |
| 823 | result = set_discard(so, tmp); |
| 824 | Py_DECREF(tmp); |
| 825 | return result; |
| 826 | } |
Raymond Hettinger | bfd334a | 2003-11-22 03:55:23 +0000 | [diff] [blame] | 827 | |
Guido van Rossum | b61982b | 2003-11-18 19:27:19 +0000 | [diff] [blame] | 828 | if (PyDict_DelItem(so->data, item) == -1) { |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 829 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) |
| 830 | return NULL; |
| 831 | PyErr_Clear(); |
Guido van Rossum | b61982b | 2003-11-18 19:27:19 +0000 | [diff] [blame] | 832 | } |
Raymond Hettinger | 438e02d | 2003-12-13 19:38:47 +0000 | [diff] [blame] | 833 | Py_RETURN_NONE; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | PyDoc_STRVAR(discard_doc, |
| 837 | "Remove an element from a set if it is a member.\n\ |
| 838 | \n\ |
| 839 | If the element is not a member, do nothing."); |
| 840 | |
| 841 | static PyObject * |
| 842 | set_pop(PySetObject *so) |
| 843 | { |
| 844 | PyObject *key, *value; |
| 845 | int pos = 0; |
| 846 | |
| 847 | if (!PyDict_Next(so->data, &pos, &key, &value)) { |
| 848 | PyErr_SetString(PyExc_KeyError, "pop from an empty set"); |
| 849 | return NULL; |
| 850 | } |
| 851 | Py_INCREF(key); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 852 | if (PyDict_DelItem(so->data, key) == -1) { |
| 853 | Py_DECREF(key); |
| 854 | return NULL; |
| 855 | } |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 856 | return key; |
| 857 | } |
| 858 | |
| 859 | PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element."); |
| 860 | |
| 861 | static PyObject * |
| 862 | set_reduce(PySetObject *so) |
| 863 | { |
Raymond Hettinger | 15056a5 | 2004-11-09 07:25:31 +0000 | [diff] [blame] | 864 | PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL; |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 865 | |
| 866 | keys = PyDict_Keys(so->data); |
| 867 | if (keys == NULL) |
| 868 | goto done; |
| 869 | args = PyTuple_Pack(1, keys); |
| 870 | if (args == NULL) |
| 871 | goto done; |
Raymond Hettinger | 15056a5 | 2004-11-09 07:25:31 +0000 | [diff] [blame] | 872 | dict = PyObject_GetAttrString((PyObject *)so, "__dict__"); |
| 873 | if (dict == NULL) { |
| 874 | PyErr_Clear(); |
| 875 | dict = Py_None; |
| 876 | Py_INCREF(dict); |
| 877 | } |
| 878 | result = PyTuple_Pack(3, so->ob_type, args, dict); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 879 | done: |
| 880 | Py_XDECREF(args); |
| 881 | Py_XDECREF(keys); |
Raymond Hettinger | 15056a5 | 2004-11-09 07:25:31 +0000 | [diff] [blame] | 882 | Py_XDECREF(dict); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 883 | return result; |
| 884 | } |
| 885 | |
| 886 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 887 | |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 888 | static int |
| 889 | set_init(PySetObject *self, PyObject *args, PyObject *kwds) |
| 890 | { |
| 891 | PyObject *iterable = NULL; |
| 892 | PyObject *result; |
| 893 | |
| 894 | if (!PyAnySet_Check(self)) |
| 895 | return -1; |
| 896 | if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable)) |
| 897 | return -1; |
| 898 | PyDict_Clear(self->data); |
| 899 | self->hash = -1; |
| 900 | if (iterable == NULL) |
| 901 | return 0; |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 902 | result = set_update(self, iterable); |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 903 | if (result != NULL) { |
| 904 | Py_DECREF(result); |
| 905 | return 0; |
| 906 | } |
| 907 | return -1; |
| 908 | } |
| 909 | |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 910 | static PySequenceMethods set_as_sequence = { |
| 911 | (inquiry)set_len, /* sq_length */ |
| 912 | 0, /* sq_concat */ |
| 913 | 0, /* sq_repeat */ |
| 914 | 0, /* sq_item */ |
| 915 | 0, /* sq_slice */ |
| 916 | 0, /* sq_ass_item */ |
| 917 | 0, /* sq_ass_slice */ |
| 918 | (objobjproc)set_contains, /* sq_contains */ |
| 919 | }; |
| 920 | |
| 921 | /* set object ********************************************************/ |
| 922 | |
| 923 | static PyMethodDef set_methods[] = { |
| 924 | {"add", (PyCFunction)set_add, METH_O, |
| 925 | add_doc}, |
| 926 | {"clear", (PyCFunction)set_clear, METH_NOARGS, |
| 927 | clear_doc}, |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 928 | {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST, |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 929 | contains_doc}, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 930 | {"copy", (PyCFunction)set_copy, METH_NOARGS, |
| 931 | copy_doc}, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 932 | {"discard", (PyCFunction)set_discard, METH_O, |
| 933 | discard_doc}, |
| 934 | {"difference", (PyCFunction)set_difference, METH_O, |
| 935 | difference_doc}, |
| 936 | {"difference_update", (PyCFunction)set_difference_update, METH_O, |
| 937 | difference_update_doc}, |
| 938 | {"intersection",(PyCFunction)set_intersection, METH_O, |
| 939 | intersection_doc}, |
| 940 | {"intersection_update",(PyCFunction)set_intersection_update, METH_O, |
| 941 | intersection_update_doc}, |
| 942 | {"issubset", (PyCFunction)set_issubset, METH_O, |
| 943 | issubset_doc}, |
| 944 | {"issuperset", (PyCFunction)set_issuperset, METH_O, |
| 945 | issuperset_doc}, |
| 946 | {"pop", (PyCFunction)set_pop, METH_NOARGS, |
| 947 | pop_doc}, |
| 948 | {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS, |
| 949 | reduce_doc}, |
| 950 | {"remove", (PyCFunction)set_remove, METH_O, |
| 951 | remove_doc}, |
| 952 | {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O, |
| 953 | symmetric_difference_doc}, |
| 954 | {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O, |
| 955 | symmetric_difference_update_doc}, |
| 956 | {"union", (PyCFunction)set_union, METH_O, |
| 957 | union_doc}, |
Raymond Hettinger | a38123e | 2003-11-24 22:18:49 +0000 | [diff] [blame] | 958 | {"update", (PyCFunction)set_update, METH_O, |
| 959 | update_doc}, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 960 | {NULL, NULL} /* sentinel */ |
| 961 | }; |
| 962 | |
| 963 | static PyNumberMethods set_as_number = { |
| 964 | 0, /*nb_add*/ |
| 965 | (binaryfunc)set_sub, /*nb_subtract*/ |
| 966 | 0, /*nb_multiply*/ |
| 967 | 0, /*nb_divide*/ |
| 968 | 0, /*nb_remainder*/ |
| 969 | 0, /*nb_divmod*/ |
| 970 | 0, /*nb_power*/ |
| 971 | 0, /*nb_negative*/ |
| 972 | 0, /*nb_positive*/ |
| 973 | 0, /*nb_absolute*/ |
| 974 | 0, /*nb_nonzero*/ |
| 975 | 0, /*nb_invert*/ |
| 976 | 0, /*nb_lshift*/ |
| 977 | 0, /*nb_rshift*/ |
| 978 | (binaryfunc)set_and, /*nb_and*/ |
| 979 | (binaryfunc)set_xor, /*nb_xor*/ |
| 980 | (binaryfunc)set_or, /*nb_or*/ |
| 981 | 0, /*nb_coerce*/ |
| 982 | 0, /*nb_int*/ |
| 983 | 0, /*nb_long*/ |
| 984 | 0, /*nb_float*/ |
| 985 | 0, /*nb_oct*/ |
| 986 | 0, /*nb_hex*/ |
| 987 | 0, /*nb_inplace_add*/ |
| 988 | (binaryfunc)set_isub, /*nb_inplace_subtract*/ |
| 989 | 0, /*nb_inplace_multiply*/ |
| 990 | 0, /*nb_inplace_divide*/ |
| 991 | 0, /*nb_inplace_remainder*/ |
| 992 | 0, /*nb_inplace_power*/ |
| 993 | 0, /*nb_inplace_lshift*/ |
| 994 | 0, /*nb_inplace_rshift*/ |
| 995 | (binaryfunc)set_iand, /*nb_inplace_and*/ |
| 996 | (binaryfunc)set_ixor, /*nb_inplace_xor*/ |
| 997 | (binaryfunc)set_ior, /*nb_inplace_or*/ |
| 998 | }; |
| 999 | |
| 1000 | PyDoc_STRVAR(set_doc, |
| 1001 | "set(iterable) --> set object\n\ |
| 1002 | \n\ |
| 1003 | Build an unordered collection."); |
| 1004 | |
| 1005 | PyTypeObject PySet_Type = { |
| 1006 | PyObject_HEAD_INIT(&PyType_Type) |
| 1007 | 0, /* ob_size */ |
| 1008 | "set", /* tp_name */ |
| 1009 | sizeof(PySetObject), /* tp_basicsize */ |
| 1010 | 0, /* tp_itemsize */ |
| 1011 | /* methods */ |
| 1012 | (destructor)set_dealloc, /* tp_dealloc */ |
| 1013 | (printfunc)set_tp_print, /* tp_print */ |
| 1014 | 0, /* tp_getattr */ |
| 1015 | 0, /* tp_setattr */ |
| 1016 | (cmpfunc)set_nocmp, /* tp_compare */ |
| 1017 | (reprfunc)set_repr, /* tp_repr */ |
| 1018 | &set_as_number, /* tp_as_number */ |
| 1019 | &set_as_sequence, /* tp_as_sequence */ |
| 1020 | 0, /* tp_as_mapping */ |
| 1021 | set_nohash, /* tp_hash */ |
| 1022 | 0, /* tp_call */ |
| 1023 | 0, /* tp_str */ |
| 1024 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1025 | 0, /* tp_setattro */ |
| 1026 | 0, /* tp_as_buffer */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 1027 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES | |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 1028 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1029 | set_doc, /* tp_doc */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 1030 | (traverseproc)set_traverse, /* tp_traverse */ |
| 1031 | (inquiry)set_tp_clear, /* tp_clear */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1032 | (richcmpfunc)set_richcompare, /* tp_richcompare */ |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 1033 | offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1034 | (getiterfunc)set_iter, /* tp_iter */ |
| 1035 | 0, /* tp_iternext */ |
| 1036 | set_methods, /* tp_methods */ |
| 1037 | 0, /* tp_members */ |
| 1038 | 0, /* tp_getset */ |
| 1039 | 0, /* tp_base */ |
| 1040 | 0, /* tp_dict */ |
| 1041 | 0, /* tp_descr_get */ |
| 1042 | 0, /* tp_descr_set */ |
| 1043 | 0, /* tp_dictoffset */ |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1044 | (initproc)set_init, /* tp_init */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1045 | PyType_GenericAlloc, /* tp_alloc */ |
| 1046 | set_new, /* tp_new */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 1047 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1048 | }; |
| 1049 | |
| 1050 | /* frozenset object ********************************************************/ |
| 1051 | |
| 1052 | |
| 1053 | static PyMethodDef frozenset_methods[] = { |
Raymond Hettinger | 0deab62 | 2003-12-13 18:53:18 +0000 | [diff] [blame] | 1054 | {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST, |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 1055 | contains_doc}, |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 1056 | {"copy", (PyCFunction)frozenset_copy, METH_NOARGS, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1057 | copy_doc}, |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 1058 | {"difference", (PyCFunction)set_difference, METH_O, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1059 | difference_doc}, |
| 1060 | {"intersection",(PyCFunction)set_intersection, METH_O, |
| 1061 | intersection_doc}, |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 1062 | {"issubset", (PyCFunction)set_issubset, METH_O, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1063 | issubset_doc}, |
Raymond Hettinger | 49ba4c3 | 2003-11-23 02:49:05 +0000 | [diff] [blame] | 1064 | {"issuperset", (PyCFunction)set_issuperset, METH_O, |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1065 | issuperset_doc}, |
| 1066 | {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS, |
| 1067 | reduce_doc}, |
| 1068 | {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O, |
| 1069 | symmetric_difference_doc}, |
| 1070 | {"union", (PyCFunction)set_union, METH_O, |
| 1071 | union_doc}, |
| 1072 | {NULL, NULL} /* sentinel */ |
| 1073 | }; |
| 1074 | |
| 1075 | static PyNumberMethods frozenset_as_number = { |
| 1076 | 0, /*nb_add*/ |
| 1077 | (binaryfunc)set_sub, /*nb_subtract*/ |
| 1078 | 0, /*nb_multiply*/ |
| 1079 | 0, /*nb_divide*/ |
| 1080 | 0, /*nb_remainder*/ |
| 1081 | 0, /*nb_divmod*/ |
| 1082 | 0, /*nb_power*/ |
| 1083 | 0, /*nb_negative*/ |
| 1084 | 0, /*nb_positive*/ |
| 1085 | 0, /*nb_absolute*/ |
| 1086 | 0, /*nb_nonzero*/ |
| 1087 | 0, /*nb_invert*/ |
| 1088 | 0, /*nb_lshift*/ |
| 1089 | 0, /*nb_rshift*/ |
| 1090 | (binaryfunc)set_and, /*nb_and*/ |
| 1091 | (binaryfunc)set_xor, /*nb_xor*/ |
| 1092 | (binaryfunc)set_or, /*nb_or*/ |
| 1093 | }; |
| 1094 | |
| 1095 | PyDoc_STRVAR(frozenset_doc, |
| 1096 | "frozenset(iterable) --> frozenset object\n\ |
| 1097 | \n\ |
| 1098 | Build an immutable unordered collection."); |
| 1099 | |
| 1100 | PyTypeObject PyFrozenSet_Type = { |
| 1101 | PyObject_HEAD_INIT(&PyType_Type) |
| 1102 | 0, /* ob_size */ |
| 1103 | "frozenset", /* tp_name */ |
| 1104 | sizeof(PySetObject), /* tp_basicsize */ |
Raymond Hettinger | a3b11e7 | 2003-12-31 14:08:58 +0000 | [diff] [blame] | 1105 | 0, /* tp_itemsize */ |
| 1106 | /* methods */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1107 | (destructor)set_dealloc, /* tp_dealloc */ |
| 1108 | (printfunc)set_tp_print, /* tp_print */ |
| 1109 | 0, /* tp_getattr */ |
| 1110 | 0, /* tp_setattr */ |
| 1111 | (cmpfunc)set_nocmp, /* tp_compare */ |
| 1112 | (reprfunc)set_repr, /* tp_repr */ |
| 1113 | &frozenset_as_number, /* tp_as_number */ |
| 1114 | &set_as_sequence, /* tp_as_sequence */ |
| 1115 | 0, /* tp_as_mapping */ |
| 1116 | frozenset_hash, /* tp_hash */ |
| 1117 | 0, /* tp_call */ |
| 1118 | 0, /* tp_str */ |
| 1119 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1120 | 0, /* tp_setattro */ |
| 1121 | 0, /* tp_as_buffer */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 1122 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES | |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 1123 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1124 | frozenset_doc, /* tp_doc */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 1125 | (traverseproc)set_traverse, /* tp_traverse */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1126 | 0, /* tp_clear */ |
| 1127 | (richcmpfunc)set_richcompare, /* tp_richcompare */ |
Raymond Hettinger | 691d805 | 2004-05-30 07:26:47 +0000 | [diff] [blame] | 1128 | offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1129 | (getiterfunc)set_iter, /* tp_iter */ |
| 1130 | 0, /* tp_iternext */ |
| 1131 | frozenset_methods, /* tp_methods */ |
| 1132 | 0, /* tp_members */ |
| 1133 | 0, /* tp_getset */ |
| 1134 | 0, /* tp_base */ |
| 1135 | 0, /* tp_dict */ |
| 1136 | 0, /* tp_descr_get */ |
| 1137 | 0, /* tp_descr_set */ |
| 1138 | 0, /* tp_dictoffset */ |
| 1139 | 0, /* tp_init */ |
| 1140 | PyType_GenericAlloc, /* tp_alloc */ |
Raymond Hettinger | 50a4bb3 | 2003-11-17 16:42:33 +0000 | [diff] [blame] | 1141 | frozenset_new, /* tp_new */ |
Raymond Hettinger | bb999b5 | 2005-06-18 21:00:26 +0000 | [diff] [blame] | 1142 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1143 | }; |