Coverage Report

Created: 2026-03-08 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Objects/setobject.c
Line
Count
Source
1
2
/* set object implementation
3
4
   Written and maintained by Raymond D. Hettinger <python@rcn.com>
5
   Derived from Objects/dictobject.c.
6
7
   The basic lookup function used by all operations.
8
   This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
9
10
   The initial probe index is computed as hash mod the table size.
11
   Subsequent probe indices are computed as explained in Objects/dictobject.c.
12
13
   To improve cache locality, each probe inspects a series of consecutive
14
   nearby entries before moving on to probes elsewhere in memory.  This leaves
15
   us with a hybrid of linear probing and randomized probing.  The linear probing
16
   reduces the cost of hash collisions because consecutive memory accesses
17
   tend to be much cheaper than scattered probes.  After LINEAR_PROBES steps,
18
   we then use more of the upper bits from the hash value and apply a simple
19
   linear congruential random number generator.  This helps break-up long
20
   chains of collisions.
21
22
   All arithmetic on hash should ignore overflow.
23
24
   Unlike the dictionary implementation, the lookkey function can return
25
   NULL if the rich comparison returns an error.
26
27
   Use cases for sets differ considerably from dictionaries where looked-up
28
   keys are more likely to be present.  In contrast, sets are primarily
29
   about membership testing where the presence of an element is not known in
30
   advance.  Accordingly, the set implementation needs to optimize for both
31
   the found and not-found case.
32
*/
33
34
#include "Python.h"
35
#include "pycore_ceval.h"               // _PyEval_GetBuiltin()
36
#include "pycore_critical_section.h"    // Py_BEGIN_CRITICAL_SECTION, Py_END_CRITICAL_SECTION
37
#include "pycore_dict.h"                // _PyDict_Contains_KnownHash()
38
#include "pycore_modsupport.h"          // _PyArg_NoKwnames()
39
#include "pycore_object.h"              // _PyObject_GC_UNTRACK()
40
#include "pycore_pyatomic_ft_wrappers.h"  // FT_ATOMIC_LOAD_SSIZE_RELAXED()
41
#include "pycore_pyerrors.h"            // _PyErr_SetKeyError()
42
#include "pycore_setobject.h"           // _PySet_NextEntry() definition
43
#include "pycore_weakref.h"             // FT_CLEAR_WEAKREFS()
44
45
#include "stringlib/eq.h"               // unicode_eq()
46
#include <stddef.h>                     // offsetof()
47
#include "clinic/setobject.c.h"
48
49
/*[clinic input]
50
class set "PySetObject *" "&PySet_Type"
51
class frozenset "PySetObject *" "&PyFrozenSet_Type"
52
[clinic start generated code]*/
53
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=97ad1d3e9f117079]*/
54
55
/*[python input]
56
class setobject_converter(self_converter):
57
    type = "PySetObject *"
58
[python start generated code]*/
59
/*[python end generated code: output=da39a3ee5e6b4b0d input=33a44506d4d57793]*/
60
61
/* Object used as dummy key to fill deleted entries */
62
static PyObject _dummy_struct;
63
64
8.59M
#define dummy (&_dummy_struct)
65
66
97.2M
#define SET_LOOKKEY_FOUND 1
67
437M
#define SET_LOOKKEY_NO_MATCH 0
68
0
#define SET_LOOKKEY_ERROR (-1)
69
81.7M
#define SET_LOOKKEY_CHANGED (-2)
70
377M
#define SET_LOOKKEY_EMPTY (-3)
71
72
typedef int (*compare_func)(PySetObject *so, setentry *table, setentry *ep,
73
                            PyObject *key, Py_hash_t hash);
74
75
#ifdef Py_GIL_DISABLED
76
77
#define SET_IS_SHARED(so) _PyObject_GC_IS_SHARED(so)
78
#define SET_MARK_SHARED(so) _PyObject_GC_SET_SHARED(so)
79
80
static void
81
ensure_shared_on_read(PySetObject *so)
82
{
83
    if (!_Py_IsOwnedByCurrentThread((PyObject *)so) && !SET_IS_SHARED(so)) {
84
        // The first time we access a set from a non-owning thread we mark it
85
        // as shared. This ensures that a concurrent resize operation will
86
        // delay freeing the old entries using QSBR, which is necessary
87
        // to safely allow concurrent reads without locking...
88
        Py_BEGIN_CRITICAL_SECTION(so);
89
        if (!SET_IS_SHARED(so)) {
90
            SET_MARK_SHARED(so);
91
        }
92
        Py_END_CRITICAL_SECTION();
93
    }
94
}
95
96
static inline Py_ALWAYS_INLINE int
97
set_compare_threadsafe(PySetObject *so, setentry *table, setentry *ep,
98
                       PyObject *key, Py_hash_t hash)
99
{
100
    PyObject *startkey = FT_ATOMIC_LOAD_PTR_ACQUIRE(ep->key);
101
    if (startkey == NULL) {
102
        return SET_LOOKKEY_EMPTY;
103
    }
104
    if (startkey == key) {
105
        return SET_LOOKKEY_FOUND;
106
    }
107
    Py_ssize_t ep_hash = FT_ATOMIC_LOAD_SSIZE_ACQUIRE(ep->hash);
108
    if (ep_hash == hash) {
109
        if (!_Py_TryIncrefCompare(&ep->key, startkey)) {
110
            return SET_LOOKKEY_CHANGED;
111
        }
112
        int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
113
        Py_DECREF(startkey);
114
        if (cmp < 0) {
115
            return SET_LOOKKEY_ERROR;
116
        }
117
        if (table == FT_ATOMIC_LOAD_PTR_ACQUIRE(so->table) &&
118
            startkey == FT_ATOMIC_LOAD_PTR_ACQUIRE(ep->key)) {
119
            assert(cmp == SET_LOOKKEY_FOUND || cmp == SET_LOOKKEY_NO_MATCH);
120
            return cmp;
121
        }
122
        else {
123
            /* The set was mutated, restart */
124
            return SET_LOOKKEY_CHANGED;
125
        }
126
    }
127
    return SET_LOOKKEY_NO_MATCH;
128
}
129
130
#else
131
132
66.8k
#define SET_IS_SHARED(so) 0
133
#define SET_MARK_SHARED(so)
134
135
#endif
136
137
static inline Py_ALWAYS_INLINE int
138
set_compare_entry_lock_held(PySetObject *so, setentry *table, setentry *entry,
139
                            PyObject *key, Py_hash_t hash)
140
95.5M
{
141
95.5M
    _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(so);
142
95.5M
    if (entry->hash == 0 && entry->key == NULL)
143
37.3M
        return SET_LOOKKEY_EMPTY;
144
58.2M
    if (entry->hash == hash) {
145
44.4M
        PyObject *startkey = entry->key;
146
44.4M
        assert(startkey != dummy);
147
44.4M
        if (startkey == key)
148
44.2M
            return SET_LOOKKEY_FOUND;
149
160k
        if (PyUnicode_CheckExact(startkey)
150
160k
            && PyUnicode_CheckExact(key)
151
7.74k
            && unicode_eq(startkey, key))
152
7.74k
            return SET_LOOKKEY_FOUND;
153
152k
        table = so->table;
154
152k
        Py_INCREF(startkey);
155
152k
        int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
156
152k
        Py_DECREF(startkey);
157
152k
        if (cmp < 0)
158
0
            return SET_LOOKKEY_ERROR;
159
152k
        if (table != so->table || entry->key != startkey)
160
0
            return SET_LOOKKEY_CHANGED;
161
152k
        if (cmp > 0)
162
152k
            return SET_LOOKKEY_FOUND;
163
152k
    }
164
13.7M
    return SET_LOOKKEY_NO_MATCH;
165
58.2M
}
166
167
// This is similar to set_compare_entry_lock_held() but we don't need to
168
// incref startkey before comparing and we don't need to check if the set has
169
// changed.  This also omits the PyUnicode_CheckExact() special case since it
170
// doesn't help much for frozensets.
171
static inline Py_ALWAYS_INLINE int
172
set_compare_frozenset(PySetObject *so, setentry *table, setentry *ep,
173
                                 PyObject *key, Py_hash_t hash)
174
171M
{
175
171M
    assert(PyFrozenSet_Check(so));
176
171M
    PyObject *startkey = ep->key;
177
171M
    if (startkey == NULL) {
178
102M
        return SET_LOOKKEY_EMPTY;
179
102M
    }
180
68.9M
    if (startkey == key) {
181
52.8M
        return SET_LOOKKEY_FOUND;
182
52.8M
    }
183
16.1M
    Py_ssize_t ep_hash = ep->hash;
184
16.1M
    if (ep_hash == hash) {
185
158k
        int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
186
158k
        if (cmp < 0) {
187
0
            return SET_LOOKKEY_ERROR;
188
0
        }
189
158k
        assert(cmp == SET_LOOKKEY_FOUND || cmp == SET_LOOKKEY_NO_MATCH);
190
158k
        return cmp;
191
158k
    }
192
16.0M
    return SET_LOOKKEY_NO_MATCH;
193
16.1M
}
194
195
static void
196
set_zero_table(setentry *table, size_t size)
197
80.6k
{
198
#ifdef Py_GIL_DISABLED
199
    for (size_t i = 0; i < size; i++) {
200
        setentry *entry = &table[i];
201
        FT_ATOMIC_STORE_SSIZE_RELAXED(entry->hash, 0);
202
        FT_ATOMIC_STORE_PTR_RELEASE(entry->key, NULL);
203
    }
204
#else
205
80.6k
    memset(table, 0, sizeof(setentry)*size);
206
80.6k
#endif
207
80.6k
}
208
209
/* ======================================================================== */
210
/* ======= Begin logic for probing the hash table ========================= */
211
212
/* Set this to zero to turn-off linear probing */
213
#ifndef LINEAR_PROBES
214
369M
#define LINEAR_PROBES 9
215
#endif
216
217
/* This must be >= 1 */
218
8.95M
#define PERTURB_SHIFT 5
219
220
static int
221
set_do_lookup(PySetObject *so, setentry *table, size_t mask, PyObject *key,
222
              Py_hash_t hash, setentry **epp, compare_func compare_entry)
