annotate Objects/setobject.c @ 39940:dfd809dcf680 2.5

Bug #1446043: correctly raise a LookupError if an encoding name given to encodings.search_function() contains a dot. (backport from rev. 52075)
author Georg Brandl <georg@python.org>
date Sat, 30 Sep 2006 11:22:35 +0000
parents 891e332cd2d6
children 796304efa268
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1
35311
541bfbb10030 * Move copyright notice to top and indicate derivation from sets.py and
Raymond Hettinger <python@rcn.com>
parents: 35301
diff changeset
2 /* set object implementation
541bfbb10030 * Move copyright notice to top and indicate derivation from sets.py and
Raymond Hettinger <python@rcn.com>
parents: 35301
diff changeset
3 Written and maintained by Raymond D. Hettinger <python@rcn.com>
541bfbb10030 * Move copyright notice to top and indicate derivation from sets.py and
Raymond Hettinger <python@rcn.com>
parents: 35301
diff changeset
4 Derived from Lib/sets.py and Objects/dictobject.c.
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
5
37602
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37315
diff changeset
6 Copyright (c) 2003-6 Python Software Foundation.
35311
541bfbb10030 * Move copyright notice to top and indicate derivation from sets.py and
Raymond Hettinger <python@rcn.com>
parents: 35301
diff changeset
7 All rights reserved.
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
8 */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
9
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
10 #include "Python.h"
35311
541bfbb10030 * Move copyright notice to top and indicate derivation from sets.py and
Raymond Hettinger <python@rcn.com>
parents: 35301
diff changeset
11 #include "structmember.h"
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
12
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
13 /* This must be >= 1. */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
14 #define PERTURB_SHIFT 5
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
15
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
16 /* Object used as dummy key to fill deleted entries */
35311
541bfbb10030 * Move copyright notice to top and indicate derivation from sets.py and
Raymond Hettinger <python@rcn.com>
parents: 35301
diff changeset
17 static PyObject *dummy = NULL; /* Initialized by first call to make_new_set() */
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
18
37638
65ecb83ea7dd Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents: 37602
diff changeset
19 #ifdef Py_REF_DEBUG
65ecb83ea7dd Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents: 37602
diff changeset
20 PyObject *
65ecb83ea7dd Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents: 37602
diff changeset
21 _PySet_Dummy(void)
65ecb83ea7dd Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents: 37602
diff changeset
22 {
65ecb83ea7dd Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents: 37602
diff changeset
23 return dummy;
65ecb83ea7dd Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents: 37602
diff changeset
24 }
65ecb83ea7dd Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents: 37602
diff changeset
25 #endif
65ecb83ea7dd Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents: 37602
diff changeset
26
35321
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
27 #define INIT_NONZERO_SET_SLOTS(so) do { \
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
28 (so)->table = (so)->smalltable; \
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
29 (so)->mask = PySet_MINSIZE - 1; \
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
30 (so)->hash = -1; \
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
31 } while(0)
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
32
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
33 #define EMPTY_TO_MINSIZE(so) do { \
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
34 memset((so)->smalltable, 0, sizeof((so)->smalltable)); \
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
35 (so)->used = (so)->fill = 0; \
35321
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
36 INIT_NONZERO_SET_SLOTS(so); \
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
37 } while(0)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
38
35321
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
39 /* Reuse scheme to save calls to malloc, free, and memset */
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
40 #define MAXFREESETS 80
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
41 static PySetObject *free_sets[MAXFREESETS];
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
42 static int num_free_sets = 0;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
43
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
44 /*
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
45 The basic lookup function used by all operations.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
46 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
47 Open addressing is preferred over chaining since the link overhead for
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
48 chaining would be substantial (100% with typical malloc overhead).
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
49
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
50 The initial probe index is computed as hash mod the table size. Subsequent
35321
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
51 probe indices are computed as explained in Objects/dictobject.c.
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
52
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
53 All arithmetic on hash should ignore overflow.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
54
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
55 Unlike the dictionary implementation, the lookkey functions can return
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
56 NULL if the rich comparison returns an error.
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
57 */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
58
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
59 static setentry *
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
60 set_lookkey(PySetObject *so, PyObject *key, register long hash)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
61 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
62 register Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
63 register size_t perturb;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
64 register setentry *freeslot;
38872
805f41881e27 Use Py_ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37747
diff changeset
65 register size_t mask = so->mask;
35312
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
66 setentry *table = so->table;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
67 register setentry *entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
68 register int cmp;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
69 PyObject *startkey;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
70
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
71 i = hash & mask;
35312
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
72 entry = &table[i];
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
73 if (entry->key == NULL || entry->key == key)
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
74 return entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
75
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
76 if (entry->key == dummy)
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
77 freeslot = entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
78 else {
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
79 if (entry->hash == hash) {
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
80 startkey = entry->key;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
81 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
82 if (cmp < 0)
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
83 return NULL;
35312
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
84 if (table == so->table && entry->key == startkey) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
85 if (cmp > 0)
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
86 return entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
87 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
88 else {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
89 /* The compare did major nasty stuff to the
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
90 * set: start over.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
91 */
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
92 return set_lookkey(so, key, hash);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
93 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
94 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
95 freeslot = NULL;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
96 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
97
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
98 /* In the loop, key == dummy is by far (factor of 100s) the
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
99 least likely outcome, so test for that last. */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
100 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
101 i = (i << 2) + i + perturb + 1;
35312
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
102 entry = &table[i & mask];
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
103 if (entry->key == NULL) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
104 if (freeslot != NULL)
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
105 entry = freeslot;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
106 break;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
107 }
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
108 if (entry->key == key)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
109 break;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
110 if (entry->hash == hash && entry->key != dummy) {
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
111 startkey = entry->key;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
112 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
113 if (cmp < 0)
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
114 return NULL;
35312
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
115 if (table == so->table && entry->key == startkey) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
116 if (cmp > 0)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
117 break;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
118 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
119 else {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
120 /* The compare did major nasty stuff to the
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
121 * set: start over.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
122 */
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
123 return set_lookkey(so, key, hash);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
124 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
125 }
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
126 else if (entry->key == dummy && freeslot == NULL)
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
127 freeslot = entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
128 }
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
129 return entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
130 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
131
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
132 /*
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
133 * Hacked up version of set_lookkey which can assume keys are always strings;
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
134 * This means we can always use _PyString_Eq directly and not have to check to
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
135 * see if the comparison altered the table.
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
136 */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
137 static setentry *
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
138 set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
139 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
140 register Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
141 register size_t perturb;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
142 register setentry *freeslot;
38872
805f41881e27 Use Py_ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37747
diff changeset
143 register size_t mask = so->mask;
35312
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
144 setentry *table = so->table;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
145 register setentry *entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
146
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
147 /* Make sure this function doesn't have to handle non-string keys,
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
148 including subclasses of str; e.g., one reason to subclass
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
149 strings is to override __eq__, and for speed we don't cater to
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
150 that here. */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
151 if (!PyString_CheckExact(key)) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
152 so->lookup = set_lookkey;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
153 return set_lookkey(so, key, hash);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
154 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
155 i = hash & mask;
35312
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
156 entry = &table[i];
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
157 if (entry->key == NULL || entry->key == key)
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
158 return entry;
35339
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
159 if (entry->key == dummy)
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
160 freeslot = entry;
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
161 else {
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
162 if (entry->hash == hash && _PyString_Eq(entry->key, key))
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
163 return entry;
35339
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
164 freeslot = NULL;
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
165 }
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
166
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
167 /* In the loop, key == dummy is by far (factor of 100s) the
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
168 least likely outcome, so test for that last. */
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
169 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
170 i = (i << 2) + i + perturb + 1;
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
171 entry = &table[i & mask];
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
172 if (entry->key == NULL)
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
173 return freeslot == NULL ? entry : freeslot;
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
174 if (entry->key == key
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
175 || (entry->hash == hash
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
176 && entry->key != dummy
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
177 && _PyString_Eq(entry->key, key)))
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
178 return entry;
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
179 if (entry->key == dummy && freeslot == NULL)
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
180 freeslot = entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
181 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
182 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
183
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
184 /*
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
185 Internal routine to insert a new key into the table.
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
186 Used both by the internal resize routine and by the public insert routine.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
187 Eats a reference to key.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
188 */
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
189 static int
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
190 set_insert_key(register PySetObject *so, PyObject *key, long hash)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
191 {
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
192 register setentry *entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
193 typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
194
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
195 assert(so->lookup != NULL);
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
196 entry = so->lookup(so, key, hash);
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
197 if (entry == NULL)
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
198 return -1;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
199 if (entry->key == NULL) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
200 /* UNUSED */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
201 so->fill++;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
202 entry->key = key;
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
203 entry->hash = hash;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
204 so->used++;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
205 } else if (entry->key == dummy) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
206 /* DUMMY */
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
207 entry->key = key;
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
208 entry->hash = hash;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
209 so->used++;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
210 Py_DECREF(dummy);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
211 } else {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
212 /* ACTIVE */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
213 Py_DECREF(key);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
214 }
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
215 return 0;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
216 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
217
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
218 /*
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
219 Restructure the table by allocating a new table and reinserting all
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
220 keys again. When entries have been deleted, the new table may
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
221 actually be smaller than the old one.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
222 */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
223 static int
38872
805f41881e27 Use Py_ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37747
diff changeset
224 set_table_resize(PySetObject *so, Py_ssize_t minused)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
225 {
38872
805f41881e27 Use Py_ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37747
diff changeset
226 Py_ssize_t newsize;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
227 setentry *oldtable, *newtable, *entry;
38872
805f41881e27 Use Py_ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37747
diff changeset
228 Py_ssize_t i;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
229 int is_oldtable_malloced;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
230 setentry small_copy[PySet_MINSIZE];
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
231
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
232 assert(minused >= 0);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
233
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
234 /* Find the smallest table size > minused. */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
235 for (newsize = PySet_MINSIZE;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
236 newsize <= minused && newsize > 0;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
237 newsize <<= 1)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
238 ;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
239 if (newsize <= 0) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
240 PyErr_NoMemory();
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
241 return -1;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
242 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
243
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
244 /* Get space for a new table. */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
245 oldtable = so->table;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
246 assert(oldtable != NULL);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
247 is_oldtable_malloced = oldtable != so->smalltable;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
248
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
249 if (newsize == PySet_MINSIZE) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
250 /* A large table is shrinking, or we can't get any smaller. */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
251 newtable = so->smalltable;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
252 if (newtable == oldtable) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
253 if (so->fill == so->used) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
254 /* No dummies, so no point doing anything. */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
255 return 0;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
256 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
257 /* We're not going to resize it, but rebuild the
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
258 table anyway to purge old dummy entries.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
259 Subtle: This is *necessary* if fill==size,
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
260 as set_lookkey needs at least one virgin slot to
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
261 terminate failing searches. If fill < size, it's
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
262 merely desirable, as dummies slow searches. */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
263 assert(so->fill > so->used);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
264 memcpy(small_copy, oldtable, sizeof(small_copy));
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
265 oldtable = small_copy;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
266 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
267 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
268 else {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
269 newtable = PyMem_NEW(setentry, newsize);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
270 if (newtable == NULL) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
271 PyErr_NoMemory();
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
272 return -1;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
273 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
274 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
275
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
276 /* Make the set empty, using the new table. */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
277 assert(newtable != oldtable);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
278 so->table = newtable;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
279 so->mask = newsize - 1;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
280 memset(newtable, 0, sizeof(setentry) * newsize);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
281 so->used = 0;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
282 i = so->fill;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
283 so->fill = 0;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
284
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
285 /* Copy the data over; this is refcount-neutral for active entries;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
286 dummy entries aren't copied over, of course */
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
287 for (entry = oldtable; i > 0; entry++) {
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
288 if (entry->key == NULL) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
289 /* UNUSED */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
290 ;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
291 } else if (entry->key == dummy) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
292 /* DUMMY */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
293 --i;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
294 assert(entry->key == dummy);
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
295 Py_DECREF(entry->key);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
296 } else {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
297 /* ACTIVE */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
298 --i;
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
299 if(set_insert_key(so, entry->key, entry->hash) == -1) {
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
300 if (is_oldtable_malloced)
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
301 PyMem_DEL(oldtable);
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
302 return -1;
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
303 }
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
304 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
305 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
306
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
307 if (is_oldtable_malloced)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
308 PyMem_DEL(oldtable);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
309 return 0;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
310 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
311
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
312 /* CAUTION: set_add_key/entry() must guarantee it won't resize the table */
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
313
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
314 static int
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
315 set_add_entry(register PySetObject *so, setentry *entry)
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
316 {
38872
805f41881e27 Use Py_ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37747
diff changeset
317 register Py_ssize_t n_used;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
318
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
319 assert(so->fill <= so->mask); /* at least one empty slot */
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
320 n_used = so->used;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
321 Py_INCREF(entry->key);
39806
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
322 if (set_insert_key(so, entry->key, entry->hash) == -1) {
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
323 Py_DECREF(entry->key);
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
324 return -1;
39806
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
325 }
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
326 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
327 return 0;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
328 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
329 }
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
330
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
331 static int
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
332 set_add_key(register PySetObject *so, PyObject *key)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
333 {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
334 register long hash;
38872
805f41881e27 Use Py_ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37747
diff changeset
335 register Py_ssize_t n_used;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
336
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
337 if (!PyString_CheckExact(key) ||
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
338 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
339 hash = PyObject_Hash(key);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
340 if (hash == -1)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
341 return -1;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
342 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
343 assert(so->fill <= so->mask); /* at least one empty slot */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
344 n_used = so->used;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
345 Py_INCREF(key);
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
346 if (set_insert_key(so, key, hash) == -1) {
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
347 Py_DECREF(key);
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
348 return -1;
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
349 }
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
350 if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
351 return 0;
35321
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
352 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
353 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
354
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
355 #define DISCARD_NOTFOUND 0
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
356 #define DISCARD_FOUND 1
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
357
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
358 static int
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
359 set_discard_entry(PySetObject *so, setentry *oldentry)
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
360 { register setentry *entry;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
361 PyObject *old_key;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
362
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
363 entry = (so->lookup)(so, oldentry->key, oldentry->hash);
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
364 if (entry == NULL)
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
365 return -1;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
366 if (entry->key == NULL || entry->key == dummy)
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
367 return DISCARD_NOTFOUND;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
368 old_key = entry->key;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
369 Py_INCREF(dummy);
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
370 entry->key = dummy;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
371 so->used--;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
372 Py_DECREF(old_key);
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
373 return DISCARD_FOUND;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
374 }
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
375
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
376 static int
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
377 set_discard_key(PySetObject *so, PyObject *key)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
378 {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
379 register long hash;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
380 register setentry *entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
381 PyObject *old_key;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
382
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
383 assert (PyAnySet_Check(so));
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
384 if (!PyString_CheckExact(key) ||
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
385 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
386 hash = PyObject_Hash(key);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
387 if (hash == -1)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
388 return -1;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
389 }
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
390 entry = (so->lookup)(so, key, hash);
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
391 if (entry == NULL)
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
392 return -1;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
393 if (entry->key == NULL || entry->key == dummy)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
394 return DISCARD_NOTFOUND;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
395 old_key = entry->key;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
396 Py_INCREF(dummy);
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
397 entry->key = dummy;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
398 so->used--;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
399 Py_DECREF(old_key);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
400 return DISCARD_FOUND;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
401 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
402
35315
a6cbfcd0e691 Factor away a redundant clear() function.
Raymond Hettinger <python@rcn.com>
parents: 35312
diff changeset
403 static int
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
404 set_clear_internal(PySetObject *so)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
405 {
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
406 setentry *entry, *table;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
407 int table_is_malloced;
38872
805f41881e27 Use Py_ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37747
diff changeset
408 Py_ssize_t fill;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
409 setentry small_copy[PySet_MINSIZE];
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
410 #ifdef Py_DEBUG
38872
805f41881e27 Use Py_ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37747
diff changeset
411 Py_ssize_t i, n;
35312
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
412 assert (PyAnySet_Check(so));
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
413
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
414 n = so->mask + 1;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
415 i = 0;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
416 #endif
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
417
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
418 table = so->table;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
419 assert(table != NULL);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
420 table_is_malloced = table != so->smalltable;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
421
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
422 /* This is delicate. During the process of clearing the set,
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
423 * decrefs can cause the set to mutate. To avoid fatal confusion
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
424 * (voice of experience), we have to make the set empty before
35315
a6cbfcd0e691 Factor away a redundant clear() function.
Raymond Hettinger <python@rcn.com>
parents: 35312
diff changeset
425 * clearing the slots, and never refer to anything via so->ref while
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
426 * clearing.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
427 */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
428 fill = so->fill;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
429 if (table_is_malloced)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
430 EMPTY_TO_MINSIZE(so);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
431
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
432 else if (fill > 0) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
433 /* It's a small table with something that needs to be cleared.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
434 * Afraid the only safe way is to copy the set entries into
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
435 * another small table first.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
436 */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
437 memcpy(small_copy, table, sizeof(small_copy));
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
438 table = small_copy;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
439 EMPTY_TO_MINSIZE(so);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
440 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
441 /* else it's a small table that's already empty */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
442
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
443 /* Now we can finally clear things. If C had refcounts, we could
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
444 * assert that the refcount on table is 1 now, i.e. that this function
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
445 * has unique access to it, so decref side-effects can't alter it.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
446 */
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
447 for (entry = table; fill > 0; ++entry) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
448 #ifdef Py_DEBUG
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
449 assert(i < n);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
450 ++i;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
451 #endif
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
452 if (entry->key) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
453 --fill;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
454 Py_DECREF(entry->key);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
455 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
456 #ifdef Py_DEBUG
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
457 else
37219
d7a6ec9de887 Tighten an overbroad and misleading assertion.
Raymond Hettinger <python@rcn.com>
parents: 36833
diff changeset
458 assert(entry->key == NULL);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
459 #endif
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
460 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
461
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
462 if (table_is_malloced)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
463 PyMem_DEL(table);
35315
a6cbfcd0e691 Factor away a redundant clear() function.
Raymond Hettinger <python@rcn.com>
parents: 35312
diff changeset
464 return 0;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
465 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
466
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
467 /*
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
468 * Iterate over a set table. Use like so:
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
469 *
38872
805f41881e27 Use Py_ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37747
diff changeset
470 * Py_ssize_t pos;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
471 * setentry *entry;
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
472 * pos = 0; # important! pos should not otherwise be changed by you
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
473 * while (set_next(yourset, &pos, &entry)) {
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
474 * Refer to borrowed reference in entry->key.
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
475 * }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
476 *
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
477 * CAUTION: In general, it isn't safe to use set_next in a loop that
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
478 * mutates the table.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
479 */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
480 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
481 set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
482 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
483 Py_ssize_t i;
38872
805f41881e27 Use Py_ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37747
diff changeset
484 Py_ssize_t mask;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
485 register setentry *table;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
486
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
487 assert (PyAnySet_Check(so));
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
488 i = *pos_ptr;
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
489 assert(i >= 0);
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
490 table = so->table;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
491 mask = so->mask;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
492 while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
493 i++;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
494 *pos_ptr = i+1;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
495 if (i > mask)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
496 return 0;
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
497 assert(table[i].key != NULL);
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
498 *entry_ptr = &table[i];
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
499 return 1;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
500 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
501
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
502 static void
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
503 set_dealloc(PySetObject *so)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
504 {
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
505 register setentry *entry;
38872
805f41881e27 Use Py_ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37747
diff changeset
506 Py_ssize_t fill = so->fill;
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
507 PyObject_GC_UnTrack(so);
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
508 Py_TRASHCAN_SAFE_BEGIN(so)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
509 if (so->weakreflist != NULL)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
510 PyObject_ClearWeakRefs((PyObject *) so);
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
511
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
512 for (entry = so->table; fill > 0; entry++) {
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
513 if (entry->key) {
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
514 --fill;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
515 Py_DECREF(entry->key);
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
516 }
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
517 }
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
518 if (so->table != so->smalltable)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
519 PyMem_DEL(so->table);
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
520 if (num_free_sets < MAXFREESETS && PyAnySet_CheckExact(so))
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
521 free_sets[num_free_sets++] = so;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
522 else
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
523 so->ob_type->tp_free(so);
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
524 Py_TRASHCAN_SAFE_END(so)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
525 }
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
526
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
527 static int
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
528 set_tp_print(PySetObject *so, FILE *fp, int flags)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
529 {
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
530 setentry *entry;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
531 Py_ssize_t pos=0;
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
532 char *emit = ""; /* No separator emitted on first pass */
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
533 char *separator = ", ";
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
534
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
535 fprintf(fp, "%s([", so->ob_type->tp_name);
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
536 while (set_next(so, &pos, &entry)) {
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
537 fputs(emit, fp);
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
538 emit = separator;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
539 if (PyObject_Print(entry->key, fp, 0) != 0)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
540 return -1;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
541 }
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
542 fputs("])", fp);
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
543 return 0;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
544 }
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
545
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
546 static PyObject *
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
547 set_repr(PySetObject *so)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
548 {
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
549 PyObject *keys, *result, *listrepr;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
550
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
551 keys = PySequence_List((PyObject *)so);
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
552 if (keys == NULL)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
553 return NULL;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
554 listrepr = PyObject_Repr(keys);
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
555 Py_DECREF(keys);
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
556 if (listrepr == NULL)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
557 return NULL;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
558
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
559 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
560 PyString_AS_STRING(listrepr));
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
561 Py_DECREF(listrepr);
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
562 return result;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
563 }
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
564
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
565 static Py_ssize_t
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
566 set_len(PyObject *so)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
567 {
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
568 return ((PySetObject *)so)->used;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
569 }
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
570
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
571 static int
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
572 set_merge(PySetObject *so, PyObject *otherset)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
573 {
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
574 PySetObject *other;
38872
805f41881e27 Use Py_ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37747
diff changeset
575 register Py_ssize_t i;
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
576 register setentry *entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
577
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
578 assert (PyAnySet_Check(so));
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
579 assert (PyAnySet_Check(otherset));
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
580
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
581 other = (PySetObject*)otherset;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
582 if (other == so || other->used == 0)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
583 /* a.update(a) or a.update({}); nothing to do */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
584 return 0;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
585 /* Do one big resize at the start, rather than
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
586 * incrementally resizing as we insert new keys. Expect
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
587 * that there will be no (or few) overlapping keys.
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
588 */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
589 if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
590 if (set_table_resize(so, (so->used + other->used)*2) != 0)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
591 return -1;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
592 }
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
593 for (i = 0; i <= other->mask; i++) {
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
594 entry = &other->table[i];
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
595 if (entry->key != NULL &&
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
596 entry->key != dummy) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
597 Py_INCREF(entry->key);
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
598 if (set_insert_key(so, entry->key, entry->hash) == -1) {
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
599 Py_DECREF(entry->key);
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
600 return -1;
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
601 }
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
602 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
603 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
604 return 0;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
605 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
606
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
607 static int
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
608 set_contains_key(PySetObject *so, PyObject *key)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
609 {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
610 long hash;
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
611 setentry *entry;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
612
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
613 if (!PyString_CheckExact(key) ||
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
614 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
615 hash = PyObject_Hash(key);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
616 if (hash == -1)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
617 return -1;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
618 }
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
619 entry = (so->lookup)(so, key, hash);
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
620 if (entry == NULL)
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
621 return -1;
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
622 key = entry->key;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
623 return key != NULL && key != dummy;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
624 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
625
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
626 static int
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
627 set_contains_entry(PySetObject *so, setentry *entry)
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
628 {
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
629 PyObject *key;
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
630 setentry *lu_entry;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
631
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
632 lu_entry = (so->lookup)(so, entry->key, entry->hash);
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
633 if (lu_entry == NULL)
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
634 return -1;
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
635 key = lu_entry->key;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
636 return key != NULL && key != dummy;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
637 }
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
638
35341
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
639 static PyObject *
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
640 set_pop(PySetObject *so)
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
641 {
38872
805f41881e27 Use Py_ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37747
diff changeset
642 register Py_ssize_t i = 0;
35341
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
643 register setentry *entry;
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
644 PyObject *key;
35341
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
645
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
646 assert (PyAnySet_Check(so));
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
647 if (so->used == 0) {
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
648 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
649 return NULL;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
650 }
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
651
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
652 /* Set entry to "the first" unused or dummy set entry. We abuse
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
653 * the hash field of slot 0 to hold a search finger:
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
654 * If slot 0 has a value, use slot 0.
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
655 * Else slot 0 is being used to hold a search finger,
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
656 * and we use its hash value as the first index to look.
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
657 */
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
658 entry = &so->table[0];
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
659 if (entry->key == NULL || entry->key == dummy) {
38872
805f41881e27 Use Py_ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37747
diff changeset
660 i = entry->hash;
35341
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
661 /* The hash field may be a real hash value, or it may be a
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
662 * legit search finger, or it may be a once-legit search
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
663 * finger that's out of bounds now because it wrapped around
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
664 * or the table shrunk -- simply make sure it's in bounds now.
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
665 */
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
666 if (i > so->mask || i < 1)
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
667 i = 1; /* skip slot 0 */
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
668 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
669 i++;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
670 if (i > so->mask)
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
671 i = 1;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
672 }
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
673 }
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
674 key = entry->key;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
675 Py_INCREF(dummy);
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
676 entry->key = dummy;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
677 so->used--;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
678 so->table[0].hash = i + 1; /* next place to start */
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
679 return key;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
680 }
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
681
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
682 PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
683
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
684 static int
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
685 set_traverse(PySetObject *so, visitproc visit, void *arg)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
686 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
687 Py_ssize_t pos = 0;
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
688 setentry *entry;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
689
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
690 while (set_next(so, &pos, &entry))
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
691 Py_VISIT(entry->key);
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
692 return 0;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
693 }
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
694
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
695 static long
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
696 frozenset_hash(PyObject *self)
35341
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
697 {
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
698 PySetObject *so = (PySetObject *)self;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
699 long h, hash = 1927868237L;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
700 setentry *entry;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
701 Py_ssize_t pos = 0;
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
702
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
703 if (so->hash != -1)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
704 return so->hash;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
705
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
706 hash *= PySet_GET_SIZE(self) + 1;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
707 while (set_next(so, &pos, &entry)) {
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
708 /* Work to increase the bit dispersion for closely spaced hash
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
709 values. The is important because some use cases have many
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
710 combinations of a small number of elements with nearby
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
711 hashes so that many distinct combinations collapse to only
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
712 a handful of distinct hash values. */
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
713 h = entry->hash;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
714 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
715 }
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
716 hash = hash * 69069L + 907133923L;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
717 if (hash == -1)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
718 hash = 590923713L;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
719 so->hash = hash;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
720 return hash;
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
721 }
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
722
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
723 static long
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
724 set_nohash(PyObject *self)
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
725 {
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
726 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
727 return -1;
35341
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
728 }
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
729
35311
541bfbb10030 * Move copyright notice to top and indicate derivation from sets.py and
Raymond Hettinger <python@rcn.com>
parents: 35301
diff changeset
730 /***** Set iterator type ***********************************************/
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
731
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
732 typedef struct {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
733 PyObject_HEAD
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
734 PySetObject *si_set; /* Set to NULL when iterator is exhausted */
38872
805f41881e27 Use Py_ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37747
diff changeset
735 Py_ssize_t si_used;
805f41881e27 Use Py_ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37747
diff changeset
736 Py_ssize_t si_pos;
805f41881e27 Use Py_ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37747
diff changeset
737 Py_ssize_t len;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
738 } setiterobject;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
739
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
740 static void
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
741 setiter_dealloc(setiterobject *si)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
742 {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
743 Py_XDECREF(si->si_set);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
744 PyObject_Del(si);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
745 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
746
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35581
diff changeset
747 static PyObject *
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
748 setiter_len(setiterobject *si)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
749 {
38872
805f41881e27 Use Py_ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37747
diff changeset
750 Py_ssize_t len = 0;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
751 if (si->si_set != NULL && si->si_used == si->si_set->used)
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35581
diff changeset
752 len = si->len;
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35581
diff changeset
753 return PyInt_FromLong(len);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
754 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
755
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 36244
diff changeset
756 PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35581
diff changeset
757
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35581
diff changeset
758 static PyMethodDef setiter_methods[] = {
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 36244
diff changeset
759 {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35581
diff changeset
760 {NULL, NULL} /* sentinel */
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
761 };
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
762
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
763 static PyObject *setiter_iternext(setiterobject *si)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
764 {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
765 PyObject *key;
38872
805f41881e27 Use Py_ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37747
diff changeset
766 register Py_ssize_t i, mask;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
767 register setentry *entry;
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
768 PySetObject *so = si->si_set;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
769
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
770 if (so == NULL)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
771 return NULL;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
772 assert (PyAnySet_Check(so));
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
773
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
774 if (si->si_used != so->used) {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
775 PyErr_SetString(PyExc_RuntimeError,
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
776 "Set changed size during iteration");
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
777 si->si_used = -1; /* Make this state sticky */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
778 return NULL;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
779 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
780
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
781 i = si->si_pos;
35349
6f1cd28d6b87 Results of a line-by-line comparison back to dictobject.c.
Raymond Hettinger <python@rcn.com>
parents: 35348
diff changeset
782 assert(i>=0);
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
783 entry = so->table;
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
784 mask = so->mask;
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
785 while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
786 i++;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
787 si->si_pos = i+1;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
788 if (i > mask)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
789 goto fail;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
790 si->len--;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
791 key = entry[i].key;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
792 Py_INCREF(key);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
793 return key;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
794
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
795 fail:
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
796 Py_DECREF(so);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
797 si->si_set = NULL;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
798 return NULL;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
799 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
800
35296
d91964f5fe0d Fix build on gcc: PySetIter_Type should be static in definition
Hye-Shik Chang <hyeshik@gmail.com>
parents: 35295
diff changeset
801 static PyTypeObject PySetIter_Type = {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
802 PyObject_HEAD_INIT(&PyType_Type)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
803 0, /* ob_size */
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
804 "setiterator", /* tp_name */
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
805 sizeof(setiterobject), /* tp_basicsize */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
806 0, /* tp_itemsize */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
807 /* methods */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
808 (destructor)setiter_dealloc, /* tp_dealloc */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
809 0, /* tp_print */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
810 0, /* tp_getattr */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
811 0, /* tp_setattr */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
812 0, /* tp_compare */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
813 0, /* tp_repr */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
814 0, /* tp_as_number */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35581
diff changeset
815 0, /* tp_as_sequence */
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
816 0, /* tp_as_mapping */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
817 0, /* tp_hash */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
818 0, /* tp_call */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
819 0, /* tp_str */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
820 PyObject_GenericGetAttr, /* tp_getattro */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
821 0, /* tp_setattro */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
822 0, /* tp_as_buffer */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
823 Py_TPFLAGS_DEFAULT, /* tp_flags */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
824 0, /* tp_doc */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
825 0, /* tp_traverse */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
826 0, /* tp_clear */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
827 0, /* tp_richcompare */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
828 0, /* tp_weaklistoffset */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
829 PyObject_SelfIter, /* tp_iter */
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
830 (iternextfunc)setiter_iternext, /* tp_iternext */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35581
diff changeset
831 setiter_methods, /* tp_methods */
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35581
diff changeset
832 0,
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
833 };
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
834
37602
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37315
diff changeset
835 static PyObject *
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37315
diff changeset
836 set_iter(PySetObject *so)
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37315
diff changeset
837 {
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37315
diff changeset
838 setiterobject *si = PyObject_New(setiterobject, &PySetIter_Type);
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37315
diff changeset
839 if (si == NULL)
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37315
diff changeset
840 return NULL;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37315
diff changeset
841 Py_INCREF(so);
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37315
diff changeset
842 si->si_set = so;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37315
diff changeset
843 si->si_used = so->used;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37315
diff changeset
844 si->si_pos = 0;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37315
diff changeset
845 si->len = so->used;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37315
diff changeset
846 return (PyObject *)si;
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37315
diff changeset
847 }
ca95ad5b0fbc Remove "static forward" declaration. Move constructors
Martin v. Löwis <martin@v.loewis.de>
parents: 37315
diff changeset
848
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
849 static int
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
850 set_update_internal(PySetObject *so, PyObject *other)
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
851 {
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
852 PyObject *key, *it;
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
853
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
854 if (PyAnySet_Check(other))
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
855 return set_merge(so, other);
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
856
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
857 if (PyDict_Check(other)) {
36244
e7880f4f53a4 Remove some shadowed variables
Neal Norwitz <nnorwitz@gmail.com>
parents: 35862
diff changeset
858 PyObject *value;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
859 Py_ssize_t pos = 0;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
860 while (PyDict_Next(other, &pos, &key, &value)) {
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
861 if (set_add_key(so, key) == -1)
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
862 return -1;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
863 }
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
864 return 0;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
865 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
866
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
867 it = PyObject_GetIter(other);
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
868 if (it == NULL)
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
869 return -1;
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
870
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
871 while ((key = PyIter_Next(it)) != NULL) {
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
872 if (set_add_key(so, key) == -1) {
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
873 Py_DECREF(it);
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
874 Py_DECREF(key);
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
875 return -1;
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
876 }
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
877 Py_DECREF(key);
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
878 }
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
879 Py_DECREF(it);
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
880 if (PyErr_Occurred())
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
881 return -1;
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
882 return 0;
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
883 }
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
884
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
885 static PyObject *
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
886 set_update(PySetObject *so, PyObject *other)
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
887 {
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
888 if (set_update_internal(so, other) == -1)
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
889 return NULL;
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
890 Py_RETURN_NONE;
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
891 }
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
892
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
893 PyDoc_STRVAR(update_doc,
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
894 "Update a set with the union of itself and another.");
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
895
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
896 static PyObject *
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
897 make_new_set(PyTypeObject *type, PyObject *iterable)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
898 {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
899 register PySetObject *so = NULL;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
900
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
901 if (dummy == NULL) { /* Auto-initialize dummy */
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
902 dummy = PyString_FromString("<dummy key>");
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
903 if (dummy == NULL)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
904 return NULL;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
905 }
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
906
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
907 /* create PySetObject structure */
35321
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
908 if (num_free_sets &&
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
909 (type == &PySet_Type || type == &PyFrozenSet_Type)) {
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
910 so = free_sets[--num_free_sets];
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
911 assert (so != NULL && PyAnySet_CheckExact(so));
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
912 so->ob_type = type;
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
913 _Py_NewReference((PyObject *)so);
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
914 EMPTY_TO_MINSIZE(so);
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
915 PyObject_GC_Track(so);
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
916 } else {
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
917 so = (PySetObject *)type->tp_alloc(type, 0);
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
918 if (so == NULL)
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
919 return NULL;
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
920 /* tp_alloc has already zeroed the structure */
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
921 assert(so->table == NULL && so->fill == 0 && so->used == 0);
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
922 INIT_NONZERO_SET_SLOTS(so);
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
923 }
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
924
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
925 so->lookup = set_lookkey_string;
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31628
diff changeset
926 so->weakreflist = NULL;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
927
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
928 if (iterable != NULL) {
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
929 if (set_update_internal(so, iterable) == -1) {
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
930 Py_DECREF(so);
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
931 return NULL;
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
932 }
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
933 }
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
934
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
935 return (PyObject *)so;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
936 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
937
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
938 /* The empty frozenset is a singleton */
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
939 static PyObject *emptyfrozenset = NULL;
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
940
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
941 static PyObject *
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
942 frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
943 {
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
944 PyObject *iterable = NULL, *result;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
945
35449
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 35398
diff changeset
946 if (!_PyArg_NoKeywords("frozenset()", kwds))
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 35398
diff changeset
947 return NULL;
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 35398
diff changeset
948
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
949 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
950 return NULL;
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
951
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
952 if (type != &PyFrozenSet_Type)
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
953 return make_new_set(type, iterable);
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
954
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
955 if (iterable != NULL) {
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
956 /* frozenset(f) is idempotent */
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
957 if (PyFrozenSet_CheckExact(iterable)) {
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
958 Py_INCREF(iterable);
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
959 return iterable;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
960 }
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
961 result = make_new_set(type, iterable);
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
962 if (result == NULL || PySet_GET_SIZE(result))
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
963 return result;
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
964 Py_DECREF(result);
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
965 }
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
966 /* The empty frozenset is a singleton */
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
967 if (emptyfrozenset == NULL)
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
968 emptyfrozenset = make_new_set(type, NULL);
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
969 Py_XINCREF(emptyfrozenset);
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
970 return emptyfrozenset;
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
971 }
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
972
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
973 void
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
974 PySet_Fini(void)
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
975 {
35321
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
976 PySetObject *so;
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
977
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
978 while (num_free_sets) {
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
979 num_free_sets--;
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
980 so = free_sets[num_free_sets];
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
981 PyObject_GC_Del(so);
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
982 }
37747
a5902e0ec07c Clear dummy and emptyfrozenset, so that we don't have
Martin v. Löwis <martin@v.loewis.de>
parents: 37638
diff changeset
983 Py_CLEAR(dummy);
a5902e0ec07c Clear dummy and emptyfrozenset, so that we don't have
Martin v. Löwis <martin@v.loewis.de>
parents: 37638
diff changeset
984 Py_CLEAR(emptyfrozenset);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
985 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
986
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
987 static PyObject *
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
988 set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
989 {
35449
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 35398
diff changeset
990 if (!_PyArg_NoKeywords("set()", kwds))
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 35398
diff changeset
991 return NULL;
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 35398
diff changeset
992
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
993 return make_new_set(type, NULL);
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
994 }
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
995
35293
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
996 /* set_swap_bodies() switches the contents of any two sets by moving their
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
997 internal data pointers and, if needed, copying the internal smalltables.
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
998 Semantically equivalent to:
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
999
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
1000 t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
1001
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
1002 The function always succeeds and it leaves both objects in a stable state.
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
1003 Useful for creating temporary frozensets from sets for membership testing
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
1004 in __contains__(), discard(), and remove(). Also useful for operations
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
1005 that update in-place (by allowing an intermediate result to be swapped
35294
3f1b48f9ff0d Fix frozenset() ref count and a comment typo.
Raymond Hettinger <python@rcn.com>
parents: 35293
diff changeset
1006 into one of the original inputs).
35293
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
1007 */
8fffb4c95487 Comment on the set_swap_bodies() helper function.
Raymond Hettinger <python@rcn.com>
parents: 35292
diff changeset
1008
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1009 static void
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1010 set_swap_bodies(PySetObject *a, PySetObject *b)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1011 {
38872
805f41881e27 Use Py_ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37747
diff changeset
1012 Py_ssize_t t;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1013 setentry *u;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1014 setentry *(*f)(PySetObject *so, PyObject *key, long hash);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1015 setentry tab[PySet_MINSIZE];
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1016 long h;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1017
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1018 t = a->fill; a->fill = b->fill; b->fill = t;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1019 t = a->used; a->used = b->used; b->used = t;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1020 t = a->mask; a->mask = b->mask; b->mask = t;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1021
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1022 u = a->table;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1023 if (a->table == a->smalltable)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1024 u = b->smalltable;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1025 a->table = b->table;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1026 if (b->table == b->smalltable)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1027 a->table = a->smalltable;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1028 b->table = u;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1029
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1030 f = a->lookup; a->lookup = b->lookup; b->lookup = f;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1031
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1032 if (a->table == a->smalltable || b->table == b->smalltable) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1033 memcpy(tab, a->smalltable, sizeof(tab));
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1034 memcpy(a->smalltable, b->smalltable, sizeof(tab));
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1035 memcpy(b->smalltable, tab, sizeof(tab));
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1036 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1037
35312
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
1038 if (PyType_IsSubtype(a->ob_type, &PyFrozenSet_Type) &&
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
1039 PyType_IsSubtype(b->ob_type, &PyFrozenSet_Type)) {
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
1040 h = a->hash; a->hash = b->hash; b->hash = h;
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
1041 } else {
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
1042 a->hash = -1;
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
1043 b->hash = -1;
8aa9bd80286b * Improve a variable name: entry0 --> table.
Raymond Hettinger <python@rcn.com>
parents: 35311
diff changeset
1044 }
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1045 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1046
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
1047 static PyObject *
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1048 set_copy(PySetObject *so)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1049 {
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
1050 return make_new_set(so->ob_type, (PyObject *)so);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1051 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1052
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1053 static PyObject *
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1054 frozenset_copy(PySetObject *so)
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1055 {
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1056 if (PyFrozenSet_CheckExact(so)) {
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1057 Py_INCREF(so);
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1058 return (PyObject *)so;
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1059 }
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1060 return set_copy(so);
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1061 }
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1062
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1063 PyDoc_STRVAR(copy_doc, "Return a shallow copy of a set.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1064
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1065 static PyObject *
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1066 set_clear(PySetObject *so)
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1067 {
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1068 set_clear_internal(so);
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1069 Py_RETURN_NONE;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1070 }
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1071
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1072 PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1073
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1074 static PyObject *
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1075 set_union(PySetObject *so, PyObject *other)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1076 {
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1077 PySetObject *result;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1078
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1079 result = (PySetObject *)set_copy(so);
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1080 if (result == NULL)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1081 return NULL;
35353
e8806094d644 Add shortcuts for a|a and a&a.
Raymond Hettinger <python@rcn.com>
parents: 35349
diff changeset
1082 if ((PyObject *)so == other)
e8806094d644 Add shortcuts for a|a and a&a.
Raymond Hettinger <python@rcn.com>
parents: 35349
diff changeset
1083 return (PyObject *)result;
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1084 if (set_update_internal(result, other) == -1) {
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1085 Py_DECREF(result);
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1086 return NULL;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1087 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1088 return (PyObject *)result;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1089 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1090
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1091 PyDoc_STRVAR(union_doc,
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1092 "Return the union of two sets as a new set.\n\
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1093 \n\
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1094 (i.e. all elements that are in either set.)");
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1095
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1096 static PyObject *
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1097 set_or(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1098 {
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1099 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1100 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1101 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1102 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1103 return set_union(so, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1104 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1105
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1106 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1107 set_ior(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1108 {
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1109 if (!PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1110 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1111 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1112 }
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1113 if (set_update_internal(so, other) == -1)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1114 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1115 Py_INCREF(so);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1116 return (PyObject *)so;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1117 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1118
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1119 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1120 set_intersection(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1121 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1122 PySetObject *result;
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1123 PyObject *key, *it, *tmp;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1124
35353
e8806094d644 Add shortcuts for a|a and a&a.
Raymond Hettinger <python@rcn.com>
parents: 35349
diff changeset
1125 if ((PyObject *)so == other)
e8806094d644 Add shortcuts for a|a and a&a.
Raymond Hettinger <python@rcn.com>
parents: 35349
diff changeset
1126 return set_copy(so);
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1127
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1128 result = (PySetObject *)make_new_set(so->ob_type, NULL);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1129 if (result == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1130 return NULL;
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1131
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1132 if (PyAnySet_Check(other)) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1133 Py_ssize_t pos = 0;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1134 setentry *entry;
35332
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1135
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1136 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
35332
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1137 tmp = (PyObject *)so;
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1138 so = (PySetObject *)other;
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1139 other = tmp;
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1140 }
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1141
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1142 while (set_next((PySetObject *)other, &pos, &entry)) {
39806
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1143 int rv = set_contains_entry(so, entry);
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1144 if (rv == -1) {
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1145 Py_DECREF(result);
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1146 return NULL;
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1147 }
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1148 if (rv) {
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1149 if (set_add_entry(result, entry) == -1) {
31300
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
1150 Py_DECREF(result);
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
1151 return NULL;
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
1152 }
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
1153 }
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
1154 }
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
1155 return (PyObject *)result;
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1156 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1157
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1158 it = PyObject_GetIter(other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1159 if (it == NULL) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1160 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1161 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1162 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1163
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1164 while ((key = PyIter_Next(it)) != NULL) {
39806
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1165 int rv = set_contains_key(so, key);
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1166 if (rv == -1) {
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1167 Py_DECREF(it);
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1168 Py_DECREF(result);
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1169 Py_DECREF(key);
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1170 return NULL;
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1171 }
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1172 if (rv) {
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1173 if (set_add_key(result, key) == -1) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1174 Py_DECREF(it);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1175 Py_DECREF(result);
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1176 Py_DECREF(key);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1177 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1178 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1179 }
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1180 Py_DECREF(key);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1181 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1182 Py_DECREF(it);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1183 if (PyErr_Occurred()) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1184 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1185 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1186 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1187 return (PyObject *)result;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1188 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1189
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1190 PyDoc_STRVAR(intersection_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1191 "Return the intersection of two sets as a new set.\n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1192 \n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1193 (i.e. all elements that are in both sets.)");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1194
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1195 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1196 set_intersection_update(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1197 {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1198 PyObject *tmp;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1199
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1200 tmp = set_intersection(so, other);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1201 if (tmp == NULL)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1202 return NULL;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1203 set_swap_bodies(so, (PySetObject *)tmp);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1204 Py_DECREF(tmp);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1205 Py_RETURN_NONE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1206 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1207
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1208 PyDoc_STRVAR(intersection_update_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1209 "Update a set with the intersection of itself and another.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1210
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1211 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1212 set_and(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1213 {
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1214 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1215 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1216 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1217 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1218 return set_intersection(so, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1219 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1220
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1221 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1222 set_iand(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1223 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1224 PyObject *result;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1225
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1226 if (!PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1227 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1228 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1229 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1230 result = set_intersection_update(so, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1231 if (result == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1232 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1233 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1234 Py_INCREF(so);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1235 return (PyObject *)so;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1236 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1237
35862
6867995a18a7 Prevent name pollution by making lots of internal functions static.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35636
diff changeset
1238 static int
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1239 set_difference_update_internal(PySetObject *so, PyObject *other)
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1240 {
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1241 if ((PyObject *)so == other)
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1242 return set_clear_internal(so);
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1243
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1244 if (PyAnySet_Check(other)) {
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1245 setentry *entry;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1246 Py_ssize_t pos = 0;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1247
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1248 while (set_next((PySetObject *)other, &pos, &entry))
39806
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1249 if (set_discard_entry(so, entry) == -1)
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1250 return -1;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1251 } else {
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1252 PyObject *key, *it;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1253 it = PyObject_GetIter(other);
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1254 if (it == NULL)
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1255 return -1;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1256
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1257 while ((key = PyIter_Next(it)) != NULL) {
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1258 if (set_discard_key(so, key) == -1) {
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1259 Py_DECREF(it);
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1260 Py_DECREF(key);
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1261 return -1;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1262 }
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1263 Py_DECREF(key);
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1264 }
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1265 Py_DECREF(it);
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1266 if (PyErr_Occurred())
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1267 return -1;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1268 }
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1269 /* If more than 1/5 are dummies, then resize them away. */
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1270 if ((so->fill - so->used) * 5 < so->mask)
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1271 return 0;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1272 return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1273 }
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1274
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1275 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1276 set_difference_update(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1277 {
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1278 if (set_difference_update_internal(so, other) != -1)
35321
bdcb01a9b60c * Bring in INIT_NONZERO_SET_SLOTS macro from dictionary code.
Raymond Hettinger <python@rcn.com>
parents: 35317
diff changeset
1279 Py_RETURN_NONE;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1280 return NULL;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1281 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1282
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1283 PyDoc_STRVAR(difference_update_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1284 "Remove all elements of another set from this set.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1285
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1286 static PyObject *
31191
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1287 set_difference(PySetObject *so, PyObject *other)
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1288 {
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1289 PyObject *result;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1290 setentry *entry;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1291 Py_ssize_t pos = 0;
31191
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1292
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1293 if (!PyAnySet_Check(other) && !PyDict_Check(other)) {
31191
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1294 result = set_copy(so);
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1295 if (result == NULL)
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1296 return NULL;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1297 if (set_difference_update_internal((PySetObject *)result, other) != -1)
31191
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1298 return result;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1299 Py_DECREF(result);
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1300 return NULL;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1301 }
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1302
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1303 result = make_new_set(so->ob_type, NULL);
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1304 if (result == NULL)
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1305 return NULL;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1306
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1307 if (PyDict_Check(other)) {
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1308 while (set_next(so, &pos, &entry)) {
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1309 setentry entrycopy;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1310 entrycopy.hash = entry->hash;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1311 entrycopy.key = entry->key;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1312 if (!PyDict_Contains(other, entry->key)) {
39806
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1313 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1314 Py_DECREF(result);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1315 return NULL;
39806
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1316 }
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1317 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1318 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1319 return result;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1320 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1321
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1322 while (set_next(so, &pos, &entry)) {
39806
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1323 int rv = set_contains_entry((PySetObject *)other, entry);
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1324 if (rv == -1) {
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1325 Py_DECREF(result);
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1326 return NULL;
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1327 }
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1328 if (!rv) {
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1329 if (set_add_entry((PySetObject *)result, entry) == -1) {
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1330 Py_DECREF(result);
31191
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1331 return NULL;
39806
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1332 }
31191
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1333 }
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1334 }
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1335 return result;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1336 }
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1337
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1338 PyDoc_STRVAR(difference_doc,
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1339 "Return the difference of two sets as a new set.\n\
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1340 \n\
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1341 (i.e. all elements that are in this set but not the other.)");
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
1342 static PyObject *
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1343 set_sub(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1344 {
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1345 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1346 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1347 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1348 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1349 return set_difference(so, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1350 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1351
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1352 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1353 set_isub(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1354 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1355 PyObject *result;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1356
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1357 if (!PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1358 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1359 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1360 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1361 result = set_difference_update(so, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1362 if (result == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1363 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1364 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1365 Py_INCREF(so);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1366 return (PyObject *)so;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1367 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1368
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1369 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1370 set_symmetric_difference_update(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1371 {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1372 PySetObject *otherset;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1373 PyObject *key;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1374 Py_ssize_t pos = 0;
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1375 setentry *entry;
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1376
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1377 if ((PyObject *)so == other)
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1378 return set_clear(so);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1379
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1380 if (PyDict_Check(other)) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1381 PyObject *value;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1382 int rv;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1383 while (PyDict_Next(other, &pos, &key, &value)) {
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1384 rv = set_discard_key(so, key);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1385 if (rv == -1)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1386 return NULL;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1387 if (rv == DISCARD_NOTFOUND) {
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1388 if (set_add_key(so, key) == -1)
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1389 return NULL;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1390 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1391 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1392 Py_RETURN_NONE;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1393 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1394
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1395 if (PyAnySet_Check(other)) {
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1396 Py_INCREF(other);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1397 otherset = (PySetObject *)other;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1398 } else {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1399 otherset = (PySetObject *)make_new_set(so->ob_type, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1400 if (otherset == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1401 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1402 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1403
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1404 while (set_next(otherset, &pos, &entry)) {
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1405 int rv = set_discard_entry(so, entry);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1406 if (rv == -1) {
39104
6064cb301209 otherset is known to be non-NULL based on checks before and DECREF after.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38872
diff changeset
1407 Py_DECREF(otherset);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1408 return NULL;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1409 }
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1410 if (rv == DISCARD_NOTFOUND) {
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1411 if (set_add_entry(so, entry) == -1) {
39104
6064cb301209 otherset is known to be non-NULL based on checks before and DECREF after.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38872
diff changeset
1412 Py_DECREF(otherset);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1413 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1414 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1415 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1416 }
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1417 Py_DECREF(otherset);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1418 Py_RETURN_NONE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1419 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1420
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1421 PyDoc_STRVAR(symmetric_difference_update_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1422 "Update a set with the symmetric difference of itself and another.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1423
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1424 static PyObject *
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1425 set_symmetric_difference(PySetObject *so, PyObject *other)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1426 {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1427 PyObject *rv;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1428 PySetObject *otherset;
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1429
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1430 otherset = (PySetObject *)make_new_set(so->ob_type, other);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1431 if (otherset == NULL)
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1432 return NULL;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1433 rv = set_symmetric_difference_update(otherset, (PyObject *)so);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1434 if (rv == NULL)
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1435 return NULL;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1436 Py_DECREF(rv);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1437 return (PyObject *)otherset;
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1438 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1439
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1440 PyDoc_STRVAR(symmetric_difference_doc,
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1441 "Return the symmetric difference of two sets as a new set.\n\
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1442 \n\
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1443 (i.e. all elements that are in exactly one of the sets.)");
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1444
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
1445 static PyObject *
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1446 set_xor(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1447 {
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1448 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1449 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1450 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1451 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1452 return set_symmetric_difference(so, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1453 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1454
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1455 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1456 set_ixor(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1457 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1458 PyObject *result;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1459
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1460 if (!PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1461 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1462 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1463 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1464 result = set_symmetric_difference_update(so, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1465 if (result == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1466 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1467 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1468 Py_INCREF(so);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1469 return (PyObject *)so;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1470 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1471
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1472 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1473 set_issubset(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1474 {
35339
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
1475 setentry *entry;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1476 Py_ssize_t pos = 0;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1477
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1478 if (!PyAnySet_Check(other)) {
35339
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
1479 PyObject *tmp, *result;
30994
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1480 tmp = make_new_set(&PySet_Type, other);
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1481 if (tmp == NULL)
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1482 return NULL;
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1483 result = set_issubset(so, tmp);
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1484 Py_DECREF(tmp);
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1485 return result;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1486 }
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1487 if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1488 Py_RETURN_FALSE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1489
35339
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
1490 while (set_next(so, &pos, &entry)) {
39806
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1491 int rv = set_contains_entry((PySetObject *)other, entry);
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1492 if (rv == -1)
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1493 return NULL;
891e332cd2d6 Backport rev. 51798 from trunk:
Georg Brandl <georg@python.org>
parents: 39268
diff changeset
1494 if (!rv)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1495 Py_RETURN_FALSE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1496 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1497 Py_RETURN_TRUE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1498 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1499
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1500 PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1501
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1502 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1503 set_issuperset(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1504 {
30994
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1505 PyObject *tmp, *result;
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1506
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1507 if (!PyAnySet_Check(other)) {
30994
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1508 tmp = make_new_set(&PySet_Type, other);
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1509 if (tmp == NULL)
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1510 return NULL;
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1511 result = set_issuperset(so, tmp);
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1512 Py_DECREF(tmp);
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
1513 return result;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1514 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1515 return set_issubset((PySetObject *)other, (PyObject *)so);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1516 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1517
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1518 PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1519
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1520 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1521 set_richcompare(PySetObject *v, PyObject *w, int op)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1522 {
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1523 PyObject *r1, *r2;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1524
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1525 if(!PyAnySet_Check(w)) {
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1526 if (op == Py_EQ)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1527 Py_RETURN_FALSE;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1528 if (op == Py_NE)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1529 Py_RETURN_TRUE;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1530 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1531 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1532 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1533 switch (op) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1534 case Py_EQ:
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1535 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1536 Py_RETURN_FALSE;
35398
1708479ade4d * Add a fast equality check path for frozensets where the hash value has
Raymond Hettinger <python@rcn.com>
parents: 35353
diff changeset
1537 if (v->hash != -1 &&
1708479ade4d * Add a fast equality check path for frozensets where the hash value has
Raymond Hettinger <python@rcn.com>
parents: 35353
diff changeset
1538 ((PySetObject *)w)->hash != -1 &&
1708479ade4d * Add a fast equality check path for frozensets where the hash value has
Raymond Hettinger <python@rcn.com>
parents: 35353
diff changeset
1539 v->hash != ((PySetObject *)w)->hash)
1708479ade4d * Add a fast equality check path for frozensets where the hash value has
Raymond Hettinger <python@rcn.com>
parents: 35353
diff changeset
1540 Py_RETURN_FALSE;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1541 return set_issubset(v, w);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1542 case Py_NE:
35398
1708479ade4d * Add a fast equality check path for frozensets where the hash value has
Raymond Hettinger <python@rcn.com>
parents: 35353
diff changeset
1543 r1 = set_richcompare(v, w, Py_EQ);
1708479ade4d * Add a fast equality check path for frozensets where the hash value has
Raymond Hettinger <python@rcn.com>
parents: 35353
diff changeset
1544 if (r1 == NULL)
1708479ade4d * Add a fast equality check path for frozensets where the hash value has
Raymond Hettinger <python@rcn.com>
parents: 35353
diff changeset
1545 return NULL;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1546 r2 = PyBool_FromLong(PyObject_Not(r1));
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1547 Py_DECREF(r1);
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1548 return r2;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1549 case Py_LE:
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1550 return set_issubset(v, w);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1551 case Py_GE:
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1552 return set_issuperset(v, w);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1553 case Py_LT:
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1554 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1555 Py_RETURN_FALSE;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1556 return set_issubset(v, w);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1557 case Py_GT:
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1558 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1559 Py_RETURN_FALSE;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1560 return set_issuperset(v, w);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1561 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1562 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1563 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1564 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1565
35339
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
1566 static int
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37219
diff changeset
1567 set_nocmp(PyObject *self, PyObject *other)
35339
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
1568 {
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
1569 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
1570 return -1;
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
1571 }
028e94a6efc5 * Bring lookkey() and lookkey_string() closer to dict version.
Raymond Hettinger <python@rcn.com>
parents: 35332
diff changeset
1572
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1573 static PyObject *
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1574 set_add(PySetObject *so, PyObject *key)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1575 {
35328
0ef64a93f3f2 * Add short-circuit code for in-place operations with self (such as
Raymond Hettinger <python@rcn.com>
parents: 35321
diff changeset
1576 if (set_add_key(so, key) == -1)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1577 return NULL;
31178
1bbc2085f213 * Refactor set.__contains__()
Raymond Hettinger <python@rcn.com>
parents: 31177
diff changeset
1578 Py_RETURN_NONE;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1579 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1580
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1581 PyDoc_STRVAR(add_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1582 "Add an element to a set.\n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1583 \n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1584 This has no effect if the element is already present.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1585
35341
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1586 static int
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1587 set_contains(PySetObject *so, PyObject *key)
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1588 {
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1589 PyObject *tmpkey;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1590 int rv;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1591
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1592 rv = set_contains_key(so, key);
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1593 if (rv == -1) {
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1594 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1595 return -1;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1596 PyErr_Clear();
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1597 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1598 if (tmpkey == NULL)
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1599 return -1;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1600 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1601 rv = set_contains(so, tmpkey);
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1602 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1603 Py_DECREF(tmpkey);
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1604 }
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1605 return rv;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1606 }
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1607
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1608 static PyObject *
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1609 set_direct_contains(PySetObject *so, PyObject *key)
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1610 {
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1611 long result;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1612
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1613 result = set_contains(so, key);
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1614 if (result == -1)
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1615 return NULL;
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1616 return PyBool_FromLong(result);
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1617 }
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1618
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1619 PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
f6b462d2a3a3 More function re-ordering (placing like functions together).
Raymond Hettinger <python@rcn.com>
parents: 35339
diff changeset
1620
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1621 static PyObject *
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1622 set_remove(PySetObject *so, PyObject *key)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1623 {
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1624 PyObject *tmpkey, *result;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1625 int rv;
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
1626
35332
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1627 rv = set_discard_key(so, key);
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1628 if (rv == -1) {
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1629 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1630 return NULL;
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1631 PyErr_Clear();
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1632 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1633 if (tmpkey == NULL)
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
1634 return NULL;
35332
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1635 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1636 result = set_remove(so, tmpkey);
35332
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1637 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1638 Py_DECREF(tmpkey);
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
1639 return result;
35332
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1640 } else if (rv == DISCARD_NOTFOUND) {
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1641 PyErr_SetObject(PyExc_KeyError, key);
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1642 return NULL;
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1643 }
31178
1bbc2085f213 * Refactor set.__contains__()
Raymond Hettinger <python@rcn.com>
parents: 31177
diff changeset
1644 Py_RETURN_NONE;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1645 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1646
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1647 PyDoc_STRVAR(remove_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1648 "Remove an element from a set; it must be a member.\n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1649 \n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1650 If the element is not a member, raise a KeyError.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1651
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1652 static PyObject *
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1653 set_discard(PySetObject *so, PyObject *key)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1654 {
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1655 PyObject *tmpkey, *result;
35332
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1656 int rv;
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
1657
35332
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1658 rv = set_discard_key(so, key);
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1659 if (rv == -1) {
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1660 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1661 return NULL;
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1662 PyErr_Clear();
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1663 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1664 if (tmpkey == NULL)
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
1665 return NULL;
35332
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1666 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1667 result = set_discard(so, tmpkey);
35332
fd02b84fb2db * Fix SF #1257731. Make __contains__(), remove(), and discard() only do
Raymond Hettinger <python@rcn.com>
parents: 35328
diff changeset
1668 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
35295
f30ebdc3bddf Improve variable names.
Raymond Hettinger <python@rcn.com>
parents: 35294
diff changeset
1669 Py_DECREF(tmpkey);
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
1670 return result;
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
1671 }
31178
1bbc2085f213 * Refactor set.__contains__()
Raymond Hettinger <python@rcn.com>
parents: 31177
diff changeset
1672 Py_RETURN_NONE;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1673 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1674
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1675 PyDoc_STRVAR(discard_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1676 "Remove an element from a set if it is a member.\n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1677 \n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1678 If the element is not a member, do nothing.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1679
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1680 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1681 set_reduce(PySetObject *so)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1682 {
33948
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
1683 PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1684
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1685 keys = PySequence_List((PyObject *)so);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1686 if (keys == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1687 goto done;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1688 args = PyTuple_Pack(1, keys);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1689 if (args == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1690 goto done;
33948
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
1691 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
1692 if (dict == NULL) {
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
1693 PyErr_Clear();
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
1694 dict = Py_None;
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
1695 Py_INCREF(dict);
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
1696 }
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
1697 result = PyTuple_Pack(3, so->ob_type, args, dict);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1698 done:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1699 Py_XDECREF(args);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1700 Py_XDECREF(keys);
33948
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
1701 Py_XDECREF(dict);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1702 return result;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1703 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1704
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1705 PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1706
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1707 static int
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1708 set_init(PySetObject *self, PyObject *args, PyObject *kwds)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1709 {
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1710 PyObject *iterable = NULL;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1711
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1712 if (!PyAnySet_Check(self))
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1713 return -1;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1714 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1715 return -1;
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1716 set_clear_internal(self);
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1717 self->hash = -1;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1718 if (iterable == NULL)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1719 return 0;
35297
26bf6c27e9ce * Improve code for the empty frozenset singleton:
Raymond Hettinger <python@rcn.com>
parents: 35296
diff changeset
1720 return set_update_internal(self, iterable);
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1721 }
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1722
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1723 static PySequenceMethods set_as_sequence = {
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37219
diff changeset
1724 set_len, /* sq_length */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1725 0, /* sq_concat */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1726 0, /* sq_repeat */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1727 0, /* sq_item */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1728 0, /* sq_slice */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1729 0, /* sq_ass_item */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1730 0, /* sq_ass_slice */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1731 (objobjproc)set_contains, /* sq_contains */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1732 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1733
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1734 /* set object ********************************************************/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1735
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1736 #ifdef Py_DEBUG
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1737 static PyObject *test_c_api(PySetObject *so);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1738
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1739 PyDoc_STRVAR(test_c_api_doc, "Exercises C API. Returns True.\n\
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1740 All is well if assertions don't fail.");
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1741 #endif
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1742
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1743 static PyMethodDef set_methods[] = {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1744 {"add", (PyCFunction)set_add, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1745 add_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1746 {"clear", (PyCFunction)set_clear, METH_NOARGS,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1747 clear_doc},
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
1748 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
1749 contains_doc},
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1750 {"copy", (PyCFunction)set_copy, METH_NOARGS,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1751 copy_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1752 {"discard", (PyCFunction)set_discard, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1753 discard_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1754 {"difference", (PyCFunction)set_difference, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1755 difference_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1756 {"difference_update", (PyCFunction)set_difference_update, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1757 difference_update_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1758 {"intersection",(PyCFunction)set_intersection, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1759 intersection_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1760 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1761 intersection_update_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1762 {"issubset", (PyCFunction)set_issubset, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1763 issubset_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1764 {"issuperset", (PyCFunction)set_issuperset, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1765 issuperset_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1766 {"pop", (PyCFunction)set_pop, METH_NOARGS,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1767 pop_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1768 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1769 reduce_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1770 {"remove", (PyCFunction)set_remove, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1771 remove_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1772 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1773 symmetric_difference_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1774 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1775 symmetric_difference_update_doc},
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1776 #ifdef Py_DEBUG
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1777 {"test_c_api", (PyCFunction)test_c_api, METH_NOARGS,
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1778 test_c_api_doc},
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1779 #endif
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1780 {"union", (PyCFunction)set_union, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1781 union_doc},
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
1782 {"update", (PyCFunction)set_update, METH_O,
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
1783 update_doc},
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1784 {NULL, NULL} /* sentinel */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1785 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1786
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1787 static PyNumberMethods set_as_number = {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1788 0, /*nb_add*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1789 (binaryfunc)set_sub, /*nb_subtract*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1790 0, /*nb_multiply*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1791 0, /*nb_divide*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1792 0, /*nb_remainder*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1793 0, /*nb_divmod*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1794 0, /*nb_power*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1795 0, /*nb_negative*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1796 0, /*nb_positive*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1797 0, /*nb_absolute*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1798 0, /*nb_nonzero*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1799 0, /*nb_invert*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1800 0, /*nb_lshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1801 0, /*nb_rshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1802 (binaryfunc)set_and, /*nb_and*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1803 (binaryfunc)set_xor, /*nb_xor*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1804 (binaryfunc)set_or, /*nb_or*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1805 0, /*nb_coerce*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1806 0, /*nb_int*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1807 0, /*nb_long*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1808 0, /*nb_float*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1809 0, /*nb_oct*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1810 0, /*nb_hex*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1811 0, /*nb_inplace_add*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1812 (binaryfunc)set_isub, /*nb_inplace_subtract*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1813 0, /*nb_inplace_multiply*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1814 0, /*nb_inplace_divide*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1815 0, /*nb_inplace_remainder*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1816 0, /*nb_inplace_power*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1817 0, /*nb_inplace_lshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1818 0, /*nb_inplace_rshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1819 (binaryfunc)set_iand, /*nb_inplace_and*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1820 (binaryfunc)set_ixor, /*nb_inplace_xor*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1821 (binaryfunc)set_ior, /*nb_inplace_or*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1822 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1823
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1824 PyDoc_STRVAR(set_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1825 "set(iterable) --> set object\n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1826 \n\
39268
2f3d871bf6ef [Bug #1414697] Change docstring of set/frozenset types to specify that the contents are unique. Raymond, please feel free to edit or revert.
Andrew M. Kuchling <amk@amk.ca>
parents: 39104
diff changeset
1827 Build an unordered collection of unique elements.");
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1828
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1829 PyTypeObject PySet_Type = {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1830 PyObject_HEAD_INIT(&PyType_Type)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1831 0, /* ob_size */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1832 "set", /* tp_name */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1833 sizeof(PySetObject), /* tp_basicsize */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1834 0, /* tp_itemsize */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1835 /* methods */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1836 (destructor)set_dealloc, /* tp_dealloc */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1837 (printfunc)set_tp_print, /* tp_print */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1838 0, /* tp_getattr */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1839 0, /* tp_setattr */
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37219
diff changeset
1840 set_nocmp, /* tp_compare */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1841 (reprfunc)set_repr, /* tp_repr */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1842 &set_as_number, /* tp_as_number */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1843 &set_as_sequence, /* tp_as_sequence */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1844 0, /* tp_as_mapping */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1845 set_nohash, /* tp_hash */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1846 0, /* tp_call */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1847 0, /* tp_str */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1848 PyObject_GenericGetAttr, /* tp_getattro */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1849 0, /* tp_setattro */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1850 0, /* tp_as_buffer */
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
1851 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1852 Py_TPFLAGS_BASETYPE, /* tp_flags */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1853 set_doc, /* tp_doc */
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
1854 (traverseproc)set_traverse, /* tp_traverse */
35315
a6cbfcd0e691 Factor away a redundant clear() function.
Raymond Hettinger <python@rcn.com>
parents: 35312
diff changeset
1855 (inquiry)set_clear_internal, /* tp_clear */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1856 (richcmpfunc)set_richcompare, /* tp_richcompare */
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31628
diff changeset
1857 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1858 (getiterfunc)set_iter, /* tp_iter */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1859 0, /* tp_iternext */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1860 set_methods, /* tp_methods */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1861 0, /* tp_members */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1862 0, /* tp_getset */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1863 0, /* tp_base */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1864 0, /* tp_dict */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1865 0, /* tp_descr_get */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1866 0, /* tp_descr_set */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1867 0, /* tp_dictoffset */
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1868 (initproc)set_init, /* tp_init */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1869 PyType_GenericAlloc, /* tp_alloc */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1870 set_new, /* tp_new */
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
1871 PyObject_GC_Del, /* tp_free */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1872 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1873
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1874 /* frozenset object ********************************************************/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1875
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1876
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1877 static PyMethodDef frozenset_methods[] = {
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
1878 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
1879 contains_doc},
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1880 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1881 copy_doc},
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1882 {"difference", (PyCFunction)set_difference, METH_O,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1883 difference_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1884 {"intersection",(PyCFunction)set_intersection, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1885 intersection_doc},
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1886 {"issubset", (PyCFunction)set_issubset, METH_O,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1887 issubset_doc},
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1888 {"issuperset", (PyCFunction)set_issuperset, METH_O,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1889 issuperset_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1890 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1891 reduce_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1892 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1893 symmetric_difference_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1894 {"union", (PyCFunction)set_union, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1895 union_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1896 {NULL, NULL} /* sentinel */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1897 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1898
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1899 static PyNumberMethods frozenset_as_number = {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1900 0, /*nb_add*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1901 (binaryfunc)set_sub, /*nb_subtract*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1902 0, /*nb_multiply*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1903 0, /*nb_divide*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1904 0, /*nb_remainder*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1905 0, /*nb_divmod*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1906 0, /*nb_power*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1907 0, /*nb_negative*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1908 0, /*nb_positive*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1909 0, /*nb_absolute*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1910 0, /*nb_nonzero*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1911 0, /*nb_invert*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1912 0, /*nb_lshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1913 0, /*nb_rshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1914 (binaryfunc)set_and, /*nb_and*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1915 (binaryfunc)set_xor, /*nb_xor*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1916 (binaryfunc)set_or, /*nb_or*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1917 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1918
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1919 PyDoc_STRVAR(frozenset_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1920 "frozenset(iterable) --> frozenset object\n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1921 \n\
39268
2f3d871bf6ef [Bug #1414697] Change docstring of set/frozenset types to specify that the contents are unique. Raymond, please feel free to edit or revert.
Andrew M. Kuchling <amk@amk.ca>
parents: 39104
diff changeset
1922 Build an immutable unordered collection of unique elements.");
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1923
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1924 PyTypeObject PyFrozenSet_Type = {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1925 PyObject_HEAD_INIT(&PyType_Type)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1926 0, /* ob_size */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1927 "frozenset", /* tp_name */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1928 sizeof(PySetObject), /* tp_basicsize */
31300
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
1929 0, /* tp_itemsize */
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
1930 /* methods */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1931 (destructor)set_dealloc, /* tp_dealloc */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1932 (printfunc)set_tp_print, /* tp_print */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1933 0, /* tp_getattr */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1934 0, /* tp_setattr */
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37219
diff changeset
1935 set_nocmp, /* tp_compare */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1936 (reprfunc)set_repr, /* tp_repr */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1937 &frozenset_as_number, /* tp_as_number */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1938 &set_as_sequence, /* tp_as_sequence */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1939 0, /* tp_as_mapping */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1940 frozenset_hash, /* tp_hash */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1941 0, /* tp_call */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1942 0, /* tp_str */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1943 PyObject_GenericGetAttr, /* tp_getattro */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1944 0, /* tp_setattro */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1945 0, /* tp_as_buffer */
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
1946 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
35292
35a698302c63 Revised the set() and frozenset() implementaion to use its own internal
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
1947 Py_TPFLAGS_BASETYPE, /* tp_flags */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1948 frozenset_doc, /* tp_doc */
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
1949 (traverseproc)set_traverse, /* tp_traverse */
35315
a6cbfcd0e691 Factor away a redundant clear() function.
Raymond Hettinger <python@rcn.com>
parents: 35312
diff changeset
1950 (inquiry)set_clear_internal, /* tp_clear */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1951 (richcmpfunc)set_richcompare, /* tp_richcompare */
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31628
diff changeset
1952 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1953 (getiterfunc)set_iter, /* tp_iter */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1954 0, /* tp_iternext */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1955 frozenset_methods, /* tp_methods */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1956 0, /* tp_members */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1957 0, /* tp_getset */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1958 0, /* tp_base */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1959 0, /* tp_dict */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1960 0, /* tp_descr_get */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1961 0, /* tp_descr_set */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1962 0, /* tp_dictoffset */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1963 0, /* tp_init */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1964 PyType_GenericAlloc, /* tp_alloc */
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1965 frozenset_new, /* tp_new */
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
1966 PyObject_GC_Del, /* tp_free */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1967 };
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1968
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1969
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1970 /***** C API functions *************************************************/
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1971
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1972 PyObject *
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1973 PySet_New(PyObject *iterable)
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1974 {
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1975 return make_new_set(&PySet_Type, iterable);
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1976 }
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1977
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1978 PyObject *
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1979 PyFrozenSet_New(PyObject *iterable)
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1980 {
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1981 PyObject *args, *result;
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1982
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1983 if (iterable == NULL)
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1984 args = PyTuple_New(0);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1985 else
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
1986 args = PyTuple_Pack(1, iterable);
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1987 if (args == NULL)
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1988 return NULL;
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1989 result = frozenset_new(&PyFrozenSet_Type, args, NULL);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1990 Py_DECREF(args);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1991 return result;
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1992 }
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1993
36833
9e81788b0d4b Use Py_ssize_t for PySet_Size() like all the other Py*_Size() functions.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36501
diff changeset
1994 Py_ssize_t
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1995 PySet_Size(PyObject *anyset)
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1996 {
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1997 if (!PyAnySet_Check(anyset)) {
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1998 PyErr_BadInternalCall();
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
1999 return -1;
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2000 }
35398
1708479ade4d * Add a fast equality check path for frozensets where the hash value has
Raymond Hettinger <python@rcn.com>
parents: 35353
diff changeset
2001 return PySet_GET_SIZE(anyset);
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2002 }
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2003
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2004 int
37315
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2005 PySet_Clear(PyObject *set)
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2006 {
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2007 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2008 PyErr_BadInternalCall();
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2009 return -1;
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2010 }
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2011 return set_clear_internal((PySetObject *)set);
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2012 }
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2013
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2014 int
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2015 PySet_Contains(PyObject *anyset, PyObject *key)
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2016 {
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2017 if (!PyAnySet_Check(anyset)) {
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2018 PyErr_BadInternalCall();
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2019 return -1;
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2020 }
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2021 return set_contains_key((PySetObject *)anyset, key);
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2022 }
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2023
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2024 int
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2025 PySet_Discard(PyObject *set, PyObject *key)
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2026 {
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2027 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2028 PyErr_BadInternalCall();
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2029 return -1;
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2030 }
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2031 return set_discard_key((PySetObject *)set, key);
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2032 }
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2033
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2034 int
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2035 PySet_Add(PyObject *set, PyObject *key)
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2036 {
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2037 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2038 PyErr_BadInternalCall();
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2039 return -1;
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2040 }
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2041 return set_add_key((PySetObject *)set, key);
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2042 }
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2043
37315
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2044 int
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2045 _PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **entry)
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2046 {
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2047 setentry *entry_ptr;
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2048
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2049 if (!PyAnySet_Check(set)) {
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2050 PyErr_BadInternalCall();
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2051 return -1;
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2052 }
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2053 if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2054 return 0;
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2055 *entry = entry_ptr->key;
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2056 return 1;
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2057 }
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2058
35346
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2059 PyObject *
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2060 PySet_Pop(PyObject *set)
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2061 {
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2062 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2063 PyErr_BadInternalCall();
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2064 return NULL;
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2065 }
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2066 return set_pop((PySetObject *)set);
dd2357953fe3 Add a C API for sets and frozensets.
Raymond Hettinger <python@rcn.com>
parents: 35341
diff changeset
2067 }
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2068
37315
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2069 int
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2070 _PySet_Update(PyObject *set, PyObject *iterable)
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2071 {
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2072 if (!PyType_IsSubtype(set->ob_type, &PySet_Type)) {
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2073 PyErr_BadInternalCall();
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2074 return -1;
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2075 }
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2076 return set_update_internal((PySetObject *)set, iterable);
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2077 }
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2078
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2079 #ifdef Py_DEBUG
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2080
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2081 /* Test code to be called with any three element set.
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2082 Returns True and original set is restored. */
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2083
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2084 #define assertRaises(call_return_value, exception) \
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2085 do { \
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2086 assert(call_return_value); \
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2087 assert(PyErr_ExceptionMatches(exception)); \
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2088 PyErr_Clear(); \
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2089 } while(0)
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2090
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2091 static PyObject *
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2092 test_c_api(PySetObject *so)
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2093 {
38872
805f41881e27 Use Py_ssize_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 37747
diff changeset
2094 Py_ssize_t count;
37315
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2095 char *s;
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2096 Py_ssize_t i;
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2097 PyObject *elem, *dup, *t, *f, *dup2;
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2098 PyObject *ob = (PyObject *)so;
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2099
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2100 /* Verify preconditions and exercise type/size checks */
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2101 assert(PyAnySet_Check(ob));
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2102 assert(PyAnySet_CheckExact(ob));
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2103 assert(!PyFrozenSet_CheckExact(ob));
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2104 assert(PySet_Size(ob) == 3);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2105 assert(PySet_GET_SIZE(ob) == 3);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2106
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2107 /* Raise TypeError for non-iterable constructor arguments */
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2108 assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2109 assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2110
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2111 /* Raise TypeError for unhashable key */
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2112 dup = PySet_New(ob);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2113 assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2114 assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2115 assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2116
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2117 /* Exercise successful pop, contains, add, and discard */
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2118 elem = PySet_Pop(ob);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2119 assert(PySet_Contains(ob, elem) == 0);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2120 assert(PySet_GET_SIZE(ob) == 2);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2121 assert(PySet_Add(ob, elem) == 0);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2122 assert(PySet_Contains(ob, elem) == 1);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2123 assert(PySet_GET_SIZE(ob) == 3);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2124 assert(PySet_Discard(ob, elem) == 1);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2125 assert(PySet_GET_SIZE(ob) == 2);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2126 assert(PySet_Discard(ob, elem) == 0);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2127 assert(PySet_GET_SIZE(ob) == 2);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2128
37315
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2129 /* Exercise clear */
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2130 dup2 = PySet_New(dup);
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2131 assert(PySet_Clear(dup2) == 0);
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2132 assert(PySet_Size(dup2) == 0);
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2133 Py_DECREF(dup2);
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2134
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2135 /* Raise SystemError on clear or update of frozen set */
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2136 f = PyFrozenSet_New(dup);
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2137 assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2138 assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2139 Py_DECREF(f);
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2140
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2141 /* Exercise direct iteration */
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2142 i = 0, count = 0;
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2143 while (_PySet_Next((PyObject *)dup, &i, &elem)) {
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2144 s = PyString_AsString(elem);
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2145 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2146 count++;
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2147 }
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2148 assert(count == 3);
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2149
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2150 /* Exercise updates */
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2151 dup2 = PySet_New(NULL);
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2152 assert(_PySet_Update(dup2, dup) == 0);
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2153 assert(PySet_Size(dup2) == 3);
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2154 assert(_PySet_Update(dup2, dup) == 0);
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2155 assert(PySet_Size(dup2) == 3);
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2156 Py_DECREF(dup2);
19059df45cfe SF patch #1458476 with modifications based on discussions in python-dev. This
Barry Warsaw <barry@python.org>
parents: 37304
diff changeset
2157
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2158 /* Raise SystemError when self argument is not a set or frozenset. */
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2159 t = PyTuple_New(0);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2160 assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2161 assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2162 Py_DECREF(t);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2163
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2164 /* Raise SystemError when self argument is not a set. */
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2165 f = PyFrozenSet_New(dup);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2166 assert(PySet_Size(f) == 3);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2167 assert(PyFrozenSet_CheckExact(f));
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2168 assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2169 assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2170 assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2171 Py_DECREF(f);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2172
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2173 /* Raise KeyError when popping from an empty set */
35353
e8806094d644 Add shortcuts for a|a and a&a.
Raymond Hettinger <python@rcn.com>
parents: 35349
diff changeset
2174 assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
e8806094d644 Add shortcuts for a|a and a&a.
Raymond Hettinger <python@rcn.com>
parents: 35349
diff changeset
2175 Py_DECREF(ob);
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2176 assert(PySet_GET_SIZE(ob) == 0);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2177 assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2178
35353
e8806094d644 Add shortcuts for a|a and a&a.
Raymond Hettinger <python@rcn.com>
parents: 35349
diff changeset
2179 /* Restore the set from the copy using the PyNumber API */
e8806094d644 Add shortcuts for a|a and a&a.
Raymond Hettinger <python@rcn.com>
parents: 35349
diff changeset
2180 assert(PyNumber_InPlaceOr(ob, dup) == ob);
e8806094d644 Add shortcuts for a|a and a&a.
Raymond Hettinger <python@rcn.com>
parents: 35349
diff changeset
2181 Py_DECREF(ob);
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2182
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2183 /* Verify constructors accept NULL arguments */
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2184 f = PySet_New(NULL);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2185 assert(f != NULL);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2186 assert(PySet_GET_SIZE(f) == 0);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2187 Py_DECREF(f);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2188 f = PyFrozenSet_New(NULL);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2189 assert(f != NULL);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2190 assert(PyFrozenSet_CheckExact(f));
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2191 assert(PySet_GET_SIZE(f) == 0);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2192 Py_DECREF(f);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2193
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2194 Py_DECREF(elem);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2195 Py_DECREF(dup);
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2196 Py_RETURN_TRUE;
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2197 }
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2198
35581
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
2199 #undef assertRaises
37859c6ad5bb No longer ignore exceptions raised by comparisons during key lookup.
Raymond Hettinger <python@rcn.com>
parents: 35449
diff changeset
2200
35348
2f1bf64b837e Numerous fix-ups to C API and docs. Added tests for C API.
Raymond Hettinger <python@rcn.com>
parents: 35347
diff changeset
2201 #endif