1 #include "parts.h"
2 #include "util.h"
3
4 static PyObject *
set_check(PyObject * self,PyObject * obj)5 set_check(PyObject *self, PyObject *obj)
6 {
7 NULLABLE(obj);
8 RETURN_INT(PySet_Check(obj));
9 }
10
11 static PyObject *
set_checkexact(PyObject * self,PyObject * obj)12 set_checkexact(PyObject *self, PyObject *obj)
13 {
14 NULLABLE(obj);
15 RETURN_INT(PySet_CheckExact(obj));
16 }
17
18 static PyObject *
frozenset_check(PyObject * self,PyObject * obj)19 frozenset_check(PyObject *self, PyObject *obj)
20 {
21 NULLABLE(obj);
22 RETURN_INT(PyFrozenSet_Check(obj));
23 }
24
25 static PyObject *
frozenset_checkexact(PyObject * self,PyObject * obj)26 frozenset_checkexact(PyObject *self, PyObject *obj)
27 {
28 NULLABLE(obj);
29 RETURN_INT(PyFrozenSet_CheckExact(obj));
30 }
31
32 static PyObject *
anyset_check(PyObject * self,PyObject * obj)33 anyset_check(PyObject *self, PyObject *obj)
34 {
35 NULLABLE(obj);
36 RETURN_INT(PyAnySet_Check(obj));
37 }
38
39 static PyObject *
anyset_checkexact(PyObject * self,PyObject * obj)40 anyset_checkexact(PyObject *self, PyObject *obj)
41 {
42 NULLABLE(obj);
43 RETURN_INT(PyAnySet_CheckExact(obj));
44 }
45
46 static PyObject *
set_new(PyObject * self,PyObject * args)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 *
frozenset_new(PyObject * self,PyObject * args)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 *
set_size(PyObject * self,PyObject * obj)67 set_size(PyObject *self, PyObject *obj)
68 {
69 NULLABLE(obj);
70 RETURN_SIZE(PySet_Size(obj));
71 }
72
73 static PyObject *
set_contains(PyObject * self,PyObject * args)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 *
set_add(PyObject * self,PyObject * args)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 *
set_discard(PyObject * self,PyObject * args)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 *
set_pop(PyObject * self,PyObject * obj)110 set_pop(PyObject *self, PyObject *obj)
111 {
112 NULLABLE(obj);
113 return PySet_Pop(obj);
114 }
115
116 static PyObject *
set_clear(PyObject * self,PyObject * obj)117 set_clear(PyObject *self, PyObject *obj)
118 {
119 NULLABLE(obj);
120 RETURN_INT(PySet_Clear(obj));
121 }
122
123 static PyObject *
test_frozenset_add_in_capi(PyObject * self,PyObject * Py_UNUSED (obj))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 PyMethodDef test_methods[] = {
159 {"set_check", set_check, METH_O},
160 {"set_checkexact", set_checkexact, METH_O},
161 {"frozenset_check", frozenset_check, METH_O},
162 {"frozenset_checkexact", frozenset_checkexact, METH_O},
163 {"anyset_check", anyset_check, METH_O},
164 {"anyset_checkexact", anyset_checkexact, METH_O},
165
166 {"set_new", set_new, METH_VARARGS},
167 {"frozenset_new", frozenset_new, METH_VARARGS},
168
169 {"set_size", set_size, METH_O},
170 {"set_contains", set_contains, METH_VARARGS},
171 {"set_add", set_add, METH_VARARGS},
172 {"set_discard", set_discard, METH_VARARGS},
173 {"set_pop", set_pop, METH_O},
174 {"set_clear", set_clear, METH_O},
175
176 {"test_frozenset_add_in_capi", test_frozenset_add_in_capi, METH_NOARGS},
177
178 {NULL},
179 };
180
181 int
_PyTestLimitedCAPI_Init_Set(PyObject * m)182 _PyTestLimitedCAPI_Init_Set(PyObject *m)
183 {
184 if (PyModule_AddFunctions(m, test_methods) < 0) {
185 return -1;
186 }
187
188 return 0;
189 }
190