Debian Codegraph @

Modules/_testlimitedcapi/set.c

raw
1 #include "parts.h"
2 #include "util.h"
3
4 static PyObject *
5 set_check(PyObject *self, PyObject *obj)
6 {
7 NULLABLE(obj);
8 RETURN_INT(PySet_Check(obj));
9 }
10
11 static PyObject *
12 set_checkexact(PyObject *self, PyObject *obj)
13 {
14 NULLABLE(obj);
15 RETURN_INT(PySet_CheckExact(obj));
16 }
17
18 static PyObject *
19 frozenset_check(PyObject *self, PyObject *obj)
20 {
21 NULLABLE(obj);
22 RETURN_INT(PyFrozenSet_Check(obj));
23 }
24
25 static PyObject *
26 frozenset_checkexact(PyObject *self, PyObject *obj)
27 {
28 NULLABLE(obj);
29 RETURN_INT(PyFrozenSet_CheckExact(obj));
30 }
31
32 static PyObject *
33 anyset_check(PyObject *self, PyObject *obj)
34 {
35 NULLABLE(obj);
36 RETURN_INT(PyAnySet_Check(obj));
37 }
38
39 static PyObject *
40 anyset_checkexact(PyObject *self, PyObject *obj)
41 {
42 NULLABLE(obj);
43 RETURN_INT(PyAnySet_CheckExact(obj));
44 }
45
46 static PyObject *
47 set_new(PyObject *self, PyObject *args)
48 {
49 PyObject *iterable = NULL;
50 if (!PyArg_ParseTuple(args, "|O", &iterable)) {
51 return NULL;
52 }
53 return PySet_New(iterable);
54 }
55
56 static PyObject *
57 frozenset_new(PyObject *self, PyObject *args)
58 {
59 PyObject *iterable = NULL;
60 if (!PyArg_ParseTuple(args, "|O", &iterable)) {
61 return NULL;
62 }
63 return PyFrozenSet_New(iterable);
64 }
65
66 static PyObject *
67 set_size(PyObject *self, PyObject *obj)
68 {
69 NULLABLE(obj);
70 RETURN_SIZE(PySet_Size(obj));
71 }
72
73 static PyObject *
74 set_contains(PyObject *self, PyObject *args)
75 {
76 PyObject *obj, *item;
77 if (!PyArg_ParseTuple(args, "OO", &obj, &item)) {
78 return NULL;
79 }
80 NULLABLE(obj);
81 NULLABLE(item);
82 RETURN_INT(PySet_Contains(obj, item));
83 }
84
85 static PyObject *
86 set_add(PyObject *self, PyObject *args)
87 {
88 PyObject *obj, *item;
89 if (!PyArg_ParseTuple(args, "OO", &obj, &item)) {
90 return NULL;
91 }
92 NULLABLE(obj);
93 NULLABLE(item);
94 RETURN_INT(PySet_Add(obj, item));
95 }
96
97 static PyObject *
98 set_discard(PyObject *self, PyObject *args)
99 {
100 PyObject *obj, *item;
101 if (!PyArg_ParseTuple(args, "OO", &obj, &item)) {
102 return NULL;
103 }
104 NULLABLE(obj);
105 NULLABLE(item);
106 RETURN_INT(PySet_Discard(obj, item));
107 }
108
109 static PyObject *
110 set_pop(PyObject *self, PyObject *obj)
111 {
112 NULLABLE(obj);
113 return PySet_Pop(obj);
114 }
115
116 static PyObject *
117 set_clear(PyObject *self, PyObject *obj)
118 {
119 NULLABLE(obj);
120 RETURN_INT(PySet_Clear(obj));
121 }
122
123 static PyObject *
124 test_frozenset_add_in_capi(PyObject *self, PyObject *Py_UNUSED(obj))
125 {
126 // Test that `frozenset` can be used with `PySet_Add`,
127 // when frozenset is just created in CAPI.
128 PyObject *fs = PyFrozenSet_New(NULL);
129 if (fs == NULL) {
130 return NULL;
131 }
132 PyObject *num = PyLong_FromLong(1);
133 if (num == NULL) {
134 goto error;
135 }
136 if (PySet_Add(fs, num) < 0) {
137 goto error;
138 }
139 int contains = PySet_Contains(fs, num);
140 if (contains < 0) {
141 goto error;
142 }
143 else if (contains == 0) {
144 goto unexpected;
145 }
146 Py_DECREF(fs);
147 Py_DECREF(num);
148 Py_RETURN_NONE;
149
150 unexpected:
151 PyErr_SetString(PyExc_ValueError, "set does not contain expected value");
152 error:
153 Py_DECREF(fs);
154 Py_XDECREF(num);
155 return NULL;
156 }
157
158 static PyObject *
159 test_set_contains_does_not_convert_unhashable_key(PyObject *self, PyObject *Py_UNUSED(obj))
160 {
161 // See https://docs.python.org/3/c-api/set.html#c.PySet_Contains
162 PyObject *outer_set = PySet_New(NULL);
163
164 PyObject *needle = PySet_New(NULL);
165 if (needle == NULL) {
166 Py_DECREF(outer_set);
167 return NULL;
168 }
169
170 PyObject *num = PyLong_FromLong(42);
171 if (num == NULL) {
172 Py_DECREF(outer_set);
173 Py_DECREF(needle);
174 return NULL;
175 }
176
177 if (PySet_Add(needle, num) < 0) {
178 Py_DECREF(outer_set);
179 Py_DECREF(needle);
180 Py_DECREF(num);
181 return NULL;
182 }
183
184 int result = PySet_Contains(outer_set, needle);
185
186 Py_DECREF(num);
187 Py_DECREF(needle);
188 Py_DECREF(outer_set);
189
190 if (result < 0) {
191 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
192 PyErr_Clear();
193 Py_RETURN_NONE;
194 }
195 return NULL;
196 }
197
198 PyErr_SetString(PyExc_AssertionError,
199 "PySet_Contains should have raised TypeError for unhashable key");
200 return NULL;
201 }
202
203 static PyMethodDef test_methods[] = {
204 {"set_check", set_check, METH_O},
205 {"set_checkexact", set_checkexact, METH_O},
206 {"frozenset_check", frozenset_check, METH_O},
207 {"frozenset_checkexact", frozenset_checkexact, METH_O},
208 {"anyset_check", anyset_check, METH_O},
209 {"anyset_checkexact", anyset_checkexact, METH_O},
210
211 {"set_new", set_new, METH_VARARGS},
212 {"frozenset_new", frozenset_new, METH_VARARGS},
213
214 {"set_size", set_size, METH_O},
215 {"set_contains", set_contains, METH_VARARGS},
216 {"set_add", set_add, METH_VARARGS},
217 {"set_discard", set_discard, METH_VARARGS},
218 {"set_pop", set_pop, METH_O},
219 {"set_clear", set_clear, METH_O},
220
221 {"test_frozenset_add_in_capi", test_frozenset_add_in_capi, METH_NOARGS},
222 {"test_set_contains_does_not_convert_unhashable_key",
223 test_set_contains_does_not_convert_unhashable_key, METH_NOARGS},
224
225 {NULL},
226 };
227
228 int
229 _PyTestLimitedCAPI_Init_Set(PyObject *m)
230 {
231 if (PyModule_AddFunctions(m, test_methods) < 0) {
232 return -1;
233 }
234
235 return 0;
236 }