blob: 8e70546a2ada9e5248e2f816c3085c3145ae1f20 [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)) {
18 if (PyDict_Merge(so->data, ((PySetObject *)other)->data, 0) == -1)
19 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{
161 PyObject *tmp;
162 long result;
163
164 result = PyDict_Contains(so->data, key);
165 if (result == -1 && PyAnySet_Check(key)) {
166 PyErr_Clear();
167 tmp = frozenset_dict_wrapper(((PySetObject *)(key))->data);
168 if (tmp == NULL)
169 return NULL;
170 result = PyDict_Contains(so->data, tmp);
171 Py_DECREF(tmp);
172 }
173 if (result == -1)
174 return NULL;
175 return PyBool_FromLong(result);
176}
177
178PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
179
180static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000181set_copy(PySetObject *so)
182{
Raymond Hettingera38123e2003-11-24 22:18:49 +0000183 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +0000184}
185
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000186static PyObject *
187frozenset_copy(PySetObject *so)
188{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000189 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000190 Py_INCREF(so);
191 return (PyObject *)so;
192 }
193 return set_copy(so);
194}
195
Raymond Hettingera690a992003-11-16 16:17:49 +0000196PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
197
198static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000199set_union(PySetObject *so, PyObject *other)
200{
201 PySetObject *result;
202 PyObject *rv;
203
204 result = (PySetObject *)set_copy(so);
205 if (result == NULL)
206 return NULL;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000207 rv = set_update(result, other);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000208 if (rv == NULL) {
209 Py_DECREF(result);
210 return NULL;
211 }
212 Py_DECREF(rv);
213 return (PyObject *)result;
214}
215
216PyDoc_STRVAR(union_doc,
217 "Return the union of two sets as a new set.\n\
218\n\
219(i.e. all elements that are in either set.)");
220
221static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000222set_or(PySetObject *so, PyObject *other)
223{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000224 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000225 Py_INCREF(Py_NotImplemented);
226 return Py_NotImplemented;
227 }
228 return set_union(so, other);
229}
230
231static PyObject *
232set_ior(PySetObject *so, PyObject *other)
233{
234 PyObject *result;
235
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000236 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000237 Py_INCREF(Py_NotImplemented);
238 return Py_NotImplemented;
239 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000240 result = set_update(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000241 if (result == NULL)
242 return NULL;
243 Py_DECREF(result);
244 Py_INCREF(so);
245 return (PyObject *)so;
246}
247
248static PyObject *
249set_intersection(PySetObject *so, PyObject *other)
250{
251 PySetObject *result;
252 PyObject *item, *selfdata, *tgtdata, *it;
253
254 result = (PySetObject *)make_new_set(so->ob_type, NULL);
255 if (result == NULL)
256 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000257 tgtdata = result->data;
258 selfdata = so->data;
259
260 if (PyAnySet_Check(other) &&
261 PyDict_Size(((PySetObject *)other)->data) > PyDict_Size(selfdata)) {
262 selfdata = ((PySetObject *)other)->data;
263 other = (PyObject *)so;
264 } else if (PyDict_Check(other) &&
265 PyDict_Size(other) > PyDict_Size(selfdata)) {
266 selfdata = other;
267 other = so->data;
268 }
269
Raymond Hettingera690a992003-11-16 16:17:49 +0000270 it = PyObject_GetIter(other);
271 if (it == NULL) {
272 Py_DECREF(result);
273 return NULL;
274 }
275
Raymond Hettingera690a992003-11-16 16:17:49 +0000276 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +0000277 if (PyDict_Contains(selfdata, item)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000278 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
279 Py_DECREF(it);
280 Py_DECREF(result);
281 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000282 return NULL;
283 }
284 }
285 Py_DECREF(item);
286 }
287 Py_DECREF(it);
288 if (PyErr_Occurred()) {
289 Py_DECREF(result);
290 return NULL;
291 }
292 return (PyObject *)result;
293}
294
295PyDoc_STRVAR(intersection_doc,
296"Return the intersection of two sets as a new set.\n\
297\n\
298(i.e. all elements that are in both sets.)");
299
300static PyObject *
301set_intersection_update(PySetObject *so, PyObject *other)
302{
303 PyObject *item, *selfdata, *it, *newdict, *tmp;
304
305 newdict = PyDict_New();
306 if (newdict == NULL)
307 return newdict;
308
309 it = PyObject_GetIter(other);
310 if (it == NULL) {
311 Py_DECREF(newdict);
312 return NULL;
313 }
314
315 selfdata = so->data;
316 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +0000317 if (PyDict_Contains(selfdata, item)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000318 if (PyDict_SetItem(newdict, item, Py_True) == -1) {
319 Py_DECREF(newdict);
320 Py_DECREF(it);
321 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000322 return NULL;
323 }
324 }
325 Py_DECREF(item);
326 }
327 Py_DECREF(it);
328 if (PyErr_Occurred()) {
329 Py_DECREF(newdict);
330 return NULL;
331 }
332 tmp = so->data;
333 so->data = newdict;
334 Py_DECREF(tmp);
335 Py_RETURN_NONE;
336}
337
338PyDoc_STRVAR(intersection_update_doc,
339"Update a set with the intersection of itself and another.");
340
341static PyObject *
342set_and(PySetObject *so, PyObject *other)
343{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000344 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000345 Py_INCREF(Py_NotImplemented);
346 return Py_NotImplemented;
347 }
348 return set_intersection(so, other);
349}
350
351static PyObject *
352set_iand(PySetObject *so, PyObject *other)
353{
354 PyObject *result;
355
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000356 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000357 Py_INCREF(Py_NotImplemented);
358 return Py_NotImplemented;
359 }
360 result = set_intersection_update(so, other);
361 if (result == NULL)
362 return NULL;
363 Py_DECREF(result);
364 Py_INCREF(so);
365 return (PyObject *)so;
366}
367
368static PyObject *
369set_difference(PySetObject *so, PyObject *other)
370{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000371 PySetObject *result, *otherset=NULL;
372 PyObject *item, *otherdata, *tgtdata, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +0000373
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000374 result = (PySetObject *)make_new_set(so->ob_type, NULL);
Raymond Hettingera690a992003-11-16 16:17:49 +0000375 if (result == NULL)
376 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000377 tgtdata = result->data;
378
379 if (PyDict_Check(other))
380 otherdata = other;
381 else if (PyAnySet_Check(other))
382 otherdata = ((PySetObject *)other)->data;
383 else {
384 otherset = (PySetObject *)make_new_set(so->ob_type, other);
385 if (otherset == NULL) {
386 Py_DECREF(result);
387 return NULL;
388 }
389 otherdata = otherset->data;
390 }
391
392 it = PyObject_GetIter(so->data);
Raymond Hettingera690a992003-11-16 16:17:49 +0000393 if (it == NULL) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000394 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000395 Py_DECREF(result);
396 return NULL;
397 }
398
Raymond Hettingera690a992003-11-16 16:17:49 +0000399 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +0000400 if (!PyDict_Contains(otherdata, item)) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000401 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
402 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000403 Py_DECREF(it);
Raymond Hettingera690a992003-11-16 16:17:49 +0000404 Py_DECREF(item);
405 return NULL;
406 }
407 }
408 Py_DECREF(item);
409 }
410 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000411 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000412 if (PyErr_Occurred()) {
413 Py_DECREF(result);
414 return NULL;
415 }
416 return (PyObject *)result;
417}
418
419PyDoc_STRVAR(difference_doc,
420"Return the difference of two sets as a new set.\n\
421\n\
422(i.e. all elements that are in this set but not the other.)");
423
424static PyObject *
425set_difference_update(PySetObject *so, PyObject *other)
426{
427 PyObject *item, *tgtdata, *it;
428
429 it = PyObject_GetIter(other);
430 if (it == NULL)
431 return NULL;
432
433 tgtdata = so->data;
434 while ((item = PyIter_Next(it)) != NULL) {
435 if (PyDict_DelItem(tgtdata, item) == -1) {
436 if (PyErr_ExceptionMatches(PyExc_KeyError))
437 PyErr_Clear();
438 else {
439 Py_DECREF(it);
440 Py_DECREF(item);
441 return NULL;
442 }
443 }
444 Py_DECREF(item);
445 }
446 Py_DECREF(it);
447 if (PyErr_Occurred())
448 return NULL;
449 Py_RETURN_NONE;
450}
451
452PyDoc_STRVAR(difference_update_doc,
453"Remove all elements of another set from this set.");
454
455static PyObject *
456set_sub(PySetObject *so, PyObject *other)
457{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000458 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000459 Py_INCREF(Py_NotImplemented);
460 return Py_NotImplemented;
461 }
462 return set_difference(so, other);
463}
464
465static PyObject *
466set_isub(PySetObject *so, PyObject *other)
467{
468 PyObject *result;
469
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000470 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000471 Py_INCREF(Py_NotImplemented);
472 return Py_NotImplemented;
473 }
474 result = set_difference_update(so, other);
475 if (result == NULL)
476 return NULL;
477 Py_DECREF(result);
478 Py_INCREF(so);
479 return (PyObject *)so;
480}
481
482static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000483set_symmetric_difference_update(PySetObject *so, PyObject *other)
484{
485 PyObject *item, *selfdata, *it, *otherdata;
486 PySetObject *otherset = NULL;
487
488 selfdata = so->data;
489
490 if (PyDict_Check(other))
491 otherdata = other;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000492 else if (PyAnySet_Check(other))
Raymond Hettingera690a992003-11-16 16:17:49 +0000493 otherdata = ((PySetObject *)other)->data;
494 else {
495 otherset = (PySetObject *)make_new_set(so->ob_type, other);
496 if (otherset == NULL)
497 return NULL;
498 otherdata = otherset->data;
499 }
500
501 it = PyObject_GetIter(otherdata);
502 if (it == NULL)
503 return NULL;
504
505 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +0000506 if (PyDict_Contains(selfdata, item)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000507 if (PyDict_DelItem(selfdata, item) == -1) {
508 Py_XDECREF(otherset);
509 Py_DECREF(it);
510 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000511 return NULL;
512 }
513 } else {
514 if (PyDict_SetItem(selfdata, item, Py_True) == -1) {
515 Py_XDECREF(otherset);
516 Py_DECREF(it);
517 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000518 return NULL;
519 }
520 }
521 Py_DECREF(item);
522 }
523 Py_XDECREF(otherset);
524 Py_DECREF(it);
525 if (PyErr_Occurred())
526 return NULL;
527 Py_RETURN_NONE;
528}
529
530PyDoc_STRVAR(symmetric_difference_update_doc,
531"Update a set with the symmetric difference of itself and another.");
532
533static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000534set_symmetric_difference(PySetObject *so, PyObject *other)
535{
536 PySetObject *result;
537 PyObject *item, *selfdata, *otherdata, *tgtdata, *it, *rv, *otherset;
538
539 if (PyDict_Check(other))
540 otherdata = other;
541 else if (PyAnySet_Check(other))
542 otherdata = ((PySetObject *)other)->data;
543 else {
544 otherset = make_new_set(so->ob_type, other);
545 if (otherset == NULL)
546 return NULL;
547 rv = set_symmetric_difference_update((PySetObject *)otherset, (PyObject *)so);
548 if (rv == NULL)
549 return NULL;
550 Py_DECREF(rv);
551 return otherset;
552 }
553
554 result = (PySetObject *)make_new_set(so->ob_type, NULL);
555 if (result == NULL)
556 return NULL;
557 tgtdata = result->data;
558 selfdata = so->data;
559
560 it = PyObject_GetIter(otherdata);
561 if (it == NULL) {
562 Py_DECREF(result);
563 return NULL;
564 }
565 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +0000566 if (!PyDict_Contains(selfdata, item)) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000567 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
568 Py_DECREF(it);
569 Py_DECREF(item);
570 return NULL;
571 }
572 }
573 Py_DECREF(item);
574 }
575 Py_DECREF(it);
576 if (PyErr_Occurred()) {
577 Py_DECREF(result);
578 return NULL;
579 }
580
581 it = PyObject_GetIter(selfdata);
582 if (it == NULL) {
583 Py_DECREF(result);
584 return NULL;
585 }
586 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +0000587 if (!PyDict_Contains(otherdata, item)) {
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000588 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
589 Py_DECREF(it);
590 Py_DECREF(item);
591 return NULL;
592 }
593 }
594 Py_DECREF(item);
595 }
596 Py_DECREF(it);
597 if (PyErr_Occurred()) {
598 Py_DECREF(result);
599 return NULL;
600 }
601
602 return (PyObject *)result;
603}
604
605PyDoc_STRVAR(symmetric_difference_doc,
606"Return the symmetric difference of two sets as a new set.\n\
607\n\
608(i.e. all elements that are in exactly one of the sets.)");
609
610static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000611set_xor(PySetObject *so, PyObject *other)
612{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000613 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000614 Py_INCREF(Py_NotImplemented);
615 return Py_NotImplemented;
616 }
617 return set_symmetric_difference(so, other);
618}
619
620static PyObject *
621set_ixor(PySetObject *so, PyObject *other)
622{
623 PyObject *result;
624
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000625 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000626 Py_INCREF(Py_NotImplemented);
627 return Py_NotImplemented;
628 }
629 result = set_symmetric_difference_update(so, other);
630 if (result == NULL)
631 return NULL;
632 Py_DECREF(result);
633 Py_INCREF(so);
634 return (PyObject *)so;
635}
636
637static PyObject *
638set_issubset(PySetObject *so, PyObject *other)
639{
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000640 PyObject *otherdata, *it, *item, *tmp, *result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000641
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000642 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000643 tmp = make_new_set(&PySet_Type, other);
644 if (tmp == NULL)
645 return NULL;
646 result = set_issubset(so, tmp);
647 Py_DECREF(tmp);
648 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000649 }
650 if (set_len(so) > set_len((PySetObject *)other))
651 Py_RETURN_FALSE;
652
653 it = PyObject_GetIter(so->data);
654 if (it == NULL)
655 return NULL;
656
657 otherdata = ((PySetObject *)other)->data;
658 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +0000659 if (!PyDict_Contains(otherdata, item)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000660 Py_DECREF(it);
661 Py_DECREF(item);
662 Py_RETURN_FALSE;
663 }
664 Py_DECREF(item);
665 }
666 Py_DECREF(it);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000667 if (PyErr_Occurred())
668 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000669 Py_RETURN_TRUE;
670}
671
672PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
673
674static PyObject *
675set_issuperset(PySetObject *so, PyObject *other)
676{
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000677 PyObject *tmp, *result;
678
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000679 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000680 tmp = make_new_set(&PySet_Type, other);
681 if (tmp == NULL)
682 return NULL;
683 result = set_issuperset(so, tmp);
684 Py_DECREF(tmp);
685 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000686 }
687 return set_issubset((PySetObject *)other, (PyObject *)so);
688}
689
690PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
691
692static long
693set_nohash(PyObject *self)
694{
695 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
696 return -1;
697}
698
699static int
700set_nocmp(PyObject *self)
701{
702 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
703 return -1;
704}
705
706static long
707frozenset_hash(PyObject *self)
708{
709 PyObject *it, *item;
710 PySetObject *so = (PySetObject *)self;
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000711 long hash = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +0000712
713 if (so->hash != -1)
714 return so->hash;
715
716 it = PyObject_GetIter(((PySetObject *)so)->data);
717 if (it == NULL)
718 return -1;
719
720 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000721 /* Multiplying by a large prime increases the bit dispersion for
722 closely spaced hash values. The is important because some
723 use cases have many combinations of a small number of
724 elements with nearby hashes so that many distinct combinations
725 collapse to only a handful of distinct hash values. */
Guido van Rossum5f4e45d2003-11-24 04:13:13 +0000726 hash ^= PyObject_Hash(item) * 3644798167u;
Raymond Hettingera690a992003-11-16 16:17:49 +0000727 Py_DECREF(item);
728 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000729 Py_DECREF(it);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000730 if (PyErr_Occurred())
731 return -1;
732 so->hash = hash;
Raymond Hettingera690a992003-11-16 16:17:49 +0000733 return hash;
734}
735
736static PyObject *
737set_richcompare(PySetObject *v, PyObject *w, int op)
738{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000739 if(!PyAnySet_Check(w)) {
740 if (op == Py_EQ)
741 Py_RETURN_FALSE;
742 if (op == Py_NE)
743 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000744 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
745 return NULL;
746 }
747 switch (op) {
748 case Py_EQ:
749 case Py_NE:
750 return PyObject_RichCompare(((PySetObject *)v)->data,
751 ((PySetObject *)w)->data, op);
752 case Py_LE:
753 return set_issubset((PySetObject *)v, w);
754 case Py_GE:
755 return set_issuperset((PySetObject *)v, w);
756 case Py_LT:
757 if (set_len(v) >= set_len((PySetObject *)w))
758 Py_RETURN_FALSE;
759 return set_issubset((PySetObject *)v, w);
760 case Py_GT:
761 if (set_len(v) <= set_len((PySetObject *)w))
762 Py_RETURN_FALSE;
763 return set_issuperset((PySetObject *)v, w);
764 }
765 Py_INCREF(Py_NotImplemented);
766 return Py_NotImplemented;
767}
768
769static PyObject *
770set_repr(PySetObject *so)
771{
772 PyObject *keys, *result, *listrepr;
773
774 keys = PyDict_Keys(so->data);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000775 if (keys == NULL)
776 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000777 listrepr = PyObject_Repr(keys);
778 Py_DECREF(keys);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000779 if (listrepr == NULL)
780 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000781
782 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
783 PyString_AS_STRING(listrepr));
784 Py_DECREF(listrepr);
785 return result;
786}
787
788static int
789set_tp_print(PySetObject *so, FILE *fp, int flags)
790{
791 PyObject *it, *item;
792 int firstpass=1;
793
794 it = PyObject_GetIter(so->data);
795 if (it == NULL)
796 return -1;
797 fprintf(fp, "%s([", so->ob_type->tp_name);
798
799 while ((item = PyIter_Next(it)) != NULL) {
800 if (firstpass == 1)
801 firstpass = 0;
802 else
Raymond Hettingere2c277a2003-11-16 16:36:58 +0000803 fprintf(fp, ", ");
Raymond Hettingera690a992003-11-16 16:17:49 +0000804 if (PyObject_Print(item, fp, 0) != 0) {
805 Py_DECREF(it);
806 Py_DECREF(item);
807 return -1;
808 }
809 Py_DECREF(item);
810 }
811 Py_DECREF(it);
812 fprintf(fp, "])");
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000813 if (PyErr_Occurred())
814 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000815 return 0;
816}
817
818static PyObject *
819set_clear(PySetObject *so)
820{
821 PyDict_Clear(so->data);
822 so->hash = -1;
823 Py_INCREF(Py_None);
824 return Py_None;
825}
826
827PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
828
829static int
830set_tp_clear(PySetObject *so)
831{
832 PyDict_Clear(so->data);
833 so->hash = -1;
834 return 0;
835}
836
837static PyObject *
838set_add(PySetObject *so, PyObject *item)
839{
840 if (PyDict_SetItem(so->data, item, Py_True) == -1)
841 return NULL;
842 Py_INCREF(Py_None);
843 return Py_None;
844}
845
846PyDoc_STRVAR(add_doc,
847"Add an element to a set.\n\
848\n\
849This has no effect if the element is already present.");
850
851static PyObject *
852set_remove(PySetObject *so, PyObject *item)
853{
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000854 PyObject *tmp;
855
856 if (PyDict_DelItem(so->data, item) == -1) {
857 if (!PyType_IsSubtype(item->ob_type, &PySet_Type))
858 return NULL;
859 PyErr_Clear();
860 tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
861 if (tmp == NULL)
862 return NULL;
863 if (PyDict_DelItem(so->data, tmp) == -1) {
864 Py_DECREF(tmp);
865 return NULL;
866 }
867 Py_DECREF(tmp);
868 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000869 Py_INCREF(Py_None);
870 return Py_None;
871}
872
873PyDoc_STRVAR(remove_doc,
874"Remove an element from a set; it must be a member.\n\
875\n\
876If the element is not a member, raise a KeyError.");
877
878static PyObject *
879set_discard(PySetObject *so, PyObject *item)
880{
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000881 PyObject *tmp;
882
Guido van Rossumb61982b2003-11-18 19:27:19 +0000883 if (PyDict_DelItem(so->data, item) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000884 if (PyErr_ExceptionMatches(PyExc_KeyError))
885 PyErr_Clear();
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000886 else {
887 if (!PyType_IsSubtype(item->ob_type, &PySet_Type))
888 return NULL;
889 PyErr_Clear();
890 tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
891 if (tmp == NULL)
892 return NULL;
893 if (PyDict_DelItem(so->data, tmp) == -1) {
894 if (PyErr_ExceptionMatches(PyExc_KeyError))
895 PyErr_Clear();
896 else {
897 Py_DECREF(tmp);
898 return NULL;
899 }
900 }
901 Py_DECREF(tmp);
902 }
Guido van Rossumb61982b2003-11-18 19:27:19 +0000903 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000904 Py_INCREF(Py_None);
905 return Py_None;
906}
907
908PyDoc_STRVAR(discard_doc,
909"Remove an element from a set if it is a member.\n\
910\n\
911If the element is not a member, do nothing.");
912
913static PyObject *
914set_pop(PySetObject *so)
915{
916 PyObject *key, *value;
917 int pos = 0;
918
919 if (!PyDict_Next(so->data, &pos, &key, &value)) {
920 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
921 return NULL;
922 }
923 Py_INCREF(key);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000924 if (PyDict_DelItem(so->data, key) == -1) {
925 Py_DECREF(key);
926 return NULL;
927 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000928 return key;
929}
930
931PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
932
933static PyObject *
934set_reduce(PySetObject *so)
935{
936 PyObject *keys=NULL, *args=NULL, *result=NULL;
937
938 keys = PyDict_Keys(so->data);
939 if (keys == NULL)
940 goto done;
941 args = PyTuple_Pack(1, keys);
942 if (args == NULL)
943 goto done;
944 result = PyTuple_Pack(2, so->ob_type, args);
945done:
946 Py_XDECREF(args);
947 Py_XDECREF(keys);
948 return result;
949}
950
951PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
952
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000953static int
954set_init(PySetObject *self, PyObject *args, PyObject *kwds)
955{
956 PyObject *iterable = NULL;
957 PyObject *result;
958
959 if (!PyAnySet_Check(self))
960 return -1;
961 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
962 return -1;
963 PyDict_Clear(self->data);
964 self->hash = -1;
965 if (iterable == NULL)
966 return 0;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000967 result = set_update(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000968 if (result != NULL) {
969 Py_DECREF(result);
970 return 0;
971 }
972 return -1;
973}
974
Raymond Hettingera690a992003-11-16 16:17:49 +0000975static PySequenceMethods set_as_sequence = {
976 (inquiry)set_len, /* sq_length */
977 0, /* sq_concat */
978 0, /* sq_repeat */
979 0, /* sq_item */
980 0, /* sq_slice */
981 0, /* sq_ass_item */
982 0, /* sq_ass_slice */
983 (objobjproc)set_contains, /* sq_contains */
984};
985
986/* set object ********************************************************/
987
988static PyMethodDef set_methods[] = {
989 {"add", (PyCFunction)set_add, METH_O,
990 add_doc},
991 {"clear", (PyCFunction)set_clear, METH_NOARGS,
992 clear_doc},
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000993 {"__contains__", (PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
994 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +0000995 {"copy", (PyCFunction)set_copy, METH_NOARGS,
996 copy_doc},
997 {"__copy__", (PyCFunction)set_copy, METH_NOARGS,
998 copy_doc},
999 {"discard", (PyCFunction)set_discard, METH_O,
1000 discard_doc},
1001 {"difference", (PyCFunction)set_difference, METH_O,
1002 difference_doc},
1003 {"difference_update", (PyCFunction)set_difference_update, METH_O,
1004 difference_update_doc},
1005 {"intersection",(PyCFunction)set_intersection, METH_O,
1006 intersection_doc},
1007 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
1008 intersection_update_doc},
1009 {"issubset", (PyCFunction)set_issubset, METH_O,
1010 issubset_doc},
1011 {"issuperset", (PyCFunction)set_issuperset, METH_O,
1012 issuperset_doc},
1013 {"pop", (PyCFunction)set_pop, METH_NOARGS,
1014 pop_doc},
1015 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1016 reduce_doc},
1017 {"remove", (PyCFunction)set_remove, METH_O,
1018 remove_doc},
1019 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1020 symmetric_difference_doc},
1021 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
1022 symmetric_difference_update_doc},
1023 {"union", (PyCFunction)set_union, METH_O,
1024 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +00001025 {"update", (PyCFunction)set_update, METH_O,
1026 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +00001027 {NULL, NULL} /* sentinel */
1028};
1029
1030static PyNumberMethods set_as_number = {
1031 0, /*nb_add*/
1032 (binaryfunc)set_sub, /*nb_subtract*/
1033 0, /*nb_multiply*/
1034 0, /*nb_divide*/
1035 0, /*nb_remainder*/
1036 0, /*nb_divmod*/
1037 0, /*nb_power*/
1038 0, /*nb_negative*/
1039 0, /*nb_positive*/
1040 0, /*nb_absolute*/
1041 0, /*nb_nonzero*/
1042 0, /*nb_invert*/
1043 0, /*nb_lshift*/
1044 0, /*nb_rshift*/
1045 (binaryfunc)set_and, /*nb_and*/
1046 (binaryfunc)set_xor, /*nb_xor*/
1047 (binaryfunc)set_or, /*nb_or*/
1048 0, /*nb_coerce*/
1049 0, /*nb_int*/
1050 0, /*nb_long*/
1051 0, /*nb_float*/
1052 0, /*nb_oct*/
1053 0, /*nb_hex*/
1054 0, /*nb_inplace_add*/
1055 (binaryfunc)set_isub, /*nb_inplace_subtract*/
1056 0, /*nb_inplace_multiply*/
1057 0, /*nb_inplace_divide*/
1058 0, /*nb_inplace_remainder*/
1059 0, /*nb_inplace_power*/
1060 0, /*nb_inplace_lshift*/
1061 0, /*nb_inplace_rshift*/
1062 (binaryfunc)set_iand, /*nb_inplace_and*/
1063 (binaryfunc)set_ixor, /*nb_inplace_xor*/
1064 (binaryfunc)set_ior, /*nb_inplace_or*/
1065};
1066
1067PyDoc_STRVAR(set_doc,
1068"set(iterable) --> set object\n\
1069\n\
1070Build an unordered collection.");
1071
1072PyTypeObject PySet_Type = {
1073 PyObject_HEAD_INIT(&PyType_Type)
1074 0, /* ob_size */
1075 "set", /* tp_name */
1076 sizeof(PySetObject), /* tp_basicsize */
1077 0, /* tp_itemsize */
1078 /* methods */
1079 (destructor)set_dealloc, /* tp_dealloc */
1080 (printfunc)set_tp_print, /* tp_print */
1081 0, /* tp_getattr */
1082 0, /* tp_setattr */
1083 (cmpfunc)set_nocmp, /* tp_compare */
1084 (reprfunc)set_repr, /* tp_repr */
1085 &set_as_number, /* tp_as_number */
1086 &set_as_sequence, /* tp_as_sequence */
1087 0, /* tp_as_mapping */
1088 set_nohash, /* tp_hash */
1089 0, /* tp_call */
1090 0, /* tp_str */
1091 PyObject_GenericGetAttr, /* tp_getattro */
1092 0, /* tp_setattro */
1093 0, /* tp_as_buffer */
1094 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
1095 Py_TPFLAGS_BASETYPE, /* tp_flags */
1096 set_doc, /* tp_doc */
1097 (traverseproc)set_traverse, /* tp_traverse */
1098 (inquiry)set_tp_clear, /* tp_clear */
1099 (richcmpfunc)set_richcompare, /* tp_richcompare */
1100 0, /* tp_weaklistoffset */
1101 (getiterfunc)set_iter, /* tp_iter */
1102 0, /* tp_iternext */
1103 set_methods, /* tp_methods */
1104 0, /* tp_members */
1105 0, /* tp_getset */
1106 0, /* tp_base */
1107 0, /* tp_dict */
1108 0, /* tp_descr_get */
1109 0, /* tp_descr_set */
1110 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001111 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001112 PyType_GenericAlloc, /* tp_alloc */
1113 set_new, /* tp_new */
1114 PyObject_GC_Del, /* tp_free */
1115};
1116
1117/* frozenset object ********************************************************/
1118
1119
1120static PyMethodDef frozenset_methods[] = {
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001121 {"__contains__", (PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
1122 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001123 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001124 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001125 {"__copy__", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001126 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001127 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001128 difference_doc},
1129 {"intersection",(PyCFunction)set_intersection, METH_O,
1130 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001131 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001132 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001133 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001134 issuperset_doc},
1135 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1136 reduce_doc},
1137 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1138 symmetric_difference_doc},
1139 {"union", (PyCFunction)set_union, METH_O,
1140 union_doc},
1141 {NULL, NULL} /* sentinel */
1142};
1143
1144static PyNumberMethods frozenset_as_number = {
1145 0, /*nb_add*/
1146 (binaryfunc)set_sub, /*nb_subtract*/
1147 0, /*nb_multiply*/
1148 0, /*nb_divide*/
1149 0, /*nb_remainder*/
1150 0, /*nb_divmod*/
1151 0, /*nb_power*/
1152 0, /*nb_negative*/
1153 0, /*nb_positive*/
1154 0, /*nb_absolute*/
1155 0, /*nb_nonzero*/
1156 0, /*nb_invert*/
1157 0, /*nb_lshift*/
1158 0, /*nb_rshift*/
1159 (binaryfunc)set_and, /*nb_and*/
1160 (binaryfunc)set_xor, /*nb_xor*/
1161 (binaryfunc)set_or, /*nb_or*/
1162};
1163
1164PyDoc_STRVAR(frozenset_doc,
1165"frozenset(iterable) --> frozenset object\n\
1166\n\
1167Build an immutable unordered collection.");
1168
1169PyTypeObject PyFrozenSet_Type = {
1170 PyObject_HEAD_INIT(&PyType_Type)
1171 0, /* ob_size */
1172 "frozenset", /* tp_name */
1173 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +00001174 0, /* tp_itemsize */ /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001175 (destructor)set_dealloc, /* tp_dealloc */
1176 (printfunc)set_tp_print, /* tp_print */
1177 0, /* tp_getattr */
1178 0, /* tp_setattr */
1179 (cmpfunc)set_nocmp, /* tp_compare */
1180 (reprfunc)set_repr, /* tp_repr */
1181 &frozenset_as_number, /* tp_as_number */
1182 &set_as_sequence, /* tp_as_sequence */
1183 0, /* tp_as_mapping */
1184 frozenset_hash, /* tp_hash */
1185 0, /* tp_call */
1186 0, /* tp_str */
1187 PyObject_GenericGetAttr, /* tp_getattro */
1188 0, /* tp_setattro */
1189 0, /* tp_as_buffer */
1190 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
1191 Py_TPFLAGS_BASETYPE, /* tp_flags */
1192 frozenset_doc, /* tp_doc */
1193 (traverseproc)set_traverse, /* tp_traverse */
1194 0, /* tp_clear */
1195 (richcmpfunc)set_richcompare, /* tp_richcompare */
1196 0, /* tp_weaklistoffset */
1197 (getiterfunc)set_iter, /* tp_iter */
1198 0, /* tp_iternext */
1199 frozenset_methods, /* tp_methods */
1200 0, /* tp_members */
1201 0, /* tp_getset */
1202 0, /* tp_base */
1203 0, /* tp_dict */
1204 0, /* tp_descr_get */
1205 0, /* tp_descr_set */
1206 0, /* tp_dictoffset */
1207 0, /* tp_init */
1208 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001209 frozenset_new, /* tp_new */
Raymond Hettingera690a992003-11-16 16:17:49 +00001210 PyObject_GC_Del, /* tp_free */
1211};