annotate Objects/setobject.c @ 65035:87f4b32905df 2.7

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