blob: fbff0779c212ac47de7714ab34140db02805a7a5 [file] [log] [blame]
Raymond Hettingera690a992003-11-16 16:17:49 +00001#include "Python.h"
Raymond Hettinger691d8052004-05-30 07:26:47 +00002#include "structmember.h"
Raymond Hettingera690a992003-11-16 16:17:49 +00003
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 Hettingera690a992003-11-16 16:17:49 +000013static PyObject *
Raymond Hettingera38123e2003-11-24 22:18:49 +000014set_update(PySetObject *so, PyObject *other)
Raymond Hettingera690a992003-11-16 16:17:49 +000015{
Raymond Hettingera38123e2003-11-24 22:18:49 +000016 PyObject *item, *data, *it;
Raymond Hettingera690a992003-11-16 16:17:49 +000017
Raymond Hettingera38123e2003-11-24 22:18:49 +000018 if (PyAnySet_Check(other)) {
Raymond Hettinger81ad32e2003-12-15 21:16:06 +000019 if (PyDict_Merge(so->data, ((PySetObject *)other)->data, 1) == -1)
Raymond Hettingera38123e2003-11-24 22:18:49 +000020 return NULL;
21 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +000022 }
23
Raymond Hettingera38123e2003-11-24 22:18:49 +000024 it = PyObject_GetIter(other);
25 if (it == NULL)
26 return NULL;
27 data = so->data;
Raymond Hettingera690a992003-11-16 16:17:49 +000028
Raymond Hettingera38123e2003-11-24 22:18:49 +000029 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingera690a992003-11-16 16:17:49 +000030 if (PyDict_SetItem(data, item, Py_True) == -1) {
Raymond Hettingera38123e2003-11-24 22:18:49 +000031 Py_DECREF(it);
Raymond Hettingera690a992003-11-16 16:17:49 +000032 Py_DECREF(item);
Raymond Hettingera38123e2003-11-24 22:18:49 +000033 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +000034 }
35 Py_DECREF(item);
36 }
Raymond Hettingera38123e2003-11-24 22:18:49 +000037 Py_DECREF(it);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +000038 if (PyErr_Occurred())
Raymond Hettingera38123e2003-11-24 22:18:49 +000039 return NULL;
40 Py_RETURN_NONE;
41}
42
43PyDoc_STRVAR(update_doc,
44"Update a set with the union of itself and another.");
45
46static PyObject *
47make_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 Hettingera690a992003-11-16 16:17:49 +000056
57 /* create PySetObject structure */
58 so = (PySetObject *)type->tp_alloc(type, 0);
Raymond Hettingera38123e2003-11-24 22:18:49 +000059 if (so == NULL) {
60 Py_DECREF(data);
61 return NULL;
62 }
Raymond Hettingera690a992003-11-16 16:17:49 +000063 so->data = data;
64 so->hash = -1;
Raymond Hettinger691d8052004-05-30 07:26:47 +000065 so->weakreflist = NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +000066
Raymond Hettingera38123e2003-11-24 22:18:49 +000067 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 Hettingera690a992003-11-16 16:17:49 +000076 return (PyObject *)so;
77}
78
79static PyObject *
Raymond Hettinger50a4bb32003-11-17 16:42:33 +000080frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Raymond Hettingera690a992003-11-16 16:17:49 +000081{
82 PyObject *iterable = NULL;
83
84 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
85 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +000086 if (iterable != NULL && PyFrozenSet_CheckExact(iterable)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +000087 Py_INCREF(iterable);
88 return iterable;
89 }
Raymond Hettingera690a992003-11-16 16:17:49 +000090 return make_new_set(type, iterable);
91}
92
Raymond Hettinger50a4bb32003-11-17 16:42:33 +000093static PyObject *
94set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
95{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +000096 return make_new_set(type, NULL);
97}
98
Raymond Hettingerbfd334a2003-11-22 03:55:23 +000099static PyObject *
100frozenset_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;
108 Py_DECREF(w->data);
109 Py_INCREF(d);
110 w->data = d;
111 return (PyObject *)w;
112}
113
Raymond Hettingera690a992003-11-16 16:17:49 +0000114static void
115set_dealloc(PySetObject *so)
116{
117 PyObject_GC_UnTrack(so);
Raymond Hettinger691d8052004-05-30 07:26:47 +0000118 if (so->weakreflist != NULL)
119 PyObject_ClearWeakRefs((PyObject *) so);
Raymond Hettingera690a992003-11-16 16:17:49 +0000120 Py_XDECREF(so->data);
121 so->ob_type->tp_free(so);
122}
123
124static int
125set_traverse(PySetObject *so, visitproc visit, void *arg)
126{
127 if (so->data)
128 return visit(so->data, arg);
129 return 0;
130}
131
132static PyObject *
133set_iter(PySetObject *so)
134{
135 return PyObject_GetIter(so->data);
136}
137
138static int
139set_len(PySetObject *so)
140{
141 return PyDict_Size(so->data);
142}
143
144static int
145set_contains(PySetObject *so, PyObject *key)
146{
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000147 PyObject *tmp;
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000148 int result;
149
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +0000150 result = PyDict_Contains(so->data, key);
Raymond Hettingera38123e2003-11-24 22:18:49 +0000151 if (result == -1 && PyAnySet_Check(key)) {
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000152 PyErr_Clear();
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000153 tmp = frozenset_dict_wrapper(((PySetObject *)(key))->data);
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000154 if (tmp == NULL)
155 return -1;
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +0000156 result = PyDict_Contains(so->data, tmp);
Raymond Hettinger19c2d772003-11-21 18:36:54 +0000157 Py_DECREF(tmp);
158 }
159 return result;
Raymond Hettingera690a992003-11-16 16:17:49 +0000160}
161
162static PyObject *
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000163set_direct_contains(PySetObject *so, PyObject *key)
164{
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000165 long result;
166
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000167 result = set_contains(so, key);
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000168 if (result == -1)
169 return NULL;
170 return PyBool_FromLong(result);
171}
172
173PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
174
175static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000176set_copy(PySetObject *so)
177{
Raymond Hettingera38123e2003-11-24 22:18:49 +0000178 return make_new_set(so->ob_type, (PyObject *)so);
Raymond Hettingera690a992003-11-16 16:17:49 +0000179}
180
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000181static PyObject *
182frozenset_copy(PySetObject *so)
183{
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000184 if (PyFrozenSet_CheckExact(so)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000185 Py_INCREF(so);
186 return (PyObject *)so;
187 }
188 return set_copy(so);
189}
190
Raymond Hettingera690a992003-11-16 16:17:49 +0000191PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
192
193static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000194set_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 Hettingera38123e2003-11-24 22:18:49 +0000202 rv = set_update(result, other);
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000203 if (rv == NULL) {
204 Py_DECREF(result);
205 return NULL;
206 }
207 Py_DECREF(rv);
208 return (PyObject *)result;
209}
210
211PyDoc_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
216static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000217set_or(PySetObject *so, PyObject *other)
218{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000219 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000220 Py_INCREF(Py_NotImplemented);
221 return Py_NotImplemented;
222 }
223 return set_union(so, other);
224}
225
226static PyObject *
227set_ior(PySetObject *so, PyObject *other)
228{
229 PyObject *result;
230
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000231 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000232 Py_INCREF(Py_NotImplemented);
233 return Py_NotImplemented;
234 }
Raymond Hettingera38123e2003-11-24 22:18:49 +0000235 result = set_update(so, other);
Raymond Hettingera690a992003-11-16 16:17:49 +0000236 if (result == NULL)
237 return NULL;
238 Py_DECREF(result);
239 Py_INCREF(so);
240 return (PyObject *)so;
241}
242
243static PyObject *
244set_intersection(PySetObject *so, PyObject *other)
245{
246 PySetObject *result;
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000247 PyObject *item, *selfdata, *tgtdata, *it, *tmp;
Raymond Hettingera690a992003-11-16 16:17:49 +0000248
249 result = (PySetObject *)make_new_set(so->ob_type, NULL);
250 if (result == NULL)
251 return NULL;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000252 tgtdata = result->data;
253 selfdata = so->data;
254
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000255 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 Hettingerf5f41bf2003-11-24 02:57:33 +0000260 selfdata = other;
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000261 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 Hettingerf5f41bf2003-11-24 02:57:33 +0000276 }
277
Raymond Hettingera690a992003-11-16 16:17:49 +0000278 it = PyObject_GetIter(other);
279 if (it == NULL) {
280 Py_DECREF(result);
281 return NULL;
282 }
283
Raymond Hettingera690a992003-11-16 16:17:49 +0000284 while ((item = PyIter_Next(it)) != NULL) {
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +0000285 if (PyDict_Contains(selfdata, item)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000286 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
287 Py_DECREF(it);
288 Py_DECREF(result);
289 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000290 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
303PyDoc_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
308static PyObject *
309set_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 Hettingerbc0f2ab2003-11-25 21:12:14 +0000325 if (PyDict_Contains(selfdata, item)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000326 if (PyDict_SetItem(newdict, item, Py_True) == -1) {
327 Py_DECREF(newdict);
328 Py_DECREF(it);
329 Py_DECREF(item);
Raymond Hettingera690a992003-11-16 16:17:49 +0000330 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
346PyDoc_STRVAR(intersection_update_doc,
347"Update a set with the intersection of itself and another.");
348
349static PyObject *
350set_and(PySetObject *so, PyObject *other)
351{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000352 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000353 Py_INCREF(Py_NotImplemented);
354 return Py_NotImplemented;
355 }
356 return set_intersection(so, other);
357}
358
359static PyObject *
360set_iand(PySetObject *so, PyObject *other)
361{
362 PyObject *result;
363
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000364 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000365 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
376static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000377set_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
404PyDoc_STRVAR(difference_update_doc,
405"Remove all elements of another set from this set.");
406
407static PyObject *
Raymond Hettingerfb4e33a2003-12-15 13:23:55 +0000408set_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
446PyDoc_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.)");
450static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000451set_sub(PySetObject *so, PyObject *other)
452{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000453 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000454 Py_INCREF(Py_NotImplemented);
455 return Py_NotImplemented;
456 }
457 return set_difference(so, other);
458}
459
460static PyObject *
461set_isub(PySetObject *so, PyObject *other)
462{
463 PyObject *result;
464
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000465 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000466 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
477static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000478set_symmetric_difference_update(PySetObject *so, PyObject *other)
479{
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000480 PyObject *selfdata, *otherdata;
Raymond Hettingera690a992003-11-16 16:17:49 +0000481 PySetObject *otherset = NULL;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000482 PyObject *key, *value;
483 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +0000484
485 selfdata = so->data;
Raymond Hettingera690a992003-11-16 16:17:49 +0000486 if (PyDict_Check(other))
487 otherdata = other;
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000488 else if (PyAnySet_Check(other))
Raymond Hettingera690a992003-11-16 16:17:49 +0000489 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 Hettingerdc5ae112003-12-13 14:46:46 +0000497 while (PyDict_Next(otherdata, &pos, &key, &value)) {
498 if (PyDict_Contains(selfdata, key)) {
499 if (PyDict_DelItem(selfdata, key) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000500 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000501 return NULL;
502 }
503 } else {
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000504 if (PyDict_SetItem(selfdata, key, Py_True) == -1) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000505 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000506 return NULL;
507 }
508 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000509 }
510 Py_XDECREF(otherset);
Raymond Hettingera690a992003-11-16 16:17:49 +0000511 Py_RETURN_NONE;
512}
513
514PyDoc_STRVAR(symmetric_difference_update_doc,
515"Update a set with the symmetric difference of itself and another.");
516
517static PyObject *
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000518set_symmetric_difference(PySetObject *so, PyObject *other)
519{
520 PySetObject *result;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000521 PyObject *selfdata, *otherdata, *tgtdata, *rv, *otherset;
522 PyObject *key, *value;
523 int pos = 0;
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000524
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 Hettingerdc5ae112003-12-13 14:46:46 +0000546 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 Hettingerf5f41bf2003-11-24 02:57:33 +0000550 return NULL;
551 }
552 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000553 }
554
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000555 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 Hettingerf5f41bf2003-11-24 02:57:33 +0000560 return NULL;
561 }
562 }
Raymond Hettingerf5f41bf2003-11-24 02:57:33 +0000563 }
564
565 return (PyObject *)result;
566}
567
568PyDoc_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
573static PyObject *
Raymond Hettingera690a992003-11-16 16:17:49 +0000574set_xor(PySetObject *so, PyObject *other)
575{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000576 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000577 Py_INCREF(Py_NotImplemented);
578 return Py_NotImplemented;
579 }
580 return set_symmetric_difference(so, other);
581}
582
583static PyObject *
584set_ixor(PySetObject *so, PyObject *other)
585{
586 PyObject *result;
587
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000588 if (!PyAnySet_Check(other)) {
Raymond Hettingera690a992003-11-16 16:17:49 +0000589 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
600static PyObject *
601set_issubset(PySetObject *so, PyObject *other)
602{
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000603 PyObject *otherdata, *tmp, *result;
604 PyObject *key, *value;
605 int pos = 0;
Raymond Hettingera690a992003-11-16 16:17:49 +0000606
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000607 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000608 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 Hettingera690a992003-11-16 16:17:49 +0000614 }
615 if (set_len(so) > set_len((PySetObject *)other))
616 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000617
618 otherdata = ((PySetObject *)other)->data;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000619 while (PyDict_Next(((PySetObject *)so)->data, &pos, &key, &value)) {
620 if (!PyDict_Contains(otherdata, key))
Raymond Hettingera690a992003-11-16 16:17:49 +0000621 Py_RETURN_FALSE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000622 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000623 Py_RETURN_TRUE;
624}
625
626PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
627
628static PyObject *
629set_issuperset(PySetObject *so, PyObject *other)
630{
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000631 PyObject *tmp, *result;
632
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000633 if (!PyAnySet_Check(other)) {
Raymond Hettinger3fbec702003-11-21 07:56:36 +0000634 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 Hettingera690a992003-11-16 16:17:49 +0000640 }
641 return set_issubset((PySetObject *)other, (PyObject *)so);
642}
643
644PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
645
646static long
647set_nohash(PyObject *self)
648{
649 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
650 return -1;
651}
652
653static int
654set_nocmp(PyObject *self)
655{
656 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
657 return -1;
658}
659
660static long
661frozenset_hash(PyObject *self)
662{
Raymond Hettingera690a992003-11-16 16:17:49 +0000663 PySetObject *so = (PySetObject *)self;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000664 PyObject *key, *value;
665 int pos = 0;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000666 long hash = 0;
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000667
Raymond Hettingera690a992003-11-16 16:17:49 +0000668 if (so->hash != -1)
669 return so->hash;
Raymond Hettingera690a992003-11-16 16:17:49 +0000670
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000671 while (PyDict_Next(so->data, &pos, &key, &value)) {
Raymond Hettinger49ba4c32003-11-23 02:49:05 +0000672 /* Multiplying by a large prime increases the bit dispersion for
673 closely spaced hash values. The is important because some
674 use cases have many combinations of a small number of
675 elements with nearby hashes so that many distinct combinations
676 collapse to only a handful of distinct hash values. */
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000677 hash ^= PyObject_Hash(key) * 3644798167u;
Raymond Hettingera690a992003-11-16 16:17:49 +0000678 }
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000679 so->hash = hash;
Raymond Hettingera690a992003-11-16 16:17:49 +0000680 return hash;
681}
682
683static PyObject *
684set_richcompare(PySetObject *v, PyObject *w, int op)
685{
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000686 if(!PyAnySet_Check(w)) {
687 if (op == Py_EQ)
688 Py_RETURN_FALSE;
689 if (op == Py_NE)
690 Py_RETURN_TRUE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000691 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
692 return NULL;
693 }
694 switch (op) {
695 case Py_EQ:
696 case Py_NE:
697 return PyObject_RichCompare(((PySetObject *)v)->data,
698 ((PySetObject *)w)->data, op);
699 case Py_LE:
700 return set_issubset((PySetObject *)v, w);
701 case Py_GE:
702 return set_issuperset((PySetObject *)v, w);
703 case Py_LT:
704 if (set_len(v) >= set_len((PySetObject *)w))
705 Py_RETURN_FALSE;
706 return set_issubset((PySetObject *)v, w);
707 case Py_GT:
708 if (set_len(v) <= set_len((PySetObject *)w))
709 Py_RETURN_FALSE;
710 return set_issuperset((PySetObject *)v, w);
711 }
712 Py_INCREF(Py_NotImplemented);
713 return Py_NotImplemented;
714}
715
716static PyObject *
717set_repr(PySetObject *so)
718{
719 PyObject *keys, *result, *listrepr;
720
721 keys = PyDict_Keys(so->data);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000722 if (keys == NULL)
723 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000724 listrepr = PyObject_Repr(keys);
725 Py_DECREF(keys);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000726 if (listrepr == NULL)
727 return NULL;
Raymond Hettingera690a992003-11-16 16:17:49 +0000728
729 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
730 PyString_AS_STRING(listrepr));
731 Py_DECREF(listrepr);
732 return result;
733}
734
735static int
736set_tp_print(PySetObject *so, FILE *fp, int flags)
737{
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000738 PyObject *key, *value;
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000739 int pos=0;
740 char *emit = ""; /* No separator emitted on first pass */
741 char *separator = ", ";
Raymond Hettingera690a992003-11-16 16:17:49 +0000742
Raymond Hettingera690a992003-11-16 16:17:49 +0000743 fprintf(fp, "%s([", so->ob_type->tp_name);
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000744 while (PyDict_Next(so->data, &pos, &key, &value)) {
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000745 fputs(emit, fp);
746 emit = separator;
Raymond Hettingerdc5ae112003-12-13 14:46:46 +0000747 if (PyObject_Print(key, fp, 0) != 0)
Raymond Hettingera690a992003-11-16 16:17:49 +0000748 return -1;
Raymond Hettingera690a992003-11-16 16:17:49 +0000749 }
Raymond Hettingera3b11e72003-12-31 14:08:58 +0000750 fputs("])", fp);
Raymond Hettingera690a992003-11-16 16:17:49 +0000751 return 0;
752}
753
754static PyObject *
755set_clear(PySetObject *so)
756{
757 PyDict_Clear(so->data);
758 so->hash = -1;
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000759 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000760}
761
762PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
763
764static int
765set_tp_clear(PySetObject *so)
766{
767 PyDict_Clear(so->data);
768 so->hash = -1;
769 return 0;
770}
771
772static PyObject *
773set_add(PySetObject *so, PyObject *item)
774{
775 if (PyDict_SetItem(so->data, item, Py_True) == -1)
776 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000777 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000778}
779
780PyDoc_STRVAR(add_doc,
781"Add an element to a set.\n\
782\n\
783This has no effect if the element is already present.");
784
785static PyObject *
786set_remove(PySetObject *so, PyObject *item)
787{
Raymond Hettinger0deab622003-12-13 18:53:18 +0000788 PyObject *tmp, *result;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000789
Raymond Hettinger0deab622003-12-13 18:53:18 +0000790 if (PyType_IsSubtype(item->ob_type, &PySet_Type)) {
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000791 tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
792 if (tmp == NULL)
793 return NULL;
Raymond Hettinger0deab622003-12-13 18:53:18 +0000794 result = set_remove(so, tmp);
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000795 Py_DECREF(tmp);
Raymond Hettinger0deab622003-12-13 18:53:18 +0000796 return result;
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000797 }
Raymond Hettinger0deab622003-12-13 18:53:18 +0000798
799 if (PyDict_DelItem(so->data, item) == -1)
800 return NULL;
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000801 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000802}
803
804PyDoc_STRVAR(remove_doc,
805"Remove an element from a set; it must be a member.\n\
806\n\
807If the element is not a member, raise a KeyError.");
808
809static PyObject *
810set_discard(PySetObject *so, PyObject *item)
811{
Raymond Hettinger0deab622003-12-13 18:53:18 +0000812 PyObject *tmp, *result;
813
814 if (PyType_IsSubtype(item->ob_type, &PySet_Type)) {
815 tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
816 if (tmp == NULL)
817 return NULL;
818 result = set_discard(so, tmp);
819 Py_DECREF(tmp);
820 return result;
821 }
Raymond Hettingerbfd334a2003-11-22 03:55:23 +0000822
Guido van Rossumb61982b2003-11-18 19:27:19 +0000823 if (PyDict_DelItem(so->data, item) == -1) {
Raymond Hettinger0deab622003-12-13 18:53:18 +0000824 if (!PyErr_ExceptionMatches(PyExc_KeyError))
825 return NULL;
826 PyErr_Clear();
Guido van Rossumb61982b2003-11-18 19:27:19 +0000827 }
Raymond Hettinger438e02d2003-12-13 19:38:47 +0000828 Py_RETURN_NONE;
Raymond Hettingera690a992003-11-16 16:17:49 +0000829}
830
831PyDoc_STRVAR(discard_doc,
832"Remove an element from a set if it is a member.\n\
833\n\
834If the element is not a member, do nothing.");
835
836static PyObject *
837set_pop(PySetObject *so)
838{
839 PyObject *key, *value;
840 int pos = 0;
841
842 if (!PyDict_Next(so->data, &pos, &key, &value)) {
843 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
844 return NULL;
845 }
846 Py_INCREF(key);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000847 if (PyDict_DelItem(so->data, key) == -1) {
848 Py_DECREF(key);
849 return NULL;
850 }
Raymond Hettingera690a992003-11-16 16:17:49 +0000851 return key;
852}
853
854PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
855
856static PyObject *
857set_reduce(PySetObject *so)
858{
859 PyObject *keys=NULL, *args=NULL, *result=NULL;
860
861 keys = PyDict_Keys(so->data);
862 if (keys == NULL)
863 goto done;
864 args = PyTuple_Pack(1, keys);
865 if (args == NULL)
866 goto done;
867 result = PyTuple_Pack(2, so->ob_type, args);
868done:
869 Py_XDECREF(args);
870 Py_XDECREF(keys);
871 return result;
872}
873
874PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
875
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000876static int
877set_init(PySetObject *self, PyObject *args, PyObject *kwds)
878{
879 PyObject *iterable = NULL;
880 PyObject *result;
881
882 if (!PyAnySet_Check(self))
883 return -1;
884 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
885 return -1;
886 PyDict_Clear(self->data);
887 self->hash = -1;
888 if (iterable == NULL)
889 return 0;
Raymond Hettingera38123e2003-11-24 22:18:49 +0000890 result = set_update(self, iterable);
Raymond Hettinger50a4bb32003-11-17 16:42:33 +0000891 if (result != NULL) {
892 Py_DECREF(result);
893 return 0;
894 }
895 return -1;
896}
897
Raymond Hettingera690a992003-11-16 16:17:49 +0000898static PySequenceMethods set_as_sequence = {
899 (inquiry)set_len, /* sq_length */
900 0, /* sq_concat */
901 0, /* sq_repeat */
902 0, /* sq_item */
903 0, /* sq_slice */
904 0, /* sq_ass_item */
905 0, /* sq_ass_slice */
906 (objobjproc)set_contains, /* sq_contains */
907};
908
909/* set object ********************************************************/
910
911static PyMethodDef set_methods[] = {
912 {"add", (PyCFunction)set_add, METH_O,
913 add_doc},
914 {"clear", (PyCFunction)set_clear, METH_NOARGS,
915 clear_doc},
Raymond Hettinger0deab622003-12-13 18:53:18 +0000916 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +0000917 contains_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +0000918 {"copy", (PyCFunction)set_copy, METH_NOARGS,
919 copy_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +0000920 {"discard", (PyCFunction)set_discard, METH_O,
921 discard_doc},
922 {"difference", (PyCFunction)set_difference, METH_O,
923 difference_doc},
924 {"difference_update", (PyCFunction)set_difference_update, METH_O,
925 difference_update_doc},
926 {"intersection",(PyCFunction)set_intersection, METH_O,
927 intersection_doc},
928 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
929 intersection_update_doc},
930 {"issubset", (PyCFunction)set_issubset, METH_O,
931 issubset_doc},
932 {"issuperset", (PyCFunction)set_issuperset, METH_O,
933 issuperset_doc},
934 {"pop", (PyCFunction)set_pop, METH_NOARGS,
935 pop_doc},
936 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
937 reduce_doc},
938 {"remove", (PyCFunction)set_remove, METH_O,
939 remove_doc},
940 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
941 symmetric_difference_doc},
942 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
943 symmetric_difference_update_doc},
944 {"union", (PyCFunction)set_union, METH_O,
945 union_doc},
Raymond Hettingera38123e2003-11-24 22:18:49 +0000946 {"update", (PyCFunction)set_update, METH_O,
947 update_doc},
Raymond Hettingera690a992003-11-16 16:17:49 +0000948 {NULL, NULL} /* sentinel */
949};
950
951static PyNumberMethods set_as_number = {
952 0, /*nb_add*/
953 (binaryfunc)set_sub, /*nb_subtract*/
954 0, /*nb_multiply*/
955 0, /*nb_divide*/
956 0, /*nb_remainder*/
957 0, /*nb_divmod*/
958 0, /*nb_power*/
959 0, /*nb_negative*/
960 0, /*nb_positive*/
961 0, /*nb_absolute*/
962 0, /*nb_nonzero*/
963 0, /*nb_invert*/
964 0, /*nb_lshift*/
965 0, /*nb_rshift*/
966 (binaryfunc)set_and, /*nb_and*/
967 (binaryfunc)set_xor, /*nb_xor*/
968 (binaryfunc)set_or, /*nb_or*/
969 0, /*nb_coerce*/
970 0, /*nb_int*/
971 0, /*nb_long*/
972 0, /*nb_float*/
973 0, /*nb_oct*/
974 0, /*nb_hex*/
975 0, /*nb_inplace_add*/
976 (binaryfunc)set_isub, /*nb_inplace_subtract*/
977 0, /*nb_inplace_multiply*/
978 0, /*nb_inplace_divide*/
979 0, /*nb_inplace_remainder*/
980 0, /*nb_inplace_power*/
981 0, /*nb_inplace_lshift*/
982 0, /*nb_inplace_rshift*/
983 (binaryfunc)set_iand, /*nb_inplace_and*/
984 (binaryfunc)set_ixor, /*nb_inplace_xor*/
985 (binaryfunc)set_ior, /*nb_inplace_or*/
986};
987
988PyDoc_STRVAR(set_doc,
989"set(iterable) --> set object\n\
990\n\
991Build an unordered collection.");
992
993PyTypeObject PySet_Type = {
994 PyObject_HEAD_INIT(&PyType_Type)
995 0, /* ob_size */
996 "set", /* tp_name */
997 sizeof(PySetObject), /* tp_basicsize */
998 0, /* tp_itemsize */
999 /* methods */
1000 (destructor)set_dealloc, /* tp_dealloc */
1001 (printfunc)set_tp_print, /* tp_print */
1002 0, /* tp_getattr */
1003 0, /* tp_setattr */
1004 (cmpfunc)set_nocmp, /* tp_compare */
1005 (reprfunc)set_repr, /* tp_repr */
1006 &set_as_number, /* tp_as_number */
1007 &set_as_sequence, /* tp_as_sequence */
1008 0, /* tp_as_mapping */
1009 set_nohash, /* tp_hash */
1010 0, /* tp_call */
1011 0, /* tp_str */
1012 PyObject_GenericGetAttr, /* tp_getattro */
1013 0, /* tp_setattro */
1014 0, /* tp_as_buffer */
1015 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger691d8052004-05-30 07:26:47 +00001016 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001017 set_doc, /* tp_doc */
1018 (traverseproc)set_traverse, /* tp_traverse */
1019 (inquiry)set_tp_clear, /* tp_clear */
1020 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001021 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001022 (getiterfunc)set_iter, /* tp_iter */
1023 0, /* tp_iternext */
1024 set_methods, /* tp_methods */
1025 0, /* tp_members */
1026 0, /* tp_getset */
1027 0, /* tp_base */
1028 0, /* tp_dict */
1029 0, /* tp_descr_get */
1030 0, /* tp_descr_set */
1031 0, /* tp_dictoffset */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001032 (initproc)set_init, /* tp_init */
Raymond Hettingera690a992003-11-16 16:17:49 +00001033 PyType_GenericAlloc, /* tp_alloc */
1034 set_new, /* tp_new */
1035 PyObject_GC_Del, /* tp_free */
1036};
1037
1038/* frozenset object ********************************************************/
1039
1040
1041static PyMethodDef frozenset_methods[] = {
Raymond Hettinger0deab622003-12-13 18:53:18 +00001042 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001043 contains_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001044 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
Raymond Hettingera690a992003-11-16 16:17:49 +00001045 copy_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001046 {"difference", (PyCFunction)set_difference, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001047 difference_doc},
1048 {"intersection",(PyCFunction)set_intersection, METH_O,
1049 intersection_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001050 {"issubset", (PyCFunction)set_issubset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001051 issubset_doc},
Raymond Hettinger49ba4c32003-11-23 02:49:05 +00001052 {"issuperset", (PyCFunction)set_issuperset, METH_O,
Raymond Hettingera690a992003-11-16 16:17:49 +00001053 issuperset_doc},
1054 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
1055 reduce_doc},
1056 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
1057 symmetric_difference_doc},
1058 {"union", (PyCFunction)set_union, METH_O,
1059 union_doc},
1060 {NULL, NULL} /* sentinel */
1061};
1062
1063static PyNumberMethods frozenset_as_number = {
1064 0, /*nb_add*/
1065 (binaryfunc)set_sub, /*nb_subtract*/
1066 0, /*nb_multiply*/
1067 0, /*nb_divide*/
1068 0, /*nb_remainder*/
1069 0, /*nb_divmod*/
1070 0, /*nb_power*/
1071 0, /*nb_negative*/
1072 0, /*nb_positive*/
1073 0, /*nb_absolute*/
1074 0, /*nb_nonzero*/
1075 0, /*nb_invert*/
1076 0, /*nb_lshift*/
1077 0, /*nb_rshift*/
1078 (binaryfunc)set_and, /*nb_and*/
1079 (binaryfunc)set_xor, /*nb_xor*/
1080 (binaryfunc)set_or, /*nb_or*/
1081};
1082
1083PyDoc_STRVAR(frozenset_doc,
1084"frozenset(iterable) --> frozenset object\n\
1085\n\
1086Build an immutable unordered collection.");
1087
1088PyTypeObject PyFrozenSet_Type = {
1089 PyObject_HEAD_INIT(&PyType_Type)
1090 0, /* ob_size */
1091 "frozenset", /* tp_name */
1092 sizeof(PySetObject), /* tp_basicsize */
Raymond Hettingera3b11e72003-12-31 14:08:58 +00001093 0, /* tp_itemsize */
1094 /* methods */
Raymond Hettingera690a992003-11-16 16:17:49 +00001095 (destructor)set_dealloc, /* tp_dealloc */
1096 (printfunc)set_tp_print, /* tp_print */
1097 0, /* tp_getattr */
1098 0, /* tp_setattr */
1099 (cmpfunc)set_nocmp, /* tp_compare */
1100 (reprfunc)set_repr, /* tp_repr */
1101 &frozenset_as_number, /* tp_as_number */
1102 &set_as_sequence, /* tp_as_sequence */
1103 0, /* tp_as_mapping */
1104 frozenset_hash, /* tp_hash */
1105 0, /* tp_call */
1106 0, /* tp_str */
1107 PyObject_GenericGetAttr, /* tp_getattro */
1108 0, /* tp_setattro */
1109 0, /* tp_as_buffer */
1110 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
Raymond Hettinger691d8052004-05-30 07:26:47 +00001111 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
Raymond Hettingera690a992003-11-16 16:17:49 +00001112 frozenset_doc, /* tp_doc */
1113 (traverseproc)set_traverse, /* tp_traverse */
1114 0, /* tp_clear */
1115 (richcmpfunc)set_richcompare, /* tp_richcompare */
Raymond Hettinger691d8052004-05-30 07:26:47 +00001116 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
Raymond Hettingera690a992003-11-16 16:17:49 +00001117 (getiterfunc)set_iter, /* tp_iter */
1118 0, /* tp_iternext */
1119 frozenset_methods, /* tp_methods */
1120 0, /* tp_members */
1121 0, /* tp_getset */
1122 0, /* tp_base */
1123 0, /* tp_dict */
1124 0, /* tp_descr_get */
1125 0, /* tp_descr_set */
1126 0, /* tp_dictoffset */
1127 0, /* tp_init */
1128 PyType_GenericAlloc, /* tp_alloc */
Raymond Hettinger50a4bb32003-11-17 16:42:33 +00001129 frozenset_new, /* tp_new */
Raymond Hettingera690a992003-11-16 16:17:49 +00001130 PyObject_GC_Del, /* tp_free */
1131};