annotate Objects/setobject.c @ 32273:95287cddbc1e legacy-trunk

add -L flag to cause leaks(1) command to run just before exit
author Skip Montanaro <skip@pobox.com>
date Sun, 06 Jun 2004 15:53:18 +0000
parents 679e5c8effae
children b9de6b665273
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;
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
108 Py_DECREF(w->data);
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 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
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
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
124 static int
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
125 set_traverse(PySetObject *so, visitproc visit, void *arg)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
126 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
127 if (so->data)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
128 return visit(so->data, arg);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
129 return 0;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
130 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
131
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;
30995
d4826e097a3f Allow temporary hashability for the __contains__ test.
Raymond Hettinger <python@rcn.com>
parents: 30994
diff changeset
148 int result;
d4826e097a3f Allow temporary hashability for the __contains__ test.
Raymond Hettinger <python@rcn.com>
parents: 30994
diff changeset
149
31019
b2fd7af17186 Expose dict_contains() and PyDict_Contains() with is about 10% faster
Raymond Hettinger <python@rcn.com>
parents: 31015
diff changeset
150 result = PyDict_Contains(so->data, key);
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
151 if (result == -1 && PyAnySet_Check(key)) {
30995
d4826e097a3f Allow temporary hashability for the __contains__ test.
Raymond Hettinger <python@rcn.com>
parents: 30994
diff changeset
152 PyErr_Clear();
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
153 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
154 if (tmp == NULL)
d4826e097a3f Allow temporary hashability for the __contains__ test.
Raymond Hettinger <python@rcn.com>
parents: 30994
diff changeset
155 return -1;
31019
b2fd7af17186 Expose dict_contains() and PyDict_Contains() with is about 10% faster
Raymond Hettinger <python@rcn.com>
parents: 31015
diff changeset
156 result = PyDict_Contains(so->data, tmp);
30995
d4826e097a3f Allow temporary hashability for the __contains__ test.
Raymond Hettinger <python@rcn.com>
parents: 30994
diff changeset
157 Py_DECREF(tmp);
d4826e097a3f Allow temporary hashability for the __contains__ test.
Raymond Hettinger <python@rcn.com>
parents: 30994
diff changeset
158 }
d4826e097a3f Allow temporary hashability for the __contains__ test.
Raymond Hettinger <python@rcn.com>
parents: 30994
diff changeset
159 return result;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
160 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
161
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
162 static PyObject *
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
163 set_direct_contains(PySetObject *so, PyObject *key)
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
164 {
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
165 long result;
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
166
31178
1bbc2085f213 * Refactor set.__contains__()
Raymond Hettinger <python@rcn.com>
parents: 31177
diff changeset
167 result = set_contains(so, key);
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
168 if (result == -1)
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
169 return NULL;
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
170 return PyBool_FromLong(result);
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
171 }
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
173 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
174
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
175 static PyObject *
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
176 set_copy(PySetObject *so)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
177 {
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
178 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
179 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
180
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
181 static PyObject *
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
182 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
183 {
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
184 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
185 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
186 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
187 }
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
188 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
189 }
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
190
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
191 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
192
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
193 static PyObject *
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
194 set_union(PySetObject *so, PyObject *other)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
195 {
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
196 PySetObject *result;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
197 PyObject *rv;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
198
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
199 result = (PySetObject *)set_copy(so);
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
200 if (result == NULL)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
201 return NULL;
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
202 rv = set_update(result, other);
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
203 if (rv == NULL) {
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
204 Py_DECREF(result);
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
205 return NULL;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
206 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
207 Py_DECREF(rv);
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
208 return (PyObject *)result;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
209 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
210
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
211 PyDoc_STRVAR(union_doc,
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
212 "Return the union of two sets as a new set.\n\
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
213 \n\
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
214 (i.e. all elements that are in either set.)");
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
215
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
216 static PyObject *
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
217 set_or(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
218 {
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
219 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
220 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
221 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
222 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
223 return set_union(so, other);
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
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
226 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
227 set_ior(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
228 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
229 PyObject *result;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
230
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
231 if (!PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
232 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
233 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
234 }
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
235 result = set_update(so, other);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
236 if (result == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
237 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
238 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
239 Py_INCREF(so);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
240 return (PyObject *)so;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
241 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
242
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
243 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
244 set_intersection(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
245 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
246 PySetObject *result;
31300
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
247 PyObject *item, *selfdata, *tgtdata, *it, *tmp;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
248
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
249 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
250 if (result == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
251 return NULL;
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
252 tgtdata = result->data;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
253 selfdata = so->data;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
254
31300
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
255 if (PyAnySet_Check(other))
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
256 other = ((PySetObject *)other)->data;
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
257
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
258 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
259 tmp = selfdata;
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
260 selfdata = other;
31300
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
261 other = tmp;
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
262 }
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
263
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
264 if (PyDict_CheckExact(other)) {
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
265 PyObject *value;
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
266 int pos = 0;
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
267 while (PyDict_Next(other, &pos, &item, &value)) {
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
268 if (PyDict_Contains(selfdata, item)) {
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
269 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
270 Py_DECREF(result);
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
271 return NULL;
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
272 }
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
273 }
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 return (PyObject *)result;
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
276 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
277
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
278 it = PyObject_GetIter(other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
279 if (it == NULL) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
280 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
281 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
282 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
283
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
284 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
285 if (PyDict_Contains(selfdata, item)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
286 if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
287 Py_DECREF(it);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
288 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
289 Py_DECREF(item);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
290 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
291 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
292 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
293 Py_DECREF(item);
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(it);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
296 if (PyErr_Occurred()) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
297 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
298 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
299 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
300 return (PyObject *)result;
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
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
303 PyDoc_STRVAR(intersection_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
304 "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
305 \n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
306 (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
307
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
308 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
309 set_intersection_update(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
310 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
311 PyObject *item, *selfdata, *it, *newdict, *tmp;
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 newdict = PyDict_New();
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
314 if (newdict == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
315 return newdict;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
316
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
317 it = PyObject_GetIter(other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
318 if (it == NULL) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
319 Py_DECREF(newdict);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
320 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
321 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
322
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
323 selfdata = so->data;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
324 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
325 if (PyDict_Contains(selfdata, item)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
326 if (PyDict_SetItem(newdict, item, Py_True) == -1) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
327 Py_DECREF(newdict);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
328 Py_DECREF(it);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
329 Py_DECREF(item);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
330 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
331 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
332 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
333 Py_DECREF(item);
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(it);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
336 if (PyErr_Occurred()) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
337 Py_DECREF(newdict);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
338 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
339 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
340 tmp = so->data;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
341 so->data = newdict;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
342 Py_DECREF(tmp);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
343 Py_RETURN_NONE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
344 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
345
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
346 PyDoc_STRVAR(intersection_update_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
347 "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
348
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
349 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
350 set_and(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
351 {
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
352 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
353 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
354 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
355 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
356 return set_intersection(so, other);
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
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
359 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
360 set_iand(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
361 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
362 PyObject *result;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
363
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
364 if (!PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
365 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
366 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
367 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
368 result = set_intersection_update(so, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
369 if (result == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
370 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
371 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
372 Py_INCREF(so);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
373 return (PyObject *)so;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
374 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
375
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
376 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
377 set_difference_update(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
378 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
379 PyObject *item, *tgtdata, *it;
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 it = PyObject_GetIter(other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
382 if (it == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
383 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
384
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
385 tgtdata = so->data;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
386 while ((item = PyIter_Next(it)) != NULL) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
387 if (PyDict_DelItem(tgtdata, item) == -1) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
388 if (PyErr_ExceptionMatches(PyExc_KeyError))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
389 PyErr_Clear();
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
390 else {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
391 Py_DECREF(it);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
392 Py_DECREF(item);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
393 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
394 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
395 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
396 Py_DECREF(item);
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(it);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
399 if (PyErr_Occurred())
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
400 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
401 Py_RETURN_NONE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
402 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
403
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
404 PyDoc_STRVAR(difference_update_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
405 "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
406
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
407 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
408 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
409 {
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
410 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
411 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
412 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
413 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
414
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
415 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
416 otherdata = other;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
417 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
418 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
419 else {
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
420 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
421 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
422 return result;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
423 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
424 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
425 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
426 return result;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
427 }
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
428 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
429 return NULL;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
430 }
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
431
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
432 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
433 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
434 return NULL;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
435 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
436
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
437 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
438 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
439 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
440 return NULL;
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
441 }
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
442 }
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
443 return result;
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
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
446 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
447 "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
448 \n\
e4682af7aea9 Improve algorithm for set.difference when the input is not a set.
Raymond Hettinger <python@rcn.com>
parents: 31178
diff changeset
449 (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
450 static PyObject *
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
451 set_sub(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
452 {
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
453 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
454 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
455 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
456 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
457 return set_difference(so, other);
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
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
460 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
461 set_isub(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
462 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
463 PyObject *result;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
464
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
465 if (!PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
466 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
467 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
468 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
469 result = set_difference_update(so, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
470 if (result == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
471 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
472 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
473 Py_INCREF(so);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
474 return (PyObject *)so;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
475 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
476
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
477 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
478 set_symmetric_difference_update(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
479 {
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
480 PyObject *selfdata, *otherdata;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
481 PySetObject *otherset = NULL;
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
482 PyObject *key, *value;
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
483 int pos = 0;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
484
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
485 selfdata = so->data;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
486 if (PyDict_Check(other))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
487 otherdata = other;
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
488 else if (PyAnySet_Check(other))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
489 otherdata = ((PySetObject *)other)->data;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
490 else {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
491 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
492 if (otherset == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
493 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
494 otherdata = otherset->data;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
495 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
496
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
497 while (PyDict_Next(otherdata, &pos, &key, &value)) {
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
498 if (PyDict_Contains(selfdata, key)) {
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
499 if (PyDict_DelItem(selfdata, key) == -1) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
500 Py_XDECREF(otherset);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
501 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
502 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
503 } else {
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
504 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
505 Py_XDECREF(otherset);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
506 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
507 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
508 }
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 Py_XDECREF(otherset);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
511 Py_RETURN_NONE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
512 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
513
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
514 PyDoc_STRVAR(symmetric_difference_update_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
515 "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
516
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
517 static PyObject *
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
518 set_symmetric_difference(PySetObject *so, PyObject *other)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
519 {
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
520 PySetObject *result;
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
521 PyObject *selfdata, *otherdata, *tgtdata, *rv, *otherset;
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
522 PyObject *key, *value;
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
523 int pos = 0;
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
524
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
525 if (PyDict_Check(other))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
526 otherdata = other;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
527 else if (PyAnySet_Check(other))
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
528 otherdata = ((PySetObject *)other)->data;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
529 else {
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
530 otherset = make_new_set(so->ob_type, other);
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
531 if (otherset == NULL)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
532 return NULL;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
533 rv = set_symmetric_difference_update((PySetObject *)otherset, (PyObject *)so);
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
534 if (rv == NULL)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
535 return NULL;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
536 Py_DECREF(rv);
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
537 return otherset;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
538 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
539
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
540 result = (PySetObject *)make_new_set(so->ob_type, NULL);
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
541 if (result == NULL)
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
542 return NULL;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
543 tgtdata = result->data;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
544 selfdata = so->data;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
545
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
546 while (PyDict_Next(otherdata, &pos, &key, &value)) {
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
547 if (!PyDict_Contains(selfdata, key)) {
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
548 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
549 Py_DECREF(result);
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
550 return NULL;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
551 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
552 }
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
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
555 pos = 0;
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
556 while (PyDict_Next(selfdata, &pos, &key, &value)) {
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
557 if (!PyDict_Contains(otherdata, key)) {
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
558 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
559 Py_DECREF(result);
31008
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
560 return NULL;
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
561 }
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
562 }
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 return (PyObject *)result;
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
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
568 PyDoc_STRVAR(symmetric_difference_doc,
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
569 "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
570 \n\
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
571 (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
572
4224d807911c * Checkin remaining documentation
Raymond Hettinger <python@rcn.com>
parents: 31004
diff changeset
573 static PyObject *
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
574 set_xor(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
575 {
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
576 if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
577 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
578 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
579 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
580 return set_symmetric_difference(so, other);
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
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
583 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
584 set_ixor(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
585 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
586 PyObject *result;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
587
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
588 if (!PyAnySet_Check(other)) {
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
589 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
590 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
591 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
592 result = set_symmetric_difference_update(so, other);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
593 if (result == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
594 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
595 Py_DECREF(result);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
596 Py_INCREF(so);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
597 return (PyObject *)so;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
598 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
599
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
600 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
601 set_issubset(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
602 {
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
603 PyObject *otherdata, *tmp, *result;
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
604 PyObject *key, *value;
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
605 int pos = 0;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
606
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
607 if (!PyAnySet_Check(other)) {
30994
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
608 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
609 if (tmp == NULL)
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
610 return NULL;
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
611 result = set_issubset(so, tmp);
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
612 Py_DECREF(tmp);
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
613 return result;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
614 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
615 if (set_len(so) > set_len((PySetObject *)other))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
616 Py_RETURN_FALSE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
617
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
618 otherdata = ((PySetObject *)other)->data;
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
619 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
620 if (!PyDict_Contains(otherdata, key))
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
621 Py_RETURN_FALSE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
622 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
623 Py_RETURN_TRUE;
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
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
626 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
627
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
628 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
629 set_issuperset(PySetObject *so, PyObject *other)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
630 {
30994
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
631 PyObject *tmp, *result;
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
632
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
633 if (!PyAnySet_Check(other)) {
30994
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
634 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
635 if (tmp == NULL)
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
636 return NULL;
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
637 result = set_issuperset(so, tmp);
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
638 Py_DECREF(tmp);
632b223cb8e8 issubset() and issuperset() to work with general iterables
Raymond Hettinger <python@rcn.com>
parents: 30993
diff changeset
639 return result;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
640 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
641 return set_issubset((PySetObject *)other, (PyObject *)so);
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
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
644 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
645
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
646 static long
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
647 set_nohash(PyObject *self)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
648 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
649 PyErr_SetString(PyExc_TypeError, "set objects are unhashable");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
650 return -1;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
651 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
652
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
653 static int
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
654 set_nocmp(PyObject *self)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
655 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
656 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
657 return -1;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
658 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
659
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
660 static long
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
661 frozenset_hash(PyObject *self)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
662 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
663 PySetObject *so = (PySetObject *)self;
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
664 PyObject *key, *value;
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
665 int pos = 0;
31178
1bbc2085f213 * Refactor set.__contains__()
Raymond Hettinger <python@rcn.com>
parents: 31177
diff changeset
666 long hash = 0;
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
667
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
668 if (so->hash != -1)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
669 return so->hash;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
670
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
671 while (PyDict_Next(so->data, &pos, &key, &value)) {
31004
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
672 /* Multiplying by a large prime increases the bit dispersion for
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
673 closely spaced hash values. The is important because some
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
674 use cases have many combinations of a small number of
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
675 elements with nearby hashes so that many distinct combinations
5b77bc4d25b1 * Simplify hash function and add test to show effectiveness of the hash
Raymond Hettinger <python@rcn.com>
parents: 31002
diff changeset
676 collapse to only a handful of distinct hash values. */
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
677 hash ^= PyObject_Hash(key) * 3644798167u;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
678 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
679 so->hash = hash;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
680 return hash;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
681 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
682
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
683 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
684 set_richcompare(PySetObject *v, PyObject *w, int op)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
685 {
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
686 if(!PyAnySet_Check(w)) {
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
687 if (op == Py_EQ)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
688 Py_RETURN_FALSE;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
689 if (op == Py_NE)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
690 Py_RETURN_TRUE;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
691 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
692 return NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
693 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
694 switch (op) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
695 case Py_EQ:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
696 case Py_NE:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
697 return PyObject_RichCompare(((PySetObject *)v)->data,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
698 ((PySetObject *)w)->data, op);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
699 case Py_LE:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
700 return set_issubset((PySetObject *)v, w);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
701 case Py_GE:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
702 return set_issuperset((PySetObject *)v, w);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
703 case Py_LT:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
704 if (set_len(v) >= set_len((PySetObject *)w))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
705 Py_RETURN_FALSE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
706 return set_issubset((PySetObject *)v, w);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
707 case Py_GT:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
708 if (set_len(v) <= set_len((PySetObject *)w))
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
709 Py_RETURN_FALSE;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
710 return set_issuperset((PySetObject *)v, w);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
711 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
712 Py_INCREF(Py_NotImplemented);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
713 return Py_NotImplemented;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
714 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
715
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
716 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
717 set_repr(PySetObject *so)
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 PyObject *keys, *result, *listrepr;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
720
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
721 keys = PyDict_Keys(so->data);
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
722 if (keys == NULL)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
723 return NULL;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
724 listrepr = PyObject_Repr(keys);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
725 Py_DECREF(keys);
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
726 if (listrepr == NULL)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
727 return NULL;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
728
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
729 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
730 PyString_AS_STRING(listrepr));
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
731 Py_DECREF(listrepr);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
732 return result;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
733 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
734
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
735 static int
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
736 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
737 {
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
738 PyObject *key, *value;
31300
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
739 int pos=0;
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
740 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
741 char *separator = ", ";
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
742
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
743 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
744 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
745 fputs(emit, fp);
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
746 emit = separator;
31175
5cc1a400a02c Use dictionary specific looping idiom where possible.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
747 if (PyObject_Print(key, fp, 0) != 0)
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
748 return -1;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
749 }
31300
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
750 fputs("])", fp);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
751 return 0;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
752 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
753
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
754 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
755 set_clear(PySetObject *so)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
756 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
757 PyDict_Clear(so->data);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
758 so->hash = -1;
31178
1bbc2085f213 * Refactor set.__contains__()
Raymond Hettinger <python@rcn.com>
parents: 31177
diff changeset
759 Py_RETURN_NONE;
30929
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
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
762 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
763
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
764 static int
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
765 set_tp_clear(PySetObject *so)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
766 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
767 PyDict_Clear(so->data);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
768 so->hash = -1;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
769 return 0;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
770 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
771
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
772 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
773 set_add(PySetObject *so, PyObject *item)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
774 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
775 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
776 return NULL;
31178
1bbc2085f213 * Refactor set.__contains__()
Raymond Hettinger <python@rcn.com>
parents: 31177
diff changeset
777 Py_RETURN_NONE;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
778 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
779
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
780 PyDoc_STRVAR(add_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
781 "Add an element to a set.\n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
782 \n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
783 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
784
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
785 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
786 set_remove(PySetObject *so, PyObject *item)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
787 {
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
788 PyObject *tmp, *result;
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
789
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
790 if (PyType_IsSubtype(item->ob_type, &PySet_Type)) {
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
791 tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
792 if (tmp == NULL)
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
793 return NULL;
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
794 result = set_remove(so, tmp);
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
795 Py_DECREF(tmp);
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
796 return result;
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
797 }
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
798
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
799 if (PyDict_DelItem(so->data, item) == -1)
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
800 return NULL;
31178
1bbc2085f213 * Refactor set.__contains__()
Raymond Hettinger <python@rcn.com>
parents: 31177
diff changeset
801 Py_RETURN_NONE;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
802 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
803
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
804 PyDoc_STRVAR(remove_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
805 "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
806 \n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
807 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
808
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
809 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
810 set_discard(PySetObject *so, PyObject *item)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
811 {
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
812 PyObject *tmp, *result;
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
813
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
814 if (PyType_IsSubtype(item->ob_type, &PySet_Type)) {
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
815 tmp = frozenset_dict_wrapper(((PySetObject *)(item))->data);
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
816 if (tmp == NULL)
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
817 return NULL;
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
818 result = set_discard(so, tmp);
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
819 Py_DECREF(tmp);
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
820 return result;
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
821 }
31002
622a76bc0237 Extend temporary hashability to remove() and discard().
Raymond Hettinger <python@rcn.com>
parents: 30995
diff changeset
822
30934
4effcbf80b45 Implement straightforward suggestions from gcc warnings (remove unused
Guido van Rossum <guido@python.org>
parents: 30932
diff changeset
823 if (PyDict_DelItem(so->data, item) == -1) {
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
824 if (!PyErr_ExceptionMatches(PyExc_KeyError))
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
825 return NULL;
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
826 PyErr_Clear();
30934
4effcbf80b45 Implement straightforward suggestions from gcc warnings (remove unused
Guido van Rossum <guido@python.org>
parents: 30932
diff changeset
827 }
31178
1bbc2085f213 * Refactor set.__contains__()
Raymond Hettinger <python@rcn.com>
parents: 31177
diff changeset
828 Py_RETURN_NONE;
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
829 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
830
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
831 PyDoc_STRVAR(discard_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
832 "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
833 \n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
834 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
835
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
836 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
837 set_pop(PySetObject *so)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
838 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
839 PyObject *key, *value;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
840 int pos = 0;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
841
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
842 if (!PyDict_Next(so->data, &pos, &key, &value)) {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
843 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
844 return NULL;
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 Py_INCREF(key);
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
847 if (PyDict_DelItem(so->data, key) == -1) {
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
848 Py_DECREF(key);
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
849 return NULL;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
850 }
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
851 return key;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
852 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
853
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
854 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
855
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
856 static PyObject *
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
857 set_reduce(PySetObject *so)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
858 {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
859 PyObject *keys=NULL, *args=NULL, *result=NULL;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
860
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
861 keys = PyDict_Keys(so->data);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
862 if (keys == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
863 goto done;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
864 args = PyTuple_Pack(1, keys);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
865 if (args == NULL)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
866 goto done;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
867 result = PyTuple_Pack(2, so->ob_type, args);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
868 done:
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
869 Py_XDECREF(args);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
870 Py_XDECREF(keys);
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
871 return result;
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
872 }
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
873
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
874 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
875
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
876 static int
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
877 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
878 {
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
879 PyObject *iterable = NULL;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
880 PyObject *result;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
881
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
882 if (!PyAnySet_Check(self))
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
883 return -1;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
884 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
885 return -1;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
886 PyDict_Clear(self->data);
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
887 self->hash = -1;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
888 if (iterable == NULL)
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
889 return 0;
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
890 result = set_update(self, iterable);
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
891 if (result != NULL) {
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
892 Py_DECREF(result);
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
893 return 0;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
894 }
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
895 return -1;
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
896 }
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
897
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
898 static PySequenceMethods set_as_sequence = {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
899 (inquiry)set_len, /* sq_length */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
900 0, /* sq_concat */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
901 0, /* sq_repeat */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
902 0, /* sq_item */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
903 0, /* sq_slice */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
904 0, /* sq_ass_item */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
905 0, /* sq_ass_slice */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
906 (objobjproc)set_contains, /* sq_contains */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
907 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
908
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
909 /* set object ********************************************************/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
910
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
911 static PyMethodDef set_methods[] = {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
912 {"add", (PyCFunction)set_add, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
913 add_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
914 {"clear", (PyCFunction)set_clear, METH_NOARGS,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
915 clear_doc},
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
916 {"__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
917 contains_doc},
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
918 {"copy", (PyCFunction)set_copy, METH_NOARGS,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
919 copy_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
920 {"discard", (PyCFunction)set_discard, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
921 discard_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
922 {"difference", (PyCFunction)set_difference, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
923 difference_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
924 {"difference_update", (PyCFunction)set_difference_update, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
925 difference_update_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
926 {"intersection",(PyCFunction)set_intersection, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
927 intersection_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
928 {"intersection_update",(PyCFunction)set_intersection_update, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
929 intersection_update_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
930 {"issubset", (PyCFunction)set_issubset, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
931 issubset_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
932 {"issuperset", (PyCFunction)set_issuperset, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
933 issuperset_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
934 {"pop", (PyCFunction)set_pop, METH_NOARGS,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
935 pop_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
936 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
937 reduce_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
938 {"remove", (PyCFunction)set_remove, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
939 remove_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
940 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
941 symmetric_difference_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
942 {"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
943 symmetric_difference_update_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
944 {"union", (PyCFunction)set_union, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
945 union_doc},
31015
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
946 {"update", (PyCFunction)set_update, METH_O,
8d413578eb1a Factor out more duplicate code.
Raymond Hettinger <python@rcn.com>
parents: 31012
diff changeset
947 update_doc},
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
948 {NULL, NULL} /* sentinel */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
949 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
950
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
951 static PyNumberMethods set_as_number = {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
952 0, /*nb_add*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
953 (binaryfunc)set_sub, /*nb_subtract*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
954 0, /*nb_multiply*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
955 0, /*nb_divide*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
956 0, /*nb_remainder*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
957 0, /*nb_divmod*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
958 0, /*nb_power*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
959 0, /*nb_negative*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
960 0, /*nb_positive*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
961 0, /*nb_absolute*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
962 0, /*nb_nonzero*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
963 0, /*nb_invert*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
964 0, /*nb_lshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
965 0, /*nb_rshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
966 (binaryfunc)set_and, /*nb_and*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
967 (binaryfunc)set_xor, /*nb_xor*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
968 (binaryfunc)set_or, /*nb_or*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
969 0, /*nb_coerce*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
970 0, /*nb_int*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
971 0, /*nb_long*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
972 0, /*nb_float*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
973 0, /*nb_oct*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
974 0, /*nb_hex*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
975 0, /*nb_inplace_add*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
976 (binaryfunc)set_isub, /*nb_inplace_subtract*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
977 0, /*nb_inplace_multiply*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
978 0, /*nb_inplace_divide*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
979 0, /*nb_inplace_remainder*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
980 0, /*nb_inplace_power*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
981 0, /*nb_inplace_lshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
982 0, /*nb_inplace_rshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
983 (binaryfunc)set_iand, /*nb_inplace_and*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
984 (binaryfunc)set_ixor, /*nb_inplace_xor*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
985 (binaryfunc)set_ior, /*nb_inplace_or*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
986 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
987
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
988 PyDoc_STRVAR(set_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
989 "set(iterable) --> set object\n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
990 \n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
991 Build an unordered collection.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
992
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
993 PyTypeObject PySet_Type = {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
994 PyObject_HEAD_INIT(&PyType_Type)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
995 0, /* ob_size */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
996 "set", /* tp_name */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
997 sizeof(PySetObject), /* tp_basicsize */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
998 0, /* tp_itemsize */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
999 /* methods */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1000 (destructor)set_dealloc, /* tp_dealloc */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1001 (printfunc)set_tp_print, /* tp_print */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1002 0, /* tp_getattr */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1003 0, /* tp_setattr */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1004 (cmpfunc)set_nocmp, /* tp_compare */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1005 (reprfunc)set_repr, /* tp_repr */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1006 &set_as_number, /* tp_as_number */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1007 &set_as_sequence, /* tp_as_sequence */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1008 0, /* tp_as_mapping */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1009 set_nohash, /* tp_hash */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1010 0, /* tp_call */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1011 0, /* tp_str */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1012 PyObject_GenericGetAttr, /* tp_getattro */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1013 0, /* tp_setattro */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1014 0, /* tp_as_buffer */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1015 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
1016 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
1017 set_doc, /* tp_doc */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1018 (traverseproc)set_traverse, /* tp_traverse */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1019 (inquiry)set_tp_clear, /* tp_clear */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1020 (richcmpfunc)set_richcompare, /* tp_richcompare */
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31628
diff changeset
1021 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1022 (getiterfunc)set_iter, /* tp_iter */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1023 0, /* tp_iternext */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1024 set_methods, /* tp_methods */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1025 0, /* tp_members */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1026 0, /* tp_getset */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1027 0, /* tp_base */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1028 0, /* tp_dict */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1029 0, /* tp_descr_get */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1030 0, /* tp_descr_set */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1031 0, /* tp_dictoffset */
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1032 (initproc)set_init, /* tp_init */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1033 PyType_GenericAlloc, /* tp_alloc */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1034 set_new, /* tp_new */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1035 PyObject_GC_Del, /* tp_free */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1036 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1037
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1038 /* frozenset object ********************************************************/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1039
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1040
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1041 static PyMethodDef frozenset_methods[] = {
31177
eeaa3660671c Refactor set.discard() and set.remove().
Raymond Hettinger <python@rcn.com>
parents: 31175
diff changeset
1042 {"__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
1043 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
1044 {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1045 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
1046 {"difference", (PyCFunction)set_difference, METH_O,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1047 difference_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1048 {"intersection",(PyCFunction)set_intersection, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1049 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
1050 {"issubset", (PyCFunction)set_issubset, METH_O,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1051 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
1052 {"issuperset", (PyCFunction)set_issuperset, METH_O,
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1053 issuperset_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1054 {"__reduce__", (PyCFunction)set_reduce, METH_NOARGS,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1055 reduce_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1056 {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1057 symmetric_difference_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1058 {"union", (PyCFunction)set_union, METH_O,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1059 union_doc},
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1060 {NULL, NULL} /* sentinel */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1061 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1062
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1063 static PyNumberMethods frozenset_as_number = {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1064 0, /*nb_add*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1065 (binaryfunc)set_sub, /*nb_subtract*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1066 0, /*nb_multiply*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1067 0, /*nb_divide*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1068 0, /*nb_remainder*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1069 0, /*nb_divmod*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1070 0, /*nb_power*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1071 0, /*nb_negative*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1072 0, /*nb_positive*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1073 0, /*nb_absolute*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1074 0, /*nb_nonzero*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1075 0, /*nb_invert*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1076 0, /*nb_lshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1077 0, /*nb_rshift*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1078 (binaryfunc)set_and, /*nb_and*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1079 (binaryfunc)set_xor, /*nb_xor*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1080 (binaryfunc)set_or, /*nb_or*/
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1081 };
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1082
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1083 PyDoc_STRVAR(frozenset_doc,
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1084 "frozenset(iterable) --> frozenset object\n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1085 \n\
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1086 Build an immutable unordered collection.");
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1087
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1088 PyTypeObject PyFrozenSet_Type = {
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1089 PyObject_HEAD_INIT(&PyType_Type)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1090 0, /* ob_size */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1091 "frozenset", /* tp_name */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1092 sizeof(PySetObject), /* tp_basicsize */
31300
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
1093 0, /* tp_itemsize */
bf8402a56fa4 * Simplify and speedup logic for tp_print.
Raymond Hettinger <python@rcn.com>
parents: 31203
diff changeset
1094 /* methods */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1095 (destructor)set_dealloc, /* tp_dealloc */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1096 (printfunc)set_tp_print, /* tp_print */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1097 0, /* tp_getattr */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1098 0, /* tp_setattr */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1099 (cmpfunc)set_nocmp, /* tp_compare */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1100 (reprfunc)set_repr, /* tp_repr */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1101 &frozenset_as_number, /* tp_as_number */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1102 &set_as_sequence, /* tp_as_sequence */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1103 0, /* tp_as_mapping */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1104 frozenset_hash, /* tp_hash */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1105 0, /* tp_call */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1106 0, /* tp_str */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1107 PyObject_GenericGetAttr, /* tp_getattro */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1108 0, /* tp_setattro */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1109 0, /* tp_as_buffer */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1110 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
1111 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
1112 frozenset_doc, /* tp_doc */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1113 (traverseproc)set_traverse, /* tp_traverse */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1114 0, /* tp_clear */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1115 (richcmpfunc)set_richcompare, /* tp_richcompare */
32158
679e5c8effae Make sets and deques weak referencable.
Raymond Hettinger <python@rcn.com>
parents: 31628
diff changeset
1116 offsetof(PySetObject, weakreflist), /* tp_weaklistoffset */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1117 (getiterfunc)set_iter, /* tp_iter */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1118 0, /* tp_iternext */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1119 frozenset_methods, /* tp_methods */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1120 0, /* tp_members */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1121 0, /* tp_getset */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1122 0, /* tp_base */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1123 0, /* tp_dict */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1124 0, /* tp_descr_get */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1125 0, /* tp_descr_set */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1126 0, /* tp_dictoffset */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1127 0, /* tp_init */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1128 PyType_GenericAlloc, /* tp_alloc */
30931
fa6e8d4f88c6 Various fixups (most suggested by Armin Rigo).
Raymond Hettinger <python@rcn.com>
parents: 30930
diff changeset
1129 frozenset_new, /* tp_new */
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1130 PyObject_GC_Del, /* tp_free */
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1131 };