8000 add tests · python/cpython@0f40f6d · GitHub
[go: up one dir, main page]

Skip to content

Commit 0f40f6d

Browse files
committed
add tests
Signed-off-by: kalyanr <kalyan.ben10@live.com>
1 parent eac8c67 commit 0f40f6d

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

Lib/test/test_capi/test_list.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ def test_list_size(self):
5959
self.assertRaises(SystemError, size, 23)
6060
self.assertRaises(SystemError, size, object())
6161
# CRASHES size(NULL)
62+
63+
def test_list_get_size(self):
64+
# Test PyList_GET_SIZE()
65+
size = _testcapi.list_get_size
66+
self.assertEqual(size([1, 2]), 2)
67+
self.assertEqual(size(ListSubclass([1, 2])), 2)
68+
# CRASHES size(object())
69+
# CRASHES size(23)
70+
# CRASHES size({})
71+
# CRASHES size(UserList())
72+
# CRASHES size(NULL)
6273

6374

6475
def test_list_getitem(self):

Modules/_testcapi/list.c

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ list_size(PyObject *Py_UNUSED(module), PyObject *obj)
2828
RETURN_SIZE(PyList_Size(obj));
2929
}
3030

31+
static PyObject *
32+
list_get_size(PyObject *Py_UNUSED(module), PyObject *obj)
33+
{
34+
NULLABLE(obj);
35+
RETURN_SIZE(PyList_GET_SIZE(obj));
36+
}
37+
3138
static PyObject *
3239
list_getitem(PyObject *Py_UNUSED(module), PyObject *args)
3340
{
@@ -67,6 +74,22 @@ list_setitem(PyObject *Py_UNUSED(module), PyObject *args)
6774

6875
}
6976

77+
static PyObject *
78+
list_set_item(PyObject *Py_UNUSED(module), PyObject *args)
79+
{
80+
PyObject *obj, *value;
81+
Py_ssize_t i;
82+
if ( !PyArg_ParseTuple(args, "OnO", &obj, &i, &value)){
83+
return NULL;
84+
}
85+
NULLABLE(obj);
86+
NULLABLE(value);
87+
value = Py_XNewRef(value);
88+
PyList_SET_ITEM(obj, i, value);
89+
Py_RETURN_NONE;
90+
91+
}
92+
7093
static PyObject *
7194
list_insert(PyObject *Py_UNUSED(module), PyObject *args)
7295
{
@@ -108,6 +131,42 @@ list_getslice(PyObject *Py_UNUSED(module), PyObject *args)
108131

109132
}
110133

134+
static PyObject *
135+
list_setslice(PyObject *Py_UNUSED(module), PyObject *args)
136+
{
137+
PyObject *obj, *value;
138+
Py_ssize_t ilow, ihigh;
139+
if ( !PyArg_ParseTuple(args, "OnnO", &obj, &ilow, &ihigh)){
140+
return NULL;
141+
}
142+
NULLABLE(obj);
143+
NULLABLE(value);
144+
value = Py_XNewRef(value);
145+
return PyList_SetSlice(obj, ilow, ihigh, value);
146+
147+
}
148+
149+
static PyObject *
150+
list_sort(PyObject* Py_UNUSED(module), PyObject *obj)
151+
{
152+
NULLABLE(obj);
153+
RETURN_INT(PyList_Sort(obj));
154+
}
155+
156+
static PyObject *
157+
list_reverse(PyObject* Py_UNUSED(module), PyObject *obj)
158+
{
159+
NULLABLE(obj);
160+
RETURN_INT(PyList_Reverse(obj));
161+
}
162+
163+
static PyObject *
164+
list_astuple(PyObject* Py_UNUSED(module), PyObject *obj)
165+
{
166+
NULLABLE(obj);
167+
return PyList_AsTuple(obj);
168+
}
169+
111170

112171

113172

@@ -116,16 +175,18 @@ static PyMethodDef test_methods[] = {
116175
{"list_check_exact", list_check_exact, METH_O},
117176
{"list_new", list_new, METH_O},
118177
{"list_size", list_size, METH_O},
178+
{"list_get_size", list_get_size, METH_O},
119179
{"list_getitem", list_getitem, METH_VARARGS},
120180
{"list_get_item", list_get_item, METH_VARARGS},
121181
{"list_setitem", list_setitem, METH_VARARGS},
182+
{"list_set_item", list_set_item, METH_VARARGS},
122183
{"list_insert", list_insert, METH_VARARGS},
123184
{"list_append", list_append, METH_VARARGS},
124185
{"list_getslice", list_getslice, METH_VARARGS},
125-
// {"list_set_slice"},
126-
// {"list_sort"},
127-
// {"list_reverse"},
128-
// {"list_as_tuple"},
186+
{"list_setslice", list_setslice, METH_VARARGS},
187+
{"list_sort", list_sort, METH_O},
188+
{"list_reverse", list_reverse, METH_O},
189+
{"list_astuple", list_astuple, METH_O},
129190
{NULL},
130191

131192

0 commit comments

Comments
 (0)
0