aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Modules/_testcapi/set.c
blob: 092715ab7d0aa40ae5f0a2d513d0ad3815fc6999 (plain) (blame)
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
#include "parts.h"
#include "util.h"

static PyObject *
set_get_size(PyObject *self, PyObject *obj)
{
    NULLABLE(obj);
    RETURN_SIZE(PySet_GET_SIZE(obj));
}


static PyObject*
test_set_type_size(PyObject *self, PyObject *Py_UNUSED(ignored))
{
    PyObject *obj = PyList_New(0);
    if (obj == NULL) {
        return NULL;
    }

    // Ensure that following tests don't modify the object,
    // to ensure that Py_DECREF() will not crash.
    assert(Py_TYPE(obj) == &PyList_Type);
    assert(Py_SIZE(obj) == 0);

    // bpo-39573: Test Py_SET_TYPE() and Py_SET_SIZE() functions.
    Py_SET_TYPE(obj, &PyList_Type);
    Py_SET_SIZE(obj, 0);

    Py_DECREF(obj);
    Py_RETURN_NONE;
}


static PyMethodDef test_methods[] = {
    {"set_get_size", set_get_size, METH_O},
    {"test_set_type_size", test_set_type_size, METH_NOARGS},
    {NULL},
};

int
_PyTestCapi_Init_Set(PyObject *m)
{
    return PyModule_AddFunctions(m, test_methods);
}