annotate Objects/setobject.c @ 35333:dc8ae499efcf 2.4

* SF bug #1257731: Fix logic in set.__contains__(), set.remove(), and set.discard for handling keys that both inherite from set and define their own __hash__() function. * Fixed O(n) performance issue with set.pop() which should have been an O(1) process.
author Raymond Hettinger <python@rcn.com>
date Fri, 12 Aug 2005 23:47:50 +0000
parents f3d3151f8417
children 503c3697578f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1 #include "Python.h"
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31628
diff changeset
2 #include "structmember.h"
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
3
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
4 /* set object implementation
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
5 written and maintained by Raymond D. Hettinger <python@rcn.com>
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
6 derived from sets.py written by Greg V. Wilson, Alex Martelli,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
7 Guido van Rossum, Raymond Hettinger, and Tim Peters.
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
8
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
9 Copyright (c) 2003 Python Software Foundation.
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
10 All rights reserved.
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
11 */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
12
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
13 static PyObject *
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
14 set_update(PySetObject *so, PyObject *other)
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
15 {
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
16 PyObject *item, *data, *it;
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
17
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
18 if (PyAnySet_Check(other)) {
31203
bfa0369d6760 Speedup set.update by using the override mode for PyDict_Merge().
Raymond Hettinger <python@rcn.com>
parents: 31191
diff changeset
19 if (PyDict_Merge(so->data, ((PySetObject *)other)->data, 1) == -1)
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
20 return NULL;
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
21 Py_RETURN_NONE;
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
22 }
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
23
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
24 it = PyObject_GetIter(other);
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
25 if (it == NULL)
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
26 return NULL;
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
27 data = so->data;
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
28
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
29 while ((item = PyIter_Next(it)) != NULL) {
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
30 if (PyDict_SetItem(data, item, Py_True) == -1) {
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
31 Py_DECREF(it);
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
32 Py_DECREF(item);
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
33 return NULL;
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
34 }
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
35 Py_DECREF(item);
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
36 }
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
37 Py_DECREF(it);
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
38 if (PyErr_Occurred())
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
39 return NULL;
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
40 Py_RETURN_NONE;
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
41 }
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
42
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
43 PyDoc_STRVAR(update_doc,
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
44 "Update a set with the union of itself and another.");
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
45
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
46 static PyObject *
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
47 make_new_set(PyTypeObject *type, PyObject *iterable)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
48 {
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
49 PyObject *data = NULL;
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
50 PyObject *tmp;
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
51 PySetObject *so = NULL;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
52
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
53 data = PyDict_New();
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
54 if (data == NULL)
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
55 return NULL;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
56
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
57 /* create PySetObject structure */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
58 so = (PySetObject *)type->tp_alloc(type, 0);
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
59 if (so == NULL) {
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
60 Py_DECREF(data);
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
61 return NULL;
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
62 }
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
63 so->data = data;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
64 so->hash = -1;
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31628
diff changeset
65 so->weakreflist = NULL;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
66
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
67 if (iterable != NULL) {
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
68 tmp = set_update(so, iterable);
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
69 if (tmp == NULL) {
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
70 Py_DECREF(so);
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
71 return NULL;
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
72 }
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
73 Py_DECREF(tmp);
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
74 }
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
75
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
76 return (PyObject *)so;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
77 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
78
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
79 static PyObject *
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
80 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
81 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
82 PyObject *iterable = NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
83
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
84 if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
85 return NULL;
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
86 if (iterable != NULL && PyFrozenSet_CheckExact(iterable)) {
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
87 Py_INCREF(iterable);
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
88 return iterable;
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
89 }
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
90 return make_new_set(type, iterable);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
91 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
92
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
93 static PyObject *
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
94 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
95 {
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
96 return make_new_set(type, NULL);
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
97 }
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
98
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
99 static PyObject *
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
100 frozenset_dict_wrapper(PyObject *d)
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
101 {
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
102 PySetObject *w;
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
103
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
104 assert(PyDict_Check(d));
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
105 w = (PySetObject *)make_new_set(&PyFrozenSet_Type, NULL);
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
106 if (w == NULL)
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
107 return NULL;
33605
b3408a85c89c Use Py_CLEAR(). Add unrelated test.
Raymond Hettinger <python@rcn.com>
parents: 32695
diff changeset
108 Py_CLEAR(w->data);
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
109 Py_INCREF(d);
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
110 w->data = d;
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
111 return (PyObject *)w;
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
112 }
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
113
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
114 static void
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
115 set_dealloc(PySetObject *so)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
116 {
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
117 PyObject_GC_UnTrack(so);
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31628
diff changeset
118 if (so->weakreflist != NULL)
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31628
diff changeset
119 PyObject_ClearWeakRefs((PyObject *) so);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
120 Py_XDECREF(so->data);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
121 so->ob_type->tp_free(so);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
122 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
123
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
124 static int
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
125 set_traverse(PySetObject *so, visitproc visit, void *arg)
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
126 {
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
127 if (so->data)
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
128 return visit(so->data, arg);
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
129 return 0;
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
130 }
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
131
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
132 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
133 set_iter(PySetObject *so)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
134 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
135 return PyObject_GetIter(so->data);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
136 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
137
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
138 static int
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
139 set_len(PySetObject *so)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
140 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
141 return PyDict_Size(so->data);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
142 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
143
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
144 static int
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
145 set_contains(PySetObject *so, PyObject *key)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
146 {
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
147 PyObject *tmp;
35333
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
148 int rv;
30995
d4826e097a3f Allow temporary hashability for the __contains__ test.
Raymond Hettinger <python@rcn.com>
parents: 30994
diff changeset
149
35333
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
150 rv = PyDict_Contains(so->data, key);
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
151 if (rv == -1) {
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
152 if (!PyAnySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
153 return -1;
30995
d4826e097a3f Allow temporary hashability for the __contains__ test.
Raymond Hettinger <python@rcn.com>
parents: 30994
diff changeset
154 PyErr_Clear();
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
155 tmp = frozenset_dict_wrapper(((PySetObject *)(key))->data);
30995
d4826e097a3f Allow temporary hashability for the __contains__ test.
Raymond Hettinger <python@rcn.com>
parents: 30994
diff changeset
156 if (tmp == NULL)
d4826e097a3f Allow temporary hashability for the __contains__ test.
Raymond Hettinger <python@rcn.com>
parents: 30994
diff changeset
157 return -1;
35333
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
158 rv = PyDict_Contains(so->data, tmp);
30995
d4826e097a3f Allow temporary hashability for the __contains__ test.
Raymond Hettinger <python@rcn.com>
parents: 30994
diff changeset
159 Py_DECREF(tmp);
d4826e097a3f Allow temporary hashability for the __contains__ test.
Raymond Hettinger <python@rcn.com>
parents: 30994
diff changeset
160 }
35333
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
161 return rv;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
162 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
163
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
164 static PyObject *
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
165 set_direct_contains(PySetObject *so, PyObject *key)
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
166 {
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
167 long result;
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
168
31178
1bbc2085f213 * Refactor set.__contains__()
Raymond Hettinger <python@rcn.com>
parents: 31177
diff changeset
169 result = set_contains(so, key);
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
170 if (result == -1)
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
171 return NULL;
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
172 return PyBool_FromLong(result);
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
173 }
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
174
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
175 PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
176
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
177 static PyObject *
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
178 set_copy(PySetObject *so)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
179 {
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
180 return make_new_set(so->ob_type, (PyObject *)so);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
181 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
182
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
183 static PyObject *
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
184 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
185 {
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
186 if (PyFrozenSet_CheckExact(so)) {
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
187 Py_INCREF(so);
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
188 return (PyObject *)so;
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
189 }
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
190 return set_copy(so);
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
191 }
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
192
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
193 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
194
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
195 static PyObject *
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
196 set_union(PySetObject *so, PyObject *other)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
197 {
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
198 PySetObject *result;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
199 PyObject *rv;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
200
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
201 result = (PySetObject *)set_copy(so);
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
202 if (result == NULL)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
203 return NULL;
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
204 rv = set_update(result, other);
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
205 if (rv == NULL) {
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
206 Py_DECREF(result);
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
207 return NULL;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
208 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
209 Py_DECREF(rv);
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
210 return (PyObject *)result;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
211 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
212
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
213 PyDoc_STRVAR(union_doc,
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
214 "Return the union of two sets as a new set.\n\
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
215 \n\
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
216 (i.e. all elements that are in either set.)");
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
217
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
218 static PyObject *
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
219 set_or(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
220 {
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
221 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
222 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
223 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
224 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
225 return set_union(so, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
226 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
227
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
228 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
229 set_ior(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
230 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
231 PyObject *result;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
232
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
233 if (!PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
234 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
235 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
236 }
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
237 result = set_update(so, other);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
238 if (result == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
239 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
240 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
241 Py_INCREF(so);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
242 return (PyObject *)so;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
243 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
244
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
245 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
246 set_intersection(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
247 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
248 PySetObject *result;
31300
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
249 PyObject *item, *selfdata, *tgtdata, *it, *tmp;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
250
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
251 result = (PySetObject *)make_new_set(so->ob_type, NULL);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
252 if (result == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
253 return NULL;
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
254 tgtdata = result->data;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
255 selfdata = so->data;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
256
31300
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
257 if (PyAnySet_Check(other))
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
258 other = ((PySetObject *)other)->data;
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
259
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
260 if (PyDict_Check(other) && PyDict_Size(other) > PyDict_Size(selfdata)) {
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
261 tmp = selfdata;
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
262 selfdata = other;
31300
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
263 other = tmp;
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
264 }
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
265
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
266 if (PyDict_CheckExact(other)) {
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
267 PyObject *value;
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
268 int pos = 0;
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
269 while (PyDict_Next(other, &pos, &item, &value)) {
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
270 if (PyDict_Contains(selfdata, item)) {
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
271 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
272 Py_DECREF(result);
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
273 return NULL;
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
274 }
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
275 }
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
276 }
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
277 return (PyObject *)result;
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
278 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
279
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
280 it = PyObject_GetIter(other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
281 if (it == NULL) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
282 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
283 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
284 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
285
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
286 while ((item = PyIter_Next(it)) != NULL) {
31019
b2fd7af17186 Expose dict_contains() and PyDict_Contains() with is about 10% faster
Raymond Hettinger <python@rcn.com>
parents: 31015
diff changeset
287 if (PyDict_Contains(selfdata, item)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
288 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
289 Py_DECREF(it);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
290 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
291 Py_DECREF(item);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
292 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
293 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
294 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
295 Py_DECREF(item);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
296 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
297 Py_DECREF(it);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
298 if (PyErr_Occurred()) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
299 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
300 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
301 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
302 return (PyObject *)result;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
303 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
304
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
305 PyDoc_STRVAR(intersection_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
306 "Return the intersection of two sets as a new set.\n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
307 \n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
308 (i.e. all elements that are in both sets.)");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
309
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
310 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
311 set_intersection_update(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
312 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
313 PyObject *item, *selfdata, *it, *newdict, *tmp;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
314
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
315 newdict = PyDict_New();
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
316 if (newdict == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
317 return newdict;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
318
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
319 it = PyObject_GetIter(other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
320 if (it == NULL) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
321 Py_DECREF(newdict);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
322 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
323 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
324
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
325 selfdata = so->data;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
326 while ((item = PyIter_Next(it)) != NULL) {
31019
b2fd7af17186 Expose dict_contains() and PyDict_Contains() with is about 10% faster
Raymond Hettinger <python@rcn.com>
parents: 31015
diff changeset
327 if (PyDict_Contains(selfdata, item)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
328 if (PyDict_SetItem(newdict, item, Py_True) == -1) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
329 Py_DECREF(newdict);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
330 Py_DECREF(it);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
331 Py_DECREF(item);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
332 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
333 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
334 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
335 Py_DECREF(item);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
336 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
337 Py_DECREF(it);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
338 if (PyErr_Occurred()) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
339 Py_DECREF(newdict);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
340 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
341 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
342 tmp = so->data;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
343 so->data = newdict;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
344 Py_DECREF(tmp);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
345 Py_RETURN_NONE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
346 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
347
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
348 PyDoc_STRVAR(intersection_update_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
349 "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
350
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
351 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
352 set_and(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
353 {
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
354 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
355 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
356 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
357 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
358 return set_intersection(so, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
359 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
360
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
361 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
362 set_iand(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
363 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
364 PyObject *result;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
365
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
366 if (!PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
367 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
368 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
369 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
370 result = set_intersection_update(so, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
371 if (result == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
372 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
373 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
374 Py_INCREF(so);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
375 return (PyObject *)so;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
376 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
377
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
378 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
379 set_difference_update(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
380 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
381 PyObject *item, *tgtdata, *it;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
382
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
383 it = PyObject_GetIter(other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
384 if (it == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
385 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
386
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
387 tgtdata = so->data;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
388 while ((item = PyIter_Next(it)) != NULL) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
389 if (PyDict_DelItem(tgtdata, item) == -1) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
390 if (PyErr_ExceptionMatches(PyExc_KeyError))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
391 PyErr_Clear();
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
392 else {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
393 Py_DECREF(it);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
394 Py_DECREF(item);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
395 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
396 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
397 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
398 Py_DECREF(item);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
399 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
400 Py_DECREF(it);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
401 if (PyErr_Occurred())
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
402 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
403 Py_RETURN_NONE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
404 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
405
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
406 PyDoc_STRVAR(difference_update_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
407 "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
408
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
409 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
410 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
411 {
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
412 PyObject *result, *tmp;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
413 PyObject *otherdata, *tgtdata;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
414 PyObject *key, *value;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
415 int pos = 0;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
416
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
417 if (PyDict_Check(other))
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
418 otherdata = other;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
419 else if (PyAnySet_Check(other))
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
420 otherdata = ((PySetObject *)other)->data;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
421 else {
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
422 result = set_copy(so);
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
423 if (result == NULL)
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
424 return result;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
425 tmp = set_difference_update((PySetObject *)result, other);
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
426 if (tmp != NULL) {
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
427 Py_DECREF(tmp);
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
428 return result;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
429 }
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
430 Py_DECREF(result);
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
431 return NULL;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
432 }
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
433
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
434 result = make_new_set(so->ob_type, NULL);
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
435 if (result == NULL)
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
436 return NULL;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
437 tgtdata = ((PySetObject *)result)->data;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
438
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
439 while (PyDict_Next(so->data, &pos, &key, &value)) {
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
440 if (!PyDict_Contains(otherdata, key)) {
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
441 if (PyDict_SetItem(tgtdata, key, Py_True) == -1)
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
442 return NULL;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
443 }
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
444 }
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
445 return result;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
446 }
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
447
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
448 PyDoc_STRVAR(difference_doc,
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
449 "Return the difference of two sets as a new set.\n\
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
450 \n\
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
451 (i.e. all elements that are in this set but not the other.)");
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
452 static PyObject *
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
453 set_sub(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
454 {
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
455 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
456 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
457 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
458 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
459 return set_difference(so, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
460 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
461
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
462 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
463 set_isub(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
464 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
465 PyObject *result;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
466
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
467 if (!PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
468 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
469 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
470 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
471 result = set_difference_update(so, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
472 if (result == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
473 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
474 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
475 Py_INCREF(so);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
476 return (PyObject *)so;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
477 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
478
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
479 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
480 set_symmetric_difference_update(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
481 {
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
482 PyObject *selfdata, *otherdata;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
483 PySetObject *otherset = NULL;
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
484 PyObject *key, *value;
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
485 int pos = 0;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
486
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
487 selfdata = so->data;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
488 if (PyDict_Check(other))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
489 otherdata = other;
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
490 else if (PyAnySet_Check(other))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
491 otherdata = ((PySetObject *)other)->data;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
492 else {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
493 otherset = (PySetObject *)make_new_set(so->ob_type, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
494 if (otherset == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
495 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
496 otherdata = otherset->data;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
497 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
498
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
499 while (PyDict_Next(otherdata, &pos, &key, &value)) {
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
500 if (PyDict_Contains(selfdata, key)) {
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
501 if (PyDict_DelItem(selfdata, key) == -1) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
502 Py_XDECREF(otherset);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
503 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
504 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
505 } else {
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
506 if (PyDict_SetItem(selfdata, key, Py_True) == -1) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
507 Py_XDECREF(otherset);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
508 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
509 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
510 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
511 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
512 Py_XDECREF(otherset);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
513 Py_RETURN_NONE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
514 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
515
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
516 PyDoc_STRVAR(symmetric_difference_update_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
517 "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
518
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
519 static PyObject *
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
520 set_symmetric_difference(PySetObject *so, PyObject *other)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
521 {
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
522 PySetObject *result;
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
523 PyObject *selfdata, *otherdata, *tgtdata, *rv, *otherset;
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
524 PyObject *key, *value;
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
525 int pos = 0;
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
526
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
527 if (PyDict_Check(other))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
528 otherdata = other;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
529 else if (PyAnySet_Check(other))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
530 otherdata = ((PySetObject *)other)->data;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
531 else {
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
532 otherset = make_new_set(so->ob_type, other);
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
533 if (otherset == NULL)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
534 return NULL;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
535 rv = set_symmetric_difference_update((PySetObject *)otherset, (PyObject *)so);
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
536 if (rv == NULL)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
537 return NULL;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
538 Py_DECREF(rv);
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
539 return otherset;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
540 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
541
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
542 result = (PySetObject *)make_new_set(so->ob_type, NULL);
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
543 if (result == NULL)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
544 return NULL;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
545 tgtdata = result->data;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
546 selfdata = so->data;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
547
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
548 while (PyDict_Next(otherdata, &pos, &key, &value)) {
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
549 if (!PyDict_Contains(selfdata, key)) {
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
550 if (PyDict_SetItem(tgtdata, key, Py_True) == -1) {
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
551 Py_DECREF(result);
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
552 return NULL;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
553 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
554 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
555 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
556
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
557 pos = 0;
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
558 while (PyDict_Next(selfdata, &pos, &key, &value)) {
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
559 if (!PyDict_Contains(otherdata, key)) {
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
560 if (PyDict_SetItem(tgtdata, key, Py_True) == -1) {
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
561 Py_DECREF(result);
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
562 return NULL;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
563 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
564 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
565 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
566
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
567 return (PyObject *)result;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
568 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
569
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
570 PyDoc_STRVAR(symmetric_difference_doc,
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
571 "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
572 \n\
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
573 (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
574
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
575 static PyObject *
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
576 set_xor(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
577 {
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
578 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
579 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
580 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
581 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
582 return set_symmetric_difference(so, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
583 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
584
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
585 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
586 set_ixor(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
587 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
588 PyObject *result;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
589
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
590 if (!PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
591 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
592 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
593 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
594 result = set_symmetric_difference_update(so, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
595 if (result == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
596 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
597 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
598 Py_INCREF(so);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
599 return (PyObject *)so;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
600 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
601
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
602 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
603 set_issubset(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
604 {
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
605 PyObject *otherdata, *tmp, *result;
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
606 PyObject *key, *value;
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
607 int pos = 0;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
608
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
609 if (!PyAnySet_Check(other)) {
30994
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
610 tmp = make_new_set(&PySet_Type, other);
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
611 if (tmp == NULL)
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
612 return NULL;
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
613 result = set_issubset(so, tmp);
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
614 Py_DECREF(tmp);
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
615 return result;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
616 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
617 if (set_len(so) > set_len((PySetObject *)other))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
618 Py_RETURN_FALSE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
619
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
620 otherdata = ((PySetObject *)other)->data;
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
621 while (PyDict_Next(((PySetObject *)so)->data, &pos, &key, &value)) {
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
622 if (!PyDict_Contains(otherdata, key))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
623 Py_RETURN_FALSE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
624 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
625 Py_RETURN_TRUE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
626 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
627
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
628 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
629
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
630 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
631 set_issuperset(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
632 {
30994
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
633 PyObject *tmp, *result;
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
634
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
635 if (!PyAnySet_Check(other)) {
30994
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
636 tmp = make_new_set(&PySet_Type, other);
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
637 if (tmp == NULL)
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
638 return NULL;
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
639 result = set_issuperset(so, tmp);
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
640 Py_DECREF(tmp);
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
641 return result;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
642 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
643 return set_issubset((PySetObject *)other, (PyObject *)so);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
644 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
645
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
646 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
647
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
648 static long
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
649 set_nohash(PyObject *self)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
650 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
651 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
652 return -1;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
653 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
654
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
655 static int
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
656 set_nocmp(PyObject *self)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
657 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
658 PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
659 return -1;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
660 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
661
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
662 static long
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
663 frozenset_hash(PyObject *self)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
664 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
665 PySetObject *so = (PySetObject *)self;
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
666 PyObject *key, *value;
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
667 int pos = 0;
32320
87302ac07255 Futher improvements to frozenset hashing (based on Yitz Gale's battery of
Raymond Hettinger <python@rcn.com>
parents: 32319
diff changeset
668 long hash = 1927868237L;
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
669
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
670 if (so->hash != -1)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
671 return so->hash;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
672
32320
87302ac07255 Futher improvements to frozenset hashing (based on Yitz Gale's battery of
Raymond Hettinger <python@rcn.com>
parents: 32319
diff changeset
673 hash *= (PyDict_Size(so->data) + 1);
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
674 while (PyDict_Next(so->data, &pos, &key, &value)) {
32320
87302ac07255 Futher improvements to frozenset hashing (based on Yitz Gale's battery of
Raymond Hettinger <python@rcn.com>
parents: 32319
diff changeset
675 /* Work to increase the bit dispersion for closely spaced hash
87302ac07255 Futher improvements to frozenset hashing (based on Yitz Gale's battery of
Raymond Hettinger <python@rcn.com>
parents: 32319
diff changeset
676 values. The is important because some use cases have many
87302ac07255 Futher improvements to frozenset hashing (based on Yitz Gale's battery of
Raymond Hettinger <python@rcn.com>
parents: 32319
diff changeset
677 combinations of a small number of elements with nearby
87302ac07255 Futher improvements to frozenset hashing (based on Yitz Gale's battery of
Raymond Hettinger <python@rcn.com>
parents: 32319
diff changeset
678 hashes so that many distinct combinations collapse to only
87302ac07255 Futher improvements to frozenset hashing (based on Yitz Gale's battery of
Raymond Hettinger <python@rcn.com>
parents: 32319
diff changeset
679 a handful of distinct hash values. */
87302ac07255 Futher improvements to frozenset hashing (based on Yitz Gale's battery of
Raymond Hettinger <python@rcn.com>
parents: 32319
diff changeset
680 long h = PyObject_Hash(key);
87302ac07255 Futher improvements to frozenset hashing (based on Yitz Gale's battery of
Raymond Hettinger <python@rcn.com>
parents: 32319
diff changeset
681 hash ^= (h ^ (h << 16) ^ 89869747L) * 3644798167u;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
682 }
32320
87302ac07255 Futher improvements to frozenset hashing (based on Yitz Gale's battery of
Raymond Hettinger <python@rcn.com>
parents: 32319
diff changeset
683 hash = hash * 69069L + 907133923L;
32319
b9de6b665273 Fixups to the hash function for frozensets.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
684 if (hash == -1)
b9de6b665273 Fixups to the hash function for frozensets.
Raymond Hettinger <python@rcn.com>
parents: 32158
diff changeset
685 hash = 590923713L;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
686 so->hash = hash;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
687 return hash;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
688 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
689
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
690 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
691 set_richcompare(PySetObject *v, PyObject *w, int op)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
692 {
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
693 if(!PyAnySet_Check(w)) {
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
694 if (op == Py_EQ)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
695 Py_RETURN_FALSE;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
696 if (op == Py_NE)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
697 Py_RETURN_TRUE;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
698 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
699 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
700 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
701 switch (op) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
702 case Py_EQ:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
703 case Py_NE:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
704 return PyObject_RichCompare(((PySetObject *)v)->data,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
705 ((PySetObject *)w)->data, op);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
706 case Py_LE:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
707 return set_issubset((PySetObject *)v, w);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
708 case Py_GE:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
709 return set_issuperset((PySetObject *)v, w);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
710 case Py_LT:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
711 if (set_len(v) >= set_len((PySetObject *)w))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
712 Py_RETURN_FALSE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
713 return set_issubset((PySetObject *)v, w);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
714 case Py_GT:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
715 if (set_len(v) <= set_len((PySetObject *)w))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
716 Py_RETURN_FALSE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
717 return set_issuperset((PySetObject *)v, w);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
718 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
719 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
720 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
721 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
722
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
723 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
724 set_repr(PySetObject *so)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
725 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
726 PyObject *keys, *result, *listrepr;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
727
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
728 keys = PyDict_Keys(so->data);
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
729 if (keys == NULL)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
730 return NULL;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
731 listrepr = PyObject_Repr(keys);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
732 Py_DECREF(keys);
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
733 if (listrepr == NULL)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
734 return NULL;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
735
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
736 result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
737 PyString_AS_STRING(listrepr));
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
738 Py_DECREF(listrepr);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
739 return result;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
740 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
741
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
742 static int
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
743 set_tp_print(PySetObject *so, FILE *fp, int flags)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
744 {
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
745 PyObject *key, *value;
31300
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
746 int pos=0;
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
747 char *emit = ""; /* No separator emitted on first pass */
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
748 char *separator = ", ";
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
749
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
750 fprintf(fp, "%s([", so->ob_type->tp_name);
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
751 while (PyDict_Next(so->data, &pos, &key, &value)) {
31300
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
752 fputs(emit, fp);
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
753 emit = separator;
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
754 if (PyObject_Print(key, fp, 0) != 0)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
755 return -1;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
756 }
31300
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
757 fputs("])", fp);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
758 return 0;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
759 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
760
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
761 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
762 set_clear(PySetObject *so)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
763 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
764 PyDict_Clear(so->data);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
765 so->hash = -1;
31178
1bbc2085f213 * Refactor set.__contains__()
Raymond Hettinger <python@rcn.com>
parents: 31177
diff changeset
766 Py_RETURN_NONE;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
767 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
768
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
769 PyDoc_STRVAR(clear_doc, "Remove all elements from this set.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
770
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
771 static int
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
772 set_tp_clear(PySetObject *so)
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
773 {
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
774 PyDict_Clear(so->data);
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
775 so->hash = -1;
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
776 return 0;
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
777 }
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
778
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
779 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
780 set_add(PySetObject *so, PyObject *item)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
781 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
782 if (PyDict_SetItem(so->data, item, Py_True) == -1)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
783 return NULL;
31178
1bbc2085f213 * Refactor set.__contains__()
Raymond Hettinger <python@rcn.com>
parents: 31177
diff changeset
784 Py_RETURN_NONE;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
785 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
786
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
787 PyDoc_STRVAR(add_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
788 "Add an element to a set.\n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
789 \n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
790 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
791
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
792 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
793 set_remove(PySetObject *so, PyObject *item)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
794 {
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
795 PyObject *tmp, *result;
35333
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
796 int rv;
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
797
35333
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
798 rv = PyDict_DelItem(so->data, item);
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
799 if (rv == 0)
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
800 Py_RETURN_NONE;
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
801 if (!PyAnySet_Check(item) || !PyErr_ExceptionMatches(PyExc_TypeError))
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
802 return NULL;
35333
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
803 PyErr_Clear();
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
804 tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
805 if (tmp == NULL)
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
806 return NULL;
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
807 result = set_remove(so, tmp);
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
808 Py_DECREF(tmp);
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
809 return result;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
810 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
811
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
812 PyDoc_STRVAR(remove_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
813 "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
814 \n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
815 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
816
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
817 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
818 set_discard(PySetObject *so, PyObject *item)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
819 {
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
820 PyObject *tmp, *result;
35333
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
821 int rv;
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
822
35333
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
823 rv = PyDict_DelItem(so->data, item);
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
824 if (rv == 0)
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
825 Py_RETURN_NONE;
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
826 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
827 PyErr_Clear();
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
828 Py_RETURN_NONE;
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
829 }
35333
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
830 if (!PyAnySet_Check(item) || !PyErr_ExceptionMatches(PyExc_TypeError))
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
831 return NULL;
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
832 PyErr_Clear();
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
833 tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
834 if (tmp == NULL)
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
835 return NULL;
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
836 result = set_discard(so, tmp);
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
837 Py_DECREF(tmp);
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
838 return result;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
839 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
840
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
841 PyDoc_STRVAR(discard_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
842 "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
843 \n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
844 If the element is not a member, do nothing.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
845
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
846 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
847 set_pop(PySetObject *so)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
848 {
35333
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
849 PyObject *item, *key;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
850
35333
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
851 if (set_len(so) == 0) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
852 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
853 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
854 }
35333
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
855 item = PyObject_CallMethod(so->data, "popitem", NULL);
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
856 if (item == NULL)
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
857 return NULL;
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
858 key = PyTuple_GET_ITEM(item, 0);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
859 Py_INCREF(key);
35333
dc8ae499efcf * SF bug #1257731: Fix logic in set.__contains__(), set.remove(),
Raymond Hettinger <python@rcn.com>
parents: 35110
diff changeset
860 Py_DECREF(item);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
861 return key;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
862 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
863
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
864 PyDoc_STRVAR(pop_doc, "Remove and return an arbitrary set element.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
865
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
866 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
867 set_reduce(PySetObject *so)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
868 {
33948
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
869 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
870
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
871 keys = PyDict_Keys(so->data);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
872 if (keys == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
873 goto done;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
874 args = PyTuple_Pack(1, keys);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
875 if (args == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
876 goto done;
33948
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
877 dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
878 if (dict == NULL) {
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
879 PyErr_Clear();
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
880 dict = Py_None;
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
881 Py_INCREF(dict);
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
882 }
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
883 result = PyTuple_Pack(3, so->ob_type, args, dict);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
884 done:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
885 Py_XDECREF(args);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
886 Py_XDECREF(keys);
33948
f2ce15b02ba3 SF 1062353: set pickling problems
Raymond Hettinger <python@rcn.com>
parents: 33605
diff changeset
887 Py_XDECREF(dict);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
888 return result;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
889 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
890
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
891 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
892
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
893 static int
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
894 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
895 {
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
896 PyObject *iterable = NULL;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
897 PyObject *result;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
898
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
899 if (!PyAnySet_Check(self))
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
900 return -1;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
901 if (!PyArg_UnpackTuple(args, self->ob_type->tp_name, 0, 1, &iterable))
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
902 return -1;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
903 PyDict_Clear(self->data);
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
904 self->hash = -1;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
905 if (iterable == NULL)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
906 return 0;
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
907 result = set_update(self, iterable);
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
908 if (result != NULL) {
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
909 Py_DECREF(result);
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
910 return 0;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
911 }
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
912 return -1;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
913 }
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
914
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
915 static PySequenceMethods set_as_sequence = {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
916 (inquiry)set_len, /* sq_length */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
917 0, /* sq_concat */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
918 0, /* sq_repeat */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
919 0, /* sq_item */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
920 0, /* sq_slice */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
921 0, /* sq_ass_item */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
922 0, /* sq_ass_slice */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
923 (objobjproc)set_contains, /* sq_contains */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
924 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
925
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
926 /* set object ********************************************************/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
927
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
928 static PyMethodDef set_methods[] = {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
929 {"add", (PyCFunction)set_add, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
930 add_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
931 {"clear", (PyCFunction)set_clear, METH_NOARGS,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
932 clear_doc},
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
933 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
934 contains_doc},
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
935 {"copy", (PyCFunction)set_copy, METH_NOARGS,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
936 copy_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
937 {"discard", (PyCFunction)set_discard, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
938 discard_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
939 {"difference", (PyCFunction)set_difference, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
940 difference_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
941 {"difference_update", (PyCFunction)set_difference_update, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
942 difference_update_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
943 {"intersection",(PyCFunction)set_intersection, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
944 intersection_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
945 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
946 intersection_update_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
947 {"issubset", (PyCFunction)set_issubset, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
948 issubset_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
949 {"issuperset", (PyCFunction)set_issuperset, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
950 issuperset_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
951 {"pop", (PyCFunction)set_pop, METH_NOARGS,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
952 pop_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
953 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
954 reduce_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
955 {"remove", (PyCFunction)set_remove, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
956 remove_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
957 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
958 symmetric_difference_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
959 {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
960 symmetric_difference_update_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
961 {"union", (PyCFunction)set_union, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
962 union_doc},
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
963 {"update", (PyCFunction)set_update, METH_O,
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
964 update_doc},
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
965 {NULL, NULL} /* sentinel */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
966 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
967
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
968 static PyNumberMethods set_as_number = {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
969 0, /*nb_add*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
970 (binaryfunc)set_sub, /*nb_subtract*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
971 0, /*nb_multiply*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
972 0, /*nb_divide*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
973 0, /*nb_remainder*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
974 0, /*nb_divmod*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
975 0, /*nb_power*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
976 0, /*nb_negative*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
977 0, /*nb_positive*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
978 0, /*nb_absolute*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
979 0, /*nb_nonzero*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
980 0, /*nb_invert*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
981 0, /*nb_lshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
982 0, /*nb_rshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
983 (binaryfunc)set_and, /*nb_and*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
984 (binaryfunc)set_xor, /*nb_xor*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
985 (binaryfunc)set_or, /*nb_or*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
986 0, /*nb_coerce*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
987 0, /*nb_int*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
988 0, /*nb_long*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
989 0, /*nb_float*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
990 0, /*nb_oct*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
991 0, /*nb_hex*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
992 0, /*nb_inplace_add*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
993 (binaryfunc)set_isub, /*nb_inplace_subtract*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
994 0, /*nb_inplace_multiply*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
995 0, /*nb_inplace_divide*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
996 0, /*nb_inplace_remainder*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
997 0, /*nb_inplace_power*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
998 0, /*nb_inplace_lshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
999 0, /*nb_inplace_rshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1000 (binaryfunc)set_iand, /*nb_inplace_and*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1001 (binaryfunc)set_ixor, /*nb_inplace_xor*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1002 (binaryfunc)set_ior, /*nb_inplace_or*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1003 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1004
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1005 PyDoc_STRVAR(set_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1006 "set(iterable) --> set object\n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1007 \n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1008 Build an unordered collection.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1009
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1010 PyTypeObject PySet_Type = {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1011 PyObject_HEAD_INIT(&PyType_Type)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1012 0, /* ob_size */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1013 "set", /* tp_name */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1014 sizeof(PySetObject), /* tp_basicsize */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1015 0, /* tp_itemsize */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1016 /* methods */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1017 (destructor)set_dealloc, /* tp_dealloc */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1018 (printfunc)set_tp_print, /* tp_print */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1019 0, /* tp_getattr */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1020 0, /* tp_setattr */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1021 (cmpfunc)set_nocmp, /* tp_compare */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1022 (reprfunc)set_repr, /* tp_repr */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1023 &set_as_number, /* tp_as_number */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1024 &set_as_sequence, /* tp_as_sequence */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1025 0, /* tp_as_mapping */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1026 set_nohash, /* tp_hash */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1027 0, /* tp_call */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1028 0, /* tp_str */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1029 PyObject_GenericGetAttr, /* tp_getattro */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1030 0, /* tp_setattro */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1031 0, /* tp_as_buffer */
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
1032 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31628
diff changeset
1033 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1034 set_doc, /* tp_doc */
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
1035 (traverseproc)set_traverse, /* tp_traverse */
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
1036 (inquiry)set_tp_clear, /* tp_clear */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1037 (richcmpfunc)set_richcompare, /* tp_richcompare */
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31628
diff changeset
1038 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1039 (getiterfunc)set_iter, /* tp_iter */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1040 0, /* tp_iternext */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1041 set_methods, /* tp_methods */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1042 0, /* tp_members */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1043 0, /* tp_getset */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1044 0, /* tp_base */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1045 0, /* tp_dict */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1046 0, /* tp_descr_get */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1047 0, /* tp_descr_set */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1048 0, /* tp_dictoffset */
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1049 (initproc)set_init, /* tp_init */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1050 PyType_GenericAlloc, /* tp_alloc */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1051 set_new, /* tp_new */
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
1052 PyObject_GC_Del, /* tp_free */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1053 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1054
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1055 /* frozenset object ********************************************************/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1056
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1057
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1058 static PyMethodDef frozenset_methods[] = {
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
1059 {"__contains__",(PyCFunction)set_direct_contains, METH_O | METH_COEXIST,
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
1060 contains_doc},
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1061 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1062 copy_doc},
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1063 {"difference", (PyCFunction)set_difference, METH_O,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1064 difference_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1065 {"intersection",(PyCFunction)set_intersection, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1066 intersection_doc},
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1067 {"issubset", (PyCFunction)set_issubset, METH_O,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1068 issubset_doc},
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
1069 {"issuperset", (PyCFunction)set_issuperset, METH_O,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1070 issuperset_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1071 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1072 reduce_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1073 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1074 symmetric_difference_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1075 {"union", (PyCFunction)set_union, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1076 union_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1077 {NULL, NULL} /* sentinel */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1078 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1079
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1080 static PyNumberMethods frozenset_as_number = {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1081 0, /*nb_add*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1082 (binaryfunc)set_sub, /*nb_subtract*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1083 0, /*nb_multiply*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1084 0, /*nb_divide*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1085 0, /*nb_remainder*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1086 0, /*nb_divmod*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1087 0, /*nb_power*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1088 0, /*nb_negative*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1089 0, /*nb_positive*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1090 0, /*nb_absolute*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1091 0, /*nb_nonzero*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1092 0, /*nb_invert*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1093 0, /*nb_lshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1094 0, /*nb_rshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1095 (binaryfunc)set_and, /*nb_and*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1096 (binaryfunc)set_xor, /*nb_xor*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1097 (binaryfunc)set_or, /*nb_or*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1098 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1099
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1100 PyDoc_STRVAR(frozenset_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1101 "frozenset(iterable) --> frozenset object\n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1102 \n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1103 Build an immutable unordered collection.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1104
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1105 PyTypeObject PyFrozenSet_Type = {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1106 PyObject_HEAD_INIT(&PyType_Type)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1107 0, /* ob_size */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1108 "frozenset", /* tp_name */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1109 sizeof(PySetObject), /* tp_basicsize */
31300
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
1110 0, /* tp_itemsize */
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
1111 /* methods */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1112 (destructor)set_dealloc, /* tp_dealloc */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1113 (printfunc)set_tp_print, /* tp_print */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1114 0, /* tp_getattr */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1115 0, /* tp_setattr */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1116 (cmpfunc)set_nocmp, /* tp_compare */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1117 (reprfunc)set_repr, /* tp_repr */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1118 &frozenset_as_number, /* tp_as_number */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1119 &set_as_sequence, /* tp_as_sequence */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1120 0, /* tp_as_mapping */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1121 frozenset_hash, /* tp_hash */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1122 0, /* tp_call */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1123 0, /* tp_str */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1124 PyObject_GenericGetAttr, /* tp_getattro */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1125 0, /* tp_setattro */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1126 0, /* tp_as_buffer */
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
1127 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31628
diff changeset
1128 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1129 frozenset_doc, /* tp_doc */
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
1130 (traverseproc)set_traverse, /* tp_traverse */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1131 0, /* tp_clear */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1132 (richcmpfunc)set_richcompare, /* tp_richcompare */
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31628
diff changeset
1133 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1134 (getiterfunc)set_iter, /* tp_iter */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1135 0, /* tp_iternext */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1136 frozenset_methods, /* tp_methods */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1137 0, /* tp_members */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1138 0, /* tp_getset */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1139 0, /* tp_base */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1140 0, /* tp_dict */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1141 0, /* tp_descr_get */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1142 0, /* tp_descr_set */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1143 0, /* tp_dictoffset */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1144 0, /* tp_init */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1145 PyType_GenericAlloc, /* tp_alloc */
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1146 frozenset_new, /* tp_new */
35110
f3d3151f8417 SF patch #1200018: Restore GC support to set objects
Raymond Hettinger <python@rcn.com>
parents: 33948
diff changeset
1147 PyObject_GC_Del, /* tp_free */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1148 };