Remove use of tuple unpacking and dict.has_key() so as to silence
[python.git] / Objects / setobject.c
blobfbbdf6ede39c63c90cab849629c89e3a0c368b79
2 /* set object implementation
3 Written and maintained by Raymond D. Hettinger <python@rcn.com>
4 Derived from Lib/sets.py and Objects/dictobject.c.
6 Copyright (c) 2003-2007 Python Software Foundation.
7 All rights reserved.
8 */
10 #include "Python.h"
11 #include "structmember.h"
13 /* Set a key error with the specified argument, wrapping it in a
14 * tuple automatically so that tuple keys are not unpacked as the
15 * exception arguments. */
16 static void
17 set_key_error(PyObject *arg)
19 PyObject *tup;
20 tup = PyTuple_Pack(1, arg);
21 if (!tup)
22 return; /* caller will expect error to be set anyway */
23 PyErr_SetObject(PyExc_KeyError, tup);
24 Py_DECREF(tup);
27 /* This must be >= 1. */
28 #define PERTURB_SHIFT 5
30 /* Object used as dummy key to fill deleted entries */
31 static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
33 #ifdef Py_REF_DEBUG
34 PyObject *
35 _PySet_Dummy(void)
37 return dummy;
39 #endif
41 #define INIT_NONZERO_SET_SLOTS(so) do { \
42 (so)->table = (so)->smalltable; \
43 (so)->mask = PySet_MINSIZE - 1; \
44 (so)->hash = -1; \
45 } while(0)
47 #define EMPTY_TO_MINSIZE(so) do { \
48 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
49 (so)->used = (so)->fill = 0; \
50 INIT_NONZERO_SET_SLOTS(so); \
51 } while(0)
53 /* Reuse scheme to save calls to malloc, free, and memset */
54 #ifndef PySet_MAXFREELIST
55 #define PySet_MAXFREELIST 80
56 #endif
57 static PySetObject *free_list[PySet_MAXFREELIST];
58 static int numfree = 0;
61 The basic lookup function used by all operations.
62 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
63 Open addressing is preferred over chaining since the link overhead for
64 chaining would be substantial (100% with typical malloc overhead).
66 The initial probe index is computed as hash mod the table size. Subsequent
67 probe indices are computed as explained in Objects/dictobject.c.
69 All arithmetic on hash should ignore overflow.
71 Unlike the dictionary implementation, the lookkey functions can return
72 NULL if the rich comparison returns an error.
75 static setentry *
76 set_lookkey(PySetObject *so, PyObject *key, register long hash)
78 register Py_ssize_t i;
79 register size_t perturb;
80 register setentry *freeslot;
81 register size_t mask = so->mask;
82 setentry *table = so->table;
83 register setentry *entry;
84 register int cmp;
85 PyObject *startkey;
87 i = hash & mask;
88 entry = &table[i];
89 if (entry->key == NULL || entry->key == key)
90 return entry;
92 if (entry->key == dummy)
93 freeslot = entry;
94 else {
95 if (entry->hash == hash) {
96 startkey = entry->key;
97 Py_INCREF(startkey);
98 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
99 Py_DECREF(startkey);
100 if (cmp < 0)
101 return NULL;
102 if (table == so->table && entry->key == startkey) {
103 if (cmp > 0)
104 return entry;
106 else {
107 /* The compare did major nasty stuff to the
108 * set: start over.
110 return set_lookkey(so, key, hash);
113 freeslot = NULL;
116 /* In the loop, key == dummy is by far (factor of 100s) the
117 least likely outcome, so test for that last. */
118 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
119 i = (i << 2) + i + perturb + 1;
120 entry = &table[i & mask];
121 if (entry->key == NULL) {
122 if (freeslot != NULL)
123 entry = freeslot;
124 break;
126 if (entry->key == key)
127 break;
128 if (entry->hash == hash && entry->key != dummy) {
129 startkey = entry->key;
130 Py_INCREF(startkey);
131 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
132 Py_DECREF(startkey);
133 if (cmp < 0)
134 return NULL;
135 if (table == so->table && entry->key == startkey) {
136 if (cmp > 0)
137 break;
139 else {
140 /* The compare did major nasty stuff to the
141 * set: start over.
143 return set_lookkey(so, key, hash);
146 else if (entry->key == dummy && freeslot == NULL)
147 freeslot = entry;
149 return entry;
153 * Hacked up version of set_lookkey which can assume keys are always strings;
154 * This means we can always use _PyString_Eq directly and not have to check to
155 * see if the comparison altered the table.
157 static setentry *
158 set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
160 register Py_ssize_t i;
161 register size_t perturb;
162 register setentry *freeslot;
163 register size_t mask = so->mask;
164 setentry *table = so->table;
165 register setentry *entry;
167 /* Make sure this function doesn't have to handle non-string keys,
168 including subclasses of str; e.g., one reason to subclass
169 strings is to override __eq__, and for speed we don't cater to
170 that here. */
171 if (!PyString_CheckExact(key)) {
172 so->lookup = set_lookkey;
173 return set_lookkey(so, key, hash);
175 i = hash & mask;
176 entry = &table[i];
177 if (entry->key == NULL || entry->key == key)
178 return entry;
179 if (entry->key == dummy)
180 freeslot = entry;
181 else {
182 if (entry->hash == hash && _PyString_Eq(entry->key, key))
183 return entry;
184 freeslot = NULL;
187 /* In the loop, key == dummy is by far (factor of 100s) the
188 least likely outcome, so test for that last. */
189 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
190 i = (i << 2) + i + perturb + 1;
191 entry = &table[i & mask];
192 if (entry->key == NULL)
193 return freeslot == NULL ? entry : freeslot;
194 if (entry->key == key
195 || (entry->hash == hash
196 && entry->key != dummy
197 && _PyString_Eq(entry->key, key)))
198 return entry;
199 if (entry->key == dummy && freeslot == NULL)
200 freeslot = entry;
202 assert(0); /* NOT REACHED */
203 return 0;
207 Internal routine to insert a new key into the table.
208 Used by the public insert routine.
209 Eats a reference to key.
211 static int
212 set_insert_key(register PySetObject *so, PyObject *key, long hash)
214 register setentry *entry;
215 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
217 assert(so->lookup != NULL);
218 entry = so->lookup(so, key, hash);
219 if (entry == NULL)
220 return -1;
221 if (entry->key == NULL) {
222 /* UNUSED */
223 so->fill++;
224 entry->key = key;
225 entry->hash = hash;
226 so->used++;
227 } else if (entry->key == dummy) {
228 /* DUMMY */
229 entry->key = key;
230 entry->hash = hash;
231 so->used++;
232 Py_DECREF(dummy);
233 } else {
234 /* ACTIVE */
235 Py_DECREF(key);
237 return 0;
241 Internal routine used by set_table_resize() to insert an item which is
242 known to be absent from the set. This routine also assumes that
243 the set contains no deleted entries. Besides the performance benefit,
244 using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
245 Note that no refcounts are changed by this routine; if needed, the caller
246 is responsible for incref'ing `key`.
248 static void
249 set_insert_clean(register PySetObject *so, PyObject *key, long hash)
251 register size_t i;
252 register size_t perturb;
253 register size_t mask = (size_t)so->mask;
254 setentry *table = so->table;
255 register setentry *entry;
257 i = hash & mask;
258 entry = &table[i];
259 for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
260 i = (i << 2) + i + perturb + 1;
261 entry = &table[i & mask];
263 so->fill++;
264 entry->key = key;
265 entry->hash = hash;
266 so->used++;
270 Restructure the table by allocating a new table and reinserting all
271 keys again. When entries have been deleted, the new table may
272 actually be smaller than the old one.
274 static int
275 set_table_resize(PySetObject *so, Py_ssize_t minused)
277 Py_ssize_t newsize;
278 setentry *oldtable, *newtable, *entry;
279 Py_ssize_t i;
280 int is_oldtable_malloced;
281 setentry small_copy[PySet_MINSIZE];
283 assert(minused >= 0);
285 /* Find the smallest table size > minused. */
286 for (newsize = PySet_MINSIZE;
287 newsize <= minused && newsize > 0;
288 newsize <<= 1)
290 if (newsize <= 0) {
291 PyErr_NoMemory();
292 return -1;
295 /* Get space for a new table. */
296 oldtable = so->table;
297 assert(oldtable != NULL);
298 is_oldtable_malloced = oldtable != so->smalltable;
300 if (newsize == PySet_MINSIZE) {
301 /* A large table is shrinking, or we can't get any smaller. */
302 newtable = so->smalltable;
303 if (newtable == oldtable) {
304 if (so->fill == so->used) {
305 /* No dummies, so no point doing anything. */
306 return 0;
308 /* We're not going to resize it, but rebuild the
309 table anyway to purge old dummy entries.
310 Subtle: This is *necessary* if fill==size,
311 as set_lookkey needs at least one virgin slot to
312 terminate failing searches. If fill < size, it's
313 merely desirable, as dummies slow searches. */
314 assert(so->fill > so->used);
315 memcpy(small_copy, oldtable, sizeof(small_copy));
316 oldtable = small_copy;
319 else {
320 newtable = PyMem_NEW(setentry, newsize);
321 if (newtable == NULL) {
322 PyErr_NoMemory();
323 return -1;
327 /* Make the set empty, using the new table. */
328 assert(newtable != oldtable);
329 so->table = newtable;
330 so->mask = newsize - 1;
331 memset(newtable, 0, sizeof(setentry) * newsize);
332 so->used = 0;
333 i = so->fill;
334 so->fill = 0;
336 /* Copy the data over; this is refcount-neutral for active entries;
337 dummy entries aren't copied over, of course */
338 for (entry = oldtable; i > 0; entry++) {
339 if (entry->key == NULL) {
340 /* UNUSED */
342 } else if (entry->key == dummy) {
343 /* DUMMY */
344 --i;
345 assert(entry->key == dummy);
346 Py_DECREF(entry->key);
347 } else {
348 /* ACTIVE */
349 --i;
350 set_insert_clean(so, entry->key, entry->hash);
354 if (is_oldtable_malloced)
355 PyMem_DEL(oldtable);
356 return 0;
359 /* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
361 static int
362 set_add_entry(register PySetObject *so, setentry *entry)
364 register Py_ssize_t n_used;
366 assert(so->fill <= so->mask); /* at least one empty slot */
367 n_used = so->used;
368 Py_INCREF(entry->key);
369 if (set_insert_key(so, entry->key, entry->hash) == -1) {
370 Py_DECREF(entry->key);
371 return -1;
373 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
374 return 0;
375 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
378 static int
379 set_add_key(register PySetObject *so, PyObject *key)
381 register long hash;
382 register Py_ssize_t n_used;
384 if (!PyString_CheckExact(key) ||
385 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
386 hash = PyObject_Hash(key);
387 if (hash == -1)
388 return -1;
390 assert(so->fill <= so->mask); /* at least one empty slot */
391 n_used = so->used;
392 Py_INCREF(key);
393 if (set_insert_key(so, key, hash) == -1) {
394 Py_DECREF(key);
395 return -1;
397 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
398 return 0;
399 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
402 #define DISCARD_NOTFOUND 0
403 #define DISCARD_FOUND 1
405 static int
406 set_discard_entry(PySetObject *so, setentry *oldentry)
407 { register setentry *entry;
408 PyObject *old_key;
410 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
411 if (entry == NULL)
412 return -1;
413 if (entry->key == NULL || entry->key == dummy)
414 return DISCARD_NOTFOUND;
415 old_key = entry->key;
416 Py_INCREF(dummy);
417 entry->key = dummy;
418 so->used--;
419 Py_DECREF(old_key);
420 return DISCARD_FOUND;
423 static int
424 set_discard_key(PySetObject *so, PyObject *key)
426 register long hash;
427 register setentry *entry;
428 PyObject *old_key;
430 assert (PyAnySet_Check(so));
431 if (!PyString_CheckExact(key) ||
432 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
433 hash = PyObject_Hash(key);
434 if (hash == -1)
435 return -1;
437 entry = (so->lookup)(so, key, hash);
438 if (entry == NULL)
439 return -1;
440 if (entry->key == NULL || entry->key == dummy)
441 return DISCARD_NOTFOUND;
442 old_key = entry->key;
443 Py_INCREF(dummy);
444 entry->key = dummy;
445 so->used--;
446 Py_DECREF(old_key);
447 return DISCARD_FOUND;
450 static int
451 set_clear_internal(PySetObject *so)
453 setentry *entry, *table;
454 int table_is_malloced;
455 Py_ssize_t fill;
456 setentry small_copy[PySet_MINSIZE];
457 #ifdef Py_DEBUG
458 Py_ssize_t i, n;
459 assert (PyAnySet_Check(so));
461 n = so->mask + 1;
462 i = 0;
463 #endif
465 table = so->table;
466 assert(table != NULL);
467 table_is_malloced = table != so->smalltable;
469 /* This is delicate. During the process of clearing the set,
470 * decrefs can cause the set to mutate. To avoid fatal confusion
471 * (voice of experience), we have to make the set empty before
472 * clearing the slots, and never refer to anything via so->ref while
473 * clearing.
475 fill = so->fill;
476 if (table_is_malloced)
477 EMPTY_TO_MINSIZE(so);
479 else if (fill > 0) {
480 /* It's a small table with something that needs to be cleared.
481 * Afraid the only safe way is to copy the set entries into
482 * another small table first.
484 memcpy(small_copy, table, sizeof(small_copy));
485 table = small_copy;
486 EMPTY_TO_MINSIZE(so);
488 /* else it's a small table that's already empty */
490 /* Now we can finally clear things. If C had refcounts, we could
491 * assert that the refcount on table is 1 now, i.e. that this function
492 * has unique access to it, so decref side-effects can't alter it.
494 for (entry = table; fill > 0; ++entry) {
495 #ifdef Py_DEBUG
496 assert(i < n);
497 ++i;
498 #endif
499 if (entry->key) {
500 --fill;
501 Py_DECREF(entry->key);
503 #ifdef Py_DEBUG
504 else
505 assert(entry->key == NULL);
506 #endif
509 if (table_is_malloced)
510 PyMem_DEL(table);
511 return 0;
515 * Iterate over a set table. Use like so:
517 * Py_ssize_t pos;
518 * setentry *entry;
519 * pos = 0; # important! pos should not otherwise be changed by you
520 * while (set_next(yourset, &pos, &entry)) {
521 * Refer to borrowed reference in entry->key.
524 * CAUTION: In general, it isn't safe to use set_next in a loop that
525 * mutates the table.
527 static int
528 set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
530 Py_ssize_t i;
531 Py_ssize_t mask;
532 register setentry *table;
534 assert (PyAnySet_Check(so));
535 i = *pos_ptr;
536 assert(i >= 0);
537 table = so->table;
538 mask = so->mask;
539 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
540 i++;
541 *pos_ptr = i+1;
542 if (i > mask)
543 return 0;
544 assert(table[i].key != NULL);
545 *entry_ptr = &table[i];
546 return 1;
549 static void
550 set_dealloc(PySetObject *so)
552 register setentry *entry;
553 Py_ssize_t fill = so->fill;
554 PyObject_GC_UnTrack(so);
555 Py_TRASHCAN_SAFE_BEGIN(so)
556 if (so->weakreflist != NULL)
557 PyObject_ClearWeakRefs((PyObject *) so);
559 for (entry = so->table; fill > 0; entry++) {
560 if (entry->key) {
561 --fill;
562 Py_DECREF(entry->key);
565 if (so->table != so->smalltable)
566 PyMem_DEL(so->table);
567 if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
568 free_list[numfree++] = so;
569 else
570 Py_TYPE(so)->tp_free(so);
571 Py_TRASHCAN_SAFE_END(so)
574 static int
575 set_tp_print(PySetObject *so, FILE *fp, int flags)
577 setentry *entry;
578 Py_ssize_t pos=0;
579 char *emit = ""; /* No separator emitted on first pass */
580 char *separator = ", ";
581 int status = Py_ReprEnter((PyObject*)so);
583 if (status != 0) {
584 if (status < 0)
585 return status;
586 Py_BEGIN_ALLOW_THREADS
587 fprintf(fp, "%s(...)", so->ob_type->tp_name);
588 Py_END_ALLOW_THREADS
589 return 0;
592 Py_BEGIN_ALLOW_THREADS
593 fprintf(fp, "%s([", so->ob_type->tp_name);
594 Py_END_ALLOW_THREADS
595 while (set_next(so, &pos, &entry)) {
596 Py_BEGIN_ALLOW_THREADS
597 fputs(emit, fp);
598 Py_END_ALLOW_THREADS
599 emit = separator;
600 if (PyObject_Print(entry->key, fp, 0) != 0) {
601 Py_ReprLeave((PyObject*)so);
602 return -1;
605 Py_BEGIN_ALLOW_THREADS
606 fputs("])", fp);
607 Py_END_ALLOW_THREADS
608 Py_ReprLeave((PyObject*)so);
609 return 0;
612 static PyObject *
613 set_repr(PySetObject *so)
615 PyObject *keys, *result=NULL, *listrepr;
616 int status = Py_ReprEnter((PyObject*)so);
618 if (status != 0) {
619 if (status < 0)
620 return NULL;
621 return PyString_FromFormat("%s(...)", so->ob_type->tp_name);
624 keys = PySequence_List((PyObject *)so);
625 if (keys == NULL)
626 goto done;
627 listrepr = PyObject_Repr(keys);
628 Py_DECREF(keys);
629 if (listrepr == NULL)
630 goto done;
632 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
633 PyString_AS_STRING(listrepr));
634 Py_DECREF(listrepr);
635 done:
636 Py_ReprLeave((PyObject*)so);
637 return result;
640 static Py_ssize_t
641 set_len(PyObject *so)
643 return ((PySetObject *)so)->used;
646 static int
647 set_merge(PySetObject *so, PyObject *otherset)
649 PySetObject *other;
650 register Py_ssize_t i;
651 register setentry *entry;
653 assert (PyAnySet_Check(so));
654 assert (PyAnySet_Check(otherset));
656 other = (PySetObject*)otherset;
657 if (other == so || other->used == 0)
658 /* a.update(a) or a.update({}); nothing to do */
659 return 0;
660 /* Do one big resize at the start, rather than
661 * incrementally resizing as we insert new keys. Expect
662 * that there will be no (or few) overlapping keys.
664 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
665 if (set_table_resize(so, (so->used + other->used)*2) != 0)
666 return -1;
668 for (i = 0; i <= other->mask; i++) {
669 entry = &other->table[i];
670 if (entry->key != NULL &&
671 entry->key != dummy) {
672 Py_INCREF(entry->key);
673 if (set_insert_key(so, entry->key, entry->hash) == -1) {
674 Py_DECREF(entry->key);
675 return -1;
679 return 0;
682 static int
683 set_contains_key(PySetObject *so, PyObject *key)
685 long hash;
686 setentry *entry;
688 if (!PyString_CheckExact(key) ||
689 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
690 hash = PyObject_Hash(key);
691 if (hash == -1)
692 return -1;
694 entry = (so->lookup)(so, key, hash);
695 if (entry == NULL)
696 return -1;
697 key = entry->key;
698 return key != NULL && key != dummy;
701 static int
702 set_contains_entry(PySetObject *so, setentry *entry)
704 PyObject *key;
705 setentry *lu_entry;
707 lu_entry = (so->lookup)(so, entry->key, entry->hash);
708 if (lu_entry == NULL)
709 return -1;
710 key = lu_entry->key;
711 return key != NULL && key != dummy;
714 static PyObject *
715 set_pop(PySetObject *so)
717 register Py_ssize_t i = 0;
718 register setentry *entry;
719 PyObject *key;
721 assert (PyAnySet_Check(so));
722 if (so->used == 0) {
723 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
724 return NULL;
727 /* Set entry to "the first" unused or dummy set entry. We abuse
728 * the hash field of slot 0 to hold a search finger:
729 * If slot 0 has a value, use slot 0.
730 * Else slot 0 is being used to hold a search finger,
731 * and we use its hash value as the first index to look.
733 entry = &so->table[0];
734 if (entry->key == NULL || entry->key == dummy) {
735 i = entry->hash;
736 /* The hash field may be a real hash value, or it may be a
737 * legit search finger, or it may be a once-legit search
738 * finger that's out of bounds now because it wrapped around
739 * or the table shrunk -- simply make sure it's in bounds now.
741 if (i > so->mask || i < 1)
742 i = 1; /* skip slot 0 */
743 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
744 i++;
745 if (i > so->mask)
746 i = 1;
749 key = entry->key;
750 Py_INCREF(dummy);
751 entry->key = dummy;
752 so->used--;
753 so->table[0].hash = i + 1; /* next place to start */
754 return key;
757 PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
759 static int
760 set_traverse(PySetObject *so, visitproc visit, void *arg)
762 Py_ssize_t pos = 0;
763 setentry *entry;
765 while (set_next(so, &pos, &entry))
766 Py_VISIT(entry->key);
767 return 0;
770 static long
771 frozenset_hash(PyObject *self)
773 PySetObject *so = (PySetObject *)self;
774 long h, hash = 1927868237L;
775 setentry *entry;
776 Py_ssize_t pos = 0;
778 if (so->hash != -1)
779 return so->hash;
781 hash *= PySet_GET_SIZE(self) + 1;
782 while (set_next(so, &pos, &entry)) {
783 /* Work to increase the bit dispersion for closely spaced hash
784 values. The is important because some use cases have many
785 combinations of a small number of elements with nearby
786 hashes so that many distinct combinations collapse to only
787 a handful of distinct hash values. */
788 h = entry->hash;
789 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
791 hash = hash * 69069L + 907133923L;
792 if (hash == -1)
793 hash = 590923713L;
794 so->hash = hash;
795 return hash;
798 /***** Set iterator type ***********************************************/
800 typedef struct {
801 PyObject_HEAD
802 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
803 Py_ssize_t si_used;
804 Py_ssize_t si_pos;
805 Py_ssize_t len;
806 } setiterobject;
808 static void
809 setiter_dealloc(setiterobject *si)
811 Py_XDECREF(si->si_set);
812 PyObject_Del(si);
815 static PyObject *
816 setiter_len(setiterobject *si)
818 Py_ssize_t len = 0;
819 if (si->si_set != NULL && si->si_used == si->si_set->used)
820 len = si->len;
821 return PyInt_FromLong(len);
824 PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
826 static PyMethodDef setiter_methods[] = {
827 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
828 {NULL, NULL} /* sentinel */
831 static PyObject *setiter_iternext(setiterobject *si)
833 PyObject *key;
834 register Py_ssize_t i, mask;
835 register setentry *entry;
836 PySetObject *so = si->si_set;
838 if (so == NULL)
839 return NULL;
840 assert (PyAnySet_Check(so));
842 if (si->si_used != so->used) {
843 PyErr_SetString(PyExc_RuntimeError,
844 "Set changed size during iteration");
845 si->si_used = -1; /* Make this state sticky */
846 return NULL;
849 i = si->si_pos;
850 assert(i>=0);
851 entry = so->table;
852 mask = so->mask;
853 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
854 i++;
855 si->si_pos = i+1;
856 if (i > mask)
857 goto fail;
858 si->len--;
859 key = entry[i].key;
860 Py_INCREF(key);
861 return key;
863 fail:
864 Py_DECREF(so);
865 si->si_set = NULL;
866 return NULL;
869 static PyTypeObject PySetIter_Type = {
870 PyVarObject_HEAD_INIT(&PyType_Type, 0)
871 "setiterator", /* tp_name */
872 sizeof(setiterobject), /* tp_basicsize */
873 0, /* tp_itemsize */
874 /* methods */
875 (destructor)setiter_dealloc, /* tp_dealloc */
876 0, /* tp_print */
877 0, /* tp_getattr */
878 0, /* tp_setattr */
879 0, /* tp_compare */
880 0, /* tp_repr */
881 0, /* tp_as_number */
882 0, /* tp_as_sequence */
883 0, /* tp_as_mapping */
884 0, /* tp_hash */
885 0, /* tp_call */
886 0, /* tp_str */
887 PyObject_GenericGetAttr, /* tp_getattro */
888 0, /* tp_setattro */
889 0, /* tp_as_buffer */
890 Py_TPFLAGS_DEFAULT, /* tp_flags */
891 0, /* tp_doc */
892 0, /* tp_traverse */
893 0, /* tp_clear */
894 0, /* tp_richcompare */
895 0, /* tp_weaklistoffset */
896 PyObject_SelfIter, /* tp_iter */
897 (iternextfunc)setiter_iternext, /* tp_iternext */
898 setiter_methods, /* tp_methods */
902 static PyObject *
903 set_iter(PySetObject *so)
905 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
906 if (si == NULL)
907 return NULL;
908 Py_INCREF(so);
909 si->si_set = so;
910 si->si_used = so->used;
911 si->si_pos = 0;
912 si->len = so->used;
913 return (PyObject *)si;
916 static int
917 set_update_internal(PySetObject *so, PyObject *other)
919 PyObject *key, *it;
921 if (PyAnySet_Check(other))
922 return set_merge(so, other);
924 if (PyDict_CheckExact(other)) {
925 PyObject *value;
926 Py_ssize_t pos = 0;
927 long hash;
928 Py_ssize_t dictsize = PyDict_Size(other);
930 /* Do one big resize at the start, rather than
931 * incrementally resizing as we insert new keys. Expect
932 * that there will be no (or few) overlapping keys.
934 if (dictsize == -1)
935 return -1;
936 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
937 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
938 return -1;
940 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
941 setentry an_entry;
943 an_entry.hash = hash;
944 an_entry.key = key;
945 if (set_add_entry(so, &an_entry) == -1)
946 return -1;
948 return 0;
951 it = PyObject_GetIter(other);
952 if (it == NULL)
953 return -1;
955 while ((key = PyIter_Next(it)) != NULL) {
956 if (set_add_key(so, key) == -1) {
957 Py_DECREF(it);
958 Py_DECREF(key);
959 return -1;
961 Py_DECREF(key);
963 Py_DECREF(it);
964 if (PyErr_Occurred())
965 return -1;
966 return 0;
969 static PyObject *
970 set_update(PySetObject *so, PyObject *args)
972 Py_ssize_t i;
974 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
975 PyObject *other = PyTuple_GET_ITEM(args, i);
976 if (set_update_internal(so, other) == -1)
977 return NULL;
979 Py_RETURN_NONE;
982 PyDoc_STRVAR(update_doc,
983 "Update a set with the union of itself and others.");
985 static PyObject *
986 make_new_set(PyTypeObject *type, PyObject *iterable)
988 register PySetObject *so = NULL;
990 if (dummy == NULL) { /* Auto-initialize dummy */
991 dummy = PyString_FromString("<dummy key>");
992 if (dummy == NULL)
993 return NULL;
996 /* create PySetObject structure */
997 if (numfree &&
998 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
999 so = free_list[--numfree];
1000 assert (so != NULL && PyAnySet_CheckExact(so));
1001 Py_TYPE(so) = type;
1002 _Py_NewReference((PyObject *)so);
1003 EMPTY_TO_MINSIZE(so);
1004 PyObject_GC_Track(so);
1005 } else {
1006 so = (PySetObject *)type->tp_alloc(type, 0);
1007 if (so == NULL)
1008 return NULL;
1009 /* tp_alloc has already zeroed the structure */
1010 assert(so->table == NULL && so->fill == 0 && so->used == 0);
1011 INIT_NONZERO_SET_SLOTS(so);
1014 so->lookup = set_lookkey_string;
1015 so->weakreflist = NULL;
1017 if (iterable != NULL) {
1018 if (set_update_internal(so, iterable) == -1) {
1019 Py_DECREF(so);
1020 return NULL;
1024 return (PyObject *)so;
1027 /* The empty frozenset is a singleton */
1028 static PyObject *emptyfrozenset = NULL;
1030 static PyObject *
1031 frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1033 PyObject *iterable = NULL, *result;
1035 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1036 return NULL;
1038 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1039 return NULL;
1041 if (type != &PyFrozenSet_Type)
1042 return make_new_set(type, iterable);
1044 if (iterable != NULL) {
1045 /* frozenset(f) is idempotent */
1046 if (PyFrozenSet_CheckExact(iterable)) {
1047 Py_INCREF(iterable);
1048 return iterable;
1050 result = make_new_set(type, iterable);
1051 if (result == NULL || PySet_GET_SIZE(result))
1052 return result;
1053 Py_DECREF(result);
1055 /* The empty frozenset is a singleton */
1056 if (emptyfrozenset == NULL)
1057 emptyfrozenset = make_new_set(type, NULL);
1058 Py_XINCREF(emptyfrozenset);
1059 return emptyfrozenset;
1062 void
1063 PySet_Fini(void)
1065 PySetObject *so;
1067 while (numfree) {
1068 numfree--;
1069 so = free_list[numfree];
1070 PyObject_GC_Del(so);
1072 Py_CLEAR(dummy);
1073 Py_CLEAR(emptyfrozenset);
1076 static PyObject *
1077 set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1079 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1080 return NULL;
1082 return make_new_set(type, NULL);
1085 /* set_swap_bodies() switches the contents of any two sets by moving their
1086 internal data pointers and, if needed, copying the internal smalltables.
1087 Semantically equivalent to:
1089 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1091 The function always succeeds and it leaves both objects in a stable state.
1092 Useful for creating temporary frozensets from sets for membership testing
1093 in __contains__(), discard(), and remove(). Also useful for operations
1094 that update in-place (by allowing an intermediate result to be swapped
1095 into one of the original inputs).
1098 static void
1099 set_swap_bodies(PySetObject *a, PySetObject *b)
1101 Py_ssize_t t;
1102 setentry *u;
1103 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
1104 setentry tab[PySet_MINSIZE];
1105 long h;
1107 t = a->fill; a->fill = b->fill; b->fill = t;
1108 t = a->used; a->used = b->used; b->used = t;
1109 t = a->mask; a->mask = b->mask; b->mask = t;
1111 u = a->table;
1112 if (a->table == a->smalltable)
1113 u = b->smalltable;
1114 a->table = b->table;
1115 if (b->table == b->smalltable)
1116 a->table = a->smalltable;
1117 b->table = u;
1119 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1121 if (a->table == a->smalltable || b->table == b->smalltable) {
1122 memcpy(tab, a->smalltable, sizeof(tab));
1123 memcpy(a->smalltable, b->smalltable, sizeof(tab));
1124 memcpy(b->smalltable, tab, sizeof(tab));
1127 if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
1128 PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1129 h = a->hash; a->hash = b->hash; b->hash = h;
1130 } else {
1131 a->hash = -1;
1132 b->hash = -1;
1136 static PyObject *
1137 set_copy(PySetObject *so)
1139 return make_new_set(Py_TYPE(so), (PyObject *)so);
1142 static PyObject *
1143 frozenset_copy(PySetObject *so)
1145 if (PyFrozenSet_CheckExact(so)) {
1146 Py_INCREF(so);
1147 return (PyObject *)so;
1149 return set_copy(so);
1152 PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
1154 static PyObject *
1155 set_clear(PySetObject *so)
1157 set_clear_internal(so);
1158 Py_RETURN_NONE;
1161 PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
1163 static PyObject *
1164 set_union(PySetObject *so, PyObject *args)
1166 PySetObject *result;
1167 PyObject *other;
1168 Py_ssize_t i;
1170 result = (PySetObject *)set_copy(so);
1171 if (result == NULL)
1172 return NULL;
1174 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1175 other = PyTuple_GET_ITEM(args, i);
1176 if ((PyObject *)so == other)
1177 return (PyObject *)result;
1178 if (set_update_internal(result, other) == -1) {
1179 Py_DECREF(result);
1180 return NULL;
1183 return (PyObject *)result;
1186 PyDoc_STRVAR(union_doc,
1187 "Return the union of sets as a new set.\n\
1189 (i.e. all elements that are in either set.)");
1191 static PyObject *
1192 set_or(PySetObject *so, PyObject *other)
1194 PySetObject *result;
1196 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1197 Py_INCREF(Py_NotImplemented);
1198 return Py_NotImplemented;
1201 result = (PySetObject *)set_copy(so);
1202 if (result == NULL)
1203 return NULL;
1204 if ((PyObject *)so == other)
1205 return (PyObject *)result;
1206 if (set_update_internal(result, other) == -1) {
1207 Py_DECREF(result);
1208 return NULL;
1210 return (PyObject *)result;
1213 static PyObject *
1214 set_ior(PySetObject *so, PyObject *other)
1216 if (!PyAnySet_Check(other)) {
1217 Py_INCREF(Py_NotImplemented);
1218 return Py_NotImplemented;
1220 if (set_update_internal(so, other) == -1)
1221 return NULL;
1222 Py_INCREF(so);
1223 return (PyObject *)so;
1226 static PyObject *
1227 set_intersection(PySetObject *so, PyObject *other)
1229 PySetObject *result;
1230 PyObject *key, *it, *tmp;
1232 if ((PyObject *)so == other)
1233 return set_copy(so);
1235 result = (PySetObject *)make_new_set(Py_TYPE(so), NULL);
1236 if (result == NULL)
1237 return NULL;
1239 if (PyAnySet_Check(other)) {
1240 Py_ssize_t pos = 0;
1241 setentry *entry;
1243 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1244 tmp = (PyObject *)so;
1245 so = (PySetObject *)other;
1246 other = tmp;
1249 while (set_next((PySetObject *)other, &pos, &entry)) {
1250 int rv = set_contains_entry(so, entry);
1251 if (rv == -1) {
1252 Py_DECREF(result);
1253 return NULL;
1255 if (rv) {
1256 if (set_add_entry(result, entry) == -1) {
1257 Py_DECREF(result);
1258 return NULL;
1262 return (PyObject *)result;
1265 it = PyObject_GetIter(other);
1266 if (it == NULL) {
1267 Py_DECREF(result);
1268 return NULL;
1271 while ((key = PyIter_Next(it)) != NULL) {
1272 int rv;
1273 setentry entry;
1274 long hash = PyObject_Hash(key);
1276 if (hash == -1) {
1277 Py_DECREF(it);
1278 Py_DECREF(result);
1279 Py_DECREF(key);
1280 return NULL;
1282 entry.hash = hash;
1283 entry.key = key;
1284 rv = set_contains_entry(so, &entry);
1285 if (rv == -1) {
1286 Py_DECREF(it);
1287 Py_DECREF(result);
1288 Py_DECREF(key);
1289 return NULL;
1291 if (rv) {
1292 if (set_add_entry(result, &entry) == -1) {
1293 Py_DECREF(it);
1294 Py_DECREF(result);
1295 Py_DECREF(key);
1296 return NULL;
1299 Py_DECREF(key);
1301 Py_DECREF(it);
1302 if (PyErr_Occurred()) {
1303 Py_DECREF(result);
1304 return NULL;
1306 return (PyObject *)result;
1309 static PyObject *
1310 set_intersection_multi(PySetObject *so, PyObject *args)
1312 Py_ssize_t i;
1313 PyObject *result = (PyObject *)so;
1315 if (PyTuple_GET_SIZE(args) == 0)
1316 return set_copy(so);
1318 Py_INCREF(so);
1319 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1320 PyObject *other = PyTuple_GET_ITEM(args, i);
1321 PyObject *newresult = set_intersection((PySetObject *)result, other);
1322 if (newresult == NULL) {
1323 Py_DECREF(result);
1324 return NULL;
1326 Py_DECREF(result);
1327 result = newresult;
1329 return result;
1332 PyDoc_STRVAR(intersection_doc,
1333 "Return the intersection of two sets as a new set.\n\
1335 (i.e. all elements that are in both sets.)");
1337 static PyObject *
1338 set_intersection_update(PySetObject *so, PyObject *other)
1340 PyObject *tmp;
1342 tmp = set_intersection(so, other);
1343 if (tmp == NULL)
1344 return NULL;
1345 set_swap_bodies(so, (PySetObject *)tmp);
1346 Py_DECREF(tmp);
1347 Py_RETURN_NONE;
1350 static PyObject *
1351 set_intersection_update_multi(PySetObject *so, PyObject *args)
1353 PyObject *tmp;
1355 tmp = set_intersection_multi(so, args);
1356 if (tmp == NULL)
1357 return NULL;
1358 set_swap_bodies(so, (PySetObject *)tmp);
1359 Py_DECREF(tmp);
1360 Py_RETURN_NONE;
1363 PyDoc_STRVAR(intersection_update_doc,
1364 "Update a set with the intersection of itself and another.");
1366 static PyObject *
1367 set_and(PySetObject *so, PyObject *other)
1369 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1370 Py_INCREF(Py_NotImplemented);
1371 return Py_NotImplemented;
1373 return set_intersection(so, other);
1376 static PyObject *
1377 set_iand(PySetObject *so, PyObject *other)
1379 PyObject *result;
1381 if (!PyAnySet_Check(other)) {
1382 Py_INCREF(Py_NotImplemented);
1383 return Py_NotImplemented;
1385 result = set_intersection_update(so, other);
1386 if (result == NULL)
1387 return NULL;
1388 Py_DECREF(result);
1389 Py_INCREF(so);
1390 return (PyObject *)so;
1393 static PyObject *
1394 set_isdisjoint(PySetObject *so, PyObject *other)
1396 PyObject *key, *it, *tmp;
1398 if ((PyObject *)so == other) {
1399 if (PySet_GET_SIZE(so) == 0)
1400 Py_RETURN_TRUE;
1401 else
1402 Py_RETURN_FALSE;
1405 if (PyAnySet_CheckExact(other)) {
1406 Py_ssize_t pos = 0;
1407 setentry *entry;
1409 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1410 tmp = (PyObject *)so;
1411 so = (PySetObject *)other;
1412 other = tmp;
1414 while (set_next((PySetObject *)other, &pos, &entry)) {
1415 int rv = set_contains_entry(so, entry);
1416 if (rv == -1)
1417 return NULL;
1418 if (rv)
1419 Py_RETURN_FALSE;
1421 Py_RETURN_TRUE;
1424 it = PyObject_GetIter(other);
1425 if (it == NULL)
1426 return NULL;
1428 while ((key = PyIter_Next(it)) != NULL) {
1429 int rv;
1430 setentry entry;
1431 long hash = PyObject_Hash(key);
1433 if (hash == -1) {
1434 Py_DECREF(key);
1435 Py_DECREF(it);
1436 return NULL;
1438 entry.hash = hash;
1439 entry.key = key;
1440 rv = set_contains_entry(so, &entry);
1441 Py_DECREF(key);
1442 if (rv == -1) {
1443 Py_DECREF(it);
1444 return NULL;
1446 if (rv) {
1447 Py_DECREF(it);
1448 Py_RETURN_FALSE;
1451 Py_DECREF(it);
1452 if (PyErr_Occurred())
1453 return NULL;
1454 Py_RETURN_TRUE;
1457 PyDoc_STRVAR(isdisjoint_doc,
1458 "Return True if two sets have a null intersection.");
1460 static int
1461 set_difference_update_internal(PySetObject *so, PyObject *other)
1463 if ((PyObject *)so == other)
1464 return set_clear_internal(so);
1466 if (PyAnySet_Check(other)) {
1467 setentry *entry;
1468 Py_ssize_t pos = 0;
1470 while (set_next((PySetObject *)other, &pos, &entry))
1471 if (set_discard_entry(so, entry) == -1)
1472 return -1;
1473 } else {
1474 PyObject *key, *it;
1475 it = PyObject_GetIter(other);
1476 if (it == NULL)
1477 return -1;
1479 while ((key = PyIter_Next(it)) != NULL) {
1480 if (set_discard_key(so, key) == -1) {
1481 Py_DECREF(it);
1482 Py_DECREF(key);
1483 return -1;
1485 Py_DECREF(key);
1487 Py_DECREF(it);
1488 if (PyErr_Occurred())
1489 return -1;
1491 /* If more than 1/5 are dummies, then resize them away. */
1492 if ((so->fill - so->used) * 5 < so->mask)
1493 return 0;
1494 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1497 static PyObject *
1498 set_difference_update(PySetObject *so, PyObject *args)
1500 Py_ssize_t i;
1502 for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
1503 PyObject *other = PyTuple_GET_ITEM(args, i);
1504 if (set_difference_update_internal(so, other) == -1)
1505 return NULL;
1507 Py_RETURN_NONE;
1510 PyDoc_STRVAR(difference_update_doc,
1511 "Remove all elements of another set from this set.");
1513 static PyObject *
1514 set_difference(PySetObject *so, PyObject *other)
1516 PyObject *result;
1517 setentry *entry;
1518 Py_ssize_t pos = 0;
1520 if (!PyAnySet_Check(other) && !PyDict_CheckExact(other)) {
1521 result = set_copy(so);
1522 if (result == NULL)
1523 return NULL;
1524 if (set_difference_update_internal((PySetObject *)result, other) != -1)
1525 return result;
1526 Py_DECREF(result);
1527 return NULL;
1530 result = make_new_set(Py_TYPE(so), NULL);
1531 if (result == NULL)
1532 return NULL;
1534 if (PyDict_CheckExact(other)) {
1535 while (set_next(so, &pos, &entry)) {
1536 setentry entrycopy;
1537 entrycopy.hash = entry->hash;
1538 entrycopy.key = entry->key;
1539 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
1540 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1541 Py_DECREF(result);
1542 return NULL;
1546 return result;
1549 while (set_next(so, &pos, &entry)) {
1550 int rv = set_contains_entry((PySetObject *)other, entry);
1551 if (rv == -1) {
1552 Py_DECREF(result);
1553 return NULL;
1555 if (!rv) {
1556 if (set_add_entry((PySetObject *)result, entry) == -1) {
1557 Py_DECREF(result);
1558 return NULL;
1562 return result;
1565 static PyObject *
1566 set_difference_multi(PySetObject *so, PyObject *args)
1568 Py_ssize_t i;
1569 PyObject *result, *other;
1571 if (PyTuple_GET_SIZE(args) == 0)
1572 return set_copy(so);
1574 other = PyTuple_GET_ITEM(args, 0);
1575 result = set_difference(so, other);
1576 if (result == NULL)
1577 return NULL;
1579 for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
1580 other = PyTuple_GET_ITEM(args, i);
1581 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1582 Py_DECREF(result);
1583 return NULL;
1586 return result;
1589 PyDoc_STRVAR(difference_doc,
1590 "Return the difference of two or more sets as a new set.\n\
1592 (i.e. all elements that are in this set but not the others.)");
1593 static PyObject *
1594 set_sub(PySetObject *so, PyObject *other)
1596 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1597 Py_INCREF(Py_NotImplemented);
1598 return Py_NotImplemented;
1600 return set_difference(so, other);
1603 static PyObject *
1604 set_isub(PySetObject *so, PyObject *other)
1606 if (!PyAnySet_Check(other)) {
1607 Py_INCREF(Py_NotImplemented);
1608 return Py_NotImplemented;
1610 if (set_difference_update_internal(so, other) == -1)
1611 return NULL;
1612 Py_INCREF(so);
1613 return (PyObject *)so;
1616 static PyObject *
1617 set_symmetric_difference_update(PySetObject *so, PyObject *other)
1619 PySetObject *otherset;
1620 PyObject *key;
1621 Py_ssize_t pos = 0;
1622 setentry *entry;
1624 if ((PyObject *)so == other)
1625 return set_clear(so);
1627 if (PyDict_CheckExact(other)) {
1628 PyObject *value;
1629 int rv;
1630 long hash;
1631 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1632 setentry an_entry;
1634 an_entry.hash = hash;
1635 an_entry.key = key;
1636 rv = set_discard_entry(so, &an_entry);
1637 if (rv == -1)
1638 return NULL;
1639 if (rv == DISCARD_NOTFOUND) {
1640 if (set_add_entry(so, &an_entry) == -1)
1641 return NULL;
1644 Py_RETURN_NONE;
1647 if (PyAnySet_Check(other)) {
1648 Py_INCREF(other);
1649 otherset = (PySetObject *)other;
1650 } else {
1651 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
1652 if (otherset == NULL)
1653 return NULL;
1656 while (set_next(otherset, &pos, &entry)) {
1657 int rv = set_discard_entry(so, entry);
1658 if (rv == -1) {
1659 Py_DECREF(otherset);
1660 return NULL;
1662 if (rv == DISCARD_NOTFOUND) {
1663 if (set_add_entry(so, entry) == -1) {
1664 Py_DECREF(otherset);
1665 return NULL;
1669 Py_DECREF(otherset);
1670 Py_RETURN_NONE;
1673 PyDoc_STRVAR(symmetric_difference_update_doc,
1674 "Update a set with the symmetric difference of itself and another.");
1676 static PyObject *
1677 set_symmetric_difference(PySetObject *so, PyObject *other)
1679 PyObject *rv;
1680 PySetObject *otherset;
1682 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
1683 if (otherset == NULL)
1684 return NULL;
1685 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1686 if (rv == NULL)
1687 return NULL;
1688 Py_DECREF(rv);
1689 return (PyObject *)otherset;
1692 PyDoc_STRVAR(symmetric_difference_doc,
1693 "Return the symmetric difference of two sets as a new set.\n\
1695 (i.e. all elements that are in exactly one of the sets.)");
1697 static PyObject *
1698 set_xor(PySetObject *so, PyObject *other)
1700 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
1701 Py_INCREF(Py_NotImplemented);
1702 return Py_NotImplemented;
1704 return set_symmetric_difference(so, other);
1707 static PyObject *
1708 set_ixor(PySetObject *so, PyObject *other)
1710 PyObject *result;
1712 if (!PyAnySet_Check(other)) {
1713 Py_INCREF(Py_NotImplemented);
1714 return Py_NotImplemented;
1716 result = set_symmetric_difference_update(so, other);
1717 if (result == NULL)
1718 return NULL;
1719 Py_DECREF(result);
1720 Py_INCREF(so);
1721 return (PyObject *)so;
1724 static PyObject *
1725 set_issubset(PySetObject *so, PyObject *other)
1727 setentry *entry;
1728 Py_ssize_t pos = 0;
1730 if (!PyAnySet_Check(other)) {
1731 PyObject *tmp, *result;
1732 tmp = make_new_set(&PySet_Type, other);
1733 if (tmp == NULL)
1734 return NULL;
1735 result = set_issubset(so, tmp);
1736 Py_DECREF(tmp);
1737 return result;
1739 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
1740 Py_RETURN_FALSE;
1742 while (set_next(so, &pos, &entry)) {
1743 int rv = set_contains_entry((PySetObject *)other, entry);
1744 if (rv == -1)
1745 return NULL;
1746 if (!rv)
1747 Py_RETURN_FALSE;
1749 Py_RETURN_TRUE;
1752 PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
1754 static PyObject *
1755 set_issuperset(PySetObject *so, PyObject *other)
1757 PyObject *tmp, *result;
1759 if (!PyAnySet_Check(other)) {
1760 tmp = make_new_set(&PySet_Type, other);
1761 if (tmp == NULL)
1762 return NULL;
1763 result = set_issuperset(so, tmp);
1764 Py_DECREF(tmp);
1765 return result;
1767 return set_issubset((PySetObject *)other, (PyObject *)so);
1770 PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
1772 static PyObject *
1773 set_richcompare(PySetObject *v, PyObject *w, int op)
1775 PyObject *r1, *r2;
1777 if(!PyAnySet_Check(w)) {
1778 if (op == Py_EQ)
1779 Py_RETURN_FALSE;
1780 if (op == Py_NE)
1781 Py_RETURN_TRUE;
1782 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
1783 return NULL;
1785 switch (op) {
1786 case Py_EQ:
1787 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
1788 Py_RETURN_FALSE;
1789 if (v->hash != -1 &&
1790 ((PySetObject *)w)->hash != -1 &&
1791 v->hash != ((PySetObject *)w)->hash)
1792 Py_RETURN_FALSE;
1793 return set_issubset(v, w);
1794 case Py_NE:
1795 r1 = set_richcompare(v, w, Py_EQ);
1796 if (r1 == NULL)
1797 return NULL;
1798 r2 = PyBool_FromLong(PyObject_Not(r1));
1799 Py_DECREF(r1);
1800 return r2;
1801 case Py_LE:
1802 return set_issubset(v, w);
1803 case Py_GE:
1804 return set_issuperset(v, w);
1805 case Py_LT:
1806 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
1807 Py_RETURN_FALSE;
1808 return set_issubset(v, w);
1809 case Py_GT:
1810 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
1811 Py_RETURN_FALSE;
1812 return set_issuperset(v, w);
1814 Py_INCREF(Py_NotImplemented);
1815 return Py_NotImplemented;
1818 static int
1819 set_nocmp(PyObject *self, PyObject *other)
1821 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
1822 return -1;
1825 static PyObject *
1826 set_add(PySetObject *so, PyObject *key)
1828 if (set_add_key(so, key) == -1)
1829 return NULL;
1830 Py_RETURN_NONE;
1833 PyDoc_STRVAR(add_doc,
1834 "Add an element to a set.\n\
1836 This has no effect if the element is already present.");
1838 static int
1839 set_contains(PySetObject *so, PyObject *key)
1841 PyObject *tmpkey;
1842 int rv;
1844 rv = set_contains_key(so, key);
1845 if (rv == -1) {
1846 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1847 return -1;
1848 PyErr_Clear();
1849 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1850 if (tmpkey == NULL)
1851 return -1;
1852 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1853 rv = set_contains(so, tmpkey);
1854 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1855 Py_DECREF(tmpkey);
1857 return rv;
1860 static PyObject *
1861 set_direct_contains(PySetObject *so, PyObject *key)
1863 long result;
1865 result = set_contains(so, key);
1866 if (result == -1)
1867 return NULL;
1868 return PyBool_FromLong(result);
1871 PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
1873 static PyObject *
1874 set_remove(PySetObject *so, PyObject *key)
1876 PyObject *tmpkey, *result;
1877 int rv;
1879 rv = set_discard_key(so, key);
1880 if (rv == -1) {
1881 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1882 return NULL;
1883 PyErr_Clear();
1884 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1885 if (tmpkey == NULL)
1886 return NULL;
1887 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1888 result = set_remove(so, tmpkey);
1889 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1890 Py_DECREF(tmpkey);
1891 return result;
1892 } else if (rv == DISCARD_NOTFOUND) {
1893 set_key_error(key);
1894 return NULL;
1896 Py_RETURN_NONE;
1899 PyDoc_STRVAR(remove_doc,
1900 "Remove an element from a set; it must be a member.\n\
1902 If the element is not a member, raise a KeyError.");
1904 static PyObject *
1905 set_discard(PySetObject *so, PyObject *key)
1907 PyObject *tmpkey, *result;
1908 int rv;
1910 rv = set_discard_key(so, key);
1911 if (rv == -1) {
1912 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
1913 return NULL;
1914 PyErr_Clear();
1915 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
1916 if (tmpkey == NULL)
1917 return NULL;
1918 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1919 result = set_discard(so, tmpkey);
1920 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
1921 Py_DECREF(tmpkey);
1922 return result;
1924 Py_RETURN_NONE;
1927 PyDoc_STRVAR(discard_doc,
1928 "Remove an element from a set if it is a member.\n\
1930 If the element is not a member, do nothing.");
1932 static PyObject *
1933 set_reduce(PySetObject *so)
1935 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
1937 keys = PySequence_List((PyObject *)so);
1938 if (keys == NULL)
1939 goto done;
1940 args = PyTuple_Pack(1, keys);
1941 if (args == NULL)
1942 goto done;
1943 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1944 if (dict == NULL) {
1945 PyErr_Clear();
1946 dict = Py_None;
1947 Py_INCREF(dict);
1949 result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
1950 done:
1951 Py_XDECREF(args);
1952 Py_XDECREF(keys);
1953 Py_XDECREF(dict);
1954 return result;
1957 PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1959 static PyObject *
1960 set_sizeof(PySetObject *so)
1962 Py_ssize_t res;
1964 res = sizeof(PySetObject);
1965 if (so->table != so->smalltable)
1966 res = res + (so->mask + 1) * sizeof(setentry);
1967 return PyInt_FromSsize_t(res);
1970 PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes");
1971 static int
1972 set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1974 PyObject *iterable = NULL;
1976 if (!PyAnySet_Check(self))
1977 return -1;
1978 if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
1979 return -1;
1980 set_clear_internal(self);
1981 self->hash = -1;
1982 if (iterable == NULL)
1983 return 0;
1984 return set_update_internal(self, iterable);
1987 static PySequenceMethods set_as_sequence = {
1988 set_len, /* sq_length */
1989 0, /* sq_concat */
1990 0, /* sq_repeat */
1991 0, /* sq_item */
1992 0, /* sq_slice */
1993 0, /* sq_ass_item */
1994 0, /* sq_ass_slice */
1995 (objobjproc)set_contains, /* sq_contains */
1998 /* set object ********************************************************/
2000 #ifdef Py_DEBUG
2001 static PyObject *test_c_api(PySetObject *so);
2003 PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2004 All is well if assertions don't fail.");
2005 #endif
2007 static PyMethodDef set_methods[] = {
2008 {"add", (PyCFunction)set_add, METH_O,
2009 add_doc},
2010 {"clear", (PyCFunction)set_clear, METH_NOARGS,
2011 clear_doc},
2012 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2013 contains_doc},
2014 {"copy", (PyCFunction)set_copy, METH_NOARGS,
2015 copy_doc},
2016 {"discard", (PyCFunction)set_discard, METH_O,
2017 discard_doc},
2018 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2019 difference_doc},
2020 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
2021 difference_update_doc},
2022 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2023 intersection_doc},
2024 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS,
2025 intersection_update_doc},
2026 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2027 isdisjoint_doc},
2028 {"issubset", (PyCFunction)set_issubset, METH_O,
2029 issubset_doc},
2030 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2031 issuperset_doc},
2032 {"pop", (PyCFunction)set_pop, METH_NOARGS,
2033 pop_doc},
2034 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2035 reduce_doc},
2036 {"remove", (PyCFunction)set_remove, METH_O,
2037 remove_doc},
2038 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2039 sizeof_doc},
2040 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2041 symmetric_difference_doc},
2042 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
2043 symmetric_difference_update_doc},
2044 #ifdef Py_DEBUG
2045 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2046 test_c_api_doc},
2047 #endif
2048 {"union", (PyCFunction)set_union, METH_VARARGS,
2049 union_doc},
2050 {"update", (PyCFunction)set_update, METH_VARARGS,
2051 update_doc},
2052 {NULL, NULL} /* sentinel */
2055 static PyNumberMethods set_as_number = {
2056 0, /*nb_add*/
2057 (binaryfunc)set_sub, /*nb_subtract*/
2058 0, /*nb_multiply*/
2059 0, /*nb_divide*/
2060 0, /*nb_remainder*/
2061 0, /*nb_divmod*/
2062 0, /*nb_power*/
2063 0, /*nb_negative*/
2064 0, /*nb_positive*/
2065 0, /*nb_absolute*/
2066 0, /*nb_nonzero*/
2067 0, /*nb_invert*/
2068 0, /*nb_lshift*/
2069 0, /*nb_rshift*/
2070 (binaryfunc)set_and, /*nb_and*/
2071 (binaryfunc)set_xor, /*nb_xor*/
2072 (binaryfunc)set_or, /*nb_or*/
2073 0, /*nb_coerce*/
2074 0, /*nb_int*/
2075 0, /*nb_long*/
2076 0, /*nb_float*/
2077 0, /*nb_oct*/
2078 0, /*nb_hex*/
2079 0, /*nb_inplace_add*/
2080 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2081 0, /*nb_inplace_multiply*/
2082 0, /*nb_inplace_divide*/
2083 0, /*nb_inplace_remainder*/
2084 0, /*nb_inplace_power*/
2085 0, /*nb_inplace_lshift*/
2086 0, /*nb_inplace_rshift*/
2087 (binaryfunc)set_iand, /*nb_inplace_and*/
2088 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2089 (binaryfunc)set_ior, /*nb_inplace_or*/
2092 PyDoc_STRVAR(set_doc,
2093 "set(iterable) --> set object\n\
2095 Build an unordered collection of unique elements.");
2097 PyTypeObject PySet_Type = {
2098 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2099 "set", /* tp_name */
2100 sizeof(PySetObject), /* tp_basicsize */
2101 0, /* tp_itemsize */
2102 /* methods */
2103 (destructor)set_dealloc, /* tp_dealloc */
2104 (printfunc)set_tp_print, /* tp_print */
2105 0, /* tp_getattr */
2106 0, /* tp_setattr */
2107 set_nocmp, /* tp_compare */
2108 (reprfunc)set_repr, /* tp_repr */
2109 &set_as_number, /* tp_as_number */
2110 &set_as_sequence, /* tp_as_sequence */
2111 0, /* tp_as_mapping */
2112 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
2113 0, /* tp_call */
2114 0, /* tp_str */
2115 PyObject_GenericGetAttr, /* tp_getattro */
2116 0, /* tp_setattro */
2117 0, /* tp_as_buffer */
2118 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
2119 Py_TPFLAGS_BASETYPE, /* tp_flags */
2120 set_doc, /* tp_doc */
2121 (traverseproc)set_traverse, /* tp_traverse */
2122 (inquiry)set_clear_internal, /* tp_clear */
2123 (richcmpfunc)set_richcompare, /* tp_richcompare */
2124 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2125 (getiterfunc)set_iter, /* tp_iter */
2126 0, /* tp_iternext */
2127 set_methods, /* tp_methods */
2128 0, /* tp_members */
2129 0, /* tp_getset */
2130 0, /* tp_base */
2131 0, /* tp_dict */
2132 0, /* tp_descr_get */
2133 0, /* tp_descr_set */
2134 0, /* tp_dictoffset */
2135 (initproc)set_init, /* tp_init */
2136 PyType_GenericAlloc, /* tp_alloc */
2137 set_new, /* tp_new */
2138 PyObject_GC_Del, /* tp_free */
2141 /* frozenset object ********************************************************/
2144 static PyMethodDef frozenset_methods[] = {
2145 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
2146 contains_doc},
2147 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
2148 copy_doc},
2149 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
2150 difference_doc},
2151 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
2152 intersection_doc},
2153 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O,
2154 isdisjoint_doc},
2155 {"issubset", (PyCFunction)set_issubset, METH_O,
2156 issubset_doc},
2157 {"issuperset", (PyCFunction)set_issuperset, METH_O,
2158 issuperset_doc},
2159 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
2160 reduce_doc},
2161 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS,
2162 sizeof_doc},
2163 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
2164 symmetric_difference_doc},
2165 {"union", (PyCFunction)set_union, METH_VARARGS,
2166 union_doc},
2167 {NULL, NULL} /* sentinel */
2170 static PyNumberMethods frozenset_as_number = {
2171 0, /*nb_add*/
2172 (binaryfunc)set_sub, /*nb_subtract*/
2173 0, /*nb_multiply*/
2174 0, /*nb_divide*/
2175 0, /*nb_remainder*/
2176 0, /*nb_divmod*/
2177 0, /*nb_power*/
2178 0, /*nb_negative*/
2179 0, /*nb_positive*/
2180 0, /*nb_absolute*/
2181 0, /*nb_nonzero*/
2182 0, /*nb_invert*/
2183 0, /*nb_lshift*/
2184 0, /*nb_rshift*/
2185 (binaryfunc)set_and, /*nb_and*/
2186 (binaryfunc)set_xor, /*nb_xor*/
2187 (binaryfunc)set_or, /*nb_or*/
2190 PyDoc_STRVAR(frozenset_doc,
2191 "frozenset(iterable) --> frozenset object\n\
2193 Build an immutable unordered collection of unique elements.");
2195 PyTypeObject PyFrozenSet_Type = {
2196 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2197 "frozenset", /* tp_name */
2198 sizeof(PySetObject), /* tp_basicsize */
2199 0, /* tp_itemsize */
2200 /* methods */
2201 (destructor)set_dealloc, /* tp_dealloc */
2202 (printfunc)set_tp_print, /* tp_print */
2203 0, /* tp_getattr */
2204 0, /* tp_setattr */
2205 set_nocmp, /* tp_compare */
2206 (reprfunc)set_repr, /* tp_repr */
2207 &frozenset_as_number, /* tp_as_number */
2208 &set_as_sequence, /* tp_as_sequence */
2209 0, /* tp_as_mapping */
2210 frozenset_hash, /* tp_hash */
2211 0, /* tp_call */
2212 0, /* tp_str */
2213 PyObject_GenericGetAttr, /* tp_getattro */
2214 0, /* tp_setattro */
2215 0, /* tp_as_buffer */
2216 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
2217 Py_TPFLAGS_BASETYPE, /* tp_flags */
2218 frozenset_doc, /* tp_doc */
2219 (traverseproc)set_traverse, /* tp_traverse */
2220 (inquiry)set_clear_internal, /* tp_clear */
2221 (richcmpfunc)set_richcompare, /* tp_richcompare */
2222 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2223 (getiterfunc)set_iter, /* tp_iter */
2224 0, /* tp_iternext */
2225 frozenset_methods, /* tp_methods */
2226 0, /* tp_members */
2227 0, /* tp_getset */
2228 0, /* tp_base */
2229 0, /* tp_dict */
2230 0, /* tp_descr_get */
2231 0, /* tp_descr_set */
2232 0, /* tp_dictoffset */
2233 0, /* tp_init */
2234 PyType_GenericAlloc, /* tp_alloc */
2235 frozenset_new, /* tp_new */
2236 PyObject_GC_Del, /* tp_free */
2240 /***** C API functions *************************************************/
2242 PyObject *
2243 PySet_New(PyObject *iterable)
2245 return make_new_set(&PySet_Type, iterable);
2248 PyObject *
2249 PyFrozenSet_New(PyObject *iterable)
2251 return make_new_set(&PyFrozenSet_Type, iterable);
2254 Py_ssize_t
2255 PySet_Size(PyObject *anyset)
2257 if (!PyAnySet_Check(anyset)) {
2258 PyErr_BadInternalCall();
2259 return -1;
2261 return PySet_GET_SIZE(anyset);
2265 PySet_Clear(PyObject *set)
2267 if (!PySet_Check(set)) {
2268 PyErr_BadInternalCall();
2269 return -1;
2271 return set_clear_internal((PySetObject *)set);
2275 PySet_Contains(PyObject *anyset, PyObject *key)
2277 if (!PyAnySet_Check(anyset)) {
2278 PyErr_BadInternalCall();
2279 return -1;
2281 return set_contains_key((PySetObject *)anyset, key);
2285 PySet_Discard(PyObject *set, PyObject *key)
2287 if (!PySet_Check(set)) {
2288 PyErr_BadInternalCall();
2289 return -1;
2291 return set_discard_key((PySetObject *)set, key);
2295 PySet_Add(PyObject *anyset, PyObject *key)
2297 if (!PySet_Check(anyset) &&
2298 (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
2299 PyErr_BadInternalCall();
2300 return -1;
2302 return set_add_key((PySetObject *)anyset, key);
2306 _PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key)
2308 setentry *entry_ptr;
2310 if (!PyAnySet_Check(set)) {
2311 PyErr_BadInternalCall();
2312 return -1;
2314 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
2315 return 0;
2316 *key = entry_ptr->key;
2317 return 1;
2321 _PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
2323 setentry *entry;
2325 if (!PyAnySet_Check(set)) {
2326 PyErr_BadInternalCall();
2327 return -1;
2329 if (set_next((PySetObject *)set, pos, &entry) == 0)
2330 return 0;
2331 *key = entry->key;
2332 *hash = entry->hash;
2333 return 1;
2336 PyObject *
2337 PySet_Pop(PyObject *set)
2339 if (!PySet_Check(set)) {
2340 PyErr_BadInternalCall();
2341 return NULL;
2343 return set_pop((PySetObject *)set);
2347 _PySet_Update(PyObject *set, PyObject *iterable)
2349 if (!PySet_Check(set)) {
2350 PyErr_BadInternalCall();
2351 return -1;
2353 return set_update_internal((PySetObject *)set, iterable);
2356 #ifdef Py_DEBUG
2358 /* Test code to be called with any three element set.
2359 Returns True and original set is restored. */
2361 #define assertRaises(call_return_value, exception) \
2362 do { \
2363 assert(call_return_value); \
2364 assert(PyErr_ExceptionMatches(exception)); \
2365 PyErr_Clear(); \
2366 } while(0)
2368 static PyObject *
2369 test_c_api(PySetObject *so)
2371 Py_ssize_t count;
2372 char *s;
2373 Py_ssize_t i;
2374 PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
2375 PyObject *ob = (PyObject *)so;
2377 /* Verify preconditions and exercise type/size checks */
2378 assert(PyAnySet_Check(ob));
2379 assert(PyAnySet_CheckExact(ob));
2380 assert(!PyFrozenSet_CheckExact(ob));
2381 assert(PySet_Size(ob) == 3);
2382 assert(PySet_GET_SIZE(ob) == 3);
2384 /* Raise TypeError for non-iterable constructor arguments */
2385 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2386 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2388 /* Raise TypeError for unhashable key */
2389 dup = PySet_New(ob);
2390 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2391 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2392 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2394 /* Exercise successful pop, contains, add, and discard */
2395 elem = PySet_Pop(ob);
2396 assert(PySet_Contains(ob, elem) == 0);
2397 assert(PySet_GET_SIZE(ob) == 2);
2398 assert(PySet_Add(ob, elem) == 0);
2399 assert(PySet_Contains(ob, elem) == 1);
2400 assert(PySet_GET_SIZE(ob) == 3);
2401 assert(PySet_Discard(ob, elem) == 1);
2402 assert(PySet_GET_SIZE(ob) == 2);
2403 assert(PySet_Discard(ob, elem) == 0);
2404 assert(PySet_GET_SIZE(ob) == 2);
2406 /* Exercise clear */
2407 dup2 = PySet_New(dup);
2408 assert(PySet_Clear(dup2) == 0);
2409 assert(PySet_Size(dup2) == 0);
2410 Py_DECREF(dup2);
2412 /* Raise SystemError on clear or update of frozen set */
2413 f = PyFrozenSet_New(dup);
2414 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2415 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2416 assert(PySet_Add(f, elem) == 0);
2417 Py_INCREF(f);
2418 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2419 Py_DECREF(f);
2420 Py_DECREF(f);
2422 /* Exercise direct iteration */
2423 i = 0, count = 0;
2424 while (_PySet_Next((PyObject *)dup, &i, &x)) {
2425 s = PyString_AsString(x);
2426 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
2427 count++;
2429 assert(count == 3);
2431 /* Exercise updates */
2432 dup2 = PySet_New(NULL);
2433 assert(_PySet_Update(dup2, dup) == 0);
2434 assert(PySet_Size(dup2) == 3);
2435 assert(_PySet_Update(dup2, dup) == 0);
2436 assert(PySet_Size(dup2) == 3);
2437 Py_DECREF(dup2);
2439 /* Raise SystemError when self argument is not a set or frozenset. */
2440 t = PyTuple_New(0);
2441 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2442 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2443 Py_DECREF(t);
2445 /* Raise SystemError when self argument is not a set. */
2446 f = PyFrozenSet_New(dup);
2447 assert(PySet_Size(f) == 3);
2448 assert(PyFrozenSet_CheckExact(f));
2449 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2450 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2451 Py_DECREF(f);
2453 /* Raise KeyError when popping from an empty set */
2454 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
2455 Py_DECREF(ob);
2456 assert(PySet_GET_SIZE(ob) == 0);
2457 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2459 /* Restore the set from the copy using the PyNumber API */
2460 assert(PyNumber_InPlaceOr(ob, dup) == ob);
2461 Py_DECREF(ob);
2463 /* Verify constructors accept NULL arguments */
2464 f = PySet_New(NULL);
2465 assert(f != NULL);
2466 assert(PySet_GET_SIZE(f) == 0);
2467 Py_DECREF(f);
2468 f = PyFrozenSet_New(NULL);
2469 assert(f != NULL);
2470 assert(PyFrozenSet_CheckExact(f));
2471 assert(PySet_GET_SIZE(f) == 0);
2472 Py_DECREF(f);
2474 Py_DECREF(elem);
2475 Py_DECREF(dup);
2476 Py_RETURN_TRUE;
2479 #undef assertRaises
2481 #endif