Bug Summary

File:Objects/setobject.c
Location:line 104, column 39
Description:Both operands to '==' always have the same value

Annotated Source Code

1
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.
5
6 Copyright (c) 2003-2008 Python Software Foundation.
7 All rights reserved.
8*/
9
10#include "Python.h"
11#include "structmember.h"
12#include "stringlib/eq.h"
13
14/* Set a key error with the specified argument, wrapping it in a
15 * tuple automatically so that tuple keys are not unpacked as the
16 * exception arguments. */
17static void
18set_key_error(PyObject *arg)
19{
20 PyObject *tup;
21 tup = PyTuple_Pack(1, arg);
22 if (!tup)
23 return; /* caller will expect error to be set anyway */
24 PyErr_SetObject(PyExc_KeyError, tup);
25 Py_DECREF(tup)do { if (_Py_RefTotal-- , --((PyObject*)(tup))->ob_refcnt !=
0) { if (((PyObject*)tup)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 25, (PyObject *)(tup)); } else _Py_Dealloc
((PyObject *)(tup)); } while (0)
;
26}
27
28/* This must be >= 1. */
29#define PERTURB_SHIFT5 5
30
31/* Object used as dummy key to fill deleted entries */
32static PyObject *dummy = NULL((void*)0); /* Initialized by first call to make_new_set() */
33
34#ifdef Py_REF_DEBUG
35PyObject *
36_PySet_Dummy(void)
37{
38 return dummy;
39}
40#endif
41
42#define INIT_NONZERO_SET_SLOTS(so)do { (so)->table = (so)->smalltable; (so)->mask = 8 -
1; (so)->hash = -1; } while(0)
do { \
43 (so)->table = (so)->smalltable; \
44 (so)->mask = PySet_MINSIZE8 - 1; \
45 (so)->hash = -1; \
46 } while(0)
47
48#define EMPTY_TO_MINSIZE(so)do { ((__builtin_object_size ((so)->smalltable, 0) != (size_t
) -1) ? __builtin___memset_chk ((so)->smalltable, 0, sizeof
((so)->smalltable), __builtin_object_size ((so)->smalltable
, 0)) : __inline_memset_chk ((so)->smalltable, 0, sizeof((
so)->smalltable))); (so)->used = (so)->fill = 0; do {
(so)->table = (so)->smalltable; (so)->mask = 8 - 1;
(so)->hash = -1; } while(0); } while(0)
do { \
49 memset((so)->smalltable, 0, sizeof((so)->smalltable))((__builtin_object_size ((so)->smalltable, 0) != (size_t) -
1) ? __builtin___memset_chk ((so)->smalltable, 0, sizeof((
so)->smalltable), __builtin_object_size ((so)->smalltable
, 0)) : __inline_memset_chk ((so)->smalltable, 0, sizeof((
so)->smalltable)))
; \
50 (so)->used = (so)->fill = 0; \
51 INIT_NONZERO_SET_SLOTS(so)do { (so)->table = (so)->smalltable; (so)->mask = 8 -
1; (so)->hash = -1; } while(0)
; \
52 } while(0)
53
54/* Reuse scheme to save calls to malloc, free, and memset */
55#ifndef PySet_MAXFREELIST80
56#define PySet_MAXFREELIST80 80
57#endif
58static PySetObject *free_list[PySet_MAXFREELIST80];
59static int numfree = 0;
60
61
62/*
63The basic lookup function used by all operations.
64This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
65Open addressing is preferred over chaining since the link overhead for
66chaining would be substantial (100% with typical malloc overhead).
67
68The initial probe index is computed as hash mod the table size. Subsequent
69probe indices are computed as explained in Objects/dictobject.c.
70
71All arithmetic on hash should ignore overflow.
72
73Unlike the dictionary implementation, the lookkey functions can return
74NULL if the rich comparison returns an error.
75*/
76
77static setentry *
78set_lookkey(PySetObject *so, PyObject *key, register Py_hash_t hash)
79{
80 register Py_ssize_t i;
81 register size_t perturb;
82 register setentry *freeslot;
83 register size_t mask = so->mask;
84 setentry *table = so->table;
85 register setentry *entry;
86 register int cmp;
87 PyObject *startkey;
88
89 i = hash & mask;
90 entry = &table[i];
91 if (entry->key == NULL((void*)0) || entry->key == key)
1
Taking false branch
92 return entry;
93
94 if (entry->key == dummy)
2
Taking false branch
95 freeslot = entry;
96 else {
97 if (entry->hash == hash) {
3
Taking true branch
98 startkey = entry->key;
99 Py_INCREF(startkey)( _Py_RefTotal++ , ((PyObject*)(startkey))->ob_refcnt++);
100 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ2);
101 Py_DECREF(startkey)do { if (_Py_RefTotal-- , --((PyObject*)(startkey))->ob_refcnt
!= 0) { if (((PyObject*)startkey)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 101, (PyObject *)(startkey)); } else _Py_Dealloc
((PyObject *)(startkey)); } while (0)
;
102 if (cmp < 0)
4
Taking false branch
103 return NULL((void*)0);
104 if (table == so->table && entry->key == startkey) {
5
Both operands to '==' always have the same value
105 if (cmp > 0)
106 return entry;
107 }
108 else {
109 /* The compare did major nasty stuff to the
110 * set: start over.
111 */
112 return set_lookkey(so, key, hash);
113 }
114 }
115 freeslot = NULL((void*)0);
116 }
117
118 /* In the loop, key == dummy is by far (factor of 100s) the
119 least likely outcome, so test for that last. */
120 for (perturb = hash; ; perturb >>= PERTURB_SHIFT5) {
121 i = (i << 2) + i + perturb + 1;
122 entry = &table[i & mask];
123 if (entry->key == NULL((void*)0)) {
124 if (freeslot != NULL((void*)0))
125 entry = freeslot;
126 break;
127 }
128 if (entry->key == key)
129 break;
130 if (entry->hash == hash && entry->key != dummy) {
131 startkey = entry->key;
132 Py_INCREF(startkey)( _Py_RefTotal++ , ((PyObject*)(startkey))->ob_refcnt++);
133 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ2);
134 Py_DECREF(startkey)do { if (_Py_RefTotal-- , --((PyObject*)(startkey))->ob_refcnt
!= 0) { if (((PyObject*)startkey)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 134, (PyObject *)(startkey)); } else _Py_Dealloc
((PyObject *)(startkey)); } while (0)
;
135 if (cmp < 0)
136 return NULL((void*)0);
137 if (table == so->table && entry->key == startkey) {
138 if (cmp > 0)
139 break;
140 }
141 else {
142 /* The compare did major nasty stuff to the
143 * set: start over.
144 */
145 return set_lookkey(so, key, hash);
146 }
147 }
148 else if (entry->key == dummy && freeslot == NULL((void*)0))
149 freeslot = entry;
150 }
151 return entry;
152}
153
154/*
155 * Hacked up version of set_lookkey which can assume keys are always unicode;
156 * This means we can always use unicode_eq directly and not have to check to
157 * see if the comparison altered the table.
158 */
159static setentry *
160set_lookkey_unicode(PySetObject *so, PyObject *key, register Py_hash_t hash)
161{
162 register Py_ssize_t i;
163 register size_t perturb;
164 register setentry *freeslot;
165 register size_t mask = so->mask;
166 setentry *table = so->table;
167 register setentry *entry;
168
169 /* Make sure this function doesn't have to handle non-unicode keys,
170 including subclasses of str; e.g., one reason to subclass
171 strings is to override __eq__, and for speed we don't cater to
172 that here. */
173 if (!PyUnicode_CheckExact(key)((((PyObject*)(key))->ob_type) == &PyUnicode_Type)) {
174 so->lookup = set_lookkey;
175 return set_lookkey(so, key, hash);
176 }
177 i = hash & mask;
178 entry = &table[i];
179 if (entry->key == NULL((void*)0) || entry->key == key)
180 return entry;
181 if (entry->key == dummy)
182 freeslot = entry;
183 else {
184 if (entry->hash == hash && unicode_eq(entry->key, key))
185 return entry;
186 freeslot = NULL((void*)0);
187 }
188
189 /* In the loop, key == dummy is by far (factor of 100s) the
190 least likely outcome, so test for that last. */
191 for (perturb = hash; ; perturb >>= PERTURB_SHIFT5) {
192 i = (i << 2) + i + perturb + 1;
193 entry = &table[i & mask];
194 if (entry->key == NULL((void*)0))
195 return freeslot == NULL((void*)0) ? entry : freeslot;
196 if (entry->key == key
197 || (entry->hash == hash
198 && entry->key != dummy
199 && unicode_eq(entry->key, key)))
200 return entry;
201 if (entry->key == dummy && freeslot == NULL((void*)0))
202 freeslot = entry;
203 }
204 assert(0)(__builtin_expect(!(0), 0) ? __assert_rtn(__func__, "Objects/setobject.c"
, 204, "0") : (void)0)
; /* NOT REACHED */
205 return 0;
206}
207
208/*
209Internal routine to insert a new key into the table.
210Used by the public insert routine.
211Eats a reference to key.
212*/
213static int
214set_insert_key(register PySetObject *so, PyObject *key, Py_hash_t hash)
215{
216 register setentry *entry;
217 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, Py_hash_t);
218
219 assert(so->lookup != NULL)(__builtin_expect(!(so->lookup != ((void*)0)), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 219, "so->lookup != NULL"
) : (void)0)
;
220 entry = so->lookup(so, key, hash);
221 if (entry == NULL((void*)0))
222 return -1;
223 if (entry->key == NULL((void*)0)) {
224 /* UNUSED */
225 so->fill++;
226 entry->key = key;
227 entry->hash = hash;
228 so->used++;
229 } else if (entry->key == dummy) {
230 /* DUMMY */
231 entry->key = key;
232 entry->hash = hash;
233 so->used++;
234 Py_DECREF(dummy)do { if (_Py_RefTotal-- , --((PyObject*)(dummy))->ob_refcnt
!= 0) { if (((PyObject*)dummy)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 234, (PyObject *)(dummy)); } else _Py_Dealloc
((PyObject *)(dummy)); } while (0)
;
235 } else {
236 /* ACTIVE */
237 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 237, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
238 }
239 return 0;
240}
241
242/*
243Internal routine used by set_table_resize() to insert an item which is
244known to be absent from the set. This routine also assumes that
245the set contains no deleted entries. Besides the performance benefit,
246using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
247Note that no refcounts are changed by this routine; if needed, the caller
248is responsible for incref'ing `key`.
249*/
250static void
251set_insert_clean(register PySetObject *so, PyObject *key, Py_hash_t hash)
252{
253 register size_t i;
254 register size_t perturb;
255 register size_t mask = (size_t)so->mask;
256 setentry *table = so->table;
257 register setentry *entry;
258
259 i = hash & mask;
260 entry = &table[i];
261 for (perturb = hash; entry->key != NULL((void*)0); perturb >>= PERTURB_SHIFT5) {
262 i = (i << 2) + i + perturb + 1;
263 entry = &table[i & mask];
264 }
265 so->fill++;
266 entry->key = key;
267 entry->hash = hash;
268 so->used++;
269}
270
271/*
272Restructure the table by allocating a new table and reinserting all
273keys again. When entries have been deleted, the new table may
274actually be smaller than the old one.
275*/
276static int
277set_table_resize(PySetObject *so, Py_ssize_t minused)
278{
279 Py_ssize_t newsize;
280 setentry *oldtable, *newtable, *entry;
281 Py_ssize_t i;
282 int is_oldtable_malloced;
283 setentry small_copy[PySet_MINSIZE8];
284
285 assert(minused >= 0)(__builtin_expect(!(minused >= 0), 0) ? __assert_rtn(__func__
, "Objects/setobject.c", 285, "minused >= 0") : (void)0)
;
286
287 /* Find the smallest table size > minused. */
288 for (newsize = PySet_MINSIZE8;
289 newsize <= minused && newsize > 0;
290 newsize <<= 1)
291 ;
292 if (newsize <= 0) {
293 PyErr_NoMemory();
294 return -1;
295 }
296
297 /* Get space for a new table. */
298 oldtable = so->table;
299 assert(oldtable != NULL)(__builtin_expect(!(oldtable != ((void*)0)), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 299, "oldtable != NULL") : (
void)0)
;
300 is_oldtable_malloced = oldtable != so->smalltable;
301
302 if (newsize == PySet_MINSIZE8) {
303 /* A large table is shrinking, or we can't get any smaller. */
304 newtable = so->smalltable;
305 if (newtable == oldtable) {
306 if (so->fill == so->used) {
307 /* No dummies, so no point doing anything. */
308 return 0;
309 }
310 /* We're not going to resize it, but rebuild the
311 table anyway to purge old dummy entries.
312 Subtle: This is *necessary* if fill==size,
313 as set_lookkey needs at least one virgin slot to
314 terminate failing searches. If fill < size, it's
315 merely desirable, as dummies slow searches. */
316 assert(so->fill > so->used)(__builtin_expect(!(so->fill > so->used), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 316, "so->fill > so->used"
) : (void)0)
;
317 memcpy(small_copy, oldtable, sizeof(small_copy))((__builtin_object_size (small_copy, 0) != (size_t) -1) ? __builtin___memcpy_chk
(small_copy, oldtable, sizeof(small_copy), __builtin_object_size
(small_copy, 0)) : __inline_memcpy_chk (small_copy, oldtable
, sizeof(small_copy)))
;
318 oldtable = small_copy;
319 }
320 }
321 else {
322 newtable = PyMem_NEW(setentry, newsize)( ((size_t)(newsize) > ((Py_ssize_t)(((size_t)-1)>>1
)) / sizeof(setentry)) ? ((void*)0) : ( (setentry *) _PyMem_DebugMalloc
((newsize) * sizeof(setentry)) ) )
;
323 if (newtable == NULL((void*)0)) {
324 PyErr_NoMemory();
325 return -1;
326 }
327 }
328
329 /* Make the set empty, using the new table. */
330 assert(newtable != oldtable)(__builtin_expect(!(newtable != oldtable), 0) ? __assert_rtn(
__func__, "Objects/setobject.c", 330, "newtable != oldtable")
: (void)0)
;
331 so->table = newtable;
332 so->mask = newsize - 1;
333 memset(newtable, 0, sizeof(setentry) * newsize)((__builtin_object_size (newtable, 0) != (size_t) -1) ? __builtin___memset_chk
(newtable, 0, sizeof(setentry) * newsize, __builtin_object_size
(newtable, 0)) : __inline_memset_chk (newtable, 0, sizeof(setentry
) * newsize))
;
334 so->used = 0;
335 i = so->fill;
336 so->fill = 0;
337
338 /* Copy the data over; this is refcount-neutral for active entries;
339 dummy entries aren't copied over, of course */
340 for (entry = oldtable; i > 0; entry++) {
341 if (entry->key == NULL((void*)0)) {
342 /* UNUSED */
343 ;
344 } else if (entry->key == dummy) {
345 /* DUMMY */
346 --i;
347 assert(entry->key == dummy)(__builtin_expect(!(entry->key == dummy), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 347, "entry->key == dummy"
) : (void)0)
;
348 Py_DECREF(entry->key)do { if (_Py_RefTotal-- , --((PyObject*)(entry->key))->
ob_refcnt != 0) { if (((PyObject*)entry->key)->ob_refcnt
< 0) _Py_NegativeRefcount("Objects/setobject.c", 348, (PyObject
*)(entry->key)); } else _Py_Dealloc((PyObject *)(entry->
key)); } while (0)
;
349 } else {
350 /* ACTIVE */
351 --i;
352 set_insert_clean(so, entry->key, entry->hash);
353 }
354 }
355
356 if (is_oldtable_malloced)
357 PyMem_DEL_PyMem_DebugFree(oldtable);
358 return 0;
359}
360
361/* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
362
363static int
364set_add_entry(register PySetObject *so, setentry *entry)
365{
366 register Py_ssize_t n_used;
367 PyObject *key = entry->key;
368
369 assert(so->fill <= so->mask)(__builtin_expect(!(so->fill <= so->mask), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 369, "so->fill <= so->mask"
) : (void)0)
; /* at least one empty slot */
370 n_used = so->used;
371 Py_INCREF(key)( _Py_RefTotal++ , ((PyObject*)(key))->ob_refcnt++);
372 if (set_insert_key(so, key, entry->hash) == -1) {
373 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 373, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
374 return -1;
375 }
376 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
377 return 0;
378 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
379}
380
381static int
382set_add_key(register PySetObject *so, PyObject *key)
383{
384 register Py_hash_t hash;
385 register Py_ssize_t n_used;
386
387 if (!PyUnicode_CheckExact(key)((((PyObject*)(key))->ob_type) == &PyUnicode_Type) ||
388 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
389 hash = PyObject_Hash(key);
390 if (hash == -1)
391 return -1;
392 }
393 assert(so->fill <= so->mask)(__builtin_expect(!(so->fill <= so->mask), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 393, "so->fill <= so->mask"
) : (void)0)
; /* at least one empty slot */
394 n_used = so->used;
395 Py_INCREF(key)( _Py_RefTotal++ , ((PyObject*)(key))->ob_refcnt++);
396 if (set_insert_key(so, key, hash) == -1) {
397 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 397, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
398 return -1;
399 }
400 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
401 return 0;
402 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
403}
404
405#define DISCARD_NOTFOUND0 0
406#define DISCARD_FOUND1 1
407
408static int
409set_discard_entry(PySetObject *so, setentry *oldentry)
410{ register setentry *entry;
411 PyObject *old_key;
412
413 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
414 if (entry == NULL((void*)0))
415 return -1;
416 if (entry->key == NULL((void*)0) || entry->key == dummy)
417 return DISCARD_NOTFOUND0;
418 old_key = entry->key;
419 Py_INCREF(dummy)( _Py_RefTotal++ , ((PyObject*)(dummy))->ob_refcnt++);
420 entry->key = dummy;
421 so->used--;
422 Py_DECREF(old_key)do { if (_Py_RefTotal-- , --((PyObject*)(old_key))->ob_refcnt
!= 0) { if (((PyObject*)old_key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 422, (PyObject *)(old_key)); } else _Py_Dealloc
((PyObject *)(old_key)); } while (0)
;
423 return DISCARD_FOUND1;
424}
425
426static int
427set_discard_key(PySetObject *so, PyObject *key)
428{
429 register Py_hash_t hash;
430 register setentry *entry;
431 PyObject *old_key;
432
433 assert (PyAnySet_Check(so))(__builtin_expect(!(((((PyObject*)(so))->ob_type) == &
PySet_Type || (((PyObject*)(so))->ob_type) == &PyFrozenSet_Type
|| PyType_IsSubtype((((PyObject*)(so))->ob_type), &PySet_Type
) || PyType_IsSubtype((((PyObject*)(so))->ob_type), &PyFrozenSet_Type
))), 0) ? __assert_rtn(__func__, "Objects/setobject.c", 433, "PyAnySet_Check(so)"
) : (void)0)
;
434
435 if (!PyUnicode_CheckExact(key)((((PyObject*)(key))->ob_type) == &PyUnicode_Type) ||
436 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
437 hash = PyObject_Hash(key);
438 if (hash == -1)
439 return -1;
440 }
441 entry = (so->lookup)(so, key, hash);
442 if (entry == NULL((void*)0))
443 return -1;
444 if (entry->key == NULL((void*)0) || entry->key == dummy)
445 return DISCARD_NOTFOUND0;
446 old_key = entry->key;
447 Py_INCREF(dummy)( _Py_RefTotal++ , ((PyObject*)(dummy))->ob_refcnt++);
448 entry->key = dummy;
449 so->used--;
450 Py_DECREF(old_key)do { if (_Py_RefTotal-- , --((PyObject*)(old_key))->ob_refcnt
!= 0) { if (((PyObject*)old_key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 450, (PyObject *)(old_key)); } else _Py_Dealloc
((PyObject *)(old_key)); } while (0)
;
451 return DISCARD_FOUND1;
452}
453
454static int
455set_clear_internal(PySetObject *so)
456{
457 setentry *entry, *table;
458 int table_is_malloced;
459 Py_ssize_t fill;
460 setentry small_copy[PySet_MINSIZE8];
461#ifdef Py_DEBUG1
462 Py_ssize_t i, n;
463 assert (PyAnySet_Check(so))(__builtin_expect(!(((((PyObject*)(so))->ob_type) == &
PySet_Type || (((PyObject*)(so))->ob_type) == &PyFrozenSet_Type
|| PyType_IsSubtype((((PyObject*)(so))->ob_type), &PySet_Type
) || PyType_IsSubtype((((PyObject*)(so))->ob_type), &PyFrozenSet_Type
))), 0) ? __assert_rtn(__func__, "Objects/setobject.c", 463, "PyAnySet_Check(so)"
) : (void)0)
;
464
465 n = so->mask + 1;
466 i = 0;
467#endif
468
469 table = so->table;
470 assert(table != NULL)(__builtin_expect(!(table != ((void*)0)), 0) ? __assert_rtn(__func__
, "Objects/setobject.c", 470, "table != NULL") : (void)0)
;
471 table_is_malloced = table != so->smalltable;
472
473 /* This is delicate. During the process of clearing the set,
474 * decrefs can cause the set to mutate. To avoid fatal confusion
475 * (voice of experience), we have to make the set empty before
476 * clearing the slots, and never refer to anything via so->ref while
477 * clearing.
478 */
479 fill = so->fill;
480 if (table_is_malloced)
481 EMPTY_TO_MINSIZE(so)do { ((__builtin_object_size ((so)->smalltable, 0) != (size_t
) -1) ? __builtin___memset_chk ((so)->smalltable, 0, sizeof
((so)->smalltable), __builtin_object_size ((so)->smalltable
, 0)) : __inline_memset_chk ((so)->smalltable, 0, sizeof((
so)->smalltable))); (so)->used = (so)->fill = 0; do {
(so)->table = (so)->smalltable; (so)->mask = 8 - 1;
(so)->hash = -1; } while(0); } while(0)
;
482
483 else if (fill > 0) {
484 /* It's a small table with something that needs to be cleared.
485 * Afraid the only safe way is to copy the set entries into
486 * another small table first.
487 */
488 memcpy(small_copy, table, sizeof(small_copy))((__builtin_object_size (small_copy, 0) != (size_t) -1) ? __builtin___memcpy_chk
(small_copy, table, sizeof(small_copy), __builtin_object_size
(small_copy, 0)) : __inline_memcpy_chk (small_copy, table, sizeof
(small_copy)))
;
489 table = small_copy;
490 EMPTY_TO_MINSIZE(so)do { ((__builtin_object_size ((so)->smalltable, 0) != (size_t
) -1) ? __builtin___memset_chk ((so)->smalltable, 0, sizeof
((so)->smalltable), __builtin_object_size ((so)->smalltable
, 0)) : __inline_memset_chk ((so)->smalltable, 0, sizeof((
so)->smalltable))); (so)->used = (so)->fill = 0; do {
(so)->table = (so)->smalltable; (so)->mask = 8 - 1;
(so)->hash = -1; } while(0); } while(0)
;
491 }
492 /* else it's a small table that's already empty */
493
494 /* Now we can finally clear things. If C had refcounts, we could
495 * assert that the refcount on table is 1 now, i.e. that this function
496 * has unique access to it, so decref side-effects can't alter it.
497 */
498 for (entry = table; fill > 0; ++entry) {
499#ifdef Py_DEBUG1
500 assert(i < n)(__builtin_expect(!(i < n), 0) ? __assert_rtn(__func__, "Objects/setobject.c"
, 500, "i < n") : (void)0)
;
501 ++i;
502#endif
503 if (entry->key) {
504 --fill;
505 Py_DECREF(entry->key)do { if (_Py_RefTotal-- , --((PyObject*)(entry->key))->
ob_refcnt != 0) { if (((PyObject*)entry->key)->ob_refcnt
< 0) _Py_NegativeRefcount("Objects/setobject.c", 505, (PyObject
*)(entry->key)); } else _Py_Dealloc((PyObject *)(entry->
key)); } while (0)
;
506 }
507#ifdef Py_DEBUG1
508 else
509 assert(entry->key == NULL)(__builtin_expect(!(entry->key == ((void*)0)), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 509, "entry->key == NULL"
) : (void)0)
;
510#endif
511 }
512
513 if (table_is_malloced)
514 PyMem_DEL_PyMem_DebugFree(table);
515 return 0;
516}
517
518/*
519 * Iterate over a set table. Use like so:
520 *
521 * Py_ssize_t pos;
522 * setentry *entry;
523 * pos = 0; # important! pos should not otherwise be changed by you
524 * while (set_next(yourset, &pos, &entry)) {
525 * Refer to borrowed reference in entry->key.
526 * }
527 *
528 * CAUTION: In general, it isn't safe to use set_next in a loop that
529 * mutates the table.
530 */
531static int
532set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
533{
534 Py_ssize_t i;
535 Py_ssize_t mask;
536 register setentry *table;
537
538 assert (PyAnySet_Check(so))(__builtin_expect(!(((((PyObject*)(so))->ob_type) == &
PySet_Type || (((PyObject*)(so))->ob_type) == &PyFrozenSet_Type
|| PyType_IsSubtype((((PyObject*)(so))->ob_type), &PySet_Type
) || PyType_IsSubtype((((PyObject*)(so))->ob_type), &PyFrozenSet_Type
))), 0) ? __assert_rtn(__func__, "Objects/setobject.c", 538, "PyAnySet_Check(so)"
) : (void)0)
;
539 i = *pos_ptr;
540 assert(i >= 0)(__builtin_expect(!(i >= 0), 0) ? __assert_rtn(__func__, "Objects/setobject.c"
, 540, "i >= 0") : (void)0)
;
541 table = so->table;
542 mask = so->mask;
543 while (i <= mask && (table[i].key == NULL((void*)0) || table[i].key == dummy))
544 i++;
545 *pos_ptr = i+1;
546 if (i > mask)
547 return 0;
548 assert(table[i].key != NULL)(__builtin_expect(!(table[i].key != ((void*)0)), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 548, "table[i].key != NULL"
) : (void)0)
;
549 *entry_ptr = &table[i];
550 return 1;
551}
552
553static void
554set_dealloc(PySetObject *so)
555{
556 register setentry *entry;
557 Py_ssize_t fill = so->fill;
558 PyObject_GC_UnTrack(so);
559 Py_TRASHCAN_SAFE_BEGIN(so)if (_PyTrash_delete_nesting < 50) { ++_PyTrash_delete_nesting
;
560 if (so->weakreflist != NULL((void*)0))
561 PyObject_ClearWeakRefs((PyObject *) so);
562
563 for (entry = so->table; fill > 0; entry++) {
564 if (entry->key) {
565 --fill;
566 Py_DECREF(entry->key)do { if (_Py_RefTotal-- , --((PyObject*)(entry->key))->
ob_refcnt != 0) { if (((PyObject*)entry->key)->ob_refcnt
< 0) _Py_NegativeRefcount("Objects/setobject.c", 566, (PyObject
*)(entry->key)); } else _Py_Dealloc((PyObject *)(entry->
key)); } while (0)
;
567 }
568 }
569 if (so->table != so->smalltable)
570 PyMem_DEL_PyMem_DebugFree(so->table);
571 if (numfree < PySet_MAXFREELIST80 && PyAnySet_CheckExact(so)((((PyObject*)(so))->ob_type) == &PySet_Type || (((PyObject
*)(so))->ob_type) == &PyFrozenSet_Type)
)
572 free_list[numfree++] = so;
573 else
574 Py_TYPE(so)(((PyObject*)(so))->ob_type)->tp_free(so);
575 Py_TRASHCAN_SAFE_END(so)--_PyTrash_delete_nesting; if (_PyTrash_delete_later &&
_PyTrash_delete_nesting <= 0) _PyTrash_destroy_chain(); }
else _PyTrash_deposit_object((PyObject*)so);
576}
577
578static PyObject *
579set_repr(PySetObject *so)
580{
581 PyObject *keys, *result=NULL((void*)0);
582 Py_UNICODE *u;
583 int status = Py_ReprEnter((PyObject*)so);
584 PyObject *listrepr;
585 Py_ssize_t newsize;
586
587 if (status != 0) {
588 if (status < 0)
589 return NULL((void*)0);
590 return PyUnicode_FromFormatPyUnicodeUCS2_FromFormat("%s(...)", Py_TYPE(so)(((PyObject*)(so))->ob_type)->tp_name);
591 }
592
593 /* shortcut for the empty set */
594 if (!so->used) {
595 Py_ReprLeave((PyObject*)so);
596 return PyUnicode_FromFormatPyUnicodeUCS2_FromFormat("%s()", Py_TYPE(so)(((PyObject*)(so))->ob_type)->tp_name);
597 }
598
599 keys = PySequence_List((PyObject *)so);
600 if (keys == NULL((void*)0))
601 goto done;
602
603 listrepr = PyObject_Repr(keys);
604 Py_DECREF(keys)do { if (_Py_RefTotal-- , --((PyObject*)(keys))->ob_refcnt
!= 0) { if (((PyObject*)keys)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 604, (PyObject *)(keys)); } else _Py_Dealloc
((PyObject *)(keys)); } while (0)
;
605 if (listrepr == NULL((void*)0))
606 goto done;
607 newsize = PyUnicode_GET_SIZE(listrepr)((__builtin_expect(!(((((((PyObject*)(listrepr))->ob_type)
)->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 607, "PyUnicode_Check(listrepr)"
) : (void)0),(((PyUnicodeObject *)(listrepr))->length))
;
608 result = PyUnicode_FromUnicodePyUnicodeUCS2_FromUnicode(NULL((void*)0), newsize);
609 if (result) {
610 u = PyUnicode_AS_UNICODE(result)((__builtin_expect(!(((((((PyObject*)(result))->ob_type))->
tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__
, "Objects/setobject.c", 610, "PyUnicode_Check(result)") : (void
)0),(((PyUnicodeObject *)(result))->str))
;
611 *u++ = '{';
612 /* Omit the brackets from the listrepr */
613 Py_UNICODE_COPY(u, PyUnicode_AS_UNICODE(listrepr)+1,((__builtin_object_size ((u), 0) != (size_t) -1) ? __builtin___memcpy_chk
((u), (((__builtin_expect(!(((((((PyObject*)(listrepr))->
ob_type))->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 613, "PyUnicode_Check(listrepr)"
) : (void)0),(((PyUnicodeObject *)(listrepr))->str))+1), (
((__builtin_expect(!(((((((PyObject*)(listrepr))->ob_type)
)->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 614, "PyUnicode_Check(listrepr)"
) : (void)0),(((PyUnicodeObject *)(listrepr))->length))-2)
*sizeof(Py_UNICODE), __builtin_object_size ((u), 0)) : __inline_memcpy_chk
((u), (((__builtin_expect(!(((((((PyObject*)(listrepr))->
ob_type))->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 613, "PyUnicode_Check(listrepr)"
) : (void)0),(((PyUnicodeObject *)(listrepr))->str))+1), (
((__builtin_expect(!(((((((PyObject*)(listrepr))->ob_type)
)->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 614, "PyUnicode_Check(listrepr)"
) : (void)0),(((PyUnicodeObject *)(listrepr))->length))-2)
*sizeof(Py_UNICODE)))
614 PyUnicode_GET_SIZE(listrepr)-2)((__builtin_object_size ((u), 0) != (size_t) -1) ? __builtin___memcpy_chk
((u), (((__builtin_expect(!(((((((PyObject*)(listrepr))->
ob_type))->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 613, "PyUnicode_Check(listrepr)"
) : (void)0),(((PyUnicodeObject *)(listrepr))->str))+1), (
((__builtin_expect(!(((((((PyObject*)(listrepr))->ob_type)
)->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 614, "PyUnicode_Check(listrepr)"
) : (void)0),(((PyUnicodeObject *)(listrepr))->length))-2)
*sizeof(Py_UNICODE), __builtin_object_size ((u), 0)) : __inline_memcpy_chk
((u), (((__builtin_expect(!(((((((PyObject*)(listrepr))->
ob_type))->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 613, "PyUnicode_Check(listrepr)"
) : (void)0),(((PyUnicodeObject *)(listrepr))->str))+1), (
((__builtin_expect(!(((((((PyObject*)(listrepr))->ob_type)
)->tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 614, "PyUnicode_Check(listrepr)"
) : (void)0),(((PyUnicodeObject *)(listrepr))->length))-2)
*sizeof(Py_UNICODE)))
;
615 u += newsize-2;
616 *u++ = '}';
617 }
618 Py_DECREF(listrepr)do { if (_Py_RefTotal-- , --((PyObject*)(listrepr))->ob_refcnt
!= 0) { if (((PyObject*)listrepr)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 618, (PyObject *)(listrepr)); } else _Py_Dealloc
((PyObject *)(listrepr)); } while (0)
;
619 if (Py_TYPE(so)(((PyObject*)(so))->ob_type) != &PySet_Type) {
620 PyObject *tmp = PyUnicode_FromFormatPyUnicodeUCS2_FromFormat("%s(%U)",
621 Py_TYPE(so)(((PyObject*)(so))->ob_type)->tp_name,
622 result);
623 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 623, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
624 result = tmp;
625 }
626done:
627 Py_ReprLeave((PyObject*)so);
628 return result;
629}
630
631static Py_ssize_t
632set_len(PyObject *so)
633{
634 return ((PySetObject *)so)->used;
635}
636
637static int
638set_merge(PySetObject *so, PyObject *otherset)
639{
640 PySetObject *other;
641 PyObject *key;
642 register Py_ssize_t i;
643 register setentry *entry;
644
645 assert (PyAnySet_Check(so))(__builtin_expect(!(((((PyObject*)(so))->ob_type) == &
PySet_Type || (((PyObject*)(so))->ob_type) == &PyFrozenSet_Type
|| PyType_IsSubtype((((PyObject*)(so))->ob_type), &PySet_Type
) || PyType_IsSubtype((((PyObject*)(so))->ob_type), &PyFrozenSet_Type
))), 0) ? __assert_rtn(__func__, "Objects/setobject.c", 645, "PyAnySet_Check(so)"
) : (void)0)
;
646 assert (PyAnySet_Check(otherset))(__builtin_expect(!(((((PyObject*)(otherset))->ob_type) ==
&PySet_Type || (((PyObject*)(otherset))->ob_type) == &
PyFrozenSet_Type || PyType_IsSubtype((((PyObject*)(otherset))
->ob_type), &PySet_Type) || PyType_IsSubtype((((PyObject
*)(otherset))->ob_type), &PyFrozenSet_Type))), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 646, "PyAnySet_Check(otherset)"
) : (void)0)
;
647
648 other = (PySetObject*)otherset;
649 if (other == so || other->used == 0)
650 /* a.update(a) or a.update({}); nothing to do */
651 return 0;
652 /* Do one big resize at the start, rather than
653 * incrementally resizing as we insert new keys. Expect
654 * that there will be no (or few) overlapping keys.
655 */
656 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
657 if (set_table_resize(so, (so->used + other->used)*2) != 0)
658 return -1;
659 }
660 for (i = 0; i <= other->mask; i++) {
661 entry = &other->table[i];
662 key = entry->key;
663 if (key != NULL((void*)0) &&
664 key != dummy) {
665 Py_INCREF(key)( _Py_RefTotal++ , ((PyObject*)(key))->ob_refcnt++);
666 if (set_insert_key(so, key, entry->hash) == -1) {
667 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 667, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
668 return -1;
669 }
670 }
671 }
672 return 0;
673}
674
675static int
676set_contains_key(PySetObject *so, PyObject *key)
677{
678 Py_hash_t hash;
679 setentry *entry;
680
681 if (!PyUnicode_CheckExact(key)((((PyObject*)(key))->ob_type) == &PyUnicode_Type) ||
682 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
683 hash = PyObject_Hash(key);
684 if (hash == -1)
685 return -1;
686 }
687 entry = (so->lookup)(so, key, hash);
688 if (entry == NULL((void*)0))
689 return -1;
690 key = entry->key;
691 return key != NULL((void*)0) && key != dummy;
692}
693
694static int
695set_contains_entry(PySetObject *so, setentry *entry)
696{
697 PyObject *key;
698 setentry *lu_entry;
699
700 lu_entry = (so->lookup)(so, entry->key, entry->hash);
701 if (lu_entry == NULL((void*)0))
702 return -1;
703 key = lu_entry->key;
704 return key != NULL((void*)0) && key != dummy;
705}
706
707static PyObject *
708set_pop(PySetObject *so)
709{
710 register Py_ssize_t i = 0;
711 register setentry *entry;
712 PyObject *key;
713
714 assert (PyAnySet_Check(so))(__builtin_expect(!(((((PyObject*)(so))->ob_type) == &
PySet_Type || (((PyObject*)(so))->ob_type) == &PyFrozenSet_Type
|| PyType_IsSubtype((((PyObject*)(so))->ob_type), &PySet_Type
) || PyType_IsSubtype((((PyObject*)(so))->ob_type), &PyFrozenSet_Type
))), 0) ? __assert_rtn(__func__, "Objects/setobject.c", 714, "PyAnySet_Check(so)"
) : (void)0)
;
715 if (so->used == 0) {
716 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
717 return NULL((void*)0);
718 }
719
720 /* Set entry to "the first" unused or dummy set entry. We abuse
721 * the hash field of slot 0 to hold a search finger:
722 * If slot 0 has a value, use slot 0.
723 * Else slot 0 is being used to hold a search finger,
724 * and we use its hash value as the first index to look.
725 */
726 entry = &so->table[0];
727 if (entry->key == NULL((void*)0) || entry->key == dummy) {
728 i = entry->hash;
729 /* The hash field may be a real hash value, or it may be a
730 * legit search finger, or it may be a once-legit search
731 * finger that's out of bounds now because it wrapped around
732 * or the table shrunk -- simply make sure it's in bounds now.
733 */
734 if (i > so->mask || i < 1)
735 i = 1; /* skip slot 0 */
736 while ((entry = &so->table[i])->key == NULL((void*)0) || entry->key==dummy) {
737 i++;
738 if (i > so->mask)
739 i = 1;
740 }
741 }
742 key = entry->key;
743 Py_INCREF(dummy)( _Py_RefTotal++ , ((PyObject*)(dummy))->ob_refcnt++);
744 entry->key = dummy;
745 so->used--;
746 so->table[0].hash = i + 1; /* next place to start */
747 return key;
748}
749
750PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.\n\static char pop_doc[] = "Remove and return an arbitrary set element.\nRaises KeyError if the set is empty."
751Raises KeyError if the set is empty.")static char pop_doc[] = "Remove and return an arbitrary set element.\nRaises KeyError if the set is empty.";
752
753static int
754set_traverse(PySetObject *so, visitproc visit, void *arg)
755{
756 Py_ssize_t pos = 0;
757 setentry *entry;
758
759 while (set_next(so, &pos, &entry))
760 Py_VISIT(entry->key)do { if (entry->key) { int vret = visit((PyObject *)(entry
->key), arg); if (vret) return vret; } } while (0)
;
761 return 0;
762}
763
764static Py_hash_t
765frozenset_hash(PyObject *self)
766{
767 PySetObject *so = (PySetObject *)self;
768 Py_hash_t h, hash = 1927868237L;
769 setentry *entry;
770 Py_ssize_t pos = 0;
771
772 if (so->hash != -1)
773 return so->hash;
774
775 hash *= PySet_GET_SIZE(self)(((PySetObject *)(self))->used) + 1;
776 while (set_next(so, &pos, &entry)) {
777 /* Work to increase the bit dispersion for closely spaced hash
778 values. The is important because some use cases have many
779 combinations of a small number of elements with nearby
780 hashes so that many distinct combinations collapse to only
781 a handful of distinct hash values. */
782 h = entry->hash;
783 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
784 }
785 hash = hash * 69069L + 907133923L;
786 if (hash == -1)
787 hash = 590923713L;
788 so->hash = hash;
789 return hash;
790}
791
792/***** Set iterator type ***********************************************/
793
794typedef struct {
795 PyObject_HEADPyObject ob_base;
796 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
797 Py_ssize_t si_used;
798 Py_ssize_t si_pos;
799 Py_ssize_t len;
800} setiterobject;
801
802static void
803setiter_dealloc(setiterobject *si)
804{
805 Py_XDECREF(si->si_set)do { if ((si->si_set) == ((void*)0)) ; else do { if (_Py_RefTotal
-- , --((PyObject*)(si->si_set))->ob_refcnt != 0) { if (
((PyObject*)si->si_set)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 805, (PyObject *)(si->si_set)); } else
_Py_Dealloc((PyObject *)(si->si_set)); } while (0); } while
(0)
;
806 PyObject_GC_Del(si);
807}
808
809static int
810setiter_traverse(setiterobject *si, visitproc visit, void *arg)
811{
812 Py_VISIT(si->si_set)do { if (si->si_set) { int vret = visit((PyObject *)(si->
si_set), arg); if (vret) return vret; } } while (0)
;
813 return 0;
814}
815
816static PyObject *
817setiter_len(setiterobject *si)
818{
819 Py_ssize_t len = 0;
820 if (si->si_set != NULL((void*)0) && si->si_used == si->si_set->used)
821 len = si->len;
822 return PyLong_FromSsize_t(len);
823}
824
825PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).")static char length_hint_doc[] = "Private method returning an estimate of len(list(it)).";
826
827static PyMethodDef setiter_methods[] = {
828 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS0x0004, length_hint_doc},
829 {NULL((void*)0), NULL((void*)0)} /* sentinel */
830};
831
832static PyObject *setiter_iternext(setiterobject *si)
833{
834 PyObject *key;
835 register Py_ssize_t i, mask;
836 register setentry *entry;
837 PySetObject *so = si->si_set;
838
839 if (so == NULL((void*)0))
840 return NULL((void*)0);
841 assert (PyAnySet_Check(so))(__builtin_expect(!(((((PyObject*)(so))->ob_type) == &
PySet_Type || (((PyObject*)(so))->ob_type) == &PyFrozenSet_Type
|| PyType_IsSubtype((((PyObject*)(so))->ob_type), &PySet_Type
) || PyType_IsSubtype((((PyObject*)(so))->ob_type), &PyFrozenSet_Type
))), 0) ? __assert_rtn(__func__, "Objects/setobject.c", 841, "PyAnySet_Check(so)"
) : (void)0)
;
842
843 if (si->si_used != so->used) {
844 PyErr_SetString(PyExc_RuntimeError,
845 "Set changed size during iteration");
846 si->si_used = -1; /* Make this state sticky */
847 return NULL((void*)0);
848 }
849
850 i = si->si_pos;
851 assert(i>=0)(__builtin_expect(!(i>=0), 0) ? __assert_rtn(__func__, "Objects/setobject.c"
, 851, "i>=0") : (void)0)
;
852 entry = so->table;
853 mask = so->mask;
854 while (i <= mask && (entry[i].key == NULL((void*)0) || entry[i].key == dummy))
855 i++;
856 si->si_pos = i+1;
857 if (i > mask)
858 goto fail;
859 si->len--;
860 key = entry[i].key;
861 Py_INCREF(key)( _Py_RefTotal++ , ((PyObject*)(key))->ob_refcnt++);
862 return key;
863
864fail:
865 Py_DECREF(so)do { if (_Py_RefTotal-- , --((PyObject*)(so))->ob_refcnt !=
0) { if (((PyObject*)so)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 865, (PyObject *)(so)); } else _Py_Dealloc
((PyObject *)(so)); } while (0)
;
866 si->si_set = NULL((void*)0);
867 return NULL((void*)0);
868}
869
870PyTypeObject PySetIter_Type = {
871 PyVarObject_HEAD_INIT(&PyType_Type, 0){ { 0, 0, 1, &PyType_Type }, 0 },
872 "set_iterator", /* tp_name */
873 sizeof(setiterobject), /* tp_basicsize */
874 0, /* tp_itemsize */
875 /* methods */
876 (destructor)setiter_dealloc, /* tp_dealloc */
877 0, /* tp_print */
878 0, /* tp_getattr */
879 0, /* tp_setattr */
880 0, /* tp_reserved */
881 0, /* tp_repr */
882 0, /* tp_as_number */
883 0, /* tp_as_sequence */
884 0, /* tp_as_mapping */
885 0, /* tp_hash */
886 0, /* tp_call */
887 0, /* tp_str */
888 PyObject_GenericGetAttr, /* tp_getattro */
889 0, /* tp_setattro */
890 0, /* tp_as_buffer */
891 Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0) | Py_TPFLAGS_HAVE_GC(1L<<14),/* tp_flags */
892 0, /* tp_doc */
893 (traverseproc)setiter_traverse, /* tp_traverse */
894 0, /* tp_clear */
895 0, /* tp_richcompare */
896 0, /* tp_weaklistoffset */
897 PyObject_SelfIter, /* tp_iter */
898 (iternextfunc)setiter_iternext, /* tp_iternext */
899 setiter_methods, /* tp_methods */
900 0,
901};
902
903static PyObject *
904set_iter(PySetObject *so)
905{
906 setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type)( (setiterobject *) _PyObject_GC_New(&PySetIter_Type) );
907 if (si == NULL((void*)0))
908 return NULL((void*)0);
909 Py_INCREF(so)( _Py_RefTotal++ , ((PyObject*)(so))->ob_refcnt++);
910 si->si_set = so;
911 si->si_used = so->used;
912 si->si_pos = 0;
913 si->len = so->used;
914 _PyObject_GC_TRACK(si)do { PyGC_Head *g = ((PyGC_Head *)(si)-1); if (g->gc.gc_refs
!= (-2)) Py_FatalError("GC object already tracked"); g->gc
.gc_refs = (-3); g->gc.gc_next = _PyGC_generation0; g->
gc.gc_prev = _PyGC_generation0->gc.gc_prev; g->gc.gc_prev
->gc.gc_next = g; _PyGC_generation0->gc.gc_prev = g; } while
(0);
;
915 return (PyObject *)si;
916}
917
918static int
919set_update_internal(PySetObject *so, PyObject *other)
920{
921 PyObject *key, *it;
922
923 if (PyAnySet_Check(other)((((PyObject*)(other))->ob_type) == &PySet_Type || (((
PyObject*)(other))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PyFrozenSet_Type))
)
924 return set_merge(so, other);
925
926 if (PyDict_CheckExact(other)((((PyObject*)(other))->ob_type) == &PyDict_Type)) {
927 PyObject *value;
928 Py_ssize_t pos = 0;
929 Py_hash_t hash;
930 Py_ssize_t dictsize = PyDict_Size(other);
931
932 /* Do one big resize at the start, rather than
933 * incrementally resizing as we insert new keys. Expect
934 * that there will be no (or few) overlapping keys.
935 */
936 if (dictsize == -1)
937 return -1;
938 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
939 if (set_table_resize(so, (so->used + dictsize)*2) != 0)
940 return -1;
941 }
942 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
943 setentry an_entry;
944
945 an_entry.hash = hash;
946 an_entry.key = key;
947 if (set_add_entry(so, &an_entry) == -1)
948 return -1;
949 }
950 return 0;
951 }
952
953 it = PyObject_GetIter(other);
954 if (it == NULL((void*)0))
955 return -1;
956
957 while ((key = PyIter_Next(it)) != NULL((void*)0)) {
958 if (set_add_key(so, key) == -1) {
959 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 959, (PyObject *)(it)); } else _Py_Dealloc
((PyObject *)(it)); } while (0)
;
960 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 960, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
961 return -1;
962 }
963 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 963, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
964 }
965 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 965, (PyObject *)(it)); } else _Py_Dealloc
((PyObject *)(it)); } while (0)
;
966 if (PyErr_Occurred())
967 return -1;
968 return 0;
969}
970
971static PyObject *
972set_update(PySetObject *so, PyObject *args)
973{
974 Py_ssize_t i;
975
976 for (i=0 ; i<PyTuple_GET_SIZE(args)(((PyVarObject*)(args))->ob_size) ; i++) {
977 PyObject *other = PyTuple_GET_ITEM(args, i)(((PyTupleObject *)(args))->ob_item[i]);
978 if (set_update_internal(so, other) == -1)
979 return NULL((void*)0);
980 }
981 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
982}
983
984PyDoc_STRVAR(update_doc,static char update_doc[] = "Update a set with the union of itself and others."
985"Update a set with the union of itself and others.")static char update_doc[] = "Update a set with the union of itself and others.";
986
987static PyObject *
988make_new_set(PyTypeObject *type, PyObject *iterable)
989{
990 register PySetObject *so = NULL((void*)0);
991
992 if (dummy == NULL((void*)0)) { /* Auto-initialize dummy */
993 dummy = PyUnicode_FromStringPyUnicodeUCS2_FromString("<dummy key>");
994 if (dummy == NULL((void*)0))
995 return NULL((void*)0);
996 }
997
998 /* create PySetObject structure */
999 if (numfree &&
1000 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
1001 so = free_list[--numfree];
1002 assert (so != NULL && PyAnySet_CheckExact(so))(__builtin_expect(!(so != ((void*)0) && ((((PyObject*
)(so))->ob_type) == &PySet_Type || (((PyObject*)(so))->
ob_type) == &PyFrozenSet_Type)), 0) ? __assert_rtn(__func__
, "Objects/setobject.c", 1002, "so != NULL && PyAnySet_CheckExact(so)"
) : (void)0)
;
1003 Py_TYPE(so)(((PyObject*)(so))->ob_type) = type;
1004 _Py_NewReference((PyObject *)so);
1005 EMPTY_TO_MINSIZE(so)do { ((__builtin_object_size ((so)->smalltable, 0) != (size_t
) -1) ? __builtin___memset_chk ((so)->smalltable, 0, sizeof
((so)->smalltable), __builtin_object_size ((so)->smalltable
, 0)) : __inline_memset_chk ((so)->smalltable, 0, sizeof((
so)->smalltable))); (so)->used = (so)->fill = 0; do {
(so)->table = (so)->smalltable; (so)->mask = 8 - 1;
(so)->hash = -1; } while(0); } while(0)
;
1006 PyObject_GC_Track(so);
1007 } else {
1008 so = (PySetObject *)type->tp_alloc(type, 0);
1009 if (so == NULL((void*)0))
1010 return NULL((void*)0);
1011 /* tp_alloc has already zeroed the structure */
1012 assert(so->table == NULL && so->fill == 0 && so->used == 0)(__builtin_expect(!(so->table == ((void*)0) && so->
fill == 0 && so->used == 0), 0) ? __assert_rtn(__func__
, "Objects/setobject.c", 1012, "so->table == NULL && so->fill == 0 && so->used == 0"
) : (void)0)
;
1013 INIT_NONZERO_SET_SLOTS(so)do { (so)->table = (so)->smalltable; (so)->mask = 8 -
1; (so)->hash = -1; } while(0)
;
1014 }
1015
1016 so->lookup = set_lookkey_unicode;
1017 so->weakreflist = NULL((void*)0);
1018
1019 if (iterable != NULL((void*)0)) {
1020 if (set_update_internal(so, iterable) == -1) {
1021 Py_DECREF(so)do { if (_Py_RefTotal-- , --((PyObject*)(so))->ob_refcnt !=
0) { if (((PyObject*)so)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1021, (PyObject *)(so)); } else _Py_Dealloc
((PyObject *)(so)); } while (0)
;
1022 return NULL((void*)0);
1023 }
1024 }
1025
1026 return (PyObject *)so;
1027}
1028
1029static PyObject *
1030make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1031{
1032 if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1033 if (PyType_IsSubtype(type, &PySet_Type))
1034 type = &PySet_Type;
1035 else
1036 type = &PyFrozenSet_Type;
1037 }
1038 return make_new_set(type, iterable);
1039}
1040
1041/* The empty frozenset is a singleton */
1042static PyObject *emptyfrozenset = NULL((void*)0);
1043
1044static PyObject *
1045frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1046{
1047 PyObject *iterable = NULL((void*)0), *result;
1048
1049 if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
1050 return NULL((void*)0);
1051
1052 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
1053 return NULL((void*)0);
1054
1055 if (type != &PyFrozenSet_Type)
1056 return make_new_set(type, iterable);
1057
1058 if (iterable != NULL((void*)0)) {
1059 /* frozenset(f) is idempotent */
1060 if (PyFrozenSet_CheckExact(iterable)((((PyObject*)(iterable))->ob_type) == &PyFrozenSet_Type
)
) {
1061 Py_INCREF(iterable)( _Py_RefTotal++ , ((PyObject*)(iterable))->ob_refcnt++);
1062 return iterable;
1063 }
1064 result = make_new_set(type, iterable);
1065 if (result == NULL((void*)0) || PySet_GET_SIZE(result)(((PySetObject *)(result))->used))
1066 return result;
1067 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1067, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
1068 }
1069 /* The empty frozenset is a singleton */
1070 if (emptyfrozenset == NULL((void*)0))
1071 emptyfrozenset = make_new_set(type, NULL((void*)0));
1072 Py_XINCREF(emptyfrozenset)do { if ((emptyfrozenset) == ((void*)0)) ; else ( _Py_RefTotal
++ , ((PyObject*)(emptyfrozenset))->ob_refcnt++); } while (
0)
;
1073 return emptyfrozenset;
1074}
1075
1076void
1077PySet_Fini(void)
1078{
1079 PySetObject *so;
1080
1081 while (numfree) {
1082 numfree--;
1083 so = free_list[numfree];
1084 PyObject_GC_Del(so);
1085 }
1086 Py_CLEAR(dummy)do { if (dummy) { PyObject *_py_tmp = (PyObject *)(dummy); (dummy
) = ((void*)0); do { if (_Py_RefTotal-- , --((PyObject*)(_py_tmp
))->ob_refcnt != 0) { if (((PyObject*)_py_tmp)->ob_refcnt
< 0) _Py_NegativeRefcount("Objects/setobject.c", 1086, (PyObject
*)(_py_tmp)); } else _Py_Dealloc((PyObject *)(_py_tmp)); } while
(0); } } while (0)
;
1087 Py_CLEAR(emptyfrozenset)do { if (emptyfrozenset) { PyObject *_py_tmp = (PyObject *)(emptyfrozenset
); (emptyfrozenset) = ((void*)0); do { if (_Py_RefTotal-- , --
((PyObject*)(_py_tmp))->ob_refcnt != 0) { if (((PyObject*)
_py_tmp)->ob_refcnt < 0) _Py_NegativeRefcount("Objects/setobject.c"
, 1087, (PyObject *)(_py_tmp)); } else _Py_Dealloc((PyObject *
)(_py_tmp)); } while (0); } } while (0)
;
1088}
1089
1090static PyObject *
1091set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1092{
1093 if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
1094 return NULL((void*)0);
1095
1096 return make_new_set(type, NULL((void*)0));
1097}
1098
1099/* set_swap_bodies() switches the contents of any two sets by moving their
1100 internal data pointers and, if needed, copying the internal smalltables.
1101 Semantically equivalent to:
1102
1103 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1104
1105 The function always succeeds and it leaves both objects in a stable state.
1106 Useful for creating temporary frozensets from sets for membership testing
1107 in __contains__(), discard(), and remove(). Also useful for operations
1108 that update in-place (by allowing an intermediate result to be swapped
1109 into one of the original inputs).
1110*/
1111
1112static void
1113set_swap_bodies(PySetObject *a, PySetObject *b)
1114{
1115 Py_ssize_t t;
1116 setentry *u;
1117 setentry *(*f)(PySetObject *so, PyObject *key, Py_ssize_t hash);
1118 setentry tab[PySet_MINSIZE8];
1119 Py_hash_t h;
1120
1121 t = a->fill; a->fill = b->fill; b->fill = t;
1122 t = a->used; a->used = b->used; b->used = t;
1123 t = a->mask; a->mask = b->mask; b->mask = t;
1124
1125 u = a->table;
1126 if (a->table == a->smalltable)
1127 u = b->smalltable;
1128 a->table = b->table;
1129 if (b->table == b->smalltable)
1130 a->table = a->smalltable;
1131 b->table = u;
1132
1133 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
1134
1135 if (a->table == a->smalltable || b->table == b->smalltable) {
1136 memcpy(tab, a->smalltable, sizeof(tab))((__builtin_object_size (tab, 0) != (size_t) -1) ? __builtin___memcpy_chk
(tab, a->smalltable, sizeof(tab), __builtin_object_size (
tab, 0)) : __inline_memcpy_chk (tab, a->smalltable, sizeof
(tab)))
;
1137 memcpy(a->smalltable, b->smalltable, sizeof(tab))((__builtin_object_size (a->smalltable, 0) != (size_t) -1)
? __builtin___memcpy_chk (a->smalltable, b->smalltable
, sizeof(tab), __builtin_object_size (a->smalltable, 0)) :
__inline_memcpy_chk (a->smalltable, b->smalltable, sizeof
(tab)))
;
1138 memcpy(b->smalltable, tab, sizeof(tab))((__builtin_object_size (b->smalltable, 0) != (size_t) -1)
? __builtin___memcpy_chk (b->smalltable, tab, sizeof(tab)
, __builtin_object_size (b->smalltable, 0)) : __inline_memcpy_chk
(b->smalltable, tab, sizeof(tab)))
;
1139 }
1140
1141 if (PyType_IsSubtype(Py_TYPE(a)(((PyObject*)(a))->ob_type), &PyFrozenSet_Type) &&
1142 PyType_IsSubtype(Py_TYPE(b)(((PyObject*)(b))->ob_type), &PyFrozenSet_Type)) {
1143 h = a->hash; a->hash = b->hash; b->hash = h;
1144 } else {
1145 a->hash = -1;
1146 b->hash = -1;
1147 }
1148}
1149
1150static PyObject *
1151set_copy(PySetObject *so)
1152{
1153 return make_new_set_basetype(Py_TYPE(so)(((PyObject*)(so))->ob_type), (PyObject *)so);
1154}
1155
1156static PyObject *
1157frozenset_copy(PySetObject *so)
1158{
1159 if (PyFrozenSet_CheckExact(so)((((PyObject*)(so))->ob_type) == &PyFrozenSet_Type)) {
1160 Py_INCREF(so)( _Py_RefTotal++ , ((PyObject*)(so))->ob_refcnt++);
1161 return (PyObject *)so;
1162 }
1163 return set_copy(so);
1164}
1165
1166PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.")static char copy_doc[] = "Return a shallow copy of a set.";
1167
1168static PyObject *
1169set_clear(PySetObject *so)
1170{
1171 set_clear_internal(so);
1172 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1173}
1174
1175PyDoc_STRVAR(clear_doc, "Remove all elements from this set.")static char clear_doc[] = "Remove all elements from this set.";
1176
1177static PyObject *
1178set_union(PySetObject *so, PyObject *args)
1179{
1180 PySetObject *result;
1181 PyObject *other;
1182 Py_ssize_t i;
1183
1184 result = (PySetObject *)set_copy(so);
1185 if (result == NULL((void*)0))
1186 return NULL((void*)0);
1187
1188 for (i=0 ; i<PyTuple_GET_SIZE(args)(((PyVarObject*)(args))->ob_size) ; i++) {
1189 other = PyTuple_GET_ITEM(args, i)(((PyTupleObject *)(args))->ob_item[i]);
1190 if ((PyObject *)so == other)
1191 continue;
1192 if (set_update_internal(result, other) == -1) {
1193 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1193, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
1194 return NULL((void*)0);
1195 }
1196 }
1197 return (PyObject *)result;
1198}
1199
1200PyDoc_STRVAR(union_doc,static char union_doc[] = "Return the union of sets as a new set.\n\n(i.e. all elements that are in either set.)"
1201 "Return the union of sets as a new set.\n\static char union_doc[] = "Return the union of sets as a new set.\n\n(i.e. all elements that are in either set.)"
1202\n\static char union_doc[] = "Return the union of sets as a new set.\n\n(i.e. all elements that are in either set.)"
1203(i.e. all elements that are in either set.)")static char union_doc[] = "Return the union of sets as a new set.\n\n(i.e. all elements that are in either set.)";
1204
1205static PyObject *
1206set_or(PySetObject *so, PyObject *other)
1207{
1208 PySetObject *result;
1209
1210 if (!PyAnySet_Check(so)((((PyObject*)(so))->ob_type) == &PySet_Type || (((PyObject
*)(so))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(so))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(so))->ob_type), &PyFrozenSet_Type))
|| !PyAnySet_Check(other)((((PyObject*)(other))->ob_type) == &PySet_Type || (((
PyObject*)(other))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PyFrozenSet_Type))
) {
1211 Py_INCREF(Py_NotImplemented)( _Py_RefTotal++ , ((PyObject*)((&_Py_NotImplementedStruct
)))->ob_refcnt++)
;
1212 return Py_NotImplemented(&_Py_NotImplementedStruct);
1213 }
1214
1215 result = (PySetObject *)set_copy(so);
1216 if (result == NULL((void*)0))
1217 return NULL((void*)0);
1218 if ((PyObject *)so == other)
1219 return (PyObject *)result;
1220 if (set_update_internal(result, other) == -1) {
1221 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1221, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
1222 return NULL((void*)0);
1223 }
1224 return (PyObject *)result;
1225}
1226
1227static PyObject *
1228set_ior(PySetObject *so, PyObject *other)
1229{
1230 if (!PyAnySet_Check(other)((((PyObject*)(other))->ob_type) == &PySet_Type || (((
PyObject*)(other))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PyFrozenSet_Type))
) {
1231 Py_INCREF(Py_NotImplemented)( _Py_RefTotal++ , ((PyObject*)((&_Py_NotImplementedStruct
)))->ob_refcnt++)
;
1232 return Py_NotImplemented(&_Py_NotImplementedStruct);
1233 }
1234 if (set_update_internal(so, other) == -1)
1235 return NULL((void*)0);
1236 Py_INCREF(so)( _Py_RefTotal++ , ((PyObject*)(so))->ob_refcnt++);
1237 return (PyObject *)so;
1238}
1239
1240static PyObject *
1241set_intersection(PySetObject *so, PyObject *other)
1242{
1243 PySetObject *result;
1244 PyObject *key, *it, *tmp;
1245
1246 if ((PyObject *)so == other)
1247 return set_copy(so);
1248
1249 result = (PySetObject *)make_new_set_basetype(Py_TYPE(so)(((PyObject*)(so))->ob_type), NULL((void*)0));
1250 if (result == NULL((void*)0))
1251 return NULL((void*)0);
1252
1253 if (PyAnySet_Check(other)((((PyObject*)(other))->ob_type) == &PySet_Type || (((
PyObject*)(other))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PyFrozenSet_Type))
) {
1254 Py_ssize_t pos = 0;
1255 setentry *entry;
1256
1257 if (PySet_GET_SIZE(other)(((PySetObject *)(other))->used) > PySet_GET_SIZE(so)(((PySetObject *)(so))->used)) {
1258 tmp = (PyObject *)so;
1259 so = (PySetObject *)other;
1260 other = tmp;
1261 }
1262
1263 while (set_next((PySetObject *)other, &pos, &entry)) {
1264 int rv = set_contains_entry(so, entry);
1265 if (rv == -1) {
1266 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1266, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
1267 return NULL((void*)0);
1268 }
1269 if (rv) {
1270 if (set_add_entry(result, entry) == -1) {
1271 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1271, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
1272 return NULL((void*)0);
1273 }
1274 }
1275 }
1276 return (PyObject *)result;
1277 }
1278
1279 it = PyObject_GetIter(other);
1280 if (it == NULL((void*)0)) {
1281 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1281, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
1282 return NULL((void*)0);
1283 }
1284
1285 while ((key = PyIter_Next(it)) != NULL((void*)0)) {
1286 int rv;
1287 setentry entry;
1288 Py_hash_t hash = PyObject_Hash(key);
1289
1290 if (hash == -1) {
1291 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1291, (PyObject *)(it)); } else _Py_Dealloc
((PyObject *)(it)); } while (0)
;
1292 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1292, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
1293 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1293, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
1294 return NULL((void*)0);
1295 }
1296 entry.hash = hash;
1297 entry.key = key;
1298 rv = set_contains_entry(so, &entry);
1299 if (rv == -1) {
1300 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1300, (PyObject *)(it)); } else _Py_Dealloc
((PyObject *)(it)); } while (0)
;
1301 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1301, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
1302 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1302, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
1303 return NULL((void*)0);
1304 }
1305 if (rv) {
1306 if (set_add_entry(result, &entry) == -1) {
1307 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1307, (PyObject *)(it)); } else _Py_Dealloc
((PyObject *)(it)); } while (0)
;
1308 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1308, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
1309 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1309, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
1310 return NULL((void*)0);
1311 }
1312 }
1313 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1313, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
1314 }
1315 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1315, (PyObject *)(it)); } else _Py_Dealloc
((PyObject *)(it)); } while (0)
;
1316 if (PyErr_Occurred()) {
1317 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1317, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
1318 return NULL((void*)0);
1319 }
1320 return (PyObject *)result;
1321}
1322
1323static PyObject *
1324set_intersection_multi(PySetObject *so, PyObject *args)
1325{
1326 Py_ssize_t i;
1327 PyObject *result = (PyObject *)so;
1328
1329 if (PyTuple_GET_SIZE(args)(((PyVarObject*)(args))->ob_size) == 0)
1330 return set_copy(so);
1331
1332 Py_INCREF(so)( _Py_RefTotal++ , ((PyObject*)(so))->ob_refcnt++);
1333 for (i=0 ; i<PyTuple_GET_SIZE(args)(((PyVarObject*)(args))->ob_size) ; i++) {
1334 PyObject *other = PyTuple_GET_ITEM(args, i)(((PyTupleObject *)(args))->ob_item[i]);
1335 PyObject *newresult = set_intersection((PySetObject *)result, other);
1336 if (newresult == NULL((void*)0)) {
1337 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1337, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
1338 return NULL((void*)0);
1339 }
1340 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1340, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
1341 result = newresult;
1342 }
1343 return result;
1344}
1345
1346PyDoc_STRVAR(intersection_doc,static char intersection_doc[] = "Return the intersection of two sets as a new set.\n\n(i.e. all elements that are in both sets.)"
1347"Return the intersection of two sets as a new set.\n\static char intersection_doc[] = "Return the intersection of two sets as a new set.\n\n(i.e. all elements that are in both sets.)"
1348\n\static char intersection_doc[] = "Return the intersection of two sets as a new set.\n\n(i.e. all elements that are in both sets.)"
1349(i.e. all elements that are in both sets.)")static char intersection_doc[] = "Return the intersection of two sets as a new set.\n\n(i.e. all elements that are in both sets.)";
1350
1351static PyObject *
1352set_intersection_update(PySetObject *so, PyObject *other)
1353{
1354 PyObject *tmp;
1355
1356 tmp = set_intersection(so, other);
1357 if (tmp == NULL((void*)0))
1358 return NULL((void*)0);
1359 set_swap_bodies(so, (PySetObject *)tmp);
1360 Py_DECREF(tmp)do { if (_Py_RefTotal-- , --((PyObject*)(tmp))->ob_refcnt !=
0) { if (((PyObject*)tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1360, (PyObject *)(tmp)); } else _Py_Dealloc
((PyObject *)(tmp)); } while (0)
;
1361 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1362}
1363
1364static PyObject *
1365set_intersection_update_multi(PySetObject *so, PyObject *args)
1366{
1367 PyObject *tmp;
1368
1369 tmp = set_intersection_multi(so, args);
1370 if (tmp == NULL((void*)0))
1371 return NULL((void*)0);
1372 set_swap_bodies(so, (PySetObject *)tmp);
1373 Py_DECREF(tmp)do { if (_Py_RefTotal-- , --((PyObject*)(tmp))->ob_refcnt !=
0) { if (((PyObject*)tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1373, (PyObject *)(tmp)); } else _Py_Dealloc
((PyObject *)(tmp)); } while (0)
;
1374 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1375}
1376
1377PyDoc_STRVAR(intersection_update_doc,static char intersection_update_doc[] = "Update a set with the intersection of itself and another."
1378"Update a set with the intersection of itself and another.")static char intersection_update_doc[] = "Update a set with the intersection of itself and another.";
1379
1380static PyObject *
1381set_and(PySetObject *so, PyObject *other)
1382{
1383 if (!PyAnySet_Check(so)((((PyObject*)(so))->ob_type) == &PySet_Type || (((PyObject
*)(so))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(so))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(so))->ob_type), &PyFrozenSet_Type))
|| !PyAnySet_Check(other)((((PyObject*)(other))->ob_type) == &PySet_Type || (((
PyObject*)(other))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PyFrozenSet_Type))
) {
1384 Py_INCREF(Py_NotImplemented)( _Py_RefTotal++ , ((PyObject*)((&_Py_NotImplementedStruct
)))->ob_refcnt++)
;
1385 return Py_NotImplemented(&_Py_NotImplementedStruct);
1386 }
1387 return set_intersection(so, other);
1388}
1389
1390static PyObject *
1391set_iand(PySetObject *so, PyObject *other)
1392{
1393 PyObject *result;
1394
1395 if (!PyAnySet_Check(other)((((PyObject*)(other))->ob_type) == &PySet_Type || (((
PyObject*)(other))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PyFrozenSet_Type))
) {
1396 Py_INCREF(Py_NotImplemented)( _Py_RefTotal++ , ((PyObject*)((&_Py_NotImplementedStruct
)))->ob_refcnt++)
;
1397 return Py_NotImplemented(&_Py_NotImplementedStruct);
1398 }
1399 result = set_intersection_update(so, other);
1400 if (result == NULL((void*)0))
1401 return NULL((void*)0);
1402 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1402, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
1403 Py_INCREF(so)( _Py_RefTotal++ , ((PyObject*)(so))->ob_refcnt++);
1404 return (PyObject *)so;
1405}
1406
1407static PyObject *
1408set_isdisjoint(PySetObject *so, PyObject *other)
1409{
1410 PyObject *key, *it, *tmp;
1411
1412 if ((PyObject *)so == other) {
1413 if (PySet_GET_SIZE(so)(((PySetObject *)(so))->used) == 0)
1414 Py_RETURN_TRUEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_TrueStruct
)))->ob_refcnt++), ((PyObject *) &_Py_TrueStruct)
;
1415 else
1416 Py_RETURN_FALSEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_FalseStruct
)))->ob_refcnt++), ((PyObject *) &_Py_FalseStruct)
;
1417 }
1418
1419 if (PyAnySet_CheckExact(other)((((PyObject*)(other))->ob_type) == &PySet_Type || (((
PyObject*)(other))->ob_type) == &PyFrozenSet_Type)
) {
1420 Py_ssize_t pos = 0;
1421 setentry *entry;
1422
1423 if (PySet_GET_SIZE(other)(((PySetObject *)(other))->used) > PySet_GET_SIZE(so)(((PySetObject *)(so))->used)) {
1424 tmp = (PyObject *)so;
1425 so = (PySetObject *)other;
1426 other = tmp;
1427 }
1428 while (set_next((PySetObject *)other, &pos, &entry)) {
1429 int rv = set_contains_entry(so, entry);
1430 if (rv == -1)
1431 return NULL((void*)0);
1432 if (rv)
1433 Py_RETURN_FALSEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_FalseStruct
)))->ob_refcnt++), ((PyObject *) &_Py_FalseStruct)
;
1434 }
1435 Py_RETURN_TRUEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_TrueStruct
)))->ob_refcnt++), ((PyObject *) &_Py_TrueStruct)
;
1436 }
1437
1438 it = PyObject_GetIter(other);
1439 if (it == NULL((void*)0))
1440 return NULL((void*)0);
1441
1442 while ((key = PyIter_Next(it)) != NULL((void*)0)) {
1443 int rv;
1444 setentry entry;
1445 Py_hash_t hash = PyObject_Hash(key);
1446
1447 if (hash == -1) {
1448 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1448, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
1449 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1449, (PyObject *)(it)); } else _Py_Dealloc
((PyObject *)(it)); } while (0)
;
1450 return NULL((void*)0);
1451 }
1452 entry.hash = hash;
1453 entry.key = key;
1454 rv = set_contains_entry(so, &entry);
1455 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1455, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
1456 if (rv == -1) {
1457 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1457, (PyObject *)(it)); } else _Py_Dealloc
((PyObject *)(it)); } while (0)
;
1458 return NULL((void*)0);
1459 }
1460 if (rv) {
1461 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1461, (PyObject *)(it)); } else _Py_Dealloc
((PyObject *)(it)); } while (0)
;
1462 Py_RETURN_FALSEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_FalseStruct
)))->ob_refcnt++), ((PyObject *) &_Py_FalseStruct)
;
1463 }
1464 }
1465 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1465, (PyObject *)(it)); } else _Py_Dealloc
((PyObject *)(it)); } while (0)
;
1466 if (PyErr_Occurred())
1467 return NULL((void*)0);
1468 Py_RETURN_TRUEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_TrueStruct
)))->ob_refcnt++), ((PyObject *) &_Py_TrueStruct)
;
1469}
1470
1471PyDoc_STRVAR(isdisjoint_doc,static char isdisjoint_doc[] = "Return True if two sets have a null intersection."
1472"Return True if two sets have a null intersection.")static char isdisjoint_doc[] = "Return True if two sets have a null intersection.";
1473
1474static int
1475set_difference_update_internal(PySetObject *so, PyObject *other)
1476{
1477 if ((PyObject *)so == other)
1478 return set_clear_internal(so);
1479
1480 if (PyAnySet_Check(other)((((PyObject*)(other))->ob_type) == &PySet_Type || (((
PyObject*)(other))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PyFrozenSet_Type))
) {
1481 setentry *entry;
1482 Py_ssize_t pos = 0;
1483
1484 while (set_next((PySetObject *)other, &pos, &entry))
1485 if (set_discard_entry(so, entry) == -1)
1486 return -1;
1487 } else {
1488 PyObject *key, *it;
1489 it = PyObject_GetIter(other);
1490 if (it == NULL((void*)0))
1491 return -1;
1492
1493 while ((key = PyIter_Next(it)) != NULL((void*)0)) {
1494 if (set_discard_key(so, key) == -1) {
1495 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1495, (PyObject *)(it)); } else _Py_Dealloc
((PyObject *)(it)); } while (0)
;
1496 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1496, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
1497 return -1;
1498 }
1499 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1499, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
1500 }
1501 Py_DECREF(it)do { if (_Py_RefTotal-- , --((PyObject*)(it))->ob_refcnt !=
0) { if (((PyObject*)it)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1501, (PyObject *)(it)); } else _Py_Dealloc
((PyObject *)(it)); } while (0)
;
1502 if (PyErr_Occurred())
1503 return -1;
1504 }
1505 /* If more than 1/5 are dummies, then resize them away. */
1506 if ((so->fill - so->used) * 5 < so->mask)
1507 return 0;
1508 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1509}
1510
1511static PyObject *
1512set_difference_update(PySetObject *so, PyObject *args)
1513{
1514 Py_ssize_t i;
1515
1516 for (i=0 ; i<PyTuple_GET_SIZE(args)(((PyVarObject*)(args))->ob_size) ; i++) {
1517 PyObject *other = PyTuple_GET_ITEM(args, i)(((PyTupleObject *)(args))->ob_item[i]);
1518 if (set_difference_update_internal(so, other) == -1)
1519 return NULL((void*)0);
1520 }
1521 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1522}
1523
1524PyDoc_STRVAR(difference_update_doc,static char difference_update_doc[] = "Remove all elements of another set from this set."
1525"Remove all elements of another set from this set.")static char difference_update_doc[] = "Remove all elements of another set from this set.";
1526
1527static PyObject *
1528set_copy_and_difference(PySetObject *so, PyObject *other)
1529{
1530 PyObject *result;
1531
1532 result = set_copy(so);
1533 if (result == NULL((void*)0))
1534 return NULL((void*)0);
1535 if (set_difference_update_internal((PySetObject *) result, other) != -1)
1536 return result;
1537 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1537, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
1538 return NULL((void*)0);
1539}
1540
1541static PyObject *
1542set_difference(PySetObject *so, PyObject *other)
1543{
1544 PyObject *result;
1545 setentry *entry;
1546 Py_ssize_t pos = 0;
1547
1548 if (!PyAnySet_Check(other)((((PyObject*)(other))->ob_type) == &PySet_Type || (((
PyObject*)(other))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PyFrozenSet_Type))
&& !PyDict_CheckExact(other)((((PyObject*)(other))->ob_type) == &PyDict_Type)) {
1549 return set_copy_and_difference(so, other);
1550 }
1551
1552 /* If len(so) much more than len(other), it's more efficient to simply copy
1553 * so and then iterate other looking for common elements. */
1554 if ((PySet_GET_SIZE(so)(((PySetObject *)(so))->used) >> 2) > PyObject_Size(other)) {
1555 return set_copy_and_difference(so, other);
1556 }
1557
1558 result = make_new_set_basetype(Py_TYPE(so)(((PyObject*)(so))->ob_type), NULL((void*)0));
1559 if (result == NULL((void*)0))
1560 return NULL((void*)0);
1561
1562 if (PyDict_CheckExact(other)((((PyObject*)(other))->ob_type) == &PyDict_Type)) {
1563 while (set_next(so, &pos, &entry)) {
1564 setentry entrycopy;
1565 entrycopy.hash = entry->hash;
1566 entrycopy.key = entry->key;
1567 if (!_PyDict_Contains(other, entry->key, entry->hash)) {
1568 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
1569 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1569, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
1570 return NULL((void*)0);
1571 }
1572 }
1573 }
1574 return result;
1575 }
1576
1577 /* Iterate over so, checking for common elements in other. */
1578 while (set_next(so, &pos, &entry)) {
1579 int rv = set_contains_entry((PySetObject *)other, entry);
1580 if (rv == -1) {
1581 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1581, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
1582 return NULL((void*)0);
1583 }
1584 if (!rv) {
1585 if (set_add_entry((PySetObject *)result, entry) == -1) {
1586 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1586, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
1587 return NULL((void*)0);
1588 }
1589 }
1590 }
1591 return result;
1592}
1593
1594static PyObject *
1595set_difference_multi(PySetObject *so, PyObject *args)
1596{
1597 Py_ssize_t i;
1598 PyObject *result, *other;
1599
1600 if (PyTuple_GET_SIZE(args)(((PyVarObject*)(args))->ob_size) == 0)
1601 return set_copy(so);
1602
1603 other = PyTuple_GET_ITEM(args, 0)(((PyTupleObject *)(args))->ob_item[0]);
1604 result = set_difference(so, other);
1605 if (result == NULL((void*)0))
1606 return NULL((void*)0);
1607
1608 for (i=1 ; i<PyTuple_GET_SIZE(args)(((PyVarObject*)(args))->ob_size) ; i++) {
1609 other = PyTuple_GET_ITEM(args, i)(((PyTupleObject *)(args))->ob_item[i]);
1610 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
1611 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1611, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
1612 return NULL((void*)0);
1613 }
1614 }
1615 return result;
1616}
1617
1618PyDoc_STRVAR(difference_doc,static char difference_doc[] = "Return the difference of two or more sets as a new set.\n\n(i.e. all elements that are in this set but not the others.)"
1619"Return the difference of two or more sets as a new set.\n\static char difference_doc[] = "Return the difference of two or more sets as a new set.\n\n(i.e. all elements that are in this set but not the others.)"
1620\n\static char difference_doc[] = "Return the difference of two or more sets as a new set.\n\n(i.e. all elements that are in this set but not the others.)"
1621(i.e. all elements that are in this set but not the others.)")static char difference_doc[] = "Return the difference of two or more sets as a new set.\n\n(i.e. all elements that are in this set but not the others.)";
1622static PyObject *
1623set_sub(PySetObject *so, PyObject *other)
1624{
1625 if (!PyAnySet_Check(so)((((PyObject*)(so))->ob_type) == &PySet_Type || (((PyObject
*)(so))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(so))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(so))->ob_type), &PyFrozenSet_Type))
|| !PyAnySet_Check(other)((((PyObject*)(other))->ob_type) == &PySet_Type || (((
PyObject*)(other))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PyFrozenSet_Type))
) {
1626 Py_INCREF(Py_NotImplemented)( _Py_RefTotal++ , ((PyObject*)((&_Py_NotImplementedStruct
)))->ob_refcnt++)
;
1627 return Py_NotImplemented(&_Py_NotImplementedStruct);
1628 }
1629 return set_difference(so, other);
1630}
1631
1632static PyObject *
1633set_isub(PySetObject *so, PyObject *other)
1634{
1635 if (!PyAnySet_Check(other)((((PyObject*)(other))->ob_type) == &PySet_Type || (((
PyObject*)(other))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PyFrozenSet_Type))
) {
1636 Py_INCREF(Py_NotImplemented)( _Py_RefTotal++ , ((PyObject*)((&_Py_NotImplementedStruct
)))->ob_refcnt++)
;
1637 return Py_NotImplemented(&_Py_NotImplementedStruct);
1638 }
1639 if (set_difference_update_internal(so, other) == -1)
1640 return NULL((void*)0);
1641 Py_INCREF(so)( _Py_RefTotal++ , ((PyObject*)(so))->ob_refcnt++);
1642 return (PyObject *)so;
1643}
1644
1645static PyObject *
1646set_symmetric_difference_update(PySetObject *so, PyObject *other)
1647{
1648 PySetObject *otherset;
1649 PyObject *key;
1650 Py_ssize_t pos = 0;
1651 setentry *entry;
1652
1653 if ((PyObject *)so == other)
1654 return set_clear(so);
1655
1656 if (PyDict_CheckExact(other)((((PyObject*)(other))->ob_type) == &PyDict_Type)) {
1657 PyObject *value;
1658 int rv;
1659 Py_hash_t hash;
1660 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1661 setentry an_entry;
1662
1663 Py_INCREF(key)( _Py_RefTotal++ , ((PyObject*)(key))->ob_refcnt++);
1664 an_entry.hash = hash;
1665 an_entry.key = key;
1666
1667 rv = set_discard_entry(so, &an_entry);
1668 if (rv == -1) {
1669 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1669, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
1670 return NULL((void*)0);
1671 }
1672 if (rv == DISCARD_NOTFOUND0) {
1673 if (set_add_entry(so, &an_entry) == -1) {
1674 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1674, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
1675 return NULL((void*)0);
1676 }
1677 }
1678 Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt !=
0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1678, (PyObject *)(key)); } else _Py_Dealloc
((PyObject *)(key)); } while (0)
;
1679 }
1680 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1681 }
1682
1683 if (PyAnySet_Check(other)((((PyObject*)(other))->ob_type) == &PySet_Type || (((
PyObject*)(other))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PyFrozenSet_Type))
) {
1684 Py_INCREF(other)( _Py_RefTotal++ , ((PyObject*)(other))->ob_refcnt++);
1685 otherset = (PySetObject *)other;
1686 } else {
1687 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so)(((PyObject*)(so))->ob_type), other);
1688 if (otherset == NULL((void*)0))
1689 return NULL((void*)0);
1690 }
1691
1692 while (set_next(otherset, &pos, &entry)) {
1693 int rv = set_discard_entry(so, entry);
1694 if (rv == -1) {
1695 Py_DECREF(otherset)do { if (_Py_RefTotal-- , --((PyObject*)(otherset))->ob_refcnt
!= 0) { if (((PyObject*)otherset)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1695, (PyObject *)(otherset)); } else
_Py_Dealloc((PyObject *)(otherset)); } while (0)
;
1696 return NULL((void*)0);
1697 }
1698 if (rv == DISCARD_NOTFOUND0) {
1699 if (set_add_entry(so, entry) == -1) {
1700 Py_DECREF(otherset)do { if (_Py_RefTotal-- , --((PyObject*)(otherset))->ob_refcnt
!= 0) { if (((PyObject*)otherset)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1700, (PyObject *)(otherset)); } else
_Py_Dealloc((PyObject *)(otherset)); } while (0)
;
1701 return NULL((void*)0);
1702 }
1703 }
1704 }
1705 Py_DECREF(otherset)do { if (_Py_RefTotal-- , --((PyObject*)(otherset))->ob_refcnt
!= 0) { if (((PyObject*)otherset)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1705, (PyObject *)(otherset)); } else
_Py_Dealloc((PyObject *)(otherset)); } while (0)
;
1706 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1707}
1708
1709PyDoc_STRVAR(symmetric_difference_update_doc,static char symmetric_difference_update_doc[] = "Update a set with the symmetric difference of itself and another."
1710"Update a set with the symmetric difference of itself and another.")static char symmetric_difference_update_doc[] = "Update a set with the symmetric difference of itself and another.";
1711
1712static PyObject *
1713set_symmetric_difference(PySetObject *so, PyObject *other)
1714{
1715 PyObject *rv;
1716 PySetObject *otherset;
1717
1718 otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so)(((PyObject*)(so))->ob_type), other);
1719 if (otherset == NULL((void*)0))
1720 return NULL((void*)0);
1721 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
1722 if (rv == NULL((void*)0))
1723 return NULL((void*)0);
1724 Py_DECREF(rv)do { if (_Py_RefTotal-- , --((PyObject*)(rv))->ob_refcnt !=
0) { if (((PyObject*)rv)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1724, (PyObject *)(rv)); } else _Py_Dealloc
((PyObject *)(rv)); } while (0)
;
1725 return (PyObject *)otherset;
1726}
1727
1728PyDoc_STRVAR(symmetric_difference_doc,static char symmetric_difference_doc[] = "Return the symmetric difference of two sets as a new set.\n\n(i.e. all elements that are in exactly one of the sets.)"
1729"Return the symmetric difference of two sets as a new set.\n\static char symmetric_difference_doc[] = "Return the symmetric difference of two sets as a new set.\n\n(i.e. all elements that are in exactly one of the sets.)"
1730\n\static char symmetric_difference_doc[] = "Return the symmetric difference of two sets as a new set.\n\n(i.e. all elements that are in exactly one of the sets.)"
1731(i.e. all elements that are in exactly one of the sets.)")static char symmetric_difference_doc[] = "Return the symmetric difference of two sets as a new set.\n\n(i.e. all elements that are in exactly one of the sets.)";
1732
1733static PyObject *
1734set_xor(PySetObject *so, PyObject *other)
1735{
1736 if (!PyAnySet_Check(so)((((PyObject*)(so))->ob_type) == &PySet_Type || (((PyObject
*)(so))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(so))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(so))->ob_type), &PyFrozenSet_Type))
|| !PyAnySet_Check(other)((((PyObject*)(other))->ob_type) == &PySet_Type || (((
PyObject*)(other))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PyFrozenSet_Type))
) {
1737 Py_INCREF(Py_NotImplemented)( _Py_RefTotal++ , ((PyObject*)((&_Py_NotImplementedStruct
)))->ob_refcnt++)
;
1738 return Py_NotImplemented(&_Py_NotImplementedStruct);
1739 }
1740 return set_symmetric_difference(so, other);
1741}
1742
1743static PyObject *
1744set_ixor(PySetObject *so, PyObject *other)
1745{
1746 PyObject *result;
1747
1748 if (!PyAnySet_Check(other)((((PyObject*)(other))->ob_type) == &PySet_Type || (((
PyObject*)(other))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PyFrozenSet_Type))
) {
1749 Py_INCREF(Py_NotImplemented)( _Py_RefTotal++ , ((PyObject*)((&_Py_NotImplementedStruct
)))->ob_refcnt++)
;
1750 return Py_NotImplemented(&_Py_NotImplementedStruct);
1751 }
1752 result = set_symmetric_difference_update(so, other);
1753 if (result == NULL((void*)0))
1754 return NULL((void*)0);
1755 Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt
!= 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1755, (PyObject *)(result)); } else _Py_Dealloc
((PyObject *)(result)); } while (0)
;
1756 Py_INCREF(so)( _Py_RefTotal++ , ((PyObject*)(so))->ob_refcnt++);
1757 return (PyObject *)so;
1758}
1759
1760static PyObject *
1761set_issubset(PySetObject *so, PyObject *other)
1762{
1763 setentry *entry;
1764 Py_ssize_t pos = 0;
1765
1766 if (!PyAnySet_Check(other)((((PyObject*)(other))->ob_type) == &PySet_Type || (((
PyObject*)(other))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PyFrozenSet_Type))
) {
1767 PyObject *tmp, *result;
1768 tmp = make_new_set(&PySet_Type, other);
1769 if (tmp == NULL((void*)0))
1770 return NULL((void*)0);
1771 result = set_issubset(so, tmp);
1772 Py_DECREF(tmp)do { if (_Py_RefTotal-- , --((PyObject*)(tmp))->ob_refcnt !=
0) { if (((PyObject*)tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1772, (PyObject *)(tmp)); } else _Py_Dealloc
((PyObject *)(tmp)); } while (0)
;
1773 return result;
1774 }
1775 if (PySet_GET_SIZE(so)(((PySetObject *)(so))->used) > PySet_GET_SIZE(other)(((PySetObject *)(other))->used))
1776 Py_RETURN_FALSEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_FalseStruct
)))->ob_refcnt++), ((PyObject *) &_Py_FalseStruct)
;
1777
1778 while (set_next(so, &pos, &entry)) {
1779 int rv = set_contains_entry((PySetObject *)other, entry);
1780 if (rv == -1)
1781 return NULL((void*)0);
1782 if (!rv)
1783 Py_RETURN_FALSEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_FalseStruct
)))->ob_refcnt++), ((PyObject *) &_Py_FalseStruct)
;
1784 }
1785 Py_RETURN_TRUEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_TrueStruct
)))->ob_refcnt++), ((PyObject *) &_Py_TrueStruct)
;
1786}
1787
1788PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.")static char issubset_doc[] = "Report whether another set contains this set.";
1789
1790static PyObject *
1791set_issuperset(PySetObject *so, PyObject *other)
1792{
1793 PyObject *tmp, *result;
1794
1795 if (!PyAnySet_Check(other)((((PyObject*)(other))->ob_type) == &PySet_Type || (((
PyObject*)(other))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(other))->ob_type), &PyFrozenSet_Type))
) {
1796 tmp = make_new_set(&PySet_Type, other);
1797 if (tmp == NULL((void*)0))
1798 return NULL((void*)0);
1799 result = set_issuperset(so, tmp);
1800 Py_DECREF(tmp)do { if (_Py_RefTotal-- , --((PyObject*)(tmp))->ob_refcnt !=
0) { if (((PyObject*)tmp)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1800, (PyObject *)(tmp)); } else _Py_Dealloc
((PyObject *)(tmp)); } while (0)
;
1801 return result;
1802 }
1803 return set_issubset((PySetObject *)other, (PyObject *)so);
1804}
1805
1806PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.")static char issuperset_doc[] = "Report whether this set contains another set.";
1807
1808static PyObject *
1809set_richcompare(PySetObject *v, PyObject *w, int op)
1810{
1811 PyObject *r1, *r2;
1812
1813 if(!PyAnySet_Check(w)((((PyObject*)(w))->ob_type) == &PySet_Type || (((PyObject
*)(w))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(w))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(w))->ob_type), &PyFrozenSet_Type))
) {
1814 Py_INCREF(Py_NotImplemented)( _Py_RefTotal++ , ((PyObject*)((&_Py_NotImplementedStruct
)))->ob_refcnt++)
;
1815 return Py_NotImplemented(&_Py_NotImplementedStruct);
1816 }
1817 switch (op) {
1818 case Py_EQ2:
1819 if (PySet_GET_SIZE(v)(((PySetObject *)(v))->used) != PySet_GET_SIZE(w)(((PySetObject *)(w))->used))
1820 Py_RETURN_FALSEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_FalseStruct
)))->ob_refcnt++), ((PyObject *) &_Py_FalseStruct)
;
1821 if (v->hash != -1 &&
1822 ((PySetObject *)w)->hash != -1 &&
1823 v->hash != ((PySetObject *)w)->hash)
1824 Py_RETURN_FALSEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_FalseStruct
)))->ob_refcnt++), ((PyObject *) &_Py_FalseStruct)
;
1825 return set_issubset(v, w);
1826 case Py_NE3:
1827 r1 = set_richcompare(v, w, Py_EQ2);
1828 if (r1 == NULL((void*)0))
1829 return NULL((void*)0);
1830 r2 = PyBool_FromLong(PyObject_Not(r1));
1831 Py_DECREF(r1)do { if (_Py_RefTotal-- , --((PyObject*)(r1))->ob_refcnt !=
0) { if (((PyObject*)r1)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1831, (PyObject *)(r1)); } else _Py_Dealloc
((PyObject *)(r1)); } while (0)
;
1832 return r2;
1833 case Py_LE1:
1834 return set_issubset(v, w);
1835 case Py_GE5:
1836 return set_issuperset(v, w);
1837 case Py_LT0:
1838 if (PySet_GET_SIZE(v)(((PySetObject *)(v))->used) >= PySet_GET_SIZE(w)(((PySetObject *)(w))->used))
1839 Py_RETURN_FALSEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_FalseStruct
)))->ob_refcnt++), ((PyObject *) &_Py_FalseStruct)
;
1840 return set_issubset(v, w);
1841 case Py_GT4:
1842 if (PySet_GET_SIZE(v)(((PySetObject *)(v))->used) <= PySet_GET_SIZE(w)(((PySetObject *)(w))->used))
1843 Py_RETURN_FALSEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_FalseStruct
)))->ob_refcnt++), ((PyObject *) &_Py_FalseStruct)
;
1844 return set_issuperset(v, w);
1845 }
1846 Py_INCREF(Py_NotImplemented)( _Py_RefTotal++ , ((PyObject*)((&_Py_NotImplementedStruct
)))->ob_refcnt++)
;
1847 return Py_NotImplemented(&_Py_NotImplementedStruct);
1848}
1849
1850static PyObject *
1851set_add(PySetObject *so, PyObject *key)
1852{
1853 if (set_add_key(so, key) == -1)
1854 return NULL((void*)0);
1855 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1856}
1857
1858PyDoc_STRVAR(add_doc,static char add_doc[] = "Add an element to a set.\n\nThis has no effect if the element is already present."
1859"Add an element to a set.\n\static char add_doc[] = "Add an element to a set.\n\nThis has no effect if the element is already present."
1860\n\static char add_doc[] = "Add an element to a set.\n\nThis has no effect if the element is already present."
1861This has no effect if the element is already present.")static char add_doc[] = "Add an element to a set.\n\nThis has no effect if the element is already present.";
1862
1863static int
1864set_contains(PySetObject *so, PyObject *key)
1865{
1866 PyObject *tmpkey;
1867 int rv;
1868
1869 rv = set_contains_key(so, key);
1870 if (rv == -1) {
1871 if (!PySet_Check(key)((((PyObject*)(key))->ob_type) == &PySet_Type || PyType_IsSubtype
((((PyObject*)(key))->ob_type), &PySet_Type))
|| !PyErr_ExceptionMatches(PyExc_TypeError))
1872 return -1;
1873 PyErr_Clear();
1874 tmpkey = make_new_set(&PyFrozenSet_Type, key);
1875 if (tmpkey == NULL((void*)0))
1876 return -1;
1877 rv = set_contains(so, tmpkey);
1878 Py_DECREF(tmpkey)do { if (_Py_RefTotal-- , --((PyObject*)(tmpkey))->ob_refcnt
!= 0) { if (((PyObject*)tmpkey)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1878, (PyObject *)(tmpkey)); } else _Py_Dealloc
((PyObject *)(tmpkey)); } while (0)
;
1879 }
1880 return rv;
1881}
1882
1883static PyObject *
1884set_direct_contains(PySetObject *so, PyObject *key)
1885{
1886 long result;
1887
1888 result = set_contains(so, key);
1889 if (result == -1)
1890 return NULL((void*)0);
1891 return PyBool_FromLong(result);
1892}
1893
1894PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.")static char contains_doc[] = "x.__contains__(y) <==> y in x.";
1895
1896static PyObject *
1897set_remove(PySetObject *so, PyObject *key)
1898{
1899 PyObject *tmpkey;
1900 int rv;
1901
1902 rv = set_discard_key(so, key);
1903 if (rv == -1) {
1904 if (!PySet_Check(key)((((PyObject*)(key))->ob_type) == &PySet_Type || PyType_IsSubtype
((((PyObject*)(key))->ob_type), &PySet_Type))
|| !PyErr_ExceptionMatches(PyExc_TypeError))
1905 return NULL((void*)0);
1906 PyErr_Clear();
1907 tmpkey = make_new_set(&PyFrozenSet_Type, key);
1908 if (tmpkey == NULL((void*)0))
1909 return NULL((void*)0);
1910 rv = set_discard_key(so, tmpkey);
1911 Py_DECREF(tmpkey)do { if (_Py_RefTotal-- , --((PyObject*)(tmpkey))->ob_refcnt
!= 0) { if (((PyObject*)tmpkey)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1911, (PyObject *)(tmpkey)); } else _Py_Dealloc
((PyObject *)(tmpkey)); } while (0)
;
1912 if (rv == -1)
1913 return NULL((void*)0);
1914 }
1915
1916 if (rv == DISCARD_NOTFOUND0) {
1917 set_key_error(key);
1918 return NULL((void*)0);
1919 }
1920 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1921}
1922
1923PyDoc_STRVAR(remove_doc,static char remove_doc[] = "Remove an element from a set; it must be a member.\n\nIf the element is not a member, raise a KeyError."
1924"Remove an element from a set; it must be a member.\n\static char remove_doc[] = "Remove an element from a set; it must be a member.\n\nIf the element is not a member, raise a KeyError."
1925\n\static char remove_doc[] = "Remove an element from a set; it must be a member.\n\nIf the element is not a member, raise a KeyError."
1926If the element is not a member, raise a KeyError.")static char remove_doc[] = "Remove an element from a set; it must be a member.\n\nIf the element is not a member, raise a KeyError.";
1927
1928static PyObject *
1929set_discard(PySetObject *so, PyObject *key)
1930{
1931 PyObject *tmpkey, *result;
1932 int rv;
1933
1934 rv = set_discard_key(so, key);
1935 if (rv == -1) {
1936 if (!PySet_Check(key)((((PyObject*)(key))->ob_type) == &PySet_Type || PyType_IsSubtype
((((PyObject*)(key))->ob_type), &PySet_Type))
|| !PyErr_ExceptionMatches(PyExc_TypeError))
1937 return NULL((void*)0);
1938 PyErr_Clear();
1939 tmpkey = make_new_set(&PyFrozenSet_Type, key);
1940 if (tmpkey == NULL((void*)0))
1941 return NULL((void*)0);
1942 result = set_discard(so, tmpkey);
1943 Py_DECREF(tmpkey)do { if (_Py_RefTotal-- , --((PyObject*)(tmpkey))->ob_refcnt
!= 0) { if (((PyObject*)tmpkey)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 1943, (PyObject *)(tmpkey)); } else _Py_Dealloc
((PyObject *)(tmpkey)); } while (0)
;
1944 return result;
1945 }
1946 Py_RETURN_NONEreturn ( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct))
)->ob_refcnt++), (&_Py_NoneStruct)
;
1947}
1948
1949PyDoc_STRVAR(discard_doc,static char discard_doc[] = "Remove an element from a set if it is a member.\n\nIf the element is not a member, do nothing."
1950"Remove an element from a set if it is a member.\n\static char discard_doc[] = "Remove an element from a set if it is a member.\n\nIf the element is not a member, do nothing."
1951\n\static char discard_doc[] = "Remove an element from a set if it is a member.\n\nIf the element is not a member, do nothing."
1952If the element is not a member, do nothing.")static char discard_doc[] = "Remove an element from a set if it is a member.\n\nIf the element is not a member, do nothing.";
1953
1954static PyObject *
1955set_reduce(PySetObject *so)
1956{
1957 PyObject *keys=NULL((void*)0), *args=NULL((void*)0), *result=NULL((void*)0), *dict=NULL((void*)0);
1958
1959 keys = PySequence_List((PyObject *)so);
1960 if (keys == NULL((void*)0))
1961 goto done;
1962 args = PyTuple_Pack(1, keys);
1963 if (args == NULL((void*)0))
1964 goto done;
1965 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
1966 if (dict == NULL((void*)0)) {
1967 PyErr_Clear();
1968 dict = Py_None(&_Py_NoneStruct);
1969 Py_INCREF(dict)( _Py_RefTotal++ , ((PyObject*)(dict))->ob_refcnt++);
1970 }
1971 result = PyTuple_Pack(3, Py_TYPE(so)(((PyObject*)(so))->ob_type), args, dict);
1972done:
1973 Py_XDECREF(args)do { if ((args) == ((void*)0)) ; else do { if (_Py_RefTotal--
, --((PyObject*)(args))->ob_refcnt != 0) { if (((PyObject
*)args)->ob_refcnt < 0) _Py_NegativeRefcount("Objects/setobject.c"
, 1973, (PyObject *)(args)); } else _Py_Dealloc((PyObject *)(
args)); } while (0); } while (0)
;
1974 Py_XDECREF(keys)do { if ((keys) == ((void*)0)) ; else do { if (_Py_RefTotal--
, --((PyObject*)(keys))->ob_refcnt != 0) { if (((PyObject
*)keys)->ob_refcnt < 0) _Py_NegativeRefcount("Objects/setobject.c"
, 1974, (PyObject *)(keys)); } else _Py_Dealloc((PyObject *)(
keys)); } while (0); } while (0)
;
1975 Py_XDECREF(dict)do { if ((dict) == ((void*)0)) ; else do { if (_Py_RefTotal--
, --((PyObject*)(dict))->ob_refcnt != 0) { if (((PyObject
*)dict)->ob_refcnt < 0) _Py_NegativeRefcount("Objects/setobject.c"
, 1975, (PyObject *)(dict)); } else _Py_Dealloc((PyObject *)(
dict)); } while (0); } while (0)
;
1976 return result;
1977}
1978
1979PyDoc_STRVAR(reduce_doc, "Return state information for pickling.")static char reduce_doc[] = "Return state information for pickling.";
1980
1981static PyObject *
1982set_sizeof(PySetObject *so)
1983{
1984 Py_ssize_t res;
1985
1986 res = sizeof(PySetObject);
1987 if (so->table != so->smalltable)
1988 res = res + (so->mask + 1) * sizeof(setentry);
1989 return PyLong_FromSsize_t(res);
1990}
1991
1992PyDoc_STRVAR(sizeof_doc, "S.__sizeof__() -> size of S in memory, in bytes")static char sizeof_doc[] = "S.__sizeof__() -> size of S in memory, in bytes";
1993static int
1994set_init(PySetObject *self, PyObject *args, PyObject *kwds)
1995{
1996 PyObject *iterable = NULL((void*)0);
1997
1998 if (!PyAnySet_Check(self)((((PyObject*)(self))->ob_type) == &PySet_Type || (((PyObject
*)(self))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(self))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(self))->ob_type), &PyFrozenSet_Type))
)
1999 return -1;
2000 if (PySet_Check(self)((((PyObject*)(self))->ob_type) == &PySet_Type || PyType_IsSubtype
((((PyObject*)(self))->ob_type), &PySet_Type))
&& !_PyArg_NoKeywords("set()", kwds))
2001 return -1;
2002 if (!PyArg_UnpackTuple(args, Py_TYPE(self)(((PyObject*)(self))->ob_type)->tp_name, 0, 1, &iterable))
2003 return -1;
2004 set_clear_internal(self);
2005 self->hash = -1;
2006 if (iterable == NULL((void*)0))
2007 return 0;
2008 return set_update_internal(self, iterable);
2009}
2010
2011static PySequenceMethods set_as_sequence = {
2012 set_len, /* sq_length */
2013 0, /* sq_concat */
2014 0, /* sq_repeat */
2015 0, /* sq_item */
2016 0, /* sq_slice */
2017 0, /* sq_ass_item */
2018 0, /* sq_ass_slice */
2019 (objobjproc)set_contains, /* sq_contains */
2020};
2021
2022/* set object ********************************************************/
2023
2024#ifdef Py_DEBUG1
2025static PyObject *test_c_api(PySetObject *so);
2026
2027PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\static char test_c_api_doc[] = "Exercises C API. Returns True.\nAll is well if assertions don't fail."
2028All is well if assertions don't fail.")static char test_c_api_doc[] = "Exercises C API. Returns True.\nAll is well if assertions don't fail.";
2029#endif
2030
2031static PyMethodDef set_methods[] = {
2032 {"add", (PyCFunction)set_add, METH_O0x0008,
2033 add_doc},
2034 {"clear", (PyCFunction)set_clear, METH_NOARGS0x0004,
2035 clear_doc},
2036 {"__contains__",(PyCFunction)set_direct_contains, METH_O0x0008 | METH_COEXIST0x0040,
2037 contains_doc},
2038 {"copy", (PyCFunction)set_copy, METH_NOARGS0x0004,
2039 copy_doc},
2040 {"discard", (PyCFunction)set_discard, METH_O0x0008,
2041 discard_doc},
2042 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS0x0001,
2043 difference_doc},
2044 {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS0x0001,
2045 difference_update_doc},
2046 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS0x0001,
2047 intersection_doc},
2048 {"intersection_update",(PyCFunction)set_intersection_update_multi, METH_VARARGS0x0001,
2049 intersection_update_doc},
2050 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O0x0008,
2051 isdisjoint_doc},
2052 {"issubset", (PyCFunction)set_issubset, METH_O0x0008,
2053 issubset_doc},
2054 {"issuperset", (PyCFunction)set_issuperset, METH_O0x0008,
2055 issuperset_doc},
2056 {"pop", (PyCFunction)set_pop, METH_NOARGS0x0004,
2057 pop_doc},
2058 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS0x0004,
2059 reduce_doc},
2060 {"remove", (PyCFunction)set_remove, METH_O0x0008,
2061 remove_doc},
2062 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS0x0004,
2063 sizeof_doc},
2064 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O0x0008,
2065 symmetric_difference_doc},
2066 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O0x0008,
2067 symmetric_difference_update_doc},
2068#ifdef Py_DEBUG1
2069 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS0x0004,
2070 test_c_api_doc},
2071#endif
2072 {"union", (PyCFunction)set_union, METH_VARARGS0x0001,
2073 union_doc},
2074 {"update", (PyCFunction)set_update, METH_VARARGS0x0001,
2075 update_doc},
2076 {NULL((void*)0), NULL((void*)0)} /* sentinel */
2077};
2078
2079static PyNumberMethods set_as_number = {
2080 0, /*nb_add*/
2081 (binaryfunc)set_sub, /*nb_subtract*/
2082 0, /*nb_multiply*/
2083 0, /*nb_remainder*/
2084 0, /*nb_divmod*/
2085 0, /*nb_power*/
2086 0, /*nb_negative*/
2087 0, /*nb_positive*/
2088 0, /*nb_absolute*/
2089 0, /*nb_bool*/
2090 0, /*nb_invert*/
2091 0, /*nb_lshift*/
2092 0, /*nb_rshift*/
2093 (binaryfunc)set_and, /*nb_and*/
2094 (binaryfunc)set_xor, /*nb_xor*/
2095 (binaryfunc)set_or, /*nb_or*/
2096 0, /*nb_int*/
2097 0, /*nb_reserved*/
2098 0, /*nb_float*/
2099 0, /*nb_inplace_add*/
2100 (binaryfunc)set_isub, /*nb_inplace_subtract*/
2101 0, /*nb_inplace_multiply*/
2102 0, /*nb_inplace_remainder*/
2103 0, /*nb_inplace_power*/
2104 0, /*nb_inplace_lshift*/
2105 0, /*nb_inplace_rshift*/
2106 (binaryfunc)set_iand, /*nb_inplace_and*/
2107 (binaryfunc)set_ixor, /*nb_inplace_xor*/
2108 (binaryfunc)set_ior, /*nb_inplace_or*/
2109};
2110
2111PyDoc_STRVAR(set_doc,static char set_doc[] = "set() -> new empty set object\nset(iterable) -> new set object\n\nBuild an unordered collection of unique elements."
2112"set() -> new empty set object\n\static char set_doc[] = "set() -> new empty set object\nset(iterable) -> new set object\n\nBuild an unordered collection of unique elements."
2113set(iterable) -> new set object\n\static char set_doc[] = "set() -> new empty set object\nset(iterable) -> new set object\n\nBuild an unordered collection of unique elements."
2114\n\static char set_doc[] = "set() -> new empty set object\nset(iterable) -> new set object\n\nBuild an unordered collection of unique elements."
2115Build an unordered collection of unique elements.")static char set_doc[] = "set() -> new empty set object\nset(iterable) -> new set object\n\nBuild an unordered collection of unique elements.";
2116
2117PyTypeObject PySet_Type = {
2118 PyVarObject_HEAD_INIT(&PyType_Type, 0){ { 0, 0, 1, &PyType_Type }, 0 },
2119 "set", /* tp_name */
2120 sizeof(PySetObject), /* tp_basicsize */
2121 0, /* tp_itemsize */
2122 /* methods */
2123 (destructor)set_dealloc, /* tp_dealloc */
2124 0, /* tp_print */
2125 0, /* tp_getattr */
2126 0, /* tp_setattr */
2127 0, /* tp_reserved */
2128 (reprfunc)set_repr, /* tp_repr */
2129 &set_as_number, /* tp_as_number */
2130 &set_as_sequence, /* tp_as_sequence */
2131 0, /* tp_as_mapping */
2132 PyObject_HashNotImplemented, /* tp_hash */
2133 0, /* tp_call */
2134 0, /* tp_str */
2135 PyObject_GenericGetAttr, /* tp_getattro */
2136 0, /* tp_setattro */
2137 0, /* tp_as_buffer */
2138 Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0) | Py_TPFLAGS_HAVE_GC(1L<<14) |
2139 Py_TPFLAGS_BASETYPE(1L<<10), /* tp_flags */
2140 set_doc, /* tp_doc */
2141 (traverseproc)set_traverse, /* tp_traverse */
2142 (inquiry)set_clear_internal, /* tp_clear */
2143 (richcmpfunc)set_richcompare, /* tp_richcompare */
2144 offsetof(PySetObject, weakreflist)__builtin_offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2145 (getiterfunc)set_iter, /* tp_iter */
2146 0, /* tp_iternext */
2147 set_methods, /* tp_methods */
2148 0, /* tp_members */
2149 0, /* tp_getset */
2150 0, /* tp_base */
2151 0, /* tp_dict */
2152 0, /* tp_descr_get */
2153 0, /* tp_descr_set */
2154 0, /* tp_dictoffset */
2155 (initproc)set_init, /* tp_init */
2156 PyType_GenericAlloc, /* tp_alloc */
2157 set_new, /* tp_new */
2158 PyObject_GC_Del, /* tp_free */
2159};
2160
2161/* frozenset object ********************************************************/
2162
2163
2164static PyMethodDef frozenset_methods[] = {
2165 {"__contains__",(PyCFunction)set_direct_contains, METH_O0x0008 | METH_COEXIST0x0040,
2166 contains_doc},
2167 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS0x0004,
2168 copy_doc},
2169 {"difference", (PyCFunction)set_difference_multi, METH_VARARGS0x0001,
2170 difference_doc},
2171 {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS0x0001,
2172 intersection_doc},
2173 {"isdisjoint", (PyCFunction)set_isdisjoint, METH_O0x0008,
2174 isdisjoint_doc},
2175 {"issubset", (PyCFunction)set_issubset, METH_O0x0008,
2176 issubset_doc},
2177 {"issuperset", (PyCFunction)set_issuperset, METH_O0x0008,
2178 issuperset_doc},
2179 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS0x0004,
2180 reduce_doc},
2181 {"__sizeof__", (PyCFunction)set_sizeof, METH_NOARGS0x0004,
2182 sizeof_doc},
2183 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O0x0008,
2184 symmetric_difference_doc},
2185 {"union", (PyCFunction)set_union, METH_VARARGS0x0001,
2186 union_doc},
2187 {NULL((void*)0), NULL((void*)0)} /* sentinel */
2188};
2189
2190static PyNumberMethods frozenset_as_number = {
2191 0, /*nb_add*/
2192 (binaryfunc)set_sub, /*nb_subtract*/
2193 0, /*nb_multiply*/
2194 0, /*nb_remainder*/
2195 0, /*nb_divmod*/
2196 0, /*nb_power*/
2197 0, /*nb_negative*/
2198 0, /*nb_positive*/
2199 0, /*nb_absolute*/
2200 0, /*nb_bool*/
2201 0, /*nb_invert*/
2202 0, /*nb_lshift*/
2203 0, /*nb_rshift*/
2204 (binaryfunc)set_and, /*nb_and*/
2205 (binaryfunc)set_xor, /*nb_xor*/
2206 (binaryfunc)set_or, /*nb_or*/
2207};
2208
2209PyDoc_STRVAR(frozenset_doc,static char frozenset_doc[] = "frozenset() -> empty frozenset object\nfrozenset(iterable) -> frozenset object\n\nBuild an immutable unordered collection of unique elements."
2210"frozenset() -> empty frozenset object\n\static char frozenset_doc[] = "frozenset() -> empty frozenset object\nfrozenset(iterable) -> frozenset object\n\nBuild an immutable unordered collection of unique elements."
2211frozenset(iterable) -> frozenset object\n\static char frozenset_doc[] = "frozenset() -> empty frozenset object\nfrozenset(iterable) -> frozenset object\n\nBuild an immutable unordered collection of unique elements."
2212\n\static char frozenset_doc[] = "frozenset() -> empty frozenset object\nfrozenset(iterable) -> frozenset object\n\nBuild an immutable unordered collection of unique elements."
2213Build an immutable unordered collection of unique elements.")static char frozenset_doc[] = "frozenset() -> empty frozenset object\nfrozenset(iterable) -> frozenset object\n\nBuild an immutable unordered collection of unique elements.";
2214
2215PyTypeObject PyFrozenSet_Type = {
2216 PyVarObject_HEAD_INIT(&PyType_Type, 0){ { 0, 0, 1, &PyType_Type }, 0 },
2217 "frozenset", /* tp_name */
2218 sizeof(PySetObject), /* tp_basicsize */
2219 0, /* tp_itemsize */
2220 /* methods */
2221 (destructor)set_dealloc, /* tp_dealloc */
2222 0, /* tp_print */
2223 0, /* tp_getattr */
2224 0, /* tp_setattr */
2225 0, /* tp_reserved */
2226 (reprfunc)set_repr, /* tp_repr */
2227 &frozenset_as_number, /* tp_as_number */
2228 &set_as_sequence, /* tp_as_sequence */
2229 0, /* tp_as_mapping */
2230 frozenset_hash, /* tp_hash */
2231 0, /* tp_call */
2232 0, /* tp_str */
2233 PyObject_GenericGetAttr, /* tp_getattro */
2234 0, /* tp_setattro */
2235 0, /* tp_as_buffer */
2236 Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0) | Py_TPFLAGS_HAVE_GC(1L<<14) |
2237 Py_TPFLAGS_BASETYPE(1L<<10), /* tp_flags */
2238 frozenset_doc, /* tp_doc */
2239 (traverseproc)set_traverse, /* tp_traverse */
2240 (inquiry)set_clear_internal, /* tp_clear */
2241 (richcmpfunc)set_richcompare, /* tp_richcompare */
2242 offsetof(PySetObject, weakreflist)__builtin_offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2243 (getiterfunc)set_iter, /* tp_iter */
2244 0, /* tp_iternext */
2245 frozenset_methods, /* tp_methods */
2246 0, /* tp_members */
2247 0, /* tp_getset */
2248 0, /* tp_base */
2249 0, /* tp_dict */
2250 0, /* tp_descr_get */
2251 0, /* tp_descr_set */
2252 0, /* tp_dictoffset */
2253 0, /* tp_init */
2254 PyType_GenericAlloc, /* tp_alloc */
2255 frozenset_new, /* tp_new */
2256 PyObject_GC_Del, /* tp_free */
2257};
2258
2259
2260/***** C API functions *************************************************/
2261
2262PyObject *
2263PySet_New(PyObject *iterable)
2264{
2265 return make_new_set(&PySet_Type, iterable);
2266}
2267
2268PyObject *
2269PyFrozenSet_New(PyObject *iterable)
2270{
2271 return make_new_set(&PyFrozenSet_Type, iterable);
2272}
2273
2274Py_ssize_t
2275PySet_Size(PyObject *anyset)
2276{
2277 if (!PyAnySet_Check(anyset)((((PyObject*)(anyset))->ob_type) == &PySet_Type || ((
(PyObject*)(anyset))->ob_type) == &PyFrozenSet_Type ||
PyType_IsSubtype((((PyObject*)(anyset))->ob_type), &PySet_Type
) || PyType_IsSubtype((((PyObject*)(anyset))->ob_type), &
PyFrozenSet_Type))
) {
2278 PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/setobject.c", 2278);
2279 return -1;
2280 }
2281 return PySet_GET_SIZE(anyset)(((PySetObject *)(anyset))->used);
2282}
2283
2284int
2285PySet_Clear(PyObject *set)
2286{
2287 if (!PySet_Check(set)((((PyObject*)(set))->ob_type) == &PySet_Type || PyType_IsSubtype
((((PyObject*)(set))->ob_type), &PySet_Type))
) {
2288 PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/setobject.c", 2288);
2289 return -1;
2290 }
2291 return set_clear_internal((PySetObject *)set);
2292}
2293
2294int
2295PySet_Contains(PyObject *anyset, PyObject *key)
2296{
2297 if (!PyAnySet_Check(anyset)((((PyObject*)(anyset))->ob_type) == &PySet_Type || ((
(PyObject*)(anyset))->ob_type) == &PyFrozenSet_Type ||
PyType_IsSubtype((((PyObject*)(anyset))->ob_type), &PySet_Type
) || PyType_IsSubtype((((PyObject*)(anyset))->ob_type), &
PyFrozenSet_Type))
) {
2298 PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/setobject.c", 2298);
2299 return -1;
2300 }
2301 return set_contains_key((PySetObject *)anyset, key);
2302}
2303
2304int
2305PySet_Discard(PyObject *set, PyObject *key)
2306{
2307 if (!PySet_Check(set)((((PyObject*)(set))->ob_type) == &PySet_Type || PyType_IsSubtype
((((PyObject*)(set))->ob_type), &PySet_Type))
) {
2308 PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/setobject.c", 2308);
2309 return -1;
2310 }
2311 return set_discard_key((PySetObject *)set, key);
2312}
2313
2314int
2315PySet_Add(PyObject *anyset, PyObject *key)
2316{
2317 if (!PySet_Check(anyset)((((PyObject*)(anyset))->ob_type) == &PySet_Type || PyType_IsSubtype
((((PyObject*)(anyset))->ob_type), &PySet_Type))
&&
2318 (!PyFrozenSet_Check(anyset)((((PyObject*)(anyset))->ob_type) == &PyFrozenSet_Type
|| PyType_IsSubtype((((PyObject*)(anyset))->ob_type), &
PyFrozenSet_Type))
|| Py_REFCNT(anyset)(((PyObject*)(anyset))->ob_refcnt) != 1)) {
2319 PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/setobject.c", 2319);
2320 return -1;
2321 }
2322 return set_add_key((PySetObject *)anyset, key);
2323}
2324
2325int
2326_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
2327{
2328 setentry *entry;
2329
2330 if (!PyAnySet_Check(set)((((PyObject*)(set))->ob_type) == &PySet_Type || (((PyObject
*)(set))->ob_type) == &PyFrozenSet_Type || PyType_IsSubtype
((((PyObject*)(set))->ob_type), &PySet_Type) || PyType_IsSubtype
((((PyObject*)(set))->ob_type), &PyFrozenSet_Type))
) {
2331 PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/setobject.c", 2331);
2332 return -1;
2333 }
2334 if (set_next((PySetObject *)set, pos, &entry) == 0)
2335 return 0;
2336 *key = entry->key;
2337 *hash = entry->hash;
2338 return 1;
2339}
2340
2341PyObject *
2342PySet_Pop(PyObject *set)
2343{
2344 if (!PySet_Check(set)((((PyObject*)(set))->ob_type) == &PySet_Type || PyType_IsSubtype
((((PyObject*)(set))->ob_type), &PySet_Type))
) {
2345 PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/setobject.c", 2345);
2346 return NULL((void*)0);
2347 }
2348 return set_pop((PySetObject *)set);
2349}
2350
2351int
2352_PySet_Update(PyObject *set, PyObject *iterable)
2353{
2354 if (!PySet_Check(set)((((PyObject*)(set))->ob_type) == &PySet_Type || PyType_IsSubtype
((((PyObject*)(set))->ob_type), &PySet_Type))
) {
2355 PyErr_BadInternalCall()_PyErr_BadInternalCall("Objects/setobject.c", 2355);
2356 return -1;
2357 }
2358 return set_update_internal((PySetObject *)set, iterable);
2359}
2360
2361#ifdef Py_DEBUG1
2362
2363/* Test code to be called with any three element set.
2364 Returns True and original set is restored. */
2365
2366#define assertRaises(call_return_value, exception) \
2367 do { \
2368 assert(call_return_value)(__builtin_expect(!(call_return_value), 0) ? __assert_rtn(__func__
, "Objects/setobject.c", 2368, "call_return_value") : (void)0
)
; \
2369 assert(PyErr_ExceptionMatches(exception))(__builtin_expect(!(PyErr_ExceptionMatches(exception)), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 2369, "PyErr_ExceptionMatches(exception)"
) : (void)0)
; \
2370 PyErr_Clear(); \
2371 } while(0)
2372
2373static PyObject *
2374test_c_api(PySetObject *so)
2375{
2376 Py_ssize_t count;
2377 char *s;
2378 Py_ssize_t i;
2379 PyObject *elem=NULL((void*)0), *dup=NULL((void*)0), *t, *f, *dup2, *x;
2380 PyObject *ob = (PyObject *)so;
2381 Py_hash_t hash;
2382 PyObject *str;
2383
2384 /* Verify preconditions */
2385 assert(PyAnySet_Check(ob))(__builtin_expect(!(((((PyObject*)(ob))->ob_type) == &
PySet_Type || (((PyObject*)(ob))->ob_type) == &PyFrozenSet_Type
|| PyType_IsSubtype((((PyObject*)(ob))->ob_type), &PySet_Type
) || PyType_IsSubtype((((PyObject*)(ob))->ob_type), &PyFrozenSet_Type
))), 0) ? __assert_rtn(__func__, "Objects/setobject.c", 2385,
"PyAnySet_Check(ob)") : (void)0)
;
2386 assert(PyAnySet_CheckExact(ob))(__builtin_expect(!(((((PyObject*)(ob))->ob_type) == &
PySet_Type || (((PyObject*)(ob))->ob_type) == &PyFrozenSet_Type
)), 0) ? __assert_rtn(__func__, "Objects/setobject.c", 2386, "PyAnySet_CheckExact(ob)"
) : (void)0)
;
2387 assert(!PyFrozenSet_CheckExact(ob))(__builtin_expect(!(!((((PyObject*)(ob))->ob_type) == &
PyFrozenSet_Type)), 0) ? __assert_rtn(__func__, "Objects/setobject.c"
, 2387, "!PyFrozenSet_CheckExact(ob)") : (void)0)
;
2388
2389 /* so.clear(); so |= set("abc"); */
2390 str = PyUnicode_FromStringPyUnicodeUCS2_FromString("abc");
2391 if (str == NULL((void*)0))
2392 return NULL((void*)0);
2393 set_clear_internal(so);
2394 if (set_update_internal(so, str) == -1) {
2395 Py_DECREF(str)do { if (_Py_RefTotal-- , --((PyObject*)(str))->ob_refcnt !=
0) { if (((PyObject*)str)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 2395, (PyObject *)(str)); } else _Py_Dealloc
((PyObject *)(str)); } while (0)
;
2396 return NULL((void*)0);
2397 }
2398 Py_DECREF(str)do { if (_Py_RefTotal-- , --((PyObject*)(str))->ob_refcnt !=
0) { if (((PyObject*)str)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 2398, (PyObject *)(str)); } else _Py_Dealloc
((PyObject *)(str)); } while (0)
;
2399
2400 /* Exercise type/size checks */
2401 assert(PySet_Size(ob) == 3)(__builtin_expect(!(PySet_Size(ob) == 3), 0) ? __assert_rtn(__func__
, "Objects/setobject.c", 2401, "PySet_Size(ob) == 3") : (void
)0)
;
2402 assert(PySet_GET_SIZE(ob) == 3)(__builtin_expect(!((((PySetObject *)(ob))->used) == 3), 0
) ? __assert_rtn(__func__, "Objects/setobject.c", 2402, "PySet_GET_SIZE(ob) == 3"
) : (void)0)
;
2403
2404 /* Raise TypeError for non-iterable constructor arguments */
2405 assertRaises(PySet_New(Py_None(&_Py_NoneStruct)) == NULL((void*)0), PyExc_TypeError);
2406 assertRaises(PyFrozenSet_New(Py_None(&_Py_NoneStruct)) == NULL((void*)0), PyExc_TypeError);
2407
2408 /* Raise TypeError for unhashable key */
2409 dup = PySet_New(ob);
2410 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2411 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2412 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2413
2414 /* Exercise successful pop, contains, add, and discard */
2415 elem = PySet_Pop(ob);
2416 assert(PySet_Contains(ob, elem) == 0)(__builtin_expect(!(PySet_Contains(ob, elem) == 0), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 2416, "PySet_Contains(ob, elem) == 0"
) : (void)0)
;
2417 assert(PySet_GET_SIZE(ob) == 2)(__builtin_expect(!((((PySetObject *)(ob))->used) == 2), 0
) ? __assert_rtn(__func__, "Objects/setobject.c", 2417, "PySet_GET_SIZE(ob) == 2"
) : (void)0)
;
2418 assert(PySet_Add(ob, elem) == 0)(__builtin_expect(!(PySet_Add(ob, elem) == 0), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 2418, "PySet_Add(ob, elem) == 0"
) : (void)0)
;
2419 assert(PySet_Contains(ob, elem) == 1)(__builtin_expect(!(PySet_Contains(ob, elem) == 1), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 2419, "PySet_Contains(ob, elem) == 1"
) : (void)0)
;
2420 assert(PySet_GET_SIZE(ob) == 3)(__builtin_expect(!((((PySetObject *)(ob))->used) == 3), 0
) ? __assert_rtn(__func__, "Objects/setobject.c", 2420, "PySet_GET_SIZE(ob) == 3"
) : (void)0)
;
2421 assert(PySet_Discard(ob, elem) == 1)(__builtin_expect(!(PySet_Discard(ob, elem) == 1), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 2421, "PySet_Discard(ob, elem) == 1"
) : (void)0)
;
2422 assert(PySet_GET_SIZE(ob) == 2)(__builtin_expect(!((((PySetObject *)(ob))->used) == 2), 0
) ? __assert_rtn(__func__, "Objects/setobject.c", 2422, "PySet_GET_SIZE(ob) == 2"
) : (void)0)
;
2423 assert(PySet_Discard(ob, elem) == 0)(__builtin_expect(!(PySet_Discard(ob, elem) == 0), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 2423, "PySet_Discard(ob, elem) == 0"
) : (void)0)
;
2424 assert(PySet_GET_SIZE(ob) == 2)(__builtin_expect(!((((PySetObject *)(ob))->used) == 2), 0
) ? __assert_rtn(__func__, "Objects/setobject.c", 2424, "PySet_GET_SIZE(ob) == 2"
) : (void)0)
;
2425
2426 /* Exercise clear */
2427 dup2 = PySet_New(dup);
2428 assert(PySet_Clear(dup2) == 0)(__builtin_expect(!(PySet_Clear(dup2) == 0), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 2428, "PySet_Clear(dup2) == 0"
) : (void)0)
;
2429 assert(PySet_Size(dup2) == 0)(__builtin_expect(!(PySet_Size(dup2) == 0), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 2429, "PySet_Size(dup2) == 0"
) : (void)0)
;
2430 Py_DECREF(dup2)do { if (_Py_RefTotal-- , --((PyObject*)(dup2))->ob_refcnt
!= 0) { if (((PyObject*)dup2)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 2430, (PyObject *)(dup2)); } else _Py_Dealloc
((PyObject *)(dup2)); } while (0)
;
2431
2432 /* Raise SystemError on clear or update of frozen set */
2433 f = PyFrozenSet_New(dup);
2434 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
2435 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
2436 assert(PySet_Add(f, elem) == 0)(__builtin_expect(!(PySet_Add(f, elem) == 0), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 2436, "PySet_Add(f, elem) == 0"
) : (void)0)
;
2437 Py_INCREF(f)( _Py_RefTotal++ , ((PyObject*)(f))->ob_refcnt++);
2438 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2439 Py_DECREF(f)do { if (_Py_RefTotal-- , --((PyObject*)(f))->ob_refcnt !=
0) { if (((PyObject*)f)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 2439, (PyObject *)(f)); } else _Py_Dealloc
((PyObject *)(f)); } while (0)
;
2440 Py_DECREF(f)do { if (_Py_RefTotal-- , --((PyObject*)(f))->ob_refcnt !=
0) { if (((PyObject*)f)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 2440, (PyObject *)(f)); } else _Py_Dealloc
((PyObject *)(f)); } while (0)
;
2441
2442 /* Exercise direct iteration */
2443 i = 0, count = 0;
2444 while (_PySet_NextEntry((PyObject *)dup, &i, &x, &hash)) {
2445 s = _PyUnicode_AsString(x);
2446 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'))(__builtin_expect(!(s && (s[0] == 'a' || s[0] == 'b' ||
s[0] == 'c')), 0) ? __assert_rtn(__func__, "Objects/setobject.c"
, 2446, "s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c')"
) : (void)0)
;
2447 count++;
2448 }
2449 assert(count == 3)(__builtin_expect(!(count == 3), 0) ? __assert_rtn(__func__, "Objects/setobject.c"
, 2449, "count == 3") : (void)0)
;
2450
2451 /* Exercise updates */
2452 dup2 = PySet_New(NULL((void*)0));
2453 assert(_PySet_Update(dup2, dup) == 0)(__builtin_expect(!(_PySet_Update(dup2, dup) == 0), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 2453, "_PySet_Update(dup2, dup) == 0"
) : (void)0)
;
2454 assert(PySet_Size(dup2) == 3)(__builtin_expect(!(PySet_Size(dup2) == 3), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 2454, "PySet_Size(dup2) == 3"
) : (void)0)
;
2455 assert(_PySet_Update(dup2, dup) == 0)(__builtin_expect(!(_PySet_Update(dup2, dup) == 0), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 2455, "_PySet_Update(dup2, dup) == 0"
) : (void)0)
;
2456 assert(PySet_Size(dup2) == 3)(__builtin_expect(!(PySet_Size(dup2) == 3), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 2456, "PySet_Size(dup2) == 3"
) : (void)0)
;
2457 Py_DECREF(dup2)do { if (_Py_RefTotal-- , --((PyObject*)(dup2))->ob_refcnt
!= 0) { if (((PyObject*)dup2)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 2457, (PyObject *)(dup2)); } else _Py_Dealloc
((PyObject *)(dup2)); } while (0)
;
2458
2459 /* Raise SystemError when self argument is not a set or frozenset. */
2460 t = PyTuple_New(0);
2461 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2462 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2463 Py_DECREF(t)do { if (_Py_RefTotal-- , --((PyObject*)(t))->ob_refcnt !=
0) { if (((PyObject*)t)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 2463, (PyObject *)(t)); } else _Py_Dealloc
((PyObject *)(t)); } while (0)
;
2464
2465 /* Raise SystemError when self argument is not a set. */
2466 f = PyFrozenSet_New(dup);
2467 assert(PySet_Size(f) == 3)(__builtin_expect(!(PySet_Size(f) == 3), 0) ? __assert_rtn(__func__
, "Objects/setobject.c", 2467, "PySet_Size(f) == 3") : (void)
0)
;
2468 assert(PyFrozenSet_CheckExact(f))(__builtin_expect(!(((((PyObject*)(f))->ob_type) == &PyFrozenSet_Type
)), 0) ? __assert_rtn(__func__, "Objects/setobject.c", 2468, "PyFrozenSet_CheckExact(f)"
) : (void)0)
;
2469 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2470 assertRaises(PySet_Pop(f) == NULL((void*)0), PyExc_SystemError);
2471 Py_DECREF(f)do { if (_Py_RefTotal-- , --((PyObject*)(f))->ob_refcnt !=
0) { if (((PyObject*)f)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 2471, (PyObject *)(f)); } else _Py_Dealloc
((PyObject *)(f)); } while (0)
;
2472
2473 /* Raise KeyError when popping from an empty set */
2474 assert(PyNumber_InPlaceSubtract(ob, ob) == ob)(__builtin_expect(!(PyNumber_InPlaceSubtract(ob, ob) == ob), 0
) ? __assert_rtn(__func__, "Objects/setobject.c", 2474, "PyNumber_InPlaceSubtract(ob, ob) == ob"
) : (void)0)
;
2475 Py_DECREF(ob)do { if (_Py_RefTotal-- , --((PyObject*)(ob))->ob_refcnt !=
0) { if (((PyObject*)ob)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 2475, (PyObject *)(ob)); } else _Py_Dealloc
((PyObject *)(ob)); } while (0)
;
2476 assert(PySet_GET_SIZE(ob) == 0)(__builtin_expect(!((((PySetObject *)(ob))->used) == 0), 0
) ? __assert_rtn(__func__, "Objects/setobject.c", 2476, "PySet_GET_SIZE(ob) == 0"
) : (void)0)
;
2477 assertRaises(PySet_Pop(ob) == NULL((void*)0), PyExc_KeyError);
2478
2479 /* Restore the set from the copy using the PyNumber API */
2480 assert(PyNumber_InPlaceOr(ob, dup) == ob)(__builtin_expect(!(PyNumber_InPlaceOr(ob, dup) == ob), 0) ? __assert_rtn
(__func__, "Objects/setobject.c", 2480, "PyNumber_InPlaceOr(ob, dup) == ob"
) : (void)0)
;
2481 Py_DECREF(ob)do { if (_Py_RefTotal-- , --((PyObject*)(ob))->ob_refcnt !=
0) { if (((PyObject*)ob)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 2481, (PyObject *)(ob)); } else _Py_Dealloc
((PyObject *)(ob)); } while (0)
;
2482
2483 /* Verify constructors accept NULL arguments */
2484 f = PySet_New(NULL((void*)0));
2485 assert(f != NULL)(__builtin_expect(!(f != ((void*)0)), 0) ? __assert_rtn(__func__
, "Objects/setobject.c", 2485, "f != NULL") : (void)0)
;
2486 assert(PySet_GET_SIZE(f) == 0)(__builtin_expect(!((((PySetObject *)(f))->used) == 0), 0)
? __assert_rtn(__func__, "Objects/setobject.c", 2486, "PySet_GET_SIZE(f) == 0"
) : (void)0)
;
2487 Py_DECREF(f)do { if (_Py_RefTotal-- , --((PyObject*)(f))->ob_refcnt !=
0) { if (((PyObject*)f)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 2487, (PyObject *)(f)); } else _Py_Dealloc
((PyObject *)(f)); } while (0)
;
2488 f = PyFrozenSet_New(NULL((void*)0));
2489 assert(f != NULL)(__builtin_expect(!(f != ((void*)0)), 0) ? __assert_rtn(__func__
, "Objects/setobject.c", 2489, "f != NULL") : (void)0)
;
2490 assert(PyFrozenSet_CheckExact(f))(__builtin_expect(!(((((PyObject*)(f))->ob_type) == &PyFrozenSet_Type
)), 0) ? __assert_rtn(__func__, "Objects/setobject.c", 2490, "PyFrozenSet_CheckExact(f)"
) : (void)0)
;
2491 assert(PySet_GET_SIZE(f) == 0)(__builtin_expect(!((((PySetObject *)(f))->used) == 0), 0)
? __assert_rtn(__func__, "Objects/setobject.c", 2491, "PySet_GET_SIZE(f) == 0"
) : (void)0)
;
2492 Py_DECREF(f)do { if (_Py_RefTotal-- , --((PyObject*)(f))->ob_refcnt !=
0) { if (((PyObject*)f)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 2492, (PyObject *)(f)); } else _Py_Dealloc
((PyObject *)(f)); } while (0)
;
2493
2494 Py_DECREF(elem)do { if (_Py_RefTotal-- , --((PyObject*)(elem))->ob_refcnt
!= 0) { if (((PyObject*)elem)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 2494, (PyObject *)(elem)); } else _Py_Dealloc
((PyObject *)(elem)); } while (0)
;
2495 Py_DECREF(dup)do { if (_Py_RefTotal-- , --((PyObject*)(dup))->ob_refcnt !=
0) { if (((PyObject*)dup)->ob_refcnt < 0) _Py_NegativeRefcount
("Objects/setobject.c", 2495, (PyObject *)(dup)); } else _Py_Dealloc
((PyObject *)(dup)); } while (0)
;
2496 Py_RETURN_TRUEreturn ( _Py_RefTotal++ , ((PyObject*)(((PyObject *) &_Py_TrueStruct
)))->ob_refcnt++), ((PyObject *) &_Py_TrueStruct)
;
2497}
2498
2499#undef assertRaises
2500
2501#endif