blob: 9aedc2f162b1a470ad87f7b3ecf7599fe5b80c5b [file] [log] [blame]
Raymond Hettingera690a992003-11-16 16:17:49 +00001#include "Python.h"
2
3/* set object implementation
4 written and maintained by Raymond D. Hettinger <python@rcn.com>
5 derived from sets.py written by Greg V. Wilson, Alex Martelli,
6 Guido van Rossum, Raymond Hettinger, and Tim Peters.
7
8 Copyright (c) 2003 Python Software Foundation.
9 All rights reserved.
10*/
11
Raymond Hettingera690a992003-11-16 16:17:49 +000012static PyObject *
Raymond Hettingera38123e2003-11-24 22:18:49 +000013set_update(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +000014{
Raymond Hettingera38123e2003-11-24 22:18:49 +000015 PyObject *item, *data, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +000016
Raymond Hettingera38123e2003-11-24 22:18:49 +000017 if (PyAnySet_Check(other)) {
Raymond Hettinger81ad32e2003-12-15 21:16:06 +000018 if (PyDict_Merge(so->data, ((PySetObject *)other)->data, 1) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +000019 return NULL;
20 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +000021 }
22
Raymond Hettingera38123e2003-11-24 22:18:49 +000023 it = PyObject_GetIter(other);
24 if (it == NULL)
25 return NULL;
26 data = so->data;
Raymond Hettingera690a992003-11-16 16:17:49 +000027
Raymond Hettingera38123e2003-11-24 22:18:49 +000028 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingera690a992003-11-16 16:17:49 +000029 if (PyDict_SetItem(data, item, Py_True) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +000030 Py_DECREF(it);
Raymond Hettingera690a992003-11-16 16:17:49 +000031 Py_DECREF(item);
Raymond Hettingera38123e2003-11-24 22:18:49 +000032 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +000033 }
34 Py_DECREF(item);
35 }
Raymond Hettingera38123e2003-11-24 22:18:49 +000036 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +000037 if (PyErr_Occurred())
Raymond Hettingera38123e2003-11-24 22:18:49 +000038 return NULL;
39 Py_RETURN_NONE;
40}
41
42PyDoc_STRVAR(update_doc,
43"Update a set with the union of itself and another.");
44
45static PyObject *
46make_new_set(PyTypeObject *type, PyObject *iterable)
47{
48 PyObject *data = NULL;
49 PyObject *tmp;
50 PySetObject *so = NULL;
51
52 data = PyDict_New();
53 if (data == NULL)
54 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +000055
56 /* create PySetObject structure */
57 so = (PySetObject *)type->tp_alloc(type, 0);
Raymond Hettingera38123e2003-11-24 22:18:49 +000058 if (so == NULL) {
59 Py_DECREF(data);
60 return NULL;
61 }
Raymond Hettingera690a992003-11-16 16:17:49 +000062 so->data = data;
63 so->hash = -1;
64
Raymond Hettingera38123e2003-11-24 22:18:49 +000065 if (iterable != NULL) {
66 tmp = set_update(so, iterable);
67 if (tmp == NULL) {
68 Py_DECREF(so);
69 return NULL;
70 }
71 Py_DECREF(tmp);
72 }
73
Raymond Hettingera690a992003-11-16 16:17:49 +000074 return (PyObject *)so;
75}
76
77static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +000078frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +000079{
80 PyObject *iterable = NULL;
81
82 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
83 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +000084 if (iterable != NULL && PyFrozenSet_CheckExact(iterable)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +000085 Py_INCREF(iterable);
86 return iterable;
87 }
Raymond Hettingera690a992003-11-16 16:17:49 +000088 return make_new_set(type, iterable);
89}
90
Raymond Hettinger50a4bb32003-11-17 16:42:33 +000091static PyObject *
92set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
93{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +000094 return make_new_set(type, NULL);
95}
96
Raymond Hettingerbfd334a2003-11-22 03:55:23 +000097static PyObject *
98frozenset_dict_wrapper(PyObject *d)
99{
100 PySetObject *w;
101
102 assert(PyDict_Check(d));
103 w = (PySetObject *)make_new_set(&PyFrozenSet_Type, NULL);
104 if (w == NULL)
105 return NULL;
106 Py_DECREF(w->data);
107 Py_INCREF(d);
108 w->data = d;
109 return (PyObject *)w;
110}
111
Raymond Hettingera690a992003-11-16 16:17:49 +0000112static void
113set_dealloc(PySetObject *so)
114{
115 PyObject_GC_UnTrack(so);
116 Py_XDECREF(so->data);
117 so->ob_type->tp_free(so);
118}
119
120static int
121set_traverse(PySetObject *so, visitproc visit, void *arg)
122{
123 if (so->data)
124 return visit(so->data, arg);
125 return 0;
126}
127
128static PyObject *
129set_iter(PySetObject *so)
130{
131 return PyObject_GetIter(so->data);
132}
133
134static int
135set_len(PySetObject *so)
136{
137 return PyDict_Size(so->data);
138}
139
140static int
141set_contains(PySetObject *so, PyObject *key)
142{
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000143 PyObject *tmp;
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000144 int result;
145
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +0000146 result = PyDict_Contains(so->data, key);
Raymond Hettingera38123e2003-11-24 22:18:49 +0000147 if (result == -1 && PyAnySet_Check(key)) {
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000148 PyErr_Clear();
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000149 tmp = frozenset_dict_wrapper(((PySetObject *)(key))->data);
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000150 if (tmp == NULL)
151 return -1;
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +0000152 result = PyDict_Contains(so->data, tmp);
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000153 Py_DECREF(tmp);
154 }
155 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000156}
157
158static PyObject *
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000159set_direct_contains(PySetObject *so, PyObject *key)
160{
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000161 long result;
162
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000163 result = set_contains(so, key);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000164 if (result == -1)
165 return NULL;
166 return PyBool_FromLong(result);
167}
168
169PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
170
171static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000172set_copy(PySetObject *so)
173{
Raymond Hettingera38123e2003-11-24 22:18:49 +0000174 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +0000175}
176
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000177static PyObject *
178frozenset_copy(PySetObject *so)
179{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000180 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000181 Py_INCREF(so);
182 return (PyObject *)so;
183 }
184 return set_copy(so);
185}
186
Raymond Hettingera690a992003-11-16 16:17:49 +0000187PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
188
189static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000190set_union(PySetObject *so, PyObject *other)
191{
192 PySetObject *result;
193 PyObject *rv;
194
195 result = (PySetObject *)set_copy(so);
196 if (result == NULL)
197 return NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000198 rv = set_update(result, other);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000199 if (rv == NULL) {
200 Py_DECREF(result);
201 return NULL;
202 }
203 Py_DECREF(rv);
204 return (PyObject *)result;
205}
206
207PyDoc_STRVAR(union_doc,
208 "Return the union of two sets as a new set.\n\
209\n\
210(i.e. all elements that are in either set.)");
211
212static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000213set_or(PySetObject *so, PyObject *other)
214{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000215 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000216 Py_INCREF(Py_NotImplemented);
217 return Py_NotImplemented;
218 }
219 return set_union(so, other);
220}
221
222static PyObject *
223set_ior(PySetObject *so, PyObject *other)
224{
225 PyObject *result;
226
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000227 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000228 Py_INCREF(Py_NotImplemented);
229 return Py_NotImplemented;
230 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000231 result = set_update(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000232 if (result == NULL)
233 return NULL;
234 Py_DECREF(result);
235 Py_INCREF(so);
236 return (PyObject *)so;
237}
238
239static PyObject *
240set_intersection(PySetObject *so, PyObject *other)
241{
242 PySetObject *result;
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000243 PyObject *item, *selfdata, *tgtdata, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +0000244
245 result = (PySetObject *)make_new_set(so->ob_type, NULL);
246 if (result == NULL)
247 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000248 tgtdata = result->data;
249 selfdata = so->data;
250
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000251 if (PyAnySet_Check(other))
252 other = ((PySetObject *)other)->data;
253
254 if (PyDict_Check(other) && PyDict_Size(other) > PyDict_Size(selfdata)) {
255 tmp = selfdata;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000256 selfdata = other;
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000257 other = tmp;
258 }
259
260 if (PyDict_CheckExact(other)) {
261 PyObject *value;
262 int pos = 0;
263 while (PyDict_Next(other, &pos, &item, &value)) {
264 if (PyDict_Contains(selfdata, item)) {
265 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
266 Py_DECREF(result);
267 return NULL;
268 }
269 }
270 }
271 return (PyObject *)result;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000272 }
273
Raymond Hettingera690a992003-11-16 16:17:49 +0000274 it = PyObject_GetIter(other);
275 if (it == NULL) {
276 Py_DECREF(result);
277 return NULL;
278 }
279
Raymond Hettingera690a992003-11-16 16:17:49 +0000280 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +0000281 if (PyDict_Contains(selfdata, item)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000282 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
283 Py_DECREF(it);
284 Py_DECREF(result);
285 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000286 return NULL;
287 }
288 }
289 Py_DECREF(item);
290 }
291 Py_DECREF(it);
292 if (PyErr_Occurred()) {
293 Py_DECREF(result);
294 return NULL;
295 }
296 return (PyObject *)result;
297}
298
299PyDoc_STRVAR(intersection_doc,
300"Return the intersection of two sets as a new set.\n\
301\n\
302(i.e. all elements that are in both sets.)");
303
304static PyObject *
305set_intersection_update(PySetObject *so, PyObject *other)
306{
307 PyObject *item, *selfdata, *it, *newdict, *tmp;
308
309 newdict = PyDict_New();
310 if (newdict == NULL)
311 return newdict;
312
313 it = PyObject_GetIter(other);
314 if (it == NULL) {
315 Py_DECREF(newdict);
316 return NULL;
317 }
318
319 selfdata = so->data;
320 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +0000321 if (PyDict_Contains(selfdata, item)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000322 if (PyDict_SetItem(newdict, item, Py_True) == -1) {
323 Py_DECREF(newdict);
324 Py_DECREF(it);
325 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000326 return NULL;
327 }
328 }
329 Py_DECREF(item);
330 }
331 Py_DECREF(it);
332 if (PyErr_Occurred()) {
333 Py_DECREF(newdict);
334 return NULL;
335 }
336 tmp = so->data;
337 so->data = newdict;
338 Py_DECREF(tmp);
339 Py_RETURN_NONE;
340}
341
342PyDoc_STRVAR(intersection_update_doc,
343"Update a set with the intersection of itself and another.");
344
345static PyObject *
346set_and(PySetObject *so, PyObject *other)
347{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000348 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000349 Py_INCREF(Py_NotImplemented);
350 return Py_NotImplemented;
351 }
352 return set_intersection(so, other);
353}
354
355static PyObject *
356set_iand(PySetObject *so, PyObject *other)
357{
358 PyObject *result;
359
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000360 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000361 Py_INCREF(Py_NotImplemented);
362 return Py_NotImplemented;
363 }
364 result = set_intersection_update(so, other);
365 if (result == NULL)
366 return NULL;
367 Py_DECREF(result);
368 Py_INCREF(so);
369 return (PyObject *)so;
370}
371
372static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000373set_difference_update(PySetObject *so, PyObject *other)
374{
375 PyObject *item, *tgtdata, *it;
376
377 it = PyObject_GetIter(other);
378 if (it == NULL)
379 return NULL;
380
381 tgtdata = so->data;
382 while ((item = PyIter_Next(it)) != NULL) {
383 if (PyDict_DelItem(tgtdata, item) == -1) {
384 if (PyErr_ExceptionMatches(PyExc_KeyError))
385 PyErr_Clear();
386 else {
387 Py_DECREF(it);
388 Py_DECREF(item);
389 return NULL;
390 }
391 }
392 Py_DECREF(item);
393 }
394 Py_DECREF(it);
395 if (PyErr_Occurred())
396 return NULL;
397 Py_RETURN_NONE;
398}
399
400PyDoc_STRVAR(difference_update_doc,
401"Remove all elements of another set from this set.");
402
403static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +0000404set_difference(PySetObject *so, PyObject *other)
405{
406 PyObject *result, *tmp;
407 PyObject *otherdata, *tgtdata;
408 PyObject *key, *value;
409 int pos = 0;
410
411 if (PyDict_Check(other))
412 otherdata = other;
413 else if (PyAnySet_Check(other))
414 otherdata = ((PySetObject *)other)->data;
415 else {
416 result = set_copy(so);
417 if (result == NULL)
418 return result;
419 tmp = set_difference_update((PySetObject *)result, other);
420 if (tmp != NULL) {
421 Py_DECREF(tmp);
422 return result;
423 }
424 Py_DECREF(result);
425 return NULL;
426 }
427
428 result = make_new_set(so->ob_type, NULL);
429 if (result == NULL)
430 return NULL;
431 tgtdata = ((PySetObject *)result)->data;
432
433 while (PyDict_Next(so->data, &pos, &key, &value)) {
434 if (!PyDict_Contains(otherdata, key)) {
435 if (PyDict_SetItem(tgtdata, key, Py_True) == -1)
436 return NULL;
437 }
438 }
439 return result;
440}
441
442PyDoc_STRVAR(difference_doc,
443"Return the difference of two sets as a new set.\n\
444\n\
445(i.e. all elements that are in this set but not the other.)");
446static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000447set_sub(PySetObject *so, PyObject *other)
448{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000449 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000450 Py_INCREF(Py_NotImplemented);
451 return Py_NotImplemented;
452 }
453 return set_difference(so, other);
454}
455
456static PyObject *
457set_isub(PySetObject *so, PyObject *other)
458{
459 PyObject *result;
460
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000461 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000462 Py_INCREF(Py_NotImplemented);
463 return Py_NotImplemented;
464 }
465 result = set_difference_update(so, other);
466 if (result == NULL)
467 return NULL;
468 Py_DECREF(result);
469 Py_INCREF(so);
470 return (PyObject *)so;
471}
472
473static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000474set_symmetric_difference_update(PySetObject *so, PyObject *other)
475{
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000476 PyObject *selfdata, *otherdata;
Raymond Hettingera690a992003-11-16 16:17:49 +0000477 PySetObject *otherset = NULL;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000478 PyObject *key, *value;
479 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +0000480
481 selfdata = so->data;
Raymond Hettingera690a992003-11-16 16:17:49 +0000482 if (PyDict_Check(other))
483 otherdata = other;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000484 else if (PyAnySet_Check(other))
Raymond Hettingera690a992003-11-16 16:17:49 +0000485 otherdata = ((PySetObject *)other)->data;
486 else {
487 otherset = (PySetObject *)make_new_set(so->ob_type, other);
488 if (otherset == NULL)
489 return NULL;
490 otherdata = otherset->data;
491 }
492
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000493 while (PyDict_Next(otherdata, &pos, &key, &value)) {
494 if (PyDict_Contains(selfdata, key)) {
495 if (PyDict_DelItem(selfdata, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000496 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000497 return NULL;
498 }
499 } else {
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000500 if (PyDict_SetItem(selfdata, key, Py_True) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000501 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000502 return NULL;
503 }
504 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000505 }
506 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000507 Py_RETURN_NONE;
508}
509
510PyDoc_STRVAR(symmetric_difference_update_doc,
511"Update a set with the symmetric difference of itself and another.");
512
513static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000514set_symmetric_difference(PySetObject *so, PyObject *other)
515{
516 PySetObject *result;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000517 PyObject *selfdata, *otherdata, *tgtdata, *rv, *otherset;
518 PyObject *key, *value;
519 int pos = 0;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000520
521 if (PyDict_Check(other))
522 otherdata = other;
523 else if (PyAnySet_Check(other))
524 otherdata = ((PySetObject *)other)->data;
525 else {
526 otherset = make_new_set(so->ob_type, other);
527 if (otherset == NULL)
528 return NULL;
529 rv = set_symmetric_difference_update((PySetObject *)otherset, (PyObject *)so);
530 if (rv == NULL)
531 return NULL;
532 Py_DECREF(rv);
533 return otherset;
534 }
535
536 result = (PySetObject *)make_new_set(so->ob_type, NULL);
537 if (result == NULL)
538 return NULL;
539 tgtdata = result->data;
540 selfdata = so->data;
541
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000542 while (PyDict_Next(otherdata, &pos, &key, &value)) {
543 if (!PyDict_Contains(selfdata, key)) {
544 if (PyDict_SetItem(tgtdata, key, Py_True) == -1) {
545 Py_DECREF(result);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000546 return NULL;
547 }
548 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000549 }
550
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000551 pos = 0;
552 while (PyDict_Next(selfdata, &pos, &key, &value)) {
553 if (!PyDict_Contains(otherdata, key)) {
554 if (PyDict_SetItem(tgtdata, key, Py_True) == -1) {
555 Py_DECREF(result);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000556 return NULL;
557 }
558 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000559 }
560
561 return (PyObject *)result;
562}
563
564PyDoc_STRVAR(symmetric_difference_doc,
565"Return the symmetric difference of two sets as a new set.\n\
566\n\
567(i.e. all elements that are in exactly one of the sets.)");
568
569static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000570set_xor(PySetObject *so, PyObject *other)
571{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000572 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000573 Py_INCREF(Py_NotImplemented);
574 return Py_NotImplemented;
575 }
576 return set_symmetric_difference(so, other);
577}
578
579static PyObject *
580set_ixor(PySetObject *so, PyObject *other)
581{
582 PyObject *result;
583
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000584 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000585 Py_INCREF(Py_NotImplemented);
586 return Py_NotImplemented;
587 }
588 result = set_symmetric_difference_update(so, other);
589 if (result == NULL)
590 return NULL;
591 Py_DECREF(result);
592 Py_INCREF(so);
593 return (PyObject *)so;
594}
595
596static PyObject *
597set_issubset(PySetObject *so, PyObject *other)
598{
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000599 PyObject *otherdata, *tmp, *result;
600 PyObject *key, *value;
601 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +0000602
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000603 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000604 tmp = make_new_set(&PySet_Type, other);
605 if (tmp == NULL)
606 return NULL;
607 result = set_issubset(so, tmp);
608 Py_DECREF(tmp);
609 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000610 }
611 if (set_len(so) > set_len((PySetObject *)other))
612 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000613
614 otherdata = ((PySetObject *)other)->data;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000615 while (PyDict_Next(((PySetObject *)so)->data, &pos, &key, &value)) {
616 if (!PyDict_Contains(otherdata, key))
Raymond Hettingera690a992003-11-16 16:17:49 +0000617 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000618 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000619 Py_RETURN_TRUE;
620}
621
622PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
623
624static PyObject *
625set_issuperset(PySetObject *so, PyObject *other)
626{
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000627 PyObject *tmp, *result;
628
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000629 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000630 tmp = make_new_set(&PySet_Type, other);
631 if (tmp == NULL)
632 return NULL;
633 result = set_issuperset(so, tmp);
634 Py_DECREF(tmp);
635 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000636 }
637 return set_issubset((PySetObject *)other, (PyObject *)so);
638}
639
640PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
641
642static long
643set_nohash(PyObject *self)
644{
645 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
646 return -1;
647}
648
649static int
650set_nocmp(PyObject *self)
651{
652 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
653 return -1;
654}
655
656static long
657frozenset_hash(PyObject *self)
658{
Raymond Hettingera690a992003-11-16 16:17:49 +0000659 PySetObject *so = (PySetObject *)self;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000660 PyObject *key, *value;
661 int pos = 0;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000662 long hash = 0;
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000663
Raymond Hettingera690a992003-11-16 16:17:49 +0000664 if (so->hash != -1)
665 return so->hash;
Raymond Hettingera690a992003-11-16 16:17:49 +0000666
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000667 while (PyDict_Next(so->data, &pos, &key, &value)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000668 /* Multiplying by a large prime increases the bit dispersion for
669 closely spaced hash values. The is important because some
670 use cases have many combinations of a small number of
671 elements with nearby hashes so that many distinct combinations
672 collapse to only a handful of distinct hash values. */
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000673 hash ^= PyObject_Hash(key) * 3644798167u;
Raymond Hettingera690a992003-11-16 16:17:49 +0000674 }
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000675 so->hash = hash;
Raymond Hettingera690a992003-11-16 16:17:49 +0000676 return hash;
677}
678
679static PyObject *
680set_richcompare(PySetObject *v, PyObject *w, int op)
681{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000682 if(!PyAnySet_Check(w)) {
683 if (op == Py_EQ)
684 Py_RETURN_FALSE;
685 if (op == Py_NE)
686 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000687 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
688 return NULL;
689 }
690 switch (op) {
691 case Py_EQ:
692 case Py_NE:
693 return PyObject_RichCompare(((PySetObject *)v)->data,
694 ((PySetObject *)w)->data, op);
695 case Py_LE:
696 return set_issubset((PySetObject *)v, w);
697 case Py_GE:
698 return set_issuperset((PySetObject *)v, w);
699 case Py_LT:
700 if (set_len(v) >= set_len((PySetObject *)w))
701 Py_RETURN_FALSE;
702 return set_issubset((PySetObject *)v, w);
703 case Py_GT:
704 if (set_len(v) <= set_len((PySetObject *)w))
705 Py_RETURN_FALSE;
706 return set_issuperset((PySetObject *)v, w);
707 }
708 Py_INCREF(Py_NotImplemented);
709 return Py_NotImplemented;
710}
711
712static PyObject *
713set_repr(PySetObject *so)
714{
715 PyObject *keys, *result, *listrepr;
716
717 keys = PyDict_Keys(so->data);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000718 if (keys == NULL)
719 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000720 listrepr = PyObject_Repr(keys);
721 Py_DECREF(keys);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000722 if (listrepr == NULL)
723 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000724
725 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
726 PyString_AS_STRING(listrepr));
727 Py_DECREF(listrepr);
728 return result;
729}
730
731static int
732set_tp_print(PySetObject *so, FILE *fp, int flags)
733{
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000734 PyObject *key, *value;
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000735 int pos=0;
736 char *emit = ""; /* No separator emitted on first pass */
737 char *separator = ", ";
Raymond Hettingera690a992003-11-16 16:17:49 +0000738
Raymond Hettingera690a992003-11-16 16:17:49 +0000739 fprintf(fp, "%s([", so->ob_type->tp_name);
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000740 while (PyDict_Next(so->data, &pos, &key, &value)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000741 fputs(emit, fp);
742 emit = separator;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000743 if (PyObject_Print(key, fp, 0) != 0)
Raymond Hettingera690a992003-11-16 16:17:49 +0000744 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000745 }
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000746 fputs("])", fp);
Raymond Hettingera690a992003-11-16 16:17:49 +0000747 return 0;
748}
749
750static PyObject *
751set_clear(PySetObject *so)
752{
753 PyDict_Clear(so->data);
754 so->hash = -1;
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000755 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000756}
757
758PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
759
760static int
761set_tp_clear(PySetObject *so)
762{
763 PyDict_Clear(so->data);
764 so->hash = -1;
765 return 0;
766}
767
768static PyObject *
769set_add(PySetObject *so, PyObject *item)
770{
771 if (PyDict_SetItem(so->data, item, Py_True) == -1)
772 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000773 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000774}
775
776PyDoc_STRVAR(add_doc,
777"Add an element to a set.\n\
778\n\
779This has no effect if the element is already present.");
780
781static PyObject *
782set_remove(PySetObject *so, PyObject *item)
783{
Raymond Hettinger0deab622003-12-13 18:53:18 +0000784 PyObject *tmp, *result;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000785
Raymond Hettinger0deab622003-12-13 18:53:18 +0000786 if (PyType_IsSubtype(item->ob_type, &PySet_Type)) {
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000787 tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
788 if (tmp == NULL)
789 return NULL;
Raymond Hettinger0deab622003-12-13 18:53:18 +0000790 result = set_remove(so, tmp);
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000791 Py_DECREF(tmp);
Raymond Hettinger0deab622003-12-13 18:53:18 +0000792 return result;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000793 }
Raymond Hettinger0deab622003-12-13 18:53:18 +0000794
795 if (PyDict_DelItem(so->data, item) == -1)
796 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000797 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000798}
799
800PyDoc_STRVAR(remove_doc,
801"Remove an element from a set; it must be a member.\n\
802\n\
803If the element is not a member, raise a KeyError.");
804
805static PyObject *
806set_discard(PySetObject *so, PyObject *item)
807{
Raymond Hettinger0deab622003-12-13 18:53:18 +0000808 PyObject *tmp, *result;
809
810 if (PyType_IsSubtype(item->ob_type, &PySet_Type)) {
811 tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
812 if (tmp == NULL)
813 return NULL;
814 result = set_discard(so, tmp);
815 Py_DECREF(tmp);
816 return result;
817 }
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000818
Guido van Rossumb61982b2003-11-18 19:27:19 +0000819 if (PyDict_DelItem(so->data, item) == -1) {
Raymond Hettinger0deab622003-12-13 18:53:18 +0000820 if (!PyErr_ExceptionMatches(PyExc_KeyError))
821 return NULL;
822 PyErr_Clear();
Guido van Rossumb61982b2003-11-18 19:27:19 +0000823 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000824 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000825}
826
827PyDoc_STRVAR(discard_doc,
828"Remove an element from a set if it is a member.\n\
829\n\
830If the element is not a member, do nothing.");
831
832static PyObject *
833set_pop(PySetObject *so)
834{
835 PyObject *key, *value;
836 int pos = 0;
837
838 if (!PyDict_Next(so->data, &pos, &key, &value)) {
839 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
840 return NULL;
841 }
842 Py_INCREF(key);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000843 if (PyDict_DelItem(so->data, key) == -1) {
844 Py_DECREF(key);
845 return NULL;
846 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000847 return key;
848}
849
850PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
851
852static PyObject *
853set_reduce(PySetObject *so)
854{
855 PyObject *keys=NULL, *args=NULL, *result=NULL;
856
857 keys = PyDict_Keys(so->data);
858 if (keys == NULL)
859 goto done;
860 args = PyTuple_Pack(1, keys);
861 if (args == NULL)
862 goto done;
863 result = PyTuple_Pack(2, so->ob_type, args);
864done:
865 Py_XDECREF(args);
866 Py_XDECREF(keys);
867 return result;
868}
869
870PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
871
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000872static int
873set_init(PySetObject *self, PyObject *args, PyObject *kwds)
874{
875 PyObject *iterable = NULL;
876 PyObject *result;
877
878 if (!PyAnySet_Check(self))
879 return -1;
880 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
881 return -1;
882 PyDict_Clear(self->data);
883 self->hash = -1;
884 if (iterable == NULL)
885 return 0;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000886 result = set_update(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000887 if (result != NULL) {
888 Py_DECREF(result);
889 return 0;
890 }
891 return -1;
892}
893
Raymond Hettingera690a992003-11-16 16:17:49 +0000894static PySequenceMethods set_as_sequence = {
895 (inquiry)set_len, /* sq_length */
896 0, /* sq_concat */
897 0, /* sq_repeat */
898 0, /* sq_item */
899 0, /* sq_slice */
900 0, /* sq_ass_item */
901 0, /* sq_ass_slice */
902 (objobjproc)set_contains, /* sq_contains */
903};
904
905/* set object ********************************************************/
906
907static PyMethodDef set_methods[] = {
908 {"add", (PyCFunction)set_add, METH_O,
909 add_doc},
910 {"clear", (PyCFunction)set_clear, METH_NOARGS,
911 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +0000912 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000913 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +0000914 {"copy", (PyCFunction)set_copy, METH_NOARGS,
915 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +0000916 {"discard", (PyCFunction)set_discard, METH_O,
917 discard_doc},
918 {"difference", (PyCFunction)set_difference, METH_O,
919 difference_doc},
920 {"difference_update", (PyCFunction)set_difference_update, METH_O,
921 difference_update_doc},
922 {"intersection",(PyCFunction)set_intersection, METH_O,
923 intersection_doc},
924 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
925 intersection_update_doc},
926 {"issubset", (PyCFunction)set_issubset, METH_O,
927 issubset_doc},
928 {"issuperset", (PyCFunction)set_issuperset, METH_O,
929 issuperset_doc},
930 {"pop", (PyCFunction)set_pop, METH_NOARGS,
931 pop_doc},
932 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
933 reduce_doc},
934 {"remove", (PyCFunction)set_remove, METH_O,
935 remove_doc},
936 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
937 symmetric_difference_doc},
938 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
939 symmetric_difference_update_doc},
940 {"union", (PyCFunction)set_union, METH_O,
941 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +0000942 {"update", (PyCFunction)set_update, METH_O,
943 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +0000944 {NULL, NULL} /* sentinel */
945};
946
947static PyNumberMethods set_as_number = {
948 0, /*nb_add*/
949 (binaryfunc)set_sub, /*nb_subtract*/
950 0, /*nb_multiply*/
951 0, /*nb_divide*/
952 0, /*nb_remainder*/
953 0, /*nb_divmod*/
954 0, /*nb_power*/
955 0, /*nb_negative*/
956 0, /*nb_positive*/
957 0, /*nb_absolute*/
958 0, /*nb_nonzero*/
959 0, /*nb_invert*/
960 0, /*nb_lshift*/
961 0, /*nb_rshift*/
962 (binaryfunc)set_and, /*nb_and*/
963 (binaryfunc)set_xor, /*nb_xor*/
964 (binaryfunc)set_or, /*nb_or*/
965 0, /*nb_coerce*/
966 0, /*nb_int*/
967 0, /*nb_long*/
968 0, /*nb_float*/
969 0, /*nb_oct*/
970 0, /*nb_hex*/
971 0, /*nb_inplace_add*/
972 (binaryfunc)set_isub, /*nb_inplace_subtract*/
973 0, /*nb_inplace_multiply*/
974 0, /*nb_inplace_divide*/
975 0, /*nb_inplace_remainder*/
976 0, /*nb_inplace_power*/
977 0, /*nb_inplace_lshift*/
978 0, /*nb_inplace_rshift*/
979 (binaryfunc)set_iand, /*nb_inplace_and*/
980 (binaryfunc)set_ixor, /*nb_inplace_xor*/
981 (binaryfunc)set_ior, /*nb_inplace_or*/
982};
983
984PyDoc_STRVAR(set_doc,
985"set(iterable) --> set object\n\
986\n\
987Build an unordered collection.");
988
989PyTypeObject PySet_Type = {
990 PyObject_HEAD_INIT(&PyType_Type)
991 0, /* ob_size */
992 "set", /* tp_name */
993 sizeof(PySetObject), /* tp_basicsize */
994 0, /* tp_itemsize */
995 /* methods */
996 (destructor)set_dealloc, /* tp_dealloc */
997 (printfunc)set_tp_print, /* tp_print */
998 0, /* tp_getattr */
999 0, /* tp_setattr */
1000 (cmpfunc)set_nocmp, /* tp_compare */
1001 (reprfunc)set_repr, /* tp_repr */
1002 &set_as_number, /* tp_as_number */
1003 &set_as_sequence, /* tp_as_sequence */
1004 0, /* tp_as_mapping */
1005 set_nohash, /* tp_hash */
1006 0, /* tp_call */
1007 0, /* tp_str */
1008 PyObject_GenericGetAttr, /* tp_getattro */
1009 0, /* tp_setattro */
1010 0, /* tp_as_buffer */
1011 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
1012 Py_TPFLAGS_BASETYPE, /* tp_flags */
1013 set_doc, /* tp_doc */
1014 (traverseproc)set_traverse, /* tp_traverse */
1015 (inquiry)set_tp_clear, /* tp_clear */
1016 (richcmpfunc)set_richcompare, /* tp_richcompare */
1017 0, /* tp_weaklistoffset */
1018 (getiterfunc)set_iter, /* tp_iter */
1019 0, /* tp_iternext */
1020 set_methods, /* tp_methods */
1021 0, /* tp_members */
1022 0, /* tp_getset */
1023 0, /* tp_base */
1024 0, /* tp_dict */
1025 0, /* tp_descr_get */
1026 0, /* tp_descr_set */
1027 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001028 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001029 PyType_GenericAlloc, /* tp_alloc */
1030 set_new, /* tp_new */
1031 PyObject_GC_Del, /* tp_free */
1032};
1033
1034/* frozenset object ********************************************************/
1035
1036
1037static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001038 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001039 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001040 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001041 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001042 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001043 difference_doc},
1044 {"intersection",(PyCFunction)set_intersection, METH_O,
1045 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001046 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001047 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001048 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001049 issuperset_doc},
1050 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1051 reduce_doc},
1052 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1053 symmetric_difference_doc},
1054 {"union", (PyCFunction)set_union, METH_O,
1055 union_doc},
1056 {NULL, NULL} /* sentinel */
1057};
1058
1059static PyNumberMethods frozenset_as_number = {
1060 0, /*nb_add*/
1061 (binaryfunc)set_sub, /*nb_subtract*/
1062 0, /*nb_multiply*/
1063 0, /*nb_divide*/
1064 0, /*nb_remainder*/
1065 0, /*nb_divmod*/
1066 0, /*nb_power*/
1067 0, /*nb_negative*/
1068 0, /*nb_positive*/
1069 0, /*nb_absolute*/
1070 0, /*nb_nonzero*/
1071 0, /*nb_invert*/
1072 0, /*nb_lshift*/
1073 0, /*nb_rshift*/
1074 (binaryfunc)set_and, /*nb_and*/
1075 (binaryfunc)set_xor, /*nb_xor*/
1076 (binaryfunc)set_or, /*nb_or*/
1077};
1078
1079PyDoc_STRVAR(frozenset_doc,
1080"frozenset(iterable) --> frozenset object\n\
1081\n\
1082Build an immutable unordered collection.");
1083
1084PyTypeObject PyFrozenSet_Type = {
1085 PyObject_HEAD_INIT(&PyType_Type)
1086 0, /* ob_size */
1087 "frozenset", /* tp_name */
1088 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001089 0, /* tp_itemsize */
1090 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001091 (destructor)set_dealloc, /* tp_dealloc */
1092 (printfunc)set_tp_print, /* tp_print */
1093 0, /* tp_getattr */
1094 0, /* tp_setattr */
1095 (cmpfunc)set_nocmp, /* tp_compare */
1096 (reprfunc)set_repr, /* tp_repr */
1097 &frozenset_as_number, /* tp_as_number */
1098 &set_as_sequence, /* tp_as_sequence */
1099 0, /* tp_as_mapping */
1100 frozenset_hash, /* tp_hash */
1101 0, /* tp_call */
1102 0, /* tp_str */
1103 PyObject_GenericGetAttr, /* tp_getattro */
1104 0, /* tp_setattro */
1105 0, /* tp_as_buffer */
1106 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
1107 Py_TPFLAGS_BASETYPE, /* tp_flags */
1108 frozenset_doc, /* tp_doc */
1109 (traverseproc)set_traverse, /* tp_traverse */
1110 0, /* tp_clear */
1111 (richcmpfunc)set_richcompare, /* tp_richcompare */
1112 0, /* tp_weaklistoffset */
1113 (getiterfunc)set_iter, /* tp_iter */
1114 0, /* tp_iternext */
1115 frozenset_methods, /* tp_methods */
1116 0, /* tp_members */
1117 0, /* tp_getset */
1118 0, /* tp_base */
1119 0, /* tp_dict */
1120 0, /* tp_descr_get */
1121 0, /* tp_descr_set */
1122 0, /* tp_dictoffset */
1123 0, /* tp_init */
1124 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001125 frozenset_new, /* tp_new */
Raymond Hettingera690a992003-11-16 16:17:49 +00001126 PyObject_GC_Del, /* tp_free */
1127};