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
|
#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 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},
{NULL},
};
int
_PyTestLimitedCAPI_Init_Set(PyObject *m)
{
if (PyModule_AddFunctions(m, test_methods) < 0) {
return -1;
}
return 0;
}
|