223
237M
{
224
237M
    setentry *entry;
225
237M
    size_t perturb = hash;
226
237M
    size_t i = (size_t)hash & mask; /* Unsigned for defined overflow behavior */
227
237M
    int probes;
228
237M
    int status;
229
230
246M
    while (1) {
231
246M
        entry = &table[i];
232
246M
        probes = (i + LINEAR_PROBES <= mask) ? LINEAR_PROBES: 0;
233
267M
        do {
234
267M
            status = compare_entry(so, table, entry, key, hash);
235
267M
            if (status != SET_LOOKKEY_NO_MATCH) {
236
237M
                if (status == SET_LOOKKEY_EMPTY) {
237
140M
                    return SET_LOOKKEY_NO_MATCH;
238
140M
                }
239
97.3M
                *epp = entry;
240
97.3M
                return status;
241
237M
            }
242
29.8M
            entry++;
243
29.8M
        } while (probes--);
244
8.78M
        perturb >>= PERTURB_SHIFT;
245
8.78M
        i = (i * 5 + 1 + perturb) & mask;
246
8.78M
    }
247
237M
    Py_UNREACHABLE();
248
237M
}
249
250
static int set_table_resize(PySetObject *, Py_ssize_t);
251
252
static int
253
set_add_entry_takeref(PySetObject *so, PyObject *key, Py_hash_t hash)
254
7.89M
{
255
7.89M
    setentry *table;
256
7.89M
    setentry *freeslot;
257
7.89M
    setentry *entry;
258
7.89M
    size_t perturb;
259
7.89M
    size_t mask;
260
7.89M
    size_t i;                       /* Unsigned for defined overflow behavior */
261
7.89M
    int probes;
262
7.89M
    int cmp;
263
264
7.89M
  restart:
265
266
7.89M
    mask = so->mask;
267
7.89M
    i = (size_t)hash & mask;
268
7.89M
    freeslot = NULL;
269
7.89M
    perturb = hash;
270
271
8.04M
    while (1) {
272
8.04M
        entry = &so->table[i];
273
8.04M
        probes = (i + LINEAR_PROBES <= mask) ? LINEAR_PROBES: 0;
274
10.3M
        do {
275
10.3M
            if (entry->hash == 0 && entry->key == NULL)
276
4.17M
                goto found_unused_or_dummy;
277
6.19M
            if (entry->hash == hash) {
278
3.71M
                PyObject *startkey = entry->key;
279
3.71M
                assert(startkey != dummy);
280
3.71M
                if (startkey == key)
281
378k
                    goto found_active;
282
3.34M
                if (PyUnicode_CheckExact(startkey)
283
3.34M
                    && PyUnicode_CheckExact(key)
284
439k
                    && unicode_eq(startkey, key))
285
439k
                    goto found_active;
286
2.90M
                table = so->table;
287
2.90M
                Py_INCREF(startkey);
288
2.90M
                cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
289
2.90M
                Py_DECREF(startkey);
290
2.90M
                if (cmp > 0)
291
2.90M
                    goto found_active;
292
947
                if (cmp < 0)
293
0
                    goto comparison_error;
294
947
                if (table != so->table || entry->key != startkey)
295
0
                    goto restart;
296
947
                mask = so->mask;
297
947
            }
298
2.48M
            else if (entry->hash == -1) {
299
0
                assert (entry->key == dummy);
300
0
                freeslot = entry;
301
0
            }
302
2.48M
            entry++;
303
2.48M
        } while (probes--);
304
159k
        perturb >>= PERTURB_SHIFT;
305
159k
        i = (i * 5 + 1 + perturb) & mask;
306
159k
    }
307
308
4.17M
  found_unused_or_dummy:
309
4.17M
    if (freeslot == NULL)
310
4.17M
        goto found_unused;
311
0
    if (freeslot->hash != -1) {
312
0
        goto restart;
313
0
    }
314
0
    FT_ATOMIC_STORE_SSIZE_RELAXED(so->used, so->used + 1);
315
0
    FT_ATOMIC_STORE_SSIZE_RELAXED(freeslot->hash, hash);
316
0
    FT_ATOMIC_STORE_PTR_RELEASE(freeslot->key, key);
317
0
    return 0;
318
319
4.17M
  found_unused:
320
4.17M
    so->fill++;
321
4.17M
    FT_ATOMIC_STORE_SSIZE_RELAXED(so->used, so->used + 1);
322
4.17M
    FT_ATOMIC_STORE_SSIZE_RELAXED(entry->hash, hash);
323
4.17M
    FT_ATOMIC_STORE_PTR_RELEASE(entry->key, key);
324
4.17M
    if ((size_t)so->fill*5 < mask*3)
325
4.13M
        return 0;
326
37.7k
    return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
327
328
3.71M
  found_active:
329
3.71M
    Py_DECREF(key);
330
3.71M
    return 0;
331
332
0
  comparison_error:
333
0
    Py_DECREF(key);
334
0
    return -1;
335
4.17M
}
336
337
static int
338
set_add_entry(PySetObject *so, PyObject *key, Py_hash_t hash)
339
7.26M
{
340
7.26M
    _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(so);
341
342
7.26M
    return set_add_entry_takeref(so, Py_NewRef(key), hash);
343
7.26M
}
344
345
static void
346
set_unhashable_type(PyObject *key)
347
0
{
348
0
    PyObject *exc = PyErr_GetRaisedException();
349
0
    assert(exc != NULL);
350
0
    if (!Py_IS_TYPE(exc, (PyTypeObject*)PyExc_TypeError)) {
351
0
        PyErr_SetRaisedException(exc);
352
0
        return;
353
0
    }
354
355
0
    PyErr_Format(PyExc_TypeError,
356
0
                 "cannot use '%T' as a set element (%S)",
357
0
                 key, exc);
358
0
    Py_DECREF(exc);
359
0
}
360
361
int
362
_PySet_AddTakeRef(PySetObject *so, PyObject *key)
363
628k
{
364
628k
    Py_hash_t hash = _PyObject_HashFast(key);
365
628k
    if (hash == -1) {
366
0
        set_unhashable_type(key);
367
0
        Py_DECREF(key);
368
0
        return -1;
369
0
    }
370
    // We don't pre-increment here, the caller holds a strong
371
    // reference to the object which we are stealing.
372
628k
    return set_add_entry_takeref(so, key, hash);
373
628k
}
374
375
/*
376
Internal routine used by set_table_resize() to insert an item which is
377
known to be absent from the set.  Besides the performance benefit,
378
there is also safety benefit since using set_add_entry() risks making
379
a callback in the middle of a set_table_resize(), see issue 1456209.
380
The caller is responsible for updating the key's reference count and
381
the setobject's fill and used fields.
382
*/
383
static void
384
set_insert_clean(setentry *table, size_t mask, PyObject *key, Py_hash_t hash)
385
1.94M
{
386
1.94M
    setentry *entry;
387
1.94M
    size_t perturb = hash;
388
1.94M
    size_t i = (size_t)hash & mask;
389
1.94M
    size_t j;
390
391
1.95M
    while (1) {
392
1.95M
        entry = &table[i];
393
1.95M
        if (entry->key == NULL)
394
1.80M
            goto found_null;
395
147k
        if (i + LINEAR_PROBES <= mask) {
396
248k
            for (j = 0; j < LINEAR_PROBES; j++) {
397
239k
                entry++;
398
239k
                if (entry->key == NULL)
399
133k
                    goto found_null;
400
239k
            }
401
142k
        }
402
13.9k
        perturb >>= PERTURB_SHIFT;
403
13.9k
        i = (i * 5 + 1 + perturb) & mask;
404
13.9k
    }
405
1.94M
  found_null:
406
1.94M
    FT_ATOMIC_STORE_SSIZE_RELAXED(entry->hash, hash);
407
1.94M
    FT_ATOMIC_STORE_PTR_RELEASE(entry->key, key);
408
1.94M
}
409
410
/* ======== End logic for probing the hash table ========================== */
411
/* ======================================================================== */
412
413
static int
414
set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash, setentry **epp)
415
237M
{
416
237M
    int status;
417
237M
    if (PyFrozenSet_CheckExact(so)) {
418
155M
        status = set_do_lookup(so, so->table, so->mask, key, hash, epp,
419
155M
                               set_compare_frozenset);
420
155M
    }
421
81.7M
    else {
422
81.7M
        Py_BEGIN_CRITICAL_SECTION(so);
423
81.7M
        do {
424
81.7M
            status = set_do_lookup(so, so->table, so->mask, key, hash, epp,
425
81.7M
                                   set_compare_entry_lock_held);
426
81.7M
        } while (status == SET_LOOKKEY_CHANGED);
427
81.7M
        Py_END_CRITICAL_SECTION();
428
81.7M
    }
429
237M
    assert(status == SET_LOOKKEY_FOUND ||
430
237M
           status == SET_LOOKKEY_NO_MATCH ||
431
237M
           status == SET_LOOKKEY_ERROR);
432
237M
    return status;
433
237M
}
434
435
#ifdef Py_GIL_DISABLED
436
static int
437
set_lookkey_threadsafe(PySetObject *so, PyObject *key, Py_hash_t hash)
438
{
439
    int status;
440
    setentry *entry;
441
    if (PyFrozenSet_CheckExact(so)) {
442
        status = set_do_lookup(so, so->table, so->mask, key, hash, &entry,
443
                               set_compare_frozenset);
444
        assert(status == SET_LOOKKEY_FOUND ||
445
               status == SET_LOOKKEY_NO_MATCH ||
446
               status == SET_LOOKKEY_ERROR);
447
        return status;
448
    }
449
    ensure_shared_on_read(so);
450
    setentry *table = FT_ATOMIC_LOAD_PTR_ACQUIRE(so->table);
451
    size_t mask = FT_ATOMIC_LOAD_SSIZE_ACQUIRE(so->mask);
452
    if (table == NULL || table != FT_ATOMIC_LOAD_PTR_ACQUIRE(so->table)) {
453
        return set_lookkey(so, key, hash, &entry);
454
    }
455
    status = set_do_lookup(so, table, mask, key, hash, &entry,
456
                           set_compare_threadsafe);
457
    if (status == SET_LOOKKEY_CHANGED) {
458
        return set_lookkey(so, key, hash, &entry);
459
    }
460
    assert(status == SET_LOOKKEY_FOUND ||
461
           status == SET_LOOKKEY_NO_MATCH ||
462
           status == SET_LOOKKEY_ERROR);
463
    return status;
464
}
465
#endif
466
467
static void free_entries(setentry *entries, size_t size, bool use_qsbr)
468
66.8k
{
469
#ifdef Py_GIL_DISABLED
470
    if (use_qsbr) {
471
        _PyMem_FreeDelayed(entries, size * sizeof(setentry));
472
        return;
473
    }
474
#endif
475
66.8k
    PyMem_Free(entries);
476
66.8k
}
477
478
/*
479
Restructure the table by allocating a new table and reinserting all
480
keys again.  When entries have been deleted, the new table may
481
actually be smaller than the old one.
482
*/
483
static int
484
set_table_resize(PySetObject *so, Py_ssize_t minused)
485
68.5k
{
486
68.5k
    setentry *oldtable, *newtable, *entry;
487
68.5k
    Py_ssize_t oldmask = so->mask;
488
68.5k
    Py_ssize_t oldsize = (size_t)oldmask + 1;
489
68.5k
    size_t newmask;
490
68.5k
    int is_oldtable_malloced;
491
68.5k
    setentry small_copy[PySet_MINSIZE];
492
493
68.5k
    assert(minused >= 0);
494
495
    /* Find the smallest table size > minused. */
496
    /* XXX speed-up with intrinsics */
497
68.5k
    size_t newsize = PySet_MINSIZE;
498
227k
    while (newsize <= (size_t)minused) {
499
159k
        newsize <<= 1; // The largest possible value is PY_SSIZE_T_MAX + 1.
500
159k
    }
501
502
    /* Get space for a new table. */
503
68.5k
    oldtable = so->table;
504
68.5k
    assert(oldtable != NULL);
505
68.5k
    is_oldtable_malloced = oldtable != so->smalltable;
506
507
68.5k
    if (newsize == PySet_MINSIZE) {
508
        /* A large table is shrinking, or we can't get any smaller. */
509
0
        newtable = so->smalltable;
510
0
        if (newtable == oldtable) {
511
0
            if (so->fill == so->used) {
512
                /* No dummies, so no point doing anything. */
513
0
                return 0;
514
0
            }
515
            /* We're not going to resize it, but rebuild the
516
               table anyway to purge old dummy entries.
517
               Subtle:  This is *necessary* if fill==size,
518
               as set_lookkey needs at least one virgin slot to
519
               terminate failing searches.  If fill < size, it's
520
               merely desirable, as dummies slow searches. */
521
0
            assert(so->fill > so->used);
522
0
            memcpy(small_copy, oldtable, sizeof(small_copy));
523
0
            oldtable = small_copy;
524
0
        }
525
0
    }
526
68.5k
    else {
527
68.5k
        newtable = PyMem_NEW(setentry, newsize);
528
68.5k
        if (newtable == NULL) {
529
0
            PyErr_NoMemory();
530
0
            return -1;
531
0
        }
532
68.5k
    }
533
534
    /* Make the set empty, using the new table. */
535
68.5k
    assert(newtable != oldtable);
536
68.5k
    set_zero_table(newtable, newsize);
537
68.5k
    FT_ATOMIC_STORE_PTR_RELEASE(so->table, NULL);
538
68.5k
    FT_ATOMIC_STORE_SSIZE_RELEASE(so->mask, newsize - 1);
539
540
    /* Copy the data over; this is refcount-neutral for active entries;
541
       dummy entries aren't copied over, of course */
542
68.5k
    newmask = (size_t)so->mask;
543
68.5k
    if (so->fill == so->used) {
544
3.42M
        for (entry = oldtable; entry <= oldtable + oldmask; entry++) {
545
3.35M
            if (entry->key != NULL) {
546
1.93M
                set_insert_clean(newtable, newmask, entry->key, entry->hash);
547
1.93M
            }
548
3.35M
        }
549
68.5k
    } else {
550
0
        so->fill = so->used;
551
0
        for (entry = oldtable; entry <= oldtable + oldmask; entry++) {
552
0
            if (entry->key != NULL && entry->key != dummy) {
553
0
                set_insert_clean(newtable, newmask, entry->key, entry->hash);
554
0
            }
555
0
        }
556
0
    }
557
558
68.5k
    FT_ATOMIC_STORE_PTR_RELEASE(so->table, newtable);
559
560
68.5k
    if (is_oldtable_malloced)
561
18.8k
        free_entries(oldtable, oldsize, SET_IS_SHARED(so));
562
68.5k
    return 0;
563
68.5k
}
564
565
static int
566
set_contains_entry(PySetObject *so, PyObject *key, Py_hash_t hash)
567
237M
{
568
#ifdef Py_GIL_DISABLED
569
    return set_lookkey_threadsafe(so, key, hash);
570
#else
571
237M
    setentry *entry; // unused
572
237M
    return set_lookkey(so, key, hash, &entry);
573
237M
#endif
574
237M
}
575
576
46.6k
#define DISCARD_NOTFOUND 0
577
1.34k
#define DISCARD_FOUND 1
578
579
static int
580
set_discard_entry(PySetObject *so, PyObject *key, Py_hash_t hash)
581
47.9k
{
582
47.9k
    setentry *entry;
583
47.9k
    PyObject *old_key;
584
47.9k
    int status = set_lookkey(so, key, hash, &entry);
585
47.9k
    if (status < 0) {
586
0
        return -1;
587
0
    }
588
47.9k
    if (status == SET_LOOKKEY_NO_MATCH) {
589
46.6k
        return DISCARD_NOTFOUND;
590
46.6k
    }
591
47.9k
    assert(status == SET_LOOKKEY_FOUND);
592
1.34k
    old_key = entry->key;
593
1.34k
    FT_ATOMIC_STORE_SSIZE_RELAXED(entry->hash, -1);
594
1.34k
    FT_ATOMIC_STORE_SSIZE_RELAXED(so->used, so->used - 1);
595
1.34k
    FT_ATOMIC_STORE_PTR_RELEASE(entry->key, dummy);
596
1.34k
    Py_DECREF(old_key);
597
1.34k
    return DISCARD_FOUND;
598
47.9k
}
599
600
static int
601
set_add_key(PySetObject *so, PyObject *key)
602
6.50M
{
603
6.50M
    Py_hash_t hash = _PyObject_HashFast(key);
604
6.50M
    if (hash == -1) {
605
0
        set_unhashable_type(key);
606
0
        return -1;
607
0
    }
608
6.50M
    return set_add_entry(so, key, hash);
609
6.50M
}
610
611
static int
612
set_contains_key(PySetObject *so, PyObject *key)
613
4.82M
{
614
4.82M
    Py_hash_t hash = _PyObject_HashFast(key);
615
4.82M
    if (hash == -1) {
616
0
        set_unhashable_type(key);
617
0
        return -1;
618
0
    }
619
4.82M
    return set_contains_entry(so, key, hash);
620
4.82M
}
621
622
static int
623
set_discard_key(PySetObject *so, PyObject *key)
624
47.9k
{
625
47.9k
    Py_hash_t hash = _PyObject_HashFast(key);
626
47.9k
    if (hash == -1) {
627
0
        set_unhashable_type(key);
628
0
        return -1;
629
0
    }
630
47.9k
    return set_discard_entry(so, key, hash);
631
47.9k
}
632
633
static void
634
set_empty_to_minsize(PySetObject *so)
635
12.0k
{
636
12.0k
    FT_ATOMIC_STORE_PTR_RELEASE(so->table, NULL);
637
12.0k
    set_zero_table(so->smalltable, PySet_MINSIZE);
638
12.0k
    so->fill = 0;
639
12.0k
    FT_ATOMIC_STORE_SSIZE_RELAXED(so->used, 0);
640
12.0k
    FT_ATOMIC_STORE_SSIZE_RELEASE(so->mask, PySet_MINSIZE - 1);
641
12.0k
    FT_ATOMIC_STORE_SSIZE_RELAXED(so->hash, -1);
642
12.0k
    FT_ATOMIC_STORE_PTR_RELEASE(so->table, so->smalltable);
643
12.0k
}
644
645
static int
646
set_clear_internal(PyObject *self)
647
32.3k
{
648
32.3k
    PySetObject *so = _PySet_CAST(self);
649
32.3k
    setentry *entry;
650
32.3k
    setentry *table = so->table;
651
32.3k
    Py_ssize_t fill = so->fill;
652
32.3k
    Py_ssize_t used = so->used;
653
32.3k
    Py_ssize_t oldsize = (size_t)so->mask + 1;
654
32.3k
    int table_is_malloced = table != so->smalltable;
655
32.3k
    setentry small_copy[PySet_MINSIZE];
656
657
32.3k
    assert (PyAnySet_Check(so));
658
32.3k
    assert(table != NULL);
659
660
    /* This is delicate.  During the process of clearing the set,
661
     * decrefs can cause the set to mutate.  To avoid fatal confusion
662
     * (voice of experience), we have to make the set empty before
663
     * clearing the slots, and never refer to anything via so->ref while
664
     * clearing.
665
     */
666
32.3k
    if (table_is_malloced)
667
6.55k
        set_empty_to_minsize(so);
668
669
25.8k
    else if (fill > 0) {
670
        /* It's a small table with something that needs to be cleared.
671
         * Afraid the only safe way is to copy the set entries into
672
         * another small table first.
673
         */
674
5.51k
        memcpy(small_copy, table, sizeof(small_copy));
675
5.51k
        table = small_copy;
676
5.51k
        set_empty_to_minsize(so);
677
5.51k
    }
678
    /* else it's a small table that's already empty */
679
680
    /* Now we can finally clear things.  If C had refcounts, we could
681
     * assert that the refcount on table is 1 now, i.e. that this function
682
     * has unique access to it, so decref side-effects can't alter it.
683
     */
684
1.98M
    for (entry = table; used > 0; entry++) {
685
1.95M
        if (entry->key && entry->key != dummy) {
686
570k
            used--;
687
570k
            Py_DECREF(entry->key);
688
570k
        }
689
1.95M
    }
690
691
32.3k
    if (table_is_malloced)
692
6.55k
        free_entries(table, oldsize, SET_IS_SHARED(so));
693
32.3k
    return 0;
694
32.3k
}
695
696
/*
697
 * Iterate over a set table.  Use like so:
698
 *
699
 *     Py_ssize_t pos;
700
 *     setentry *entry;
701
 *     pos = 0;   # important!  pos should not otherwise be changed by you
702
 *     while (set_next(yourset, &pos, &entry)) {
703
 *              Refer to borrowed reference in entry->key.
704
 *     }
705
 *
706
 * CAUTION:  In general, it isn't safe to use set_next in a loop that
707
 * mutates the table.
708
 */
