annotate Objects/setobject.c @ 38664:7619216bbc19 2.4

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