forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathset.c
More file actions
252 lines (220 loc) · 5.73 KB
/
set.c
File metadata and controls
252 lines (220 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include "parts.h"
#include "util.h"
static PyObject *
set_check(PyObject *self, PyObject *obj)
{
NULLABLE(obj);
RETURN_INT(PySet_Check(obj));
}
static PyObject *
set_checkexact(PyObject *self, PyObject *obj)
{
NULLABLE(obj);
RETURN_INT(PySet_CheckExact(obj));
}
static PyObject *
frozenset_check(PyObject *self, PyObject *obj)
{
NULLABLE(obj);
RETURN_INT(PyFrozenSet_Check(obj));
}
static PyObject *
frozenset_checkexact(PyObject *self, PyObject *obj)
{
NULLABLE(obj);
RETURN_INT(PyFrozenSet_CheckExact(obj));
}
static PyObject *
anyset_check(PyObject *self, PyObject *obj)
{
NULLABLE(obj);
RETURN_INT(PyAnySet_Check(obj));
}
static PyObject *
anyset_checkexact(PyObject *self, PyObject *obj)
{
NULLABLE(obj);
RETURN_INT(PyAnySet_CheckExact(obj));
}
static PyObject *
set_new(PyObject *self, PyObject *args)
{
PyObject *iterable = NULL;
if (!PyArg_ParseTuple(args, "|O", &iterable)) {
return NULL;
}
return PySet_New(iterable);
}
static PyObject *
frozenset_new(PyObject *self, PyObject *args)
{
PyObject *iterable = NULL;
if (!PyArg_ParseTuple(args, "|O", &iterable)) {
return NULL;
}
return PyFrozenSet_New(iterable);
}
static PyObject *
set_size(PyObject *self, PyObject *obj)
{
NULLABLE(obj);
RETURN_SIZE(PySet_Size(obj));
}
static PyObject *
set_contains(PyObject *self, PyObject *args)
{
PyObject *obj, *item;
if (!PyArg_ParseTuple(args, "OO", &obj, &item)) {
return NULL;
}
NULLABLE(obj);
NULLABLE(item);
RETURN_INT(PySet_Contains(obj, item));
}
static PyObject *
set_add(PyObject *self, PyObject *args)
{
PyObject *obj, *item;
if (!PyArg_ParseTuple(args, "OO", &obj, &item)) {
return NULL;
}
NULLABLE(obj);
NULLABLE(item);
RETURN_INT(PySet_Add(obj, item));
}
static PyObject *
set_discard(PyObject *self, PyObject *args)
{
PyObject *obj, *item;
if (!PyArg_ParseTuple(args, "OO", &obj, &item)) {
return NULL;
}
NULLABLE(obj);
NULLABLE(item);
RETURN_INT(PySet_Discard(obj, item));
}
static PyObject *
set_pop(PyObject *self, PyObject *obj)
{
NULLABLE(obj);
return PySet_Pop(obj);
}
static PyObject *
set_clear(PyObject *self, PyObject *obj)
{
NULLABLE(obj);
RETURN_INT(PySet_Clear(obj));
}
static PyObject *
test_frozenset_add_in_capi(PyObject *self, PyObject *Py_UNUSED(obj))
{
// Test that `frozenset` can be used with `PySet_Add`,
// when frozenset is just created in CAPI.
PyObject *fs = PyFrozenSet_New(NULL);
if (fs == NULL) {
return NULL;
}
PyObject *num = PyLong_FromLong(1);
if (num == NULL) {
goto error;
}
if (PySet_Add(fs, num) < 0) {
goto error;
}
int contains = PySet_Contains(fs, num);
if (contains < 0) {
goto error;
}
else if (contains == 0) {
goto unexpected;
}
Py_DECREF(fs);
Py_DECREF(num);
Py_RETURN_NONE;
unexpected:
PyErr_SetString(PyExc_ValueError, "set does not contain expected value");
error:
Py_DECREF(fs);
Py_XDECREF(num);
return NULL;
}
static PyObject *
test_set_contains_does_not_convert_unhashable_key(PyObject *self, PyObject *Py_UNUSED(obj))
{
// The documentation of PySet_Contains state:
//
// int PySet_Contains(PyObject *anyset, PyObject *key)
//
// Part of the Stable ABI.
//
// ... Unlike the Python __contains__() method, this function does not
// automatically convert unhashable sets [key] into temporary frozensets.
// Raise a TypeError if the key is unhashable.
//
// That is to say {2,3} in {1, 2, frozenset({2,3})}
// ^_ will be converted in a frozenset in Python code.
// But not if using PySet_Contains(..., key)
//
// We test that this behavior is unchanged as this is a stable API.
PyObject *outer_set = PySet_New(NULL);
PyObject *needle = PySet_New(NULL);
if (needle == NULL) {
Py_DECREF(outer_set);
return NULL;
}
PyObject *num = PyLong_FromLong(42);
if (num == NULL) {
Py_DECREF(outer_set);
Py_DECREF(needle);
return NULL;
}
// Add an element to needle to make it {42}
if (PySet_Add(needle, num) < 0) {
Py_DECREF(outer_set);
Py_DECREF(needle);
Py_DECREF(num);
return NULL;
}
int result = PySet_Contains(outer_set, needle);
Py_DECREF(num);
Py_DECREF(needle);
Py_DECREF(outer_set);
if (result < 0) {
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
PyErr_Clear();
Py_RETURN_NONE;
}
return NULL;
}
PyErr_SetString(PyExc_AssertionError,
"PySet_Contains should have raised TypeError for unhashable key");
return NULL;
}
static PyMethodDef test_methods[] = {
{"set_check", set_check, METH_O},
{"set_checkexact", set_checkexact, METH_O},
{"frozenset_check", frozenset_check, METH_O},
{"frozenset_checkexact", frozenset_checkexact, METH_O},
{"anyset_check", anyset_check, METH_O},
{"anyset_checkexact", anyset_checkexact, METH_O},
{"set_new", set_new, METH_VARARGS},
{"frozenset_new", frozenset_new, METH_VARARGS},
{"set_size", set_size, METH_O},
{"set_contains", set_contains, METH_VARARGS},
{"set_add", set_add, METH_VARARGS},
{"set_discard", set_discard, METH_VARARGS},
{"set_pop", set_pop, METH_O},
{"set_clear", set_clear, METH_O},
{"test_frozenset_add_in_capi", test_frozenset_add_in_capi, METH_NOARGS},
{"test_set_contains_does_not_convert_unhashable_key",
test_set_contains_does_not_convert_unhashable_key, METH_NOARGS},
{NULL},
};
int
_PyTestLimitedCAPI_Init_Set(PyObject *m)
{
if (PyModule_AddFunctions(m, test_methods) < 0) {
return -1;
}
return 0;
}