709
static int
710
set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
711
5.68M
{
712
5.68M
    Py_ssize_t i;
713
5.68M
    Py_ssize_t mask;
714
5.68M
    setentry *entry;
715
716
5.68M
    assert (PyAnySet_Check(so));
717
5.68M
    i = *pos_ptr;
718
5.68M
    assert(i >= 0);
719
5.68M
    mask = so->mask;
720
5.68M
    entry = &so->table[i];
721
35.7M
    while (i <= mask && (entry->key == NULL || entry->key == dummy)) {
722
30.0M
        i++;
723
30.0M
        entry++;
724
30.0M
    }
725
5.68M
    *pos_ptr = i+1;
726
5.68M
    if (i > mask)
727
3.26M
        return 0;
728
5.68M
    assert(entry != NULL);
729
2.41M
    *entry_ptr = entry;
730
2.41M
    return 1;
731
5.68M
}
732
733
static void
734
set_dealloc(PyObject *self)
735
4.95M
{
736
4.95M
    PySetObject *so = _PySet_CAST(self);
737
4.95M
    setentry *entry;
738
4.95M
    Py_ssize_t used = so->used;
739
4.95M
    Py_ssize_t oldsize = (size_t)so->mask + 1;
740
741
    /* bpo-31095: UnTrack is needed before calling any callbacks */
742
4.95M
    PyObject_GC_UnTrack(so);
743
4.95M
    FT_CLEAR_WEAKREFS(self, so->weakreflist);
744
745
18.9M
    for (entry = so->table; used > 0; entry++) {
746
13.9M
        if (entry->key && entry->key != dummy) {
747
4.05M
                used--;
748
4.05M
                Py_DECREF(entry->key);
749
4.05M
        }
750
13.9M
    }
751
4.95M
    if (so->table != so->smalltable)
752
41.3k
        free_entries(so->table, oldsize, SET_IS_SHARED(so));
753
4.95M
    Py_TYPE(so)->tp_free(so);
754
4.95M
}
755
756
static PyObject *
757
set_repr_lock_held(PySetObject *so)
758
0
{
759
0
    PyObject *result=NULL, *keys, *listrepr, *tmp;
760
0
    int status = Py_ReprEnter((PyObject*)so);
761
762
0
    if (status != 0) {
763
0
        if (status < 0)
764
0
            return NULL;
765
0
        return PyUnicode_FromFormat("%s(...)", Py_TYPE(so)->tp_name);
766
0
    }
767
768
    /* shortcut for the empty set */
769
0
    if (!so->used) {
770
0
        Py_ReprLeave((PyObject*)so);
771
0
        return PyUnicode_FromFormat("%s()", Py_TYPE(so)->tp_name);
772
0
    }
773
774
    // gh-129967: avoid PySequence_List because it might re-lock the object
775
    // lock or the GIL and allow something to clear the set from underneath us.
776
0
    keys = PyList_New(so->used);
777
0
    if (keys == NULL) {
778
0
        goto done;
779
0
    }
780
781
0
    Py_ssize_t pos = 0, idx = 0;
782
0
    setentry *entry;
783
0
    while (set_next(so, &pos, &entry)) {
784
0
        PyList_SET_ITEM(keys, idx++, Py_NewRef(entry->key));
785
0
    }
786
787
    /* repr(keys)[1:-1] */
788
0
    listrepr = PyObject_Repr(keys);
789
0
    Py_DECREF(keys);
790
0
    if (listrepr == NULL)
791
0
        goto done;
792
0
    tmp = PyUnicode_Substring(listrepr, 1, PyUnicode_GET_LENGTH(listrepr)-1);
793
0
    Py_DECREF(listrepr);
794
0
    if (tmp == NULL)
795
0
        goto done;
796
0
    listrepr = tmp;
797
798
0
    if (!PySet_CheckExact(so))
799
0
        result = PyUnicode_FromFormat("%s({%U})",
800
0
                                      Py_TYPE(so)->tp_name,
801
0
                                      listrepr);
802
0
    else
803
0
        result = PyUnicode_FromFormat("{%U}", listrepr);
804
0
    Py_DECREF(listrepr);
805
0
done:
806
0
    Py_ReprLeave((PyObject*)so);
807
0
    return result;
808
0
}
809
810
static PyObject *
811
set_repr(PyObject *self)
812
0
{
813
0
    PySetObject *so = _PySet_CAST(self);
814
0
    PyObject *result;
815
0
    Py_BEGIN_CRITICAL_SECTION(so);
816
0
    result = set_repr_lock_held(so);
817
0
    Py_END_CRITICAL_SECTION();
818
0
    return result;
819
0
}
820
821
static Py_ssize_t
822
set_len(PyObject *self)
823
378k
{
824
378k
    PySetObject *so = _PySet_CAST(self);
825
378k
    return FT_ATOMIC_LOAD_SSIZE_RELAXED(so->used);
826
378k
}
827
828
static int
829
set_merge_lock_held(PySetObject *so, PyObject *otherset)
830
873k
{
831
873k
    PySetObject *other;
832
873k
    PyObject *key;
833
873k
    Py_ssize_t i;
834
873k
    setentry *so_entry;
835
873k
    setentry *other_entry;
836
837
873k
    assert (PyAnySet_Check(so));
838
873k
    _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(so);
839
873k
    _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(otherset);
840
841
873k
    other = _PySet_CAST(otherset);
842
873k
    if (other == so || other->used == 0)
843
        /* a.update(a) or a.update(set()); nothing to do */
844
230k
        return 0;
845
    /* Do one big resize at the start, rather than
846
     * incrementally resizing as we insert new keys.  Expect
847
     * that there will be no (or few) overlapping keys.
848
     */
849
643k
    if ((so->fill + other->used)*5 >= so->mask*3) {
850
30.8k
        if (set_table_resize(so, (so->used + other->used)*2) != 0)
851
0
            return -1;
852
30.8k
    }
853
643k
    so_entry = so->table;
854
643k
    other_entry = other->table;
855
856
    /* If our table is empty, and both tables have the same size, and
857
       there are no dummies to eliminate, then just copy the pointers. */
858
643k
    if (so->fill == 0 && so->mask == other->mask && other->fill == other->used) {
859
1.80M
        for (i = 0; i <= other->mask; i++, so_entry++, other_entry++) {
860
1.60M
            key = other_entry->key;
861
1.60M
            if (key != NULL) {
862
498k
                assert(so_entry->key == NULL);
863
498k
                FT_ATOMIC_STORE_SSIZE_RELAXED(so_entry->hash, other_entry->hash);
864
498k
                FT_ATOMIC_STORE_PTR_RELEASE(so_entry->key, Py_NewRef(key));
865
498k
            }
866
1.60M
        }
867
191k
        so->fill = other->fill;
868
191k
        FT_ATOMIC_STORE_SSIZE_RELAXED(so->used, other->used);
869
191k
        return 0;
870
191k
    }
871
872
    /* If our table is empty, we can use set_insert_clean() */
873
452k
    if (so->fill == 0) {
874
1.13k
        setentry *newtable = so->table;
875
1.13k
        size_t newmask = (size_t)so->mask;
876
1.13k
        so->fill = other->used;
877
1.13k
        FT_ATOMIC_STORE_SSIZE_RELAXED(so->used, other->used);
878
47.4k
        for (i = other->mask + 1; i > 0 ; i--, other_entry++) {
879
46.3k
            key = other_entry->key;
880
46.3k
            if (key != NULL && key != dummy) {
881
9.85k
                set_insert_clean(newtable, newmask, Py_NewRef(key),
882
9.85k
                                 other_entry->hash);
883
9.85k
            }
884
46.3k
        }
885
1.13k
        return 0;
886
1.13k
    }
887
888
    /* We can't assure there are no duplicates, so do normal insertions */
889
4.30M
    for (i = 0; i <= other->mask; i++) {
890
3.85M
        other_entry = &other->table[i];
891
3.85M
        key = other_entry->key;
892
3.85M
        if (key != NULL && key != dummy) {
893
753k
            if (set_add_entry(so, key, other_entry->hash))
894
0
                return -1;
895
753k
        }
896
3.85M
    }
897
451k
    return 0;
898
451k
}
899
900
/*[clinic input]
901
@critical_section
902
set.pop
903
    so: setobject
904
905
Remove and return an arbitrary set element.
906
907
Raises KeyError if the set is empty.
908
[clinic start generated code]*/
909
910
static PyObject *
911
set_pop_impl(PySetObject *so)
912
/*[clinic end generated code: output=4d65180f1271871b input=9296c84921125060]*/
913
337
{
914
    /* Make sure the search finger is in bounds */
915
337
    setentry *entry = so->table + (so->finger & so->mask);
916
337
    setentry *limit = so->table + so->mask;
917
337
    PyObject *key;
918
919
337
    if (so->used == 0) {
920
0
        PyErr_SetString(PyExc_KeyError, "pop from an empty set");
921
0
        return NULL;
922
0
    }
923
1.76k
    while (entry->key == NULL || entry->key==dummy) {
924
1.42k
        entry++;
925
1.42k
        if (entry > limit)
926
0
            entry = so->table;
927
1.42k
    }
928
337
    FT_ATOMIC_STORE_SSIZE_RELAXED(entry->hash, -1);
929
337
    FT_ATOMIC_STORE_SSIZE_RELAXED(so->used, so->used - 1);
930
337
    key = entry->key;
931
337
    FT_ATOMIC_STORE_PTR_RELEASE(entry->key, dummy);
932
337
    so->finger = entry - so->table + 1;   /* next place to start */
933
337
    return key;
934
337
}
935
936
static int
937
set_traverse(PyObject *self, visitproc visit, void *arg)
938
2.79M
{
939
2.79M
    PySetObject *so = _PySet_CAST(self);
940
2.79M
    Py_ssize_t pos = 0;
941
2.79M
    setentry *entry;
942
943
4.85M
    while (set_next(so, &pos, &entry))
944
2.06M
        Py_VISIT(entry->key);
945
2.79M
    return 0;
946
2.79M
}
947
948
/* Work to increase the bit dispersion for closely spaced hash values.
949
   This is important because some use cases have many combinations of a
950
   small number of elements with nearby hashes so that many distinct
951
   combinations collapse to only a handful of distinct hash values. */
952
953
static Py_uhash_t
954
_shuffle_bits(Py_uhash_t h)
955
2.13M
{
956
2.13M
    return ((h ^ 89869747UL) ^ (h << 16)) * 3644798167UL;
957
2.13M
}
958
959
/* Most of the constants in this hash algorithm are randomly chosen
960
   large primes with "interesting bit patterns" and that passed tests
961
   for good collision statistics on a variety of problematic datasets
962
   including powersets and graph structures (such as David Eppstein's
963
   graph recipes in Lib/test/test_set.py).
964
965
   This hash algorithm can be used on either a frozenset or a set.
966
   When it is used on a set, it computes the hash value of the equivalent
967
   frozenset without creating a new frozenset object.
968
969
   If you update this code, update also frozendict_hash() which copied this
970
   code. */
971
972
static Py_hash_t
973
frozenset_hash_impl(PyObject *self)
974
263k
{
975
263k
    PySetObject *so = _PySet_CAST(self);
976
263k
    Py_uhash_t hash = 0;
977
263k
    setentry *entry;
978
979
    /* Xor-in shuffled bits from every entry's hash field because xor is
980
       commutative and a frozenset hash should be independent of order.
981
982
       For speed, include null entries and dummy entries and then
983
       subtract out their effect afterwards so that the final hash
984
       depends only on active entries.  This allows the code to be
985
       vectorized by the compiler and it saves the unpredictable
986
       branches that would arise when trying to exclude null and dummy
987
       entries on every iteration. */
988
989
2.39M
    for (entry = so->table; entry <= &so->table[so->mask]; entry++)
990
2.13M
        hash ^= _shuffle_bits(entry->hash);
991
992
    /* Remove the effect of an odd number of NULL entries */
993
263k
    if ((so->mask + 1 - so->fill) & 1)
994
453
        hash ^= _shuffle_bits(0);
995
996
    /* Remove the effect of an odd number of dummy entries */
997
263k
    if ((so->fill - so->used) & 1)
998
0
        hash ^= _shuffle_bits(-1);
999
1000
    /* Factor in the number of active entries */
1001
263k
    hash ^= ((Py_uhash_t)PySet_GET_SIZE(self) + 1) * 1927868237UL;
1002
1003
    /* Disperse patterns arising in nested frozensets */
1004
263k
    hash ^= (hash >> 11) ^ (hash >> 25);
1005
263k
    hash = hash * 69069U + 907133923UL;
1006
1007
    /* -1 is reserved as an error code */
1008
263k
    if (hash == (Py_uhash_t)-1)
1009
0
        hash = 590923713UL;
1010
1011
263k
    return (Py_hash_t)hash;
1012
263k
}
1013
1014
static Py_hash_t
1015
frozenset_hash(PyObject *self)
1016
264k
{
1017
264k
    PySetObject *so = _PySet_CAST(self);
1018
264k
    Py_uhash_t hash;
1019
1020
264k
    if (FT_ATOMIC_LOAD_SSIZE_RELAXED(so->hash) != -1) {
1021
482
        return FT_ATOMIC_LOAD_SSIZE_ACQUIRE(so->hash);
1022
482
    }
1023
1024
263k
    hash = frozenset_hash_impl(self);
1025
263k
    FT_ATOMIC_STORE_SSIZE_RELEASE(so->hash, hash);
1026
263k
    return hash;
1027
264k
}
1028
1029
/***** Set iterator type ***********************************************/
1030
1031
typedef struct {
1032
    PyObject_HEAD
1033
    PySetObject *si_set; /* Set to NULL when iterator is exhausted */
1034
    Py_ssize_t si_used;
1035
    Py_ssize_t si_pos;
1036
    Py_ssize_t len;
1037
} setiterobject;
1038
1039
static void
1040
setiter_dealloc(PyObject *self)
1041
347k
{
1042
347k
    setiterobject *si = (setiterobject*)self;
1043
    /* bpo-31095: UnTrack is needed before calling any callbacks */
1044
347k
    _PyObject_GC_UNTRACK(si);
1045
347k
    Py_XDECREF(si->si_set);
1046
347k
    PyObject_GC_Del(si);
1047
347k
}
1048
1049
static int
1050
setiter_traverse(PyObject *self, visitproc visit, void *arg)
1051
133
{
1052
133
    setiterobject *si = (setiterobject*)self;
1053
133
    Py_VISIT(si->si_set);
1054
133
    return 0;
1055
133
}
1056
1057
static PyObject *
1058
setiter_len(PyObject *op, PyObject *Py_UNUSED(ignored))
1059
10
{
1060
10
    setiterobject *si = (setiterobject*)op;
1061
10
    Py_ssize_t len = 0;
1062
10
    if (si->si_set != NULL && si->si_used == si->si_set->used)
1063
10
        len = si->len;
1064
10
    return PyLong_FromSsize_t(len);
1065
10
}
1066
1067
PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
1068
1069
static PyObject *
1070
setiter_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
1071
0
{
1072
0
    setiterobject *si = (setiterobject*)op;
1073
1074
    /* copy the iterator state */
1075
0
    setiterobject tmp = *si;
1076
0
    Py_XINCREF(tmp.si_set);
1077
1078
    /* iterate the temporary into a list */
1079
0
    PyObject *list = PySequence_List((PyObject*)&tmp);
1080
0
    Py_XDECREF(tmp.si_set);
1081
0
    if (list == NULL) {
1082
0
        return NULL;
1083
0
    }
1084
0
    return Py_BuildValue("N(N)", _PyEval_GetBuiltin(&_Py_ID(iter)), list);
1085
0
}
1086
1087
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
1088
1089
static PyMethodDef setiter_methods[] = {
1090
    {"__length_hint__", setiter_len, METH_NOARGS, length_hint_doc},
1091
    {"__reduce__", setiter_reduce, METH_NOARGS, reduce_doc},
1092
    {NULL,              NULL}           /* sentinel */
1093
};
1094
1095
static PyObject *setiter_iternext(PyObject *self)
1096
1.13M
{
1097
1.13M
    setiterobject *si = (setiterobject*)self;
1098
1.13M
    PyObject *key = NULL;
1099
1.13M
    Py_ssize_t i, mask;
1100
1.13M
    setentry *entry;
1101
1.13M
    PySetObject *so = si->si_set;
1102
1103
1.13M
    if (so == NULL)
1104
0
        return NULL;
1105
1.13M
    assert (PyAnySet_Check(so));
1106
1107
1.13M
    Py_ssize_t so_used = FT_ATOMIC_LOAD_SSIZE_RELAXED(so->used);
1108
1.13M
    Py_ssize_t si_used = FT_ATOMIC_LOAD_SSIZE_RELAXED(si->si_used);
1109
1.13M
    if (si_used != so_used) {
1110
0
        PyErr_SetString(PyExc_RuntimeError,
1111
0
                        "Set changed size during iteration");
1112
0
        si->si_used = -1; /* Make this state sticky */
1113
0
        return NULL;
1114
0
    }
1115
1116
1.13M
    Py_BEGIN_CRITICAL_SECTION(so);
1117
1.13M
    i = si->si_pos;
1118
1.13M
    assert(i>=0);
1119
1.13M
    entry = so->table;
1120
1.13M
    mask = so->mask;
1121
5.12M
    while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy)) {
1122
3.99M
        i++;
1123
3.99M
    }
1124
1.13M
    if (i <= mask) {
1125
784k
        key = Py_NewRef(entry[i].key);
1126
784k
    }
1127
1.13M
    Py_END_CRITICAL_SECTION();
1128
1.13M
    si->si_pos = i+1;
1129
1.13M
    if (key == NULL) {
1130
347k
        si->si_set = NULL;
1131
347k
        Py_DECREF(so);
1132
347k
        return NULL;
1133
347k
    }
1134
784k
    si->len--;
1135
784k
    return key;
1136
1.13M
}
1137
1138
PyTypeObject PySetIter_Type = {
1139
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
1140
    "set_iterator",                             /* tp_name */
1141
    sizeof(setiterobject),                      /* tp_basicsize */
1142
    0,                                          /* tp_itemsize */
1143
    /* methods */
1144
    setiter_dealloc,                            /* tp_dealloc */
1145
    0,                                          /* tp_vectorcall_offset */
1146
    0,                                          /* tp_getattr */
1147
    0,                                          /* tp_setattr */
1148
    0,                                          /* tp_as_async */
1149
    0,                                          /* tp_repr */
1150
    0,                                          /* tp_as_number */
1151
    0,                                          /* tp_as_sequence */
1152
    0,                                          /* tp_as_mapping */
1153
    0,                                          /* tp_hash */
1154
    0,                                          /* tp_call */
1155
    0,                                          /* tp_str */
1156
    PyObject_GenericGetAttr,                    /* tp_getattro */
1157
    0,                                          /* tp_setattro */
1158
    0,                                          /* tp_as_buffer */
1159
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,    /* tp_flags */
1160
    0,                                          /* tp_doc */
1161
    setiter_traverse,                           /* tp_traverse */
1162
    0,                                          /* tp_clear */
1163
    0,                                          /* tp_richcompare */
1164
    0,                                          /* tp_weaklistoffset */
1165
    PyObject_SelfIter,                          /* tp_iter */
1166
    setiter_iternext,                           /* tp_iternext */
1167
    setiter_methods,                            /* tp_methods */
1168
    0,
1169
};
1170
1171
static PyObject *
1172
set_iter(PyObject *so)
1173
347k
{
1174
347k
    Py_ssize_t size = set_len(so);
1175
347k
    setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
1176
347k
    if (si == NULL)
1177
0
        return NULL;
1178
347k
    si->si_set = (PySetObject*)Py_NewRef(so);
1179
347k
    si->si_used = size;
1180
347k
    si->si_pos = 0;
1181
347k
    si->len = size;
1182
347k
    _PyObject_GC_TRACK(si);
1183
347k
    return (PyObject *)si;
1184
347k
}
1185
1186
static int
1187
set_update_dict_lock_held(PySetObject *so, PyObject *other)
1188
302
{
1189
302
    assert(PyAnyDict_CheckExact(other));
1190
1191
302
    _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(so);
1192
#ifdef Py_DEBUG
1193
    if (!PyFrozenDict_CheckExact(other)) {
1194
        _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(other);
1195
    }
1196
#endif
1197
1198
    /* Do one big resize at the start, rather than
1199
    * incrementally resizing as we insert new keys.  Expect
1200
    * that there will be no (or few) overlapping keys.
1201
    */
1202
302
    Py_ssize_t dictsize = PyDict_GET_SIZE(other);
1203
302
    if ((so->fill + dictsize)*5 >= so->mask*3) {
1204
40
        if (set_table_resize(so, (so->used + dictsize)*2) != 0) {
1205
0
            return -1;
1206
0
        }
1207
40
    }
1208
1209
302
    Py_ssize_t pos = 0;
1210
302
    PyObject *key;
1211
302
    PyObject *value;
1212
302
    Py_hash_t hash;
1213
938
    while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
1214
636
        if (set_add_entry(so, key, hash)) {
1215
0
            return -1;
1216
0
        }
1217
636
    }
1218
302
    return 0;
1219
302
}
1220
1221
static int
1222
set_update_iterable_lock_held(PySetObject *so, PyObject *other)
1223
35.1k
{
1224
35.1k
    _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(so);
1225
1226
35.1k
    PyObject *it = PyObject_GetIter(other);
1227
35.1k
    if (it == NULL) {
1228
0
        return -1;
1229
0
    }
1230
1231
35.1k
    PyObject *key;
1232
199k
    while ((key = PyIter_Next(it)) != NULL) {
1233
163k
        if (set_add_key(so, key)) {
1234
0
            Py_DECREF(it);
1235
0
            Py_DECREF(key);
1236
0
            return -1;
1237
0
        }
1238
163k
        Py_DECREF(key);
1239
163k
    }
1240
35.1k
    Py_DECREF(it);
1241
35.1k
    if (PyErr_Occurred())
1242
0
        return -1;
1243
35.1k
    return 0;
1244
35.1k
}
1245
1246
static int
1247
set_update_lock_held(PySetObject *so, PyObject *other)
1248
0
{
1249
0
    if (PyAnySet_Check(other)) {
1250
0
        return set_merge_lock_held(so, other);
1251
0
    }
1252
0
    else if (PyAnyDict_CheckExact(other)) {
1253
0
        return set_update_dict_lock_held(so, other);
1254
0
    }
1255
0
    return set_update_iterable_lock_held(so, other);
1256
0
}
1257
1258
// set_update for a `so` that is only visible to the current thread
1259
static int
1260
set_update_local(PySetObject *so, PyObject *other)
1261
316k
{
1262
316k
    assert(Py_REFCNT(so) == 1);
1263
316k
    if (PyAnySet_Check(other)) {
1264
298k
        int rv;
1265
298k
        Py_BEGIN_CRITICAL_SECTION(other);
1266
298k
        rv = set_merge_lock_held(so, other);
1267
298k
        Py_END_CRITICAL_SECTION();
1268
298k
        return rv;
1269
298k
    }
1270
18.4k
    else if (PyDict_CheckExact(other)) {
1271
302
        int rv;
1272
302
        Py_BEGIN_CRITICAL_SECTION(other);
1273
302
        rv = set_update_dict_lock_held(so, other);
1274
302
        Py_END_CRITICAL_SECTION();
1275
302
        return rv;
1276
302
    }
1277
18.1k
    else if (PyFrozenDict_CheckExact(other)) {
1278
0
        return set_update_dict_lock_held(so, other);
1279
0
    }
1280
18.1k
    return set_update_iterable_lock_held(so, other);
1281
316k
}
1282
1283
static int
1284
set_update_internal(PySetObject *so, PyObject *other)
1285
592k
{
1286
592k
    if (PyAnySet_Check(other)) {
1287
575k
        if (Py_Is((PyObject *)so, other)) {
1288
0
            return 0;
1289
0
        }
1290
575k
        int rv;
1291
575k
        Py_BEGIN_CRITICAL_SECTION2(so, other);
1292
575k
        rv = set_merge_lock_held(so, other);
1293
575k
        Py_END_CRITICAL_SECTION2();
1294
575k
        return rv;
1295
575k
    }
1296
16.9k
    else if (PyDict_CheckExact(other)) {
1297
0
        int rv;
1298
0
        Py_BEGIN_CRITICAL_SECTION2(so, other);
1299
0
        rv = set_update_dict_lock_held(so, other);
1300
0
        Py_END_CRITICAL_SECTION2();
1301
0
        return rv;
1302
0
    }
1303
16.9k
    else if (PyFrozenDict_CheckExact(other)) {
1304
0
        int rv;
1305
0
        Py_BEGIN_CRITICAL_SECTION(so);
1306
0
        rv = set_update_dict_lock_held(so, other);
1307
0
        Py_END_CRITICAL_SECTION();
1308
0
        return rv;
1309
0
    }
1310
16.9k
    else {
1311
16.9k
        int rv;
1312
16.9k
        Py_BEGIN_CRITICAL_SECTION(so);
1313
16.9k
        rv = set_update_iterable_lock_held(so, other);
1314
16.9k
        Py_END_CRITICAL_SECTION();
1315
16.9k
        return rv;
1316
16.9k
    }
1317
592k
}
1318
1319
/*[clinic input]
1320
set.update
1321
    so: setobject
1322
    *others: array
1323
1324
Update the set, adding elements from all others.
1325
[clinic start generated code]*/
1326
1327
static PyObject *
1328
set_update_impl(PySetObject *so, PyObject * const *others,
1329
                Py_ssize_t others_length)
1330
/*[clinic end generated code: output=017c781c992d5c23 input=ed5d78885b076636]*/
1331
448k
{
1332
448k
    Py_ssize_t i;
1333
1334
897k
    for (i = 0; i < others_length; i++) {
1335
448k
        PyObject *other = others[i];
1336
448k
        if (set_update_internal(so, other))
1337
0
            return NULL;
1338
448k
    }
1339
448k
    Py_RETURN_NONE;
1340
448k
}
1341
1342
/* XXX Todo:
1343
   If aligned memory allocations become available, make the
1344
   set object 64 byte aligned so that most of the fields
1345
   can be retrieved or updated in a single cache line.
1346
*/
1347
1348
static PyObject *
1349
make_new_set(PyTypeObject *type, PyObject *iterable)
1350
4.96M
{
1351
4.96M
    assert(PyType_Check(type));
1352
4.96M
    PySetObject *so;
1353
1354
4.96M
    so = (PySetObject *)type->tp_alloc(type, 0);
1355
4.96M
    if (so == NULL)
1356
0
        return NULL;
1357
1358
4.96M
    so->fill = 0;
1359
4.96M
    so->used = 0;
1360
4.96M
    so->mask = PySet_MINSIZE - 1;
1361
4.96M
    so->table = so->smalltable;
1362
4.96M
    so->hash = -1;
1363
4.96M
    so->finger = 0;
1364
4.96M
    so->weakreflist = NULL;
1365
1366
4.96M
    if (iterable != NULL) {
1367
316k
        if (set_update_local(so, iterable)) {
1368
0
            Py_DECREF(so);
1369
0
            return NULL;
1370
0
        }
1371
316k
    }
1372
1373
4.96M
    return (PyObject *)so;
1374
4.96M
}
1375
1376
static PyObject *
1377
make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
1378
1.04k
{
1379
1.04k
    if (type != &PySet_Type && type != &PyFrozenSet_Type) {
1380
0
        if (PyType_IsSubtype(type, &PySet_Type))
1381
0
            type = &PySet_Type;
1382
0
        else
1383
0
            type = &PyFrozenSet_Type;
1384
0
    }
1385
1.04k
    return make_new_set(type, iterable);
1386
1.04k
}
1387
1388
// gh-140232: check whether a frozenset can be untracked from the GC
1389
static void
1390
_PyFrozenSet_MaybeUntrack(PyObject *op)
1391
269k
{
1392
269k
    assert(op != NULL);
1393
    // subclasses of a frozenset can generate reference cycles, so do not untrack
1394
269k
    if (!PyFrozenSet_CheckExact(op)) {
1395
0
        return;
1396
0
    }
1397
    // if no elements of a frozenset are tracked by the GC, we untrack the object
1398
269k
    Py_ssize_t pos = 0;
1399
269k
    setentry *entry;
1400
293k
    while (set_next((PySetObject *)op, &pos, &entry)) {
1401
104k
        if (_PyObject_GC_MAY_BE_TRACKED(entry->key)) {
1402
80.3k
            return;
1403
80.3k
        }
1404
104k
    }
1405
188k
    _PyObject_GC_UNTRACK(op);
1406
188k
}
1407
1408
static PyObject *
1409
make_new_frozenset(PyTypeObject *type, PyObject *iterable)
1410
264k
{
1411
264k
    if (type != &PyFrozenSet_Type) {
1412
0
        return make_new_set(type, iterable);
1413
0
    }
1414
1415
264k
    if (iterable != NULL && PyFrozenSet_CheckExact(iterable)) {
1416
        /* frozenset(f) is idempotent */
1417
0
        return Py_NewRef(iterable);
1418
0
    }
1419
264k
    PyObject *obj = make_new_set(type, iterable);
1420
264k
    if (obj != NULL) {
1421
264k
        _PyFrozenSet_MaybeUntrack(obj);
1422
264k
    }
1423
264k
    return obj;
1424
264k
}
1425
1426
static PyObject *
1427
frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1428
0
{
1429
0
    PyObject *iterable = NULL;
1430
1431
0
    if ((type == &PyFrozenSet_Type ||
1432
0
         type->tp_init == PyFrozenSet_Type.tp_init) &&
1433
0
        !_PyArg_NoKeywords("frozenset", kwds)) {
1434
0
        return NULL;
1435
0
    }
1436
1437
0
    if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable)) {
1438
0
        return NULL;
1439
0
    }
1440
1441
0
    return make_new_frozenset(type, iterable);
1442
0
}
1443
1444
static PyObject *
1445
frozenset_vectorcall(PyObject *type, PyObject * const*args,
1446
                     size_t nargsf, PyObject *kwnames)
1447
264k
{
1448
264k
    if (!_PyArg_NoKwnames("frozenset", kwnames)) {
1449
0
        return NULL;
1450
0
    }
1451
1452
264k
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
1453
264k
    if (!_PyArg_CheckPositional("frozenset", nargs, 0, 1)) {
1454
0
        return NULL;
1455
0
    }
1456
1457
264k
    PyObject *iterable = (nargs ? args[0] : NULL);
1458
264k
    return make_new_frozenset(_PyType_CAST(type), iterable);
1459
264k
}
1460
1461
static PyObject *
1462
set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1463
0
{
1464
0
    return make_new_set(type, NULL);
1465
0
}
1466
1467
#ifdef Py_GIL_DISABLED
1468
static void
1469
copy_small_table(setentry *dest, setentry *src)
1470
{
1471
    for (Py_ssize_t i = 0; i < PySet_MINSIZE; i++) {
1472
        _Py_atomic_store_ptr_release(&dest[i].key, src[i].key);
1473
        _Py_atomic_store_ssize_relaxed(&dest[i].hash, src[i].hash);
1474
    }
1475
}
1476
#endif
1477
1478
/* set_swap_bodies() switches the contents of any two sets by moving their
1479
   internal data pointers and, if needed, copying the internal smalltables.
1480
   Semantically equivalent to:
1481
1482
     t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
1483
1484
   The function always succeeds and it leaves both objects in a stable state.
1485
   Useful for operations that update in-place (by allowing an intermediate
1486
   result to be swapped into one of the original inputs).
1487
*/
1488
1489
static void
1490
set_swap_bodies(PySetObject *a, PySetObject *b)
1491
{
1492
    Py_ssize_t t;
1493
    setentry *u;
1494
    setentry tab[PySet_MINSIZE];
1495
    Py_hash_t h;
1496
1497
    setentry *a_table = a->table;
1498
    setentry *b_table = b->table;
1499
    FT_ATOMIC_STORE_PTR_RELEASE(a->table, NULL);
1500
    FT_ATOMIC_STORE_PTR_RELEASE(b->table, NULL);
1501
1502
    t = a->fill;     a->fill   = b->fill;        b->fill  = t;
1503
    t = a->used;
1504
    FT_ATOMIC_STORE_SSIZE_RELAXED(a->used, b->used);
1505
    FT_ATOMIC_STORE_SSIZE_RELAXED(b->used, t);
1506
    t = a->mask;
1507
    FT_ATOMIC_STORE_SSIZE_RELEASE(a->mask, b->mask);
1508
    FT_ATOMIC_STORE_SSIZE_RELEASE(b->mask, t);
1509
1510
    u = a_table;
1511
    if (a_table == a->smalltable)
1512
        u = b->smalltable;
1513
    a_table  = b_table;
1514
    if (b_table == b->smalltable)
1515
        a_table = a->smalltable;
1516
    b_table = u;
1517
1518
    if (a_table == a->smalltable || b_table == b->smalltable) {
1519
        memcpy(tab, a->smalltable, sizeof(tab));
1520
#ifndef Py_GIL_DISABLED
1521
        memcpy(a->smalltable, b->smalltable, sizeof(tab));
1522
        memcpy(b->smalltable, tab, sizeof(tab));
1523
#else
1524
        copy_small_table(a->smalltable, b->smalltable);
1525
        copy_small_table(b->smalltable, tab);
1526
#endif
1527
    }
1528
1529
    if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type)  &&
1530
        PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
1531
        h = FT_ATOMIC_LOAD_SSIZE_RELAXED(a->hash);
1532
        FT_ATOMIC_STORE_SSIZE_RELAXED(a->hash, FT_ATOMIC_LOAD_SSIZE_RELAXED(b->hash));
1533
        FT_ATOMIC_STORE_SSIZE_RELAXED(b->hash, h);
1534
    } else {
1535
        FT_ATOMIC_STORE_SSIZE_RELAXED(a->hash, -1);
1536
        FT_ATOMIC_STORE_SSIZE_RELAXED(b->hash, -1);
1537
    }
1538
    if (!SET_IS_SHARED(b) && SET_IS_SHARED(a)) {
1539
        SET_MARK_SHARED(b);
1540
    }
1541
    if (!SET_IS_SHARED(a) && SET_IS_SHARED(b)) {
1542
        SET_MARK_SHARED(a);
1543
    }
1544
    FT_ATOMIC_STORE_PTR_RELEASE(a->table, a_table);
1545
    FT_ATOMIC_STORE_PTR_RELEASE(b->table, b_table);
1546
}
1547
1548
/*[clinic input]
1549
@critical_section
1550
set.copy
1551
    so: setobject
1552
1553
Return a shallow copy of a set.
1554
[clinic start generated code]*/
1555
1556
static PyObject *
1557
set_copy_impl(PySetObject *so)
1558
/*[clinic end generated code: output=c9223a1e1cc6b041 input=c169a4fbb8209257]*/
1559
733
{
1560
733
    _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(so);
1561
733
    PyObject *copy = make_new_set_basetype(Py_TYPE(so), NULL);
1562
733
    if (copy == NULL) {
1563
0
        return NULL;
1564
0
    }
1565
733
    if (set_merge_lock_held((PySetObject *)copy, (PyObject *)so) < 0) {
1566
0
        Py_DECREF(copy);
1567
0
        return NULL;
1568
0
    }
1569
733
    return copy;
1570
733
}
1571
1572
/*[clinic input]
1573
@critical_section
1574
frozenset.copy
1575
    so: setobject
1576
1577
Return a shallow copy of a set.
1578
[clinic start generated code]*/
1579
1580
static PyObject *
1581
frozenset_copy_impl(PySetObject *so)
1582
/*[clinic end generated code: output=b356263526af9e70 input=fbf5bef131268dd7]*/
1583
0
{
1584
0
    if (PyFrozenSet_CheckExact(so)) {
1585
0
        return Py_NewRef(so);
1586
0
    }
1587
0
    return set_copy_impl(so);
1588
0
}
1589
1590
/*[clinic input]
1591
@critical_section
1592
set.clear
1593
    so: setobject
1594
1595
Remove all elements from this set.
1596
[clinic start generated code]*/
1597
1598
static PyObject *
1599
set_clear_impl(PySetObject *so)
1600
/*[clinic end generated code: output=4e71d5a83904161a input=c6f831b366111950]*/
1601
32.3k
{
1602
32.3k
    set_clear_internal((PyObject*)so);
1603
32.3k
    Py_RETURN_NONE;
1604
32.3k
}
1605
1606
/*[clinic input]
1607
set.union
1608
    so: setobject
1609
    *others: array
1610
1611
Return a new set with elements from the set and all others.
1612
[clinic start generated code]*/
1613
1614
static PyObject *
1615
set_union_impl(PySetObject *so, PyObject * const *others,
1616
               Py_ssize_t others_length)
1617
/*[clinic end generated code: output=b1bfa3d74065f27e input=55a2e81db6347a4f]*/
1618
6
{
1619
6
    PySetObject *result;
1620
6
    PyObject *other;
1621
6
    Py_ssize_t i;
1622
1623
6
    result = (PySetObject *)set_copy((PyObject *)so, NULL);
1624
6
    if (result == NULL)
1625
0
        return NULL;
1626
1627
12
    for (i = 0; i < others_length; i++) {
1628
6
        other = others[i];
1629
6
        if ((PyObject *)so == other)
1630
0
            continue;
1631
6
        if (set_update_local(result, other)) {
1632
0
            Py_DECREF(result);
1633
0
            return NULL;
1634
0
        }
1635
6
    }
1636
6
    return (PyObject *)result;
1637
6
}
1638
1639
static PyObject *
1640
set_or(PyObject *self, PyObject *other)
1641
102
{
1642
102
    PySetObject *result;
1643
1644
102
    if (!PyAnySet_Check(self) || !PyAnySet_Check(other))
1645
0
        Py_RETURN_NOTIMPLEMENTED;
1646
1647
102
    result = (PySetObject *)set_copy(self, NULL);
1648
102
    if (result == NULL) {
1649
0
        return NULL;
1650
0
    }
1651
102
    if (Py_Is(self, other)) {
1652
0
        return (PyObject *)result;
1653
0
    }
1654
102
    if (set_update_local(result, other)) {
1655
0
        Py_DECREF(result);
1656
0
        return NULL;
1657
0
    }
1658
102
    return (PyObject *)result;
1659
102
}
1660
1661
static PyObject *
1662
set_ior(PyObject *self, PyObject *other)
1663
41.6k
{
1664
41.6k
    if (!PyAnySet_Check(other))
1665
0
        Py_RETURN_NOTIMPLEMENTED;
1666
41.6k
    PySetObject *so = _PySet_CAST(self);
1667
1668
41.6k
    if (set_update_internal(so, other)) {
1669
0
        return NULL;
1670
0
    }
1671
41.6k
    return Py_NewRef(so);
1672
41.6k
}
1673
1674
static PyObject *
1675
set_intersection(PySetObject *so, PyObject *other)
1676
314
{
1677
314
    PySetObject *result;
1678
314
    PyObject *key, *it, *tmp;
1679
314
    Py_hash_t hash;
1680
314
    int rv;
1681
1682
314
    if ((PyObject *)so == other)
1683
0
        return set_copy_impl(so);
1684
1685
314
    result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
1686
314
    if (result == NULL)
1687
0
        return NULL;
1688
1689
314
    if (PyAnySet_Check(other)) {
1690
302
        Py_ssize_t pos = 0;
1691
302
        setentry *entry;
1692
1693
302
        if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1694
180
            tmp = (PyObject *)so;
1695
180
            so = (PySetObject *)other;
1696
180
            other = tmp;
1697
180
        }
1698
1699
546
        while (set_next((PySetObject *)other, &pos, &entry)) {
1700
244
            key = entry->key;
1701
244
            hash = entry->hash;
1702
244
            Py_INCREF(key);
1703
244
            rv = set_contains_entry(so, key, hash);
1704
244
            if (rv < 0) {
1705
0
                Py_DECREF(result);
1706
0
                Py_DECREF(key);
1707
0
                return NULL;
1708
0
            }
1709
244
            if (rv) {
1710
0
                if (set_add_entry(result, key, hash)) {
1711
0
                    Py_DECREF(result);
1712
0
                    Py_DECREF(key);
1713
0
                    return NULL;
1714
0
                }
1715
0
            }
1716
244
            Py_DECREF(key);
1717
244
        }
1718
302
        return (PyObject *)result;
1719
302
    }
1720
1721
12
    it = PyObject_GetIter(other);
1722
12
    if (it == NULL) {
1723
0
        Py_DECREF(result);
1724
0
        return NULL;
1725
0
    }
1726
1727
124
    while ((key = PyIter_Next(it)) != NULL) {
1728
116
        hash = PyObject_Hash(key);
1729
116
        if (hash == -1)
1730
0
            goto error;
1731
116
        rv = set_contains_entry(so, key, hash);
1732
116
        if (rv < 0)
1733
0
            goto error;
1734
116
        if (rv) {
1735
116
            if (set_add_entry(result, key, hash))
1736
0
                goto error;
1737
116
            if (PySet_GET_SIZE(result) >= PySet_GET_SIZE(so)) {
1738
4
                Py_DECREF(key);
1739
4
                break;
1740
4
            }
1741
116
        }
1742
112
        Py_DECREF(key);
1743
112
    }
1744
12
    Py_DECREF(it);
1745
12
    if (PyErr_Occurred()) {
1746
0
        Py_DECREF(result);
1747
0
        return NULL;
1748
0
    }
1749
12
    return (PyObject *)result;
1750
0
  error:
1751
0
    Py_DECREF(it);
1752
0
    Py_DECREF(result);
1753
0
    Py_DECREF(key);
1754
0
    return NULL;
1755
12
}
1756
1757
/*[clinic input]
1758
set.intersection as set_intersection_multi
1759
    so: setobject
1760
    *others: array
1761
1762
Return a new set with elements common to the set and all others.
1763
[clinic start generated code]*/
1764
1765
static PyObject *
1766
set_intersection_multi_impl(PySetObject *so, PyObject * const *others,
1767
                            Py_ssize_t others_length)
1768
/*[clinic end generated code: output=db9ff9f875132b6b input=36c7b615694cadae]*/
1769
8
{
1770
8
    Py_ssize_t i;
1771
1772
8
    if (others_length == 0) {
1773
0
        return set_copy((PyObject *)so, NULL);
1774
0
    }
1775
1776
8
    PyObject *result = Py_NewRef(so);
1777
16
    for (i = 0; i < others_length; i++) {
1778
8
        PyObject *other = others[i];
1779
8
        PyObject *newresult;
1780
8
        Py_BEGIN_CRITICAL_SECTION2(result, other);
1781
8
        newresult = set_intersection((PySetObject *)result, other);
1782
8
        Py_END_CRITICAL_SECTION2();
1783
8
        if (newresult == NULL) {
1784
0
            Py_DECREF(result);
1785
0
            return NULL;
1786
0
        }
1787
8
        Py_SETREF(result, newresult);
1788
8
    }
1789
8
    return result;
1790
8
}
1791
1792
static PyObject *
1793
set_intersection_update(PySetObject *so, PyObject *other)
1794
0
{
1795
0
    PyObject *tmp;
1796
1797
0
    tmp = set_intersection(so, other);
1798
0
    if (tmp == NULL)
1799
0
        return NULL;
1800
0
    set_swap_bodies(so, (PySetObject *)tmp);
1801
0
    Py_DECREF(tmp);
1802
0
    Py_RETURN_NONE;
1803
0
}
1804
1805
/*[clinic input]
1806
set.intersection_update as set_intersection_update_multi
1807
    so: setobject
1808
    *others: array
1809
1810
Update the set, keeping only elements found in it and all others.
1811
[clinic start generated code]*/
1812
1813
static PyObject *
1814
set_intersection_update_multi_impl(PySetObject *so, PyObject * const *others,
1815
                                   Py_ssize_t others_length)
1816
/*[clinic end generated code: output=d768b5584675b48d input=782e422fc370e4fc]*/
1817
0
{
1818
0
    PyObject *tmp;
1819
1820
0
    tmp = set_intersection_multi_impl(so, others, others_length);
1821
0
    if (tmp == NULL)
1822
0
        return NULL;
1823
0
    Py_BEGIN_CRITICAL_SECTION(so);
1824
0
    set_swap_bodies(so, (PySetObject *)tmp);
1825
0
    Py_END_CRITICAL_SECTION();
1826
0
    Py_DECREF(tmp);
1827
0
    Py_RETURN_NONE;
1828
0
}
1829
1830
static PyObject *
1831
set_and(PyObject *self, PyObject *other)
1832
302
{
1833
302
    if (!PyAnySet_Check(self) || !PyAnySet_Check(other))
1834
0
        Py_RETURN_NOTIMPLEMENTED;
1835
302
    PySetObject *so = _PySet_CAST(self);
1836
1837
302
    PyObject *rv;
1838
302
    Py_BEGIN_CRITICAL_SECTION2(so, other);
1839
302
    rv = set_intersection(so, other);
1840
302
    Py_END_CRITICAL_SECTION2();
1841
1842
302
    return rv;
1843
302
}
1844
1845
static PyObject *
1846
set_iand(PyObject *self, PyObject *other)
1847
0
{
1848
0
    PyObject *result;
1849
1850
0
    if (!PyAnySet_Check(other))
1851
0
        Py_RETURN_NOTIMPLEMENTED;
1852
0
    PySetObject *so = _PySet_CAST(self);
1853
1854
0
    Py_BEGIN_CRITICAL_SECTION2(so, other);
1855
0
    result = set_intersection_update(so, other);
1856
0
    Py_END_CRITICAL_SECTION2();
1857
1858
0
    if (result == NULL)
1859
0
        return NULL;
1860
0
    Py_DECREF(result);
1861
0
    return Py_NewRef(so);
1862
0
}
1863
1864
/*[clinic input]
1865
@critical_section so other
1866
set.isdisjoint
1867
    so: setobject
1868
    other: object
1869
    /
1870
1871
Return True if two sets have a null intersection.
1872
[clinic start generated code]*/
1873
1874
static PyObject *
1875
set_isdisjoint_impl(PySetObject *so, PyObject *other)
1876
/*[clinic end generated code: output=273493f2d57c565e input=32f8dcab5e0fc7d6]*/
1877
63.2k
{
1878
63.2k
    PyObject *key, *it, *tmp;
1879
63.2k
    int rv;
1880
1881
63.2k
    if ((PyObject *)so == other) {
1882
0
        if (PySet_GET_SIZE(so) == 0)
1883
0
            Py_RETURN_TRUE;
1884
0
        else
1885
0
            Py_RETURN_FALSE;
1886
0
    }
1887
1888
63.2k
    if (PyAnySet_CheckExact(other)) {
1889
36
        Py_ssize_t pos = 0;
1890
36
        setentry *entry;
1891
1892
36
        if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
1893
4
            tmp = (PyObject *)so;
1894
4
            so = (PySetObject *)other;
1895
4
            other = tmp;
1896
4
        }
1897
36
        while (set_next((PySetObject *)other, &pos, &entry)) {
1898
0
            PyObject *key = entry->key;
1899
0
            Py_INCREF(key);
1900
0
            rv = set_contains_entry(so, key, entry->hash);
1901
0
            Py_DECREF(key);
1902
0
            if (rv < 0) {
1903
0
                return NULL;
1904
0
            }
1905
0
            if (rv) {
1906
0
                Py_RETURN_FALSE;
1907
0
            }
1908
0
        }
1909
36
        Py_RETURN_TRUE;
1910
36
    }
1911
1912
63.2k
    it = PyObject_GetIter(other);
1913
63.2k
    if (it == NULL)
1914
0
        return NULL;
1915
1916
4.56M
    while ((key = PyIter_Next(it)) != NULL) {
1917
4.51M
        rv = set_contains_key(so, key);
1918
4.51M
        Py_DECREF(key);
1919
4.51M
        if (rv < 0) {
1920
0
            Py_DECREF(it);
1921
0
            return NULL;
1922
0
        }
1923
4.51M
        if (rv) {
1924
15.8k
            Py_DECREF(it);
1925
15.8k
            Py_RETURN_FALSE;
1926
15.8k
        }
1927
4.51M
    }
1928
47.4k
    Py_DECREF(it);
1929
47.4k
    if (PyErr_Occurred())
1930
0
        return NULL;
1931
47.4k
    Py_RETURN_TRUE;
1932
47.4k
}
1933
1934
static int
1935
set_difference_update_internal(PySetObject *so, PyObject *other)
1936
78
{
1937
78
    _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(so);
1938
78
    _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(other);
1939
1940
78
    if ((PyObject *)so == other)
1941
0
        return set_clear_internal((PyObject*)so);
1942
1943
78
    if (PyAnySet_Check(other)) {
1944
78
        setentry *entry;
1945
78
        Py_ssize_t pos = 0;
1946
1947
        /* Optimization:  When the other set is more than 8 times
1948
           larger than the base set, replace the other set with
1949
           intersection of the two sets.
1950
        */
1951
78
        if ((PySet_GET_SIZE(other) >> 3) > PySet_GET_SIZE(so)) {
1952
0
            other = set_intersection(so, other);
1953
0
            if (other == NULL)
1954
0
                return -1;
1955
78
        } else {
1956
78
            Py_INCREF(other);
1957
78
        }
1958
1959
118
        while (set_next((PySetObject *)other, &pos, &entry)) {
1960
40
            PyObject *key = entry->key;
1961
40
            Py_INCREF(key);
1962
40
            if (set_discard_entry(so, key, entry->hash) < 0) {
1963
0
                Py_DECREF(other);
1964
0
                Py_DECREF(key);
1965
0
                return -1;
1966
0
            }
1967
40
            Py_DECREF(key);
1968
40
        }
1969
1970
78
        Py_DECREF(other);
1971
78
    } else {
1972
0
        PyObject *key, *it;
1973
0
        it = PyObject_GetIter(other);
1974
0
        if (it == NULL)
1975
0
            return -1;
1976
1977
0
        while ((key = PyIter_Next(it)) != NULL) {
1978
0
            if (set_discard_key(so, key) < 0) {
1979
0
                Py_DECREF(it);
1980
0
                Py_DECREF(key);
1981
0
                return -1;
1982
0
            }
1983
0
            Py_DECREF(key);
1984
0
        }
1985
0
        Py_DECREF(it);
1986
0
        if (PyErr_Occurred())
1987
0
            return -1;
1988
0
    }
1989
    /* If more than 1/4th are dummies, then resize them away. */
1990
78
    if ((size_t)(so->fill - so->used) <= (size_t)so->mask / 4)
1991
78
        return 0;
1992
0
    return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
1993
78
}
1994
1995
/*[clinic input]
1996
set.difference_update
1997
    so: setobject
1998
    *others: array
1999
2000
Update the set, removing elements found in others.
2001
[clinic start generated code]*/
2002
2003
static PyObject *
2004
set_difference_update_impl(PySetObject *so, PyObject * const *others,
2005
                           Py_ssize_t others_length)
2006
/*[clinic end generated code: output=04a22179b322cfe6 input=93ac28ba5b233696]*/
2007
0
{
2008
0
    Py_ssize_t i;
2009
2010
0
    for (i = 0; i < others_length; i++) {
2011
0
        PyObject *other = others[i];
2012
0
        int rv;
2013
0
        Py_BEGIN_CRITICAL_SECTION2(so, other);
2014
0
        rv = set_difference_update_internal(so, other);
2015
0
        Py_END_CRITICAL_SECTION2();
2016
0
        if (rv) {
2017
0
            return NULL;
2018
0
        }
2019
0
    }
2020
0
    Py_RETURN_NONE;
2021
0
}
2022
2023
static PyObject *
2024
set_copy_and_difference(PySetObject *so, PyObject *other)
2025
22
{
2026
22
    PyObject *result;
2027
2028
22
    result = set_copy_impl(so);
2029
22
    if (result == NULL)
2030
0
        return NULL;
2031
22
    if (set_difference_update_internal((PySetObject *) result, other) == 0)
2032
22
        return result;
2033
0
    Py_DECREF(result);
2034
0
    return NULL;
2035
22
}
2036
2037
static PyObject *
2038
set_difference(PySetObject *so, PyObject *other)
2039
24
{
2040
24
    PyObject *result;
2041
24
    PyObject *key;
2042
24
    Py_hash_t hash;
2043
24
    setentry *entry;
2044
24
    Py_ssize_t pos = 0, other_size;
2045
24
    int rv;
2046
2047
24
    if (PyAnySet_Check(other)) {
2048
24
        other_size = PySet_GET_SIZE(other);
2049
24
    }
2050
0
    else if (PyAnyDict_CheckExact(other)) {
2051
0
        other_size = PyDict_GET_SIZE(other);
2052
0
    }
2053
0
    else {
2054
0
        return set_copy_and_difference(so, other);
2055
0
    }
2056
2057
    /* If len(so) much more than len(other), it's more efficient to simply copy
2058
     * so and then iterate other looking for common elements. */
2059
24
    if ((PySet_GET_SIZE(so) >> 2) > other_size) {
2060
22
        return set_copy_and_difference(so, other);
2061
22
    }
2062
2063
2
    result = make_new_set_basetype(Py_TYPE(so), NULL);
2064
2
    if (result == NULL)
2065
0
        return NULL;
2066
2067
2
    if (PyAnyDict_CheckExact(other)) {
2068
0
        while (set_next(so, &pos, &entry)) {
2069
0
            key = entry->key;
2070
0
            hash = entry->hash;
2071
0
            Py_INCREF(key);
2072
0
            rv = _PyDict_Contains_KnownHash(other, key, hash);
2073
0
            if (rv < 0) {
2074
0
                Py_DECREF(result);
2075
0
                Py_DECREF(key);
2076
0
                return NULL;
2077
0
            }
2078
0
            if (!rv) {
2079
0
                if (set_add_entry((PySetObject *)result, key, hash)) {
2080
0
                    Py_DECREF(result);
2081
0
                    Py_DECREF(key);
2082
0
                    return NULL;
2083
0
                }
2084
0
            }
2085
0
            Py_DECREF(key);
2086
0
        }
2087
0
        return result;
2088
0
    }
2089
2090
    /* Iterate over so, checking for common elements in other. */
2091
28
    while (set_next(so, &pos, &entry)) {
2092
26
        key = entry->key;
2093
26
        hash = entry->hash;
2094
26
        Py_INCREF(key);
2095
26
        rv = set_contains_entry((PySetObject *)other, key, hash);
2096
26
        if (rv < 0) {
2097
0
            Py_DECREF(result);
2098
0
            Py_DECREF(key);
2099
0
            return NULL;
2100
0
        }
2101
26
        if (!rv) {
2102
20
            if (set_add_entry((PySetObject *)result, key, hash)) {
2103
0
                Py_DECREF(result);
2104
0
                Py_DECREF(key);
2105
0
                return NULL;
2106
0
            }
2107
20
        }
2108
26
        Py_DECREF(key);
2109
26
    }
2110
2
    return result;
2111
2
}
2112
2113
/*[clinic input]
2114
set.difference as set_difference_multi
2115
    so: setobject
2116
    *others: array
2117
2118
Return a new set with elements in the set that are not in the others.
2119
[clinic start generated code]*/
2120
2121
static PyObject *
2122
set_difference_multi_impl(PySetObject *so, PyObject * const *others,
2123
                          Py_ssize_t others_length)
2124
/*[clinic end generated code: output=b0d33fb05d5477a7 input=c1eb448d483416ad]*/
2125
0
{
2126
0
    Py_ssize_t i;
2127
0
    PyObject *result, *other;
2128
2129
0
    if (others_length == 0) {
2130
0
        return set_copy((PyObject *)so, NULL);
2131
0
    }
2132
2133
0
    other = others[0];
2134
0
    Py_BEGIN_CRITICAL_SECTION2(so, other);
2135
0
    result = set_difference(so, other);
2136
0
    Py_END_CRITICAL_SECTION2();
2137
0
    if (result == NULL)
2138
0
        return NULL;
2139
2140
0
    for (i = 1; i < others_length; i++) {
2141
0
        other = others[i];
2142
0
        int rv;
2143
0
        Py_BEGIN_CRITICAL_SECTION(other);
2144
0
        rv = set_difference_update_internal((PySetObject *)result, other);
2145
0
        Py_END_CRITICAL_SECTION();
2146
0
        if (rv) {
2147
0
            Py_DECREF(result);
2148
0
            return NULL;
2149
0
        }
2150
0
    }
2151
0
    return result;
2152
0
}
2153
2154
static PyObject *
2155
set_sub(PyObject *self, PyObject *other)
2156
24
{
2157
24
    if (!PyAnySet_Check(self) || !PyAnySet_Check(other))
2158
0
        Py_RETURN_NOTIMPLEMENTED;
2159
24
    PySetObject *so = _PySet_CAST(self);
2160
2161
24
    PyObject *rv;
2162
24
    Py_BEGIN_CRITICAL_SECTION2(so, other);
2163
24
    rv = set_difference(so, other);
2164
24
    Py_END_CRITICAL_SECTION2();
2165
24
    return rv;
2166
24
}
2167
2168
static PyObject *
2169
set_isub(PyObject *self, PyObject *other)
2170
56
{
2171
56
    if (!PyAnySet_Check(other))
2172
0
        Py_RETURN_NOTIMPLEMENTED;
2173
56
    PySetObject *so = _PySet_CAST(self);
2174
2175
56
    int rv;
2176
56
    Py_BEGIN_CRITICAL_SECTION2(so, other);
2177
56
    rv = set_difference_update_internal(so, other);
2178
56
    Py_END_CRITICAL_SECTION2();
2179
56
    if (rv < 0) {
2180
0
        return NULL;
2181
0
    }
2182
56
    return Py_NewRef(so);
2183
56
}
2184
2185
static int
2186
set_symmetric_difference_update_dict(PySetObject *so, PyObject *other)
2187
0
{
2188
0
    _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(so);
2189
#ifdef Py_DEBUG
2190
    if (!PyFrozenDict_CheckExact(other)) {
2191
        _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(other);
2192
    }
2193
#endif
2194
2195
0
    Py_ssize_t pos = 0;
2196
0
    PyObject *key, *value;
2197
0
    Py_hash_t hash;
2198
0
    while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
2199
0
        Py_INCREF(key);
2200
0
        int rv = set_discard_entry(so, key, hash);
2201
0
        if (rv < 0) {
2202
0
            Py_DECREF(key);
2203
0
            return -1;
2204
0
        }
2205
0
        if (rv == DISCARD_NOTFOUND) {
2206
0
            if (set_add_entry(so, key, hash)) {
2207
0
                Py_DECREF(key);
2208
0
                return -1;
2209
0
            }
2210
0
        }
2211
0
        Py_DECREF(key);
2212
0
    }
2213
0
    return 0;
2214
0
}
2215
2216
static int
2217
set_symmetric_difference_update_set(PySetObject *so, PySetObject *other)
2218
0
{
2219
0
    _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(so);
2220
0
    _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(other);
2221
2222
0
    Py_ssize_t pos = 0;
2223
0
    setentry *entry;
2224
0
    while (set_next(other, &pos, &entry)) {
2225
0
        PyObject *key = Py_NewRef(entry->key);
2226
0
        Py_hash_t hash = entry->hash;
2227
0
        int rv = set_discard_entry(so, key, hash);
2228
0
        if (rv < 0) {
2229
0
            Py_DECREF(key);
2230
0
            return -1;
2231
0
        }
2232
0
        if (rv == DISCARD_NOTFOUND) {
2233
0
            if (set_add_entry(so, key, hash)) {
2234
0
                Py_DECREF(key);
2235
0
                return -1;
2236
0
            }
2237
0
        }
2238
0
        Py_DECREF(key);
2239
0
    }
2240
0
    return 0;
2241
0
}
2242
2243
/*[clinic input]
2244
@permit_long_summary
2245
set.symmetric_difference_update
2246
    so: setobject
2247
    other: object
2248
    /
2249
2250
Update the set, keeping only elements found in either set, but not in both.
2251
[clinic start generated code]*/
2252
2253
static PyObject *
2254
set_symmetric_difference_update_impl(PySetObject *so, PyObject *other)
2255
/*[clinic end generated code: output=79f80b4ee5da66c1 input=86a3dddac9bfb15e]*/
2256
0
{
2257
0
    if (Py_Is((PyObject *)so, other)) {
2258
0
        return set_clear((PyObject *)so, NULL);
2259
0
    }
2260
2261
0
    int rv;
2262
0
    if (PyDict_CheckExact(other)) {
2263
0
        Py_BEGIN_CRITICAL_SECTION2(so, other);
2264
0
        rv = set_symmetric_difference_update_dict(so, other);
2265
0
        Py_END_CRITICAL_SECTION2();
2266
0
    }
2267
0
    else if (PyFrozenDict_CheckExact(other)) {
2268
0
        Py_BEGIN_CRITICAL_SECTION(so);
2269
0
        rv = set_symmetric_difference_update_dict(so, other);
2270
0
        Py_END_CRITICAL_SECTION();
2271
0
    }
2272
0
    else if (PyAnySet_Check(other)) {
2273
0
        Py_BEGIN_CRITICAL_SECTION2(so, other);
2274
0
        rv = set_symmetric_difference_update_set(so, (PySetObject *)other);
2275
0
        Py_END_CRITICAL_SECTION2();
2276
0
    }
2277
0
    else {
2278
0
        PySetObject *otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
2279
0
        if (otherset == NULL) {
2280
0
            return NULL;
2281
0
        }
2282
2283
0
        Py_BEGIN_CRITICAL_SECTION(so);
2284
0
        rv = set_symmetric_difference_update_set(so, otherset);
2285
0
        Py_END_CRITICAL_SECTION();
2286
2287
0
        Py_DECREF(otherset);
2288
0
    }
2289
0
    if (rv < 0) {
2290
0
        return NULL;
2291
0
    }
2292
0
    Py_RETURN_NONE;
2293
0
}
2294
2295
/*[clinic input]
2296
@critical_section so other
2297
set.symmetric_difference
2298
    so: setobject
2299
    other: object
2300
    /
2301
2302
Return a new set with elements in either the set or other but not both.
2303
[clinic start generated code]*/
2304
2305
static PyObject *
2306
set_symmetric_difference_impl(PySetObject *so, PyObject *other)
2307
/*[clinic end generated code: output=270ee0b5d42b0797 input=624f6e7bbdf70db1]*/
2308
0
{
2309
0
    PySetObject *result = (PySetObject *)make_new_set_basetype(Py_TYPE(so), NULL);
2310
0
    if (result == NULL) {
2311
0
        return NULL;
2312
0
    }
2313
0
    if (set_update_lock_held(result, other) < 0) {
2314
0
        Py_DECREF(result);
2315
0
        return NULL;
2316
0
    }
2317
0
    if (set_symmetric_difference_update_set(result, so) < 0) {
2318
0
        Py_DECREF(result);
2319
0
        return NULL;
2320
0
    }
2321
0
    return (PyObject *)result;
2322
0
}
2323
2324
static PyObject *
2325
set_xor(PyObject *self, PyObject *other)
2326
0
{
2327
0
    if (!PyAnySet_Check(self) || !PyAnySet_Check(other))
2328
0
        Py_RETURN_NOTIMPLEMENTED;
2329
0
    PySetObject *so = _PySet_CAST(self);
2330
0
    return set_symmetric_difference((PyObject*)so, other);
2331
0
}
2332
2333
static PyObject *
2334
set_ixor(PyObject *self, PyObject *other)
2335
0
{
2336
0
    PyObject *result;
2337
2338
0
    if (!PyAnySet_Check(other))
2339
0
        Py_RETURN_NOTIMPLEMENTED;
2340
0
    PySetObject *so = _PySet_CAST(self);
2341
2342
0
    result = set_symmetric_difference_update((PyObject*)so, other);
2343
0
    if (result == NULL)
2344
0
        return NULL;
2345
0
    Py_DECREF(result);
2346
0
    return Py_NewRef(so);
2347
0
}
2348
2349
/*[clinic input]
2350
@critical_section so other
2351
set.issubset
2352
    so: setobject
2353
    other: object
2354
    /
2355
2356
Report whether another set contains this set.
2357
[clinic start generated code]*/
2358
2359
static PyObject *
2360
set_issubset_impl(PySetObject *so, PyObject *other)
2361
/*[clinic end generated code: output=b2b59d5f314555ce input=f2a4fd0f2537758b]*/
2362
263k
{
2363
263k
    setentry *entry;
2364
263k
    Py_ssize_t pos = 0;
2365
263k
    int rv;
2366
2367
263k
    if (!PyAnySet_Check(other)) {
2368
4
        PyObject *tmp = set_intersection(so, other);
2369
4
        if (tmp == NULL) {
2370
0
            return NULL;
2371
0
        }
2372
4
        int result = (PySet_GET_SIZE(tmp) == PySet_GET_SIZE(so));
2373
4
        Py_DECREF(tmp);
2374
4
        return PyBool_FromLong(result);
2375
4
    }
2376
263k
    if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
2377
0
        Py_RETURN_FALSE;
2378
2379
423k
    while (set_next(so, &pos, &entry)) {
2380
159k
        PyObject *key = entry->key;
2381
159k
        Py_INCREF(key);
2382
159k
        rv = set_contains_entry((PySetObject *)other, key, entry->hash);
2383
159k
        Py_DECREF(key);
2384
159k
        if (rv < 0) {
2385
0
            return NULL;
2386
0
        }
2387
159k
        if (!rv) {
2388
0
            Py_RETURN_FALSE;
2389
0
        }
2390
159k
    }
2391
263k
    Py_RETURN_TRUE;
2392
263k
}
2393
2394
/*[clinic input]
2395
@critical_section so other
2396
set.issuperset
2397
    so: setobject
2398
    other: object
2399
    /
2400
2401
Report whether this set contains another set.
2402
[clinic start generated code]*/
2403
2404
static PyObject *
2405
set_issuperset_impl(PySetObject *so, PyObject *other)
2406
/*[clinic end generated code: output=ecf00ce552c09461 input=5f2e1f262e6e4ccc]*/
2407
5.45k
{
2408
5.45k
    if (PyAnySet_Check(other)) {
2409
0
        return set_issubset(other, (PyObject *)so);
2410
0
    }
2411
2412
5.45k
    PyObject *key, *it = PyObject_GetIter(other);
2413
5.45k
    if (it == NULL) {
2414
0
        return NULL;
2415
0
    }
2416
37.1k
    while ((key = PyIter_Next(it)) != NULL) {
2417
31.7k
        int rv = set_contains_key(so, key);
2418
31.7k
        Py_DECREF(key);
2419
31.7k
        if (rv < 0) {
2420
0
            Py_DECREF(it);
2421
0
            return NULL;
2422
0
        }
2423
31.7k
        if (!rv) {
2424
18
            Py_DECREF(it);
2425
18
            Py_RETURN_FALSE;
2426
18
        }
2427
31.7k
    }
2428
5.43k
    Py_DECREF(it);
2429
5.43k
    if (PyErr_Occurred()) {
2430
0
        return NULL;
2431
0
    }
2432
5.43k
    Py_RETURN_TRUE;
2433
5.43k
}
2434
2435
static PyObject *
2436
set_richcompare(PyObject *self, PyObject *w, int op)
2437
263k
{
2438
263k
    PySetObject *v = _PySet_CAST(self);
2439
263k
    PyObject *r1;
2440
263k
    int r2;
2441
2442
263k
    if(!PyAnySet_Check(w))
2443
0
        Py_RETURN_NOTIMPLEMENTED;
2444
2445
263k
    switch (op) {
2446
263k
    case Py_EQ:
2447
263k
        if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
2448
116
            Py_RETURN_FALSE;
2449
263k
        Py_hash_t v_hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(v->hash);
2450
263k
        Py_hash_t w_hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(((PySetObject *)w)->hash);
2451
263k
        if (v_hash != -1 && w_hash != -1 && v_hash != w_hash)
2452
0
            Py_RETURN_FALSE;
2453
263k
        return set_issubset((PyObject*)v, w);
2454
0
    case Py_NE:
2455
0
        r1 = set_richcompare((PyObject*)v, w, Py_EQ);
2456
0
        if (r1 == NULL)
2457
0
            return NULL;
2458
0
        r2 = PyObject_IsTrue(r1);
2459
0
        Py_DECREF(r1);
2460
0
        if (r2 < 0)
2461
0
            return NULL;
2462
0
        return PyBool_FromLong(!r2);
2463
134
    case Py_LE:
2464
134
        return set_issubset((PyObject*)v, w);
2465
0
    case Py_GE:
2466
0
        return set_issuperset((PyObject*)v, w);
2467
0
    case Py_LT:
2468
0
        if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
2469
0
            Py_RETURN_FALSE;
2470
0
        return set_issubset((PyObject*)v, w);
2471
0
    case Py_GT:
2472
0
        if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
2473
0
            Py_RETURN_FALSE;
2474
0
        return set_issuperset((PyObject*)v, w);
2475
263k
    }
2476
263k
    Py_RETURN_NOTIMPLEMENTED;
2477
263k
}
2478
2479
/*[clinic input]
2480
@critical_section
2481
set.add
2482
    so: setobject
2483
    object as key: object
2484
    /
2485
2486
Add an element to a set.
2487
2488
This has no effect if the element is already present.
2489
[clinic start generated code]*/
2490
2491
static PyObject *
2492
set_add_impl(PySetObject *so, PyObject *key)
2493
/*[clinic end generated code: output=4cc4a937f1425c96 input=03baf62cb0e66514]*/
2494
6.30M
{
2495
6.30M
    if (set_add_key(so, key))
2496
0
        return NULL;
2497
6.30M
    Py_RETURN_NONE;
2498
6.30M
}
2499
2500
int
2501
_PySet_Contains(PySetObject *so, PyObject *key)
2502
232M
{
2503
232M
    assert(so);
2504
2505
232M
    Py_hash_t hash = _PyObject_HashFast(key);
2506
232M
    if (hash == -1) {
2507
0
        if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError)) {
2508
0
            set_unhashable_type(key);
2509
0
            return -1;
2510
0
        }
2511
0
        PyErr_Clear();
2512
        // Note that 'key' could be a set() or frozenset() object.  Unlike most
2513
        // container types, set allows membership testing with a set key, even
2514
        // though it is not hashable.
2515
0
        Py_BEGIN_CRITICAL_SECTION(key);
2516
0
        hash = frozenset_hash_impl(key);
2517
0
        Py_END_CRITICAL_SECTION();
2518
0
    }
2519
232M
    return set_contains_entry(so, key, hash);
2520
232M
}
2521
2522
static int
2523
set_contains(PyObject *self, PyObject *key)
2524
890
{
2525
890
    PySetObject *so = _PySet_CAST(self);
2526
890
    return _PySet_Contains(so, key);
2527
890
}
2528
2529
/*[clinic input]
2530
@coexist
2531
set.__contains__
2532
    so: setobject
2533
    object as key: object
2534
    /
2535
2536
x.__contains__(y) <==> y in x.
2537
[clinic start generated code]*/
2538
2539
static PyObject *
2540
set___contains___impl(PySetObject *so, PyObject *key)
2541
/*[clinic end generated code: output=b44863d034b3c70e input=cf4c72db704e4cf0]*/
2542
19.5M
{
2543
19.5M
    long result;
2544
2545
19.5M
    result = _PySet_Contains(so, key);
2546
19.5M
    if (result < 0)
2547
0
        return NULL;
2548
19.5M
    return PyBool_FromLong(result);
2549
19.5M
}
2550
2551
/*[clinic input]
2552
@coexist
2553
frozenset.__contains__
2554
    so: setobject
2555
    object as key: object
2556
    /
2557
2558
x.__contains__(y) <==> y in x.
2559
[clinic start generated code]*/
2560
2561
static PyObject *
2562
frozenset___contains___impl(PySetObject *so, PyObject *key)
2563
/*[clinic end generated code: output=2301ed91bc3a6dd5 input=2f04922a98d8bab7]*/
2564
50.7k
{
2565
50.7k
    Py_hash_t hash = _PyObject_HashFast(key);
2566
50.7k
    if (hash == -1) {
2567
0
        if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError)) {
2568
0
            set_unhashable_type(key);
2569
0
            return NULL;
2570
0
        }
2571
0
        PyErr_Clear();
2572
0
        Py_BEGIN_CRITICAL_SECTION(key);
2573
0
        hash = frozenset_hash_impl(key);
2574
0
        Py_END_CRITICAL_SECTION();
2575
0
    }
2576
50.7k
    setentry *entry; // unused
2577
50.7k
    int status = set_do_lookup(so, so->table, so->mask, key, hash, &entry,
2578
50.7k
                           set_compare_frozenset);
2579
50.7k
    if (status < 0)
2580
0
        return NULL;
2581
50.7k
    return PyBool_FromLong(status);
2582
50.7k
}
2583
2584
/*[clinic input]
2585
@critical_section
2586
set.remove
2587
    so: setobject
2588
    object as key: object
2589
    /
2590
2591
Remove an element from a set; it must be a member.
2592
2593
If the element is not a member, raise a KeyError.
2594
[clinic start generated code]*/
2595
2596
static PyObject *
2597
set_remove_impl(PySetObject *so, PyObject *key)
2598
/*[clinic end generated code: output=0b9134a2a2200363 input=893e1cb1df98227a]*/
2599
0
{
2600
0
    int rv;
2601
2602
0
    rv = set_discard_key(so, key);
2603
0
    if (rv < 0) {
2604
0
        if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
2605
0
            return NULL;
2606
0
        PyErr_Clear();
2607
0
        Py_hash_t hash;
2608
0
        Py_BEGIN_CRITICAL_SECTION(key);
2609
0
        hash = frozenset_hash_impl(key);
2610
0
        Py_END_CRITICAL_SECTION();
2611
0
        rv = set_discard_entry(so, key, hash);
2612
0
        if (rv < 0)
2613
0
            return NULL;
2614
0
    }
2615
2616
0
    if (rv == DISCARD_NOTFOUND) {
2617
0
        _PyErr_SetKeyError(key);
2618
0
        return NULL;
2619
0
    }
2620
0
    Py_RETURN_NONE;
2621
0
}
2622
2623
/*[clinic input]
2624
@critical_section
2625
set.discard
2626
    so: setobject
2627
    object as key: object
2628
    /
2629
2630
Remove an element from a set if it is a member.
2631
2632
Unlike set.remove(), the discard() method does not raise
2633
an exception when an element is missing from the set.
2634
[clinic start generated code]*/
2635
2636
static PyObject *
2637
set_discard_impl(PySetObject *so, PyObject *key)
2638
/*[clinic end generated code: output=eec3b687bf32759e input=861cb7fb69b4def0]*/
2639
256
{
2640
256
    int rv;
2641
2642
256
    rv = set_discard_key(so, key);
2643
256
    if (rv < 0) {
2644
0
        if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
2645
0
            return NULL;
2646
0
        PyErr_Clear();
2647
0
        Py_hash_t hash;
2648
0
        Py_BEGIN_CRITICAL_SECTION(key);
2649
0
        hash = frozenset_hash_impl(key);
2650
0
        Py_END_CRITICAL_SECTION();
2651
0
        rv = set_discard_entry(so, key, hash);
2652
0
        if (rv < 0)
2653
0
            return NULL;
2654
0
    }
2655
256
    Py_RETURN_NONE;
2656
256
}
2657
2658
/*[clinic input]
2659
@critical_section
2660
set.__reduce__
2661
    so: setobject
2662
2663
Return state information for pickling.
2664
[clinic start generated code]*/
2665
2666
static PyObject *
2667
set___reduce___impl(PySetObject *so)
2668
/*[clinic end generated code: output=9af7d0e029df87ee input=59405a4249e82f71]*/
2669
0
{
2670
0
    PyObject *keys=NULL, *args=NULL, *result=NULL, *state=NULL;
2671
2672
0
    keys = PySequence_List((PyObject *)so);
2673
0
    if (keys == NULL)
2674
0
        goto done;
2675
0
    args = PyTuple_Pack(1, keys);
2676
0
    if (args == NULL)
2677
0
        goto done;
2678
0
    state = _PyObject_GetState((PyObject *)so);
2679
0
    if (state == NULL)
2680
0
        goto done;
2681
0
    result = PyTuple_Pack(3, Py_TYPE(so), args, state);
2682
0
done:
2683
0
    Py_XDECREF(args);
2684
0
    Py_XDECREF(keys);
2685
0
    Py_XDECREF(state);
2686
0
    return result;
2687
0
}
2688
2689
/*[clinic input]
2690
@critical_section
2691
set.__sizeof__
2692
    so: setobject
2693
2694
S.__sizeof__() -> size of S in memory, in bytes.
2695
[clinic start generated code]*/
2696
2697
static PyObject *
2698
set___sizeof___impl(PySetObject *so)
2699
/*[clinic end generated code: output=4bfa3df7bd38ed88 input=09e1a09f168eaa23]*/
2700
0
{
2701
0
    size_t res = _PyObject_SIZE(Py_TYPE(so));
2702
0
    if (so->table != so->smalltable) {
2703
0
        res += ((size_t)so->mask + 1) * sizeof(setentry);
2704
0
    }
2705
0
    return PyLong_FromSize_t(res);
2706
0
}
2707
2708
static int
2709
set_init(PyObject *so, PyObject *args, PyObject *kwds)
2710
0
{
2711
0
    PySetObject *self = _PySet_CAST(so);
2712
0
    PyObject *iterable = NULL;
2713
2714
0
    if (!_PyArg_NoKeywords("set", kwds))
2715
0
        return -1;
2716
0
    if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
2717
0
        return -1;
2718
2719
0
    if (_PyObject_IsUniquelyReferenced((PyObject *)self) && self->fill == 0) {
2720
0
        self->hash = -1;
2721
0
        if (iterable == NULL) {
2722
0
            return 0;
2723
0
        }
2724
0
        return set_update_local(self, iterable);
2725
0
    }
2726
0
    Py_BEGIN_CRITICAL_SECTION(self);
2727
0
    if (self->fill)
2728
0
        set_clear_internal((PyObject*)self);
2729
0
    self->hash = -1;
2730
0
    Py_END_CRITICAL_SECTION();
2731
2732
0
    if (iterable == NULL)
2733
0
        return 0;
2734
0
    return set_update_internal(self, iterable);
2735
0
}
2736
2737
static PyObject*
2738
set_vectorcall(PyObject *type, PyObject * const*args,
2739
               size_t nargsf, PyObject *kwnames)
2740
3.78M
{
2741
3.78M
    assert(PyType_Check(type));
2742
2743
3.78M
    if (!_PyArg_NoKwnames("set", kwnames)) {
2744
0
        return NULL;
2745
0
    }
2746
2747
3.78M
    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
2748
3.78M
    if (!_PyArg_CheckPositional("set", nargs, 0, 1)) {
2749
0
        return NULL;
2750
0
    }
2751
2752
3.78M
    if (nargs) {
2753
16.9k
        return make_new_set(_PyType_CAST(type), args[0]);
2754
16.9k
    }
2755
2756
3.77M
    return make_new_set(_PyType_CAST(type), NULL);
2757
3.78M
}
2758
2759
static PySequenceMethods set_as_sequence = {
2760
    set_len,                            /* sq_length */
2761
    0,                                  /* sq_concat */
2762
    0,                                  /* sq_repeat */
2763
    0,                                  /* sq_item */
2764
    0,                                  /* sq_slice */
2765
    0,                                  /* sq_ass_item */
2766
    0,                                  /* sq_ass_slice */
2767
    set_contains,                       /* sq_contains */
2768
};
2769
2770
/* set object ********************************************************/
2771
2772
static PyMethodDef set_methods[] = {
2773
    SET_ADD_METHODDEF
2774
    SET_CLEAR_METHODDEF
2775
    SET___CONTAINS___METHODDEF
2776
    SET_COPY_METHODDEF
2777
    SET_DISCARD_METHODDEF
2778
    SET_DIFFERENCE_MULTI_METHODDEF
2779
    SET_DIFFERENCE_UPDATE_METHODDEF
2780
    SET_INTERSECTION_MULTI_METHODDEF
2781
    SET_INTERSECTION_UPDATE_MULTI_METHODDEF
2782
    SET_ISDISJOINT_METHODDEF
2783
    SET_ISSUBSET_METHODDEF
2784
    SET_ISSUPERSET_METHODDEF
2785
    SET_POP_METHODDEF
2786
    SET___REDUCE___METHODDEF
2787
    SET_REMOVE_METHODDEF
2788
    SET___SIZEOF___METHODDEF
2789
    SET_SYMMETRIC_DIFFERENCE_METHODDEF
2790
    SET_SYMMETRIC_DIFFERENCE_UPDATE_METHODDEF
2791
    SET_UNION_METHODDEF
2792
    SET_UPDATE_METHODDEF
2793
    {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
2794
    {NULL,              NULL}   /* sentinel */
2795
};
2796
2797
static PyNumberMethods set_as_number = {
2798
    0,                                  /*nb_add*/
2799
    set_sub,                            /*nb_subtract*/
2800
    0,                                  /*nb_multiply*/
2801
    0,                                  /*nb_remainder*/
2802
    0,                                  /*nb_divmod*/
2803
    0,                                  /*nb_power*/
2804
    0,                                  /*nb_negative*/
2805
    0,                                  /*nb_positive*/
2806
    0,                                  /*nb_absolute*/
2807
    0,                                  /*nb_bool*/
2808
    0,                                  /*nb_invert*/
2809
    0,                                  /*nb_lshift*/
2810
    0,                                  /*nb_rshift*/
2811
    set_and,                            /*nb_and*/
2812
    set_xor,                            /*nb_xor*/
2813
    set_or,                             /*nb_or*/
2814
    0,                                  /*nb_int*/
2815
    0,                                  /*nb_reserved*/
2816
    0,                                  /*nb_float*/
2817
    0,                                  /*nb_inplace_add*/
2818
    set_isub,                           /*nb_inplace_subtract*/
2819
    0,                                  /*nb_inplace_multiply*/
2820
    0,                                  /*nb_inplace_remainder*/
2821
    0,                                  /*nb_inplace_power*/
2822
    0,                                  /*nb_inplace_lshift*/
2823
    0,                                  /*nb_inplace_rshift*/
2824
    set_iand,                           /*nb_inplace_and*/
2825
    set_ixor,                           /*nb_inplace_xor*/
2826
    set_ior,                            /*nb_inplace_or*/
2827
};
2828
2829
PyDoc_STRVAR(set_doc,
2830
"set(iterable=(), /)\n\
2831
--\n\
2832
\n\
2833
Build an unordered collection of unique elements.");
2834
2835
PyTypeObject PySet_Type = {
2836
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
2837
    "set",                              /* tp_name */
2838
    sizeof(PySetObject),                /* tp_basicsize */
2839
    0,                                  /* tp_itemsize */
2840
    /* methods */
2841
    set_dealloc,                        /* tp_dealloc */
2842
    0,                                  /* tp_vectorcall_offset */
2843
    0,                                  /* tp_getattr */
2844
    0,                                  /* tp_setattr */
2845
    0,                                  /* tp_as_async */
2846
    set_repr,                           /* tp_repr */
2847
    &set_as_number,                     /* tp_as_number */
2848
    &set_as_sequence,                   /* tp_as_sequence */
2849
    0,                                  /* tp_as_mapping */
2850
    PyObject_HashNotImplemented,        /* tp_hash */
2851
    0,                                  /* tp_call */
2852
    0,                                  /* tp_str */
2853
    PyObject_GenericGetAttr,            /* tp_getattro */
2854
    0,                                  /* tp_setattro */
2855
    0,                                  /* tp_as_buffer */
2856
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2857
        Py_TPFLAGS_BASETYPE |
2858
        _Py_TPFLAGS_MATCH_SELF,         /* tp_flags */
2859
    set_doc,                            /* tp_doc */
2860
    set_traverse,                       /* tp_traverse */
2861
    set_clear_internal,                 /* tp_clear */
2862
    set_richcompare,                    /* tp_richcompare */
2863
    offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2864
    set_iter,                           /* tp_iter */
2865
    0,                                  /* tp_iternext */
2866
    set_methods,                        /* tp_methods */
2867
    0,                                  /* tp_members */
2868
    0,                                  /* tp_getset */
2869
    0,                                  /* tp_base */
2870
    0,                                  /* tp_dict */
2871
    0,                                  /* tp_descr_get */
2872
    0,                                  /* tp_descr_set */
2873
    0,                                  /* tp_dictoffset */
2874
    set_init,                           /* tp_init */
2875
    PyType_GenericAlloc,                /* tp_alloc */
2876
    set_new,                            /* tp_new */
2877
    PyObject_GC_Del,                    /* tp_free */
2878
    .tp_vectorcall = set_vectorcall,
2879
    .tp_version_tag = _Py_TYPE_VERSION_SET,
2880
};
2881
2882
/* frozenset object ********************************************************/
2883
2884
2885
static PyMethodDef frozenset_methods[] = {
2886
    FROZENSET___CONTAINS___METHODDEF
2887
    FROZENSET_COPY_METHODDEF
2888
    SET_DIFFERENCE_MULTI_METHODDEF
2889
    SET_INTERSECTION_MULTI_METHODDEF
2890
    SET_ISDISJOINT_METHODDEF
2891
    SET_ISSUBSET_METHODDEF
2892
    SET_ISSUPERSET_METHODDEF
2893
    SET___REDUCE___METHODDEF
2894
    SET___SIZEOF___METHODDEF
2895
    SET_SYMMETRIC_DIFFERENCE_METHODDEF
2896
    SET_UNION_METHODDEF
2897
    {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
2898
    {NULL,              NULL}   /* sentinel */
2899
};
2900
2901
static PyNumberMethods frozenset_as_number = {
2902
    0,                                  /*nb_add*/
2903
    set_sub,                            /*nb_subtract*/
2904
    0,                                  /*nb_multiply*/
2905
    0,                                  /*nb_remainder*/
2906
    0,                                  /*nb_divmod*/
2907
    0,                                  /*nb_power*/
2908
    0,                                  /*nb_negative*/
2909
    0,                                  /*nb_positive*/
2910
    0,                                  /*nb_absolute*/
2911
    0,                                  /*nb_bool*/
2912
    0,                                  /*nb_invert*/
2913
    0,                                  /*nb_lshift*/
2914
    0,                                  /*nb_rshift*/
2915
    set_and,                            /*nb_and*/
2916
    set_xor,                            /*nb_xor*/
2917
    set_or,                             /*nb_or*/
2918
};
2919
2920
PyDoc_STRVAR(frozenset_doc,
2921
"frozenset(iterable=(), /)\n\
2922
--\n\
2923
\n\
2924
Build an immutable unordered collection of unique elements.");
2925
2926
PyTypeObject PyFrozenSet_Type = {
2927
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
2928
    "frozenset",                        /* tp_name */
2929
    sizeof(PySetObject),                /* tp_basicsize */
2930
    0,                                  /* tp_itemsize */
2931
    /* methods */
2932
    set_dealloc,                        /* tp_dealloc */
2933
    0,                                  /* tp_vectorcall_offset */
2934
    0,                                  /* tp_getattr */
2935
    0,                                  /* tp_setattr */
2936
    0,                                  /* tp_as_async */
2937
    set_repr,                           /* tp_repr */
2938
    &frozenset_as_number,               /* tp_as_number */
2939
    &set_as_sequence,                   /* tp_as_sequence */
2940
    0,                                  /* tp_as_mapping */
2941
    frozenset_hash,                     /* tp_hash */
2942
    0,                                  /* tp_call */
2943
    0,                                  /* tp_str */
2944
    PyObject_GenericGetAttr,            /* tp_getattro */
2945
    0,                                  /* tp_setattro */
2946
    0,                                  /* tp_as_buffer */
2947
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2948
        Py_TPFLAGS_BASETYPE |
2949
        _Py_TPFLAGS_MATCH_SELF,         /* tp_flags */
2950
    frozenset_doc,                      /* tp_doc */
2951
    set_traverse,                       /* tp_traverse */
2952
    set_clear_internal,                 /* tp_clear */
2953
    set_richcompare,                    /* tp_richcompare */
2954
    offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
2955
    set_iter,                           /* tp_iter */
2956
    0,                                  /* tp_iternext */
2957
    frozenset_methods,                  /* tp_methods */
2958
    0,                                  /* tp_members */
2959
    0,                                  /* tp_getset */
2960
    0,                                  /* tp_base */
2961
    0,                                  /* tp_dict */
2962
    0,                                  /* tp_descr_get */
2963
    0,                                  /* tp_descr_set */
2964
    0,                                  /* tp_dictoffset */
2965
    0,                                  /* tp_init */
2966
    PyType_GenericAlloc,                /* tp_alloc */
2967
    frozenset_new,                      /* tp_new */
2968
    PyObject_GC_Del,                    /* tp_free */
2969
    .tp_vectorcall = frozenset_vectorcall,
2970
    .tp_version_tag = _Py_TYPE_VERSION_FROZEN_SET,
2971
};
2972
2973
2974
/***** C API functions *************************************************/
2975
2976
PyObject *
2977
PySet_New(PyObject *iterable)
2978
902k
{
2979
902k
    return make_new_set(&PySet_Type, iterable);
2980
902k
}
2981
2982
PyObject *
2983
PyFrozenSet_New(PyObject *iterable)
2984
5.09k
{
2985
5.09k
    PyObject *result = make_new_set(&PyFrozenSet_Type, iterable);
2986
5.09k
    if (result != NULL) {
2987
5.09k
        _PyFrozenSet_MaybeUntrack(result);
2988
5.09k
    }
2989
5.09k
    return result;
2990
5.09k
}
2991
2992
Py_ssize_t
2993
PySet_Size(PyObject *anyset)
2994
321
{
2995
321
    if (!PyAnySet_Check(anyset)) {
2996
0
        PyErr_BadInternalCall();
2997
0
        return -1;
2998
0
    }
2999
321
    return set_len(anyset);
3000
321
}
3001
3002
int
3003
PySet_Clear(PyObject *set)
3004
535
{
3005
535
    if (!PySet_Check(set)) {
3006
0
        PyErr_BadInternalCall();
3007
0
        return -1;
3008
0
    }
3009
535
    (void)set_clear(set, NULL);
3010
535
    return 0;
3011
535
}
3012
3013
void
3014
_PySet_ClearInternal(PySetObject *so)
3015
0
{
3016
0
    (void)set_clear_internal((PyObject*)so);
3017
0
}
3018
3019
int
3020
PySet_Contains(PyObject *anyset, PyObject *key)
3021
276k
{
3022
276k
    if (!PyAnySet_Check(anyset)) {
3023
0
        PyErr_BadInternalCall();
3024
0
        return -1;
3025
0
    }
3026
276k
    if (PyFrozenSet_CheckExact(anyset)) {
3027
0
        return set_contains_key((PySetObject *)anyset, key);
3028
0
    }
3029
276k
    int rv;
3030
276k
    Py_BEGIN_CRITICAL_SECTION(anyset);
3031
276k
    rv = set_contains_key((PySetObject *)anyset, key);
3032
276k
    Py_END_CRITICAL_SECTION();
3033
276k
    return rv;
3034
276k
}
3035
3036
int
3037
PySet_Discard(PyObject *set, PyObject *key)
3038
47.6k
{
3039
47.6k
    if (!PySet_Check(set)) {
3040
0
        PyErr_BadInternalCall();
3041
0
        return -1;
3042
0
    }
3043
3044
47.6k
    int rv;
3045
47.6k
    Py_BEGIN_CRITICAL_SECTION(set);
3046
47.6k
    rv = set_discard_key((PySetObject *)set, key);
3047
47.6k
    Py_END_CRITICAL_SECTION();
3048
47.6k
    return rv;
3049
47.6k
}
3050
3051
int
3052
PySet_Add(PyObject *anyset, PyObject *key)
3053
38.8k
{
3054
38.8k
    if (PySet_Check(anyset)) {
3055
34.1k
        int rv;
3056
34.1k
        Py_BEGIN_CRITICAL_SECTION(anyset);
3057
34.1k
        rv = set_add_key((PySetObject *)anyset, key);
3058
34.1k
        Py_END_CRITICAL_SECTION();
3059
34.1k
        return rv;
3060
34.1k
    }
3061
3062
4.70k
    if (PyFrozenSet_Check(anyset) && _PyObject_IsUniquelyReferenced(anyset)) {
3063
        // We can only change frozensets if they are uniquely referenced. The
3064
        // API limits the usage of `PySet_Add` to "fill in the values of brand
3065
        // new frozensets before they are exposed to other code". In this case,
3066
        // this can be done without a lock.
3067
        // Since another key is added to the set, we must track the frozenset
3068
        // if needed.
3069
4.70k
        if (PyFrozenSet_CheckExact(anyset) && !PyObject_GC_IsTracked(anyset) && PyObject_GC_IsTracked(key)) {
3070
10
            _PyObject_GC_TRACK(anyset);
3071
10
        }
3072
4.70k
        return set_add_key((PySetObject *)anyset, key);
3073
4.70k
    }
3074
3075
0
    PyErr_BadInternalCall();
3076
0
    return -1;
3077
4.70k
}
3078
3079
int
3080
_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
3081
4.30k
{
3082
4.30k
    setentry *entry;
3083
3084
4.30k
    if (!PyAnySet_Check(set)) {
3085
0
        PyErr_BadInternalCall();
3086
0
        return -1;
3087
0
    }
3088
4.30k
    if (set_next((PySetObject *)set, pos, &entry) == 0)
3089
875
        return 0;
3090
3.42k
    *key = entry->key;
3091
3.42k
    *hash = entry->hash;
3092
3.42k
    return 1;
3093
4.30k
}
3094
3095
int
3096
_PySet_NextEntryRef(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
3097
107k
{
3098
107k
    setentry *entry;
3099
3100
107k
    if (!PyAnySet_Check(set)) {
3101
0
        PyErr_BadInternalCall();
3102
0
        return -1;
3103
0
    }
3104
107k
    _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(set);
3105
107k
    if (set_next((PySetObject *)set, pos, &entry) == 0)
3106
16.5k
        return 0;
3107
90.7k
    *key = Py_NewRef(entry->key);
3108
90.7k
    *hash = entry->hash;
3109
90.7k
    return 1;
3110
107k
}
3111
3112
PyObject *
3113
PySet_Pop(PyObject *set)
3114
5
{
3115
5
    if (!PySet_Check(set)) {
3116
0
        PyErr_BadInternalCall();
3117
0
        return NULL;
3118
0
    }
3119
5
    return set_pop(set, NULL);
3120
5
}
3121
3122
int
3123
_PySet_Update(PyObject *set, PyObject *iterable)
3124
101k
{
3125
101k
    if (!PySet_Check(set)) {
3126
0
        PyErr_BadInternalCall();
3127
0
        return -1;
3128
0
    }
3129
101k
    return set_update_internal((PySetObject *)set, iterable);
3130
101k
}
3131
3132
/* Exported for the gdb plugin's benefit. */
3133
PyObject *_PySet_Dummy = dummy;
3134
3135
/***** Dummy Struct  *************************************************/
3136
3137
static PyObject *
3138
dummy_repr(PyObject *op)
3139
0
{
3140
0
    return PyUnicode_FromString("<dummy key>");
3141
0
}
3142
3143
static void _Py_NO_RETURN
3144
dummy_dealloc(PyObject* ignore)
3145
0
{
3146
0
    Py_FatalError("deallocating <dummy key>");
3147
0
}
3148
3149
static PyTypeObject _PySetDummy_Type = {
3150
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
3151
    "<dummy key> type",
3152
    0,
3153
    0,
3154
    dummy_dealloc,      /*tp_dealloc*/ /*never called*/
3155
    0,                  /*tp_vectorcall_offset*/
3156
    0,                  /*tp_getattr*/
3157
    0,                  /*tp_setattr*/
3158
    0,                  /*tp_as_async*/
3159
    dummy_repr,         /*tp_repr*/
3160
    0,                  /*tp_as_number*/
3161
    0,                  /*tp_as_sequence*/
3162
    0,                  /*tp_as_mapping*/
3163
    0,                  /*tp_hash */
3164
    0,                  /*tp_call */
3165
    0,                  /*tp_str */
3166
    0,                  /*tp_getattro */
3167
    0,                  /*tp_setattro */
3168
    0,                  /*tp_as_buffer */
3169
    Py_TPFLAGS_DEFAULT, /*tp_flags */
3170
};
3171
3172
static PyObject _dummy_struct = _PyObject_HEAD_INIT(&_PySetDummy_Type);