From d43d9b284241475892b0d2ea57748c0020487849 Mon Sep 17 00:00:00 2001 From: kalyanr Date: Tue, 31 Oct 2023 21:37:07 +0530 Subject: [PATCH 01/26] init test c file --- Modules/_testcapi/list.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Modules/_testcapi/list.c diff --git a/Modules/_testcapi/list.c b/Modules/_testcapi/list.c new file mode 100644 index 00000000000000..e69de29bb2d1d6 From 119cd0ea7f6c03035e821eae514e499b79bb5dd0 Mon Sep 17 00:00:00 2001 From: kalyanr Date: Tue, 31 Oct 2023 22:17:31 +0530 Subject: [PATCH 02/26] single test added --- Lib/test/test_capi/test_list.py | 20 ++++++++++++++++++++ Modules/Setup.stdlib.in | 2 +- Modules/_testcapi/list.c | 23 +++++++++++++++++++++++ Modules/_testcapi/parts.h | 1 + Modules/_testcapimodule.c | 3 +++ PCbuild/_testcapi.vcxproj | 1 + PCbuild/_testcapi.vcxproj.filters | 3 +++ 7 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 Lib/test/test_capi/test_list.py diff --git a/Lib/test/test_capi/test_list.py b/Lib/test/test_capi/test_list.py new file mode 100644 index 00000000000000..f8aa341c3d339d --- /dev/null +++ b/Lib/test/test_capi/test_list.py @@ -0,0 +1,20 @@ +import unittest +import sys +from test.support import import_helper + +_testcapi = import_helper.import_module('_testcapi') + +NULL = None + +class CAPITest(unittest.TestCase): + def test_check(self): + # Test PyList_Check() + check = _testcapi.list_check() + self.assertFalse(check({1: 2})) + self.assertTrue(check([1, 2])) + self.assertTrue(check((1, 2))) + self.assertTrue(check('abc')) + self.assertTrue(check(b'abc')) + self.assertFalse(check(42)) + self.assertFalse(check(object())) + # CRASHES check(NULL) \ No newline at end of file diff --git a/Modules/Setup.stdlib.in b/Modules/Setup.stdlib.in index c73522b8ecf426..4a0d59a08099c4 100644 --- a/Modules/Setup.stdlib.in +++ b/Modules/Setup.stdlib.in @@ -159,7 +159,7 @@ @MODULE__XXTESTFUZZ_TRUE@_xxtestfuzz _xxtestfuzz/_xxtestfuzz.c _xxtestfuzz/fuzzer.c @MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c @MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c _testinternalcapi/test_lock.c _testinternalcapi/pytime.c _testinternalcapi/set.c -@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/vectorcall_limited.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/getargs.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyatomic.c _testcapi/pyos.c _testcapi/immortal.c _testcapi/heaptype_relative.c _testcapi/gc.c _testcapi/sys.c +@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/vectorcall_limited.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/list.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/getargs.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyatomic.c _testcapi/pyos.c _testcapi/immortal.c _testcapi/heaptype_relative.c _testcapi/gc.c _testcapi/sys.c @MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c @MODULE__TESTCLINIC_LIMITED_TRUE@_testclinic_limited _testclinic_limited.c diff --git a/Modules/_testcapi/list.c b/Modules/_testcapi/list.c index e69de29bb2d1d6..09382f067a5f16 100644 --- a/Modules/_testcapi/list.c +++ b/Modules/_testcapi/list.c @@ -0,0 +1,23 @@ +#include "parts.h" +#include "util.h" + +static PyObject * +list_check(PyObject* self, PyObject *obj) +{ + NULLABLE(obj); + return PyLong_FromLong(PyList_Check(obj)); +} + +static PyMethodDef test_methods[] = { + {"list_check", list_check, METH_O} +}; + +int +_PyTestCapi_Init_List(PyObject *m) +{ + if (PyModule_AddFunctions(m, test_methods) < 0){ + return -1; + } + + return 0; +} \ No newline at end of file diff --git a/Modules/_testcapi/parts.h b/Modules/_testcapi/parts.h index 4fa77a8844ee4d..10ead2045fd6bb 100644 --- a/Modules/_testcapi/parts.h +++ b/Modules/_testcapi/parts.h @@ -31,6 +31,7 @@ int _PyTestCapi_Init_Vectorcall(PyObject *module); int _PyTestCapi_Init_Heaptype(PyObject *module); int _PyTestCapi_Init_Abstract(PyObject *module); +int _PyTestCapi_Init_List(PyObject *module); int _PyTestCapi_Init_Unicode(PyObject *module); int _PyTestCapi_Init_GetArgs(PyObject *module); int _PyTestCapi_Init_DateTime(PyObject *module); diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index fc9dc746095b72..a728967eacdc04 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -3908,6 +3908,9 @@ PyInit__testcapi(void) if (_PyTestCapi_Init_Abstract(m) < 0) { return NULL; } + if (_PyTestCapi_Init_List(m) < 0) { + return NULL; + } if (_PyTestCapi_Init_Unicode(m) < 0) { return NULL; } diff --git a/PCbuild/_testcapi.vcxproj b/PCbuild/_testcapi.vcxproj index ae97c34eed50e0..eab1553a1eac87 100644 --- a/PCbuild/_testcapi.vcxproj +++ b/PCbuild/_testcapi.vcxproj @@ -100,6 +100,7 @@ + diff --git a/PCbuild/_testcapi.vcxproj.filters b/PCbuild/_testcapi.vcxproj.filters index c3ad2564edd686..b22ac964b9865e 100644 --- a/PCbuild/_testcapi.vcxproj.filters +++ b/PCbuild/_testcapi.vcxproj.filters @@ -30,6 +30,9 @@ Source Files + + Source Files + Source Files From ccf1bff3b30ee838264b6d98da398e4bb36cec18 Mon Sep 17 00:00:00 2001 From: kalyanr Date: Tue, 31 Oct 2023 22:43:53 +0530 Subject: [PATCH 03/26] fix lint --- Lib/test/test_capi/test_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_capi/test_list.py b/Lib/test/test_capi/test_list.py index f8aa341c3d339d..8119033f1ff137 100644 --- a/Lib/test/test_capi/test_list.py +++ b/Lib/test/test_capi/test_list.py @@ -17,4 +17,4 @@ def test_check(self): self.assertTrue(check(b'abc')) self.assertFalse(check(42)) self.assertFalse(check(object())) - # CRASHES check(NULL) \ No newline at end of file + # CRASHES check(NULL) From 8d17ca67d29e3e4d70a420b454b5301cc7ed7190 Mon Sep 17 00:00:00 2001 From: kalyanr Date: Tue, 31 Oct 2023 22:48:47 +0530 Subject: [PATCH 04/26] fix list check tests --- Lib/test/test_capi/test_list.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_capi/test_list.py b/Lib/test/test_capi/test_list.py index 8119033f1ff137..004a19fefe50c7 100644 --- a/Lib/test/test_capi/test_list.py +++ b/Lib/test/test_capi/test_list.py @@ -9,12 +9,13 @@ class CAPITest(unittest.TestCase): def test_check(self): # Test PyList_Check() - check = _testcapi.list_check() - self.assertFalse(check({1: 2})) + check = _testcapi.list_check self.assertTrue(check([1, 2])) - self.assertTrue(check((1, 2))) - self.assertTrue(check('abc')) - self.assertTrue(check(b'abc')) + self.assertTrue(check([])) + self.assertFalse(check({1: 2})) + self.assertFalse(check((1, 2))) + self.assertFalse(check('abc')) + self.assertFalse(check(b'abc')) self.assertFalse(check(42)) self.assertFalse(check(object())) - # CRASHES check(NULL) + From bd9415d151e17d94313055afcdd05feef428365d Mon Sep 17 00:00:00 2001 From: kalyanr Date: Tue, 31 Oct 2023 22:49:13 +0530 Subject: [PATCH 05/26] lint --- Lib/test/test_capi/test_list.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_capi/test_list.py b/Lib/test/test_capi/test_list.py index 004a19fefe50c7..c9b273a755d757 100644 --- a/Lib/test/test_capi/test_list.py +++ b/Lib/test/test_capi/test_list.py @@ -18,4 +18,3 @@ def test_check(self): self.assertFalse(check(b'abc')) self.assertFalse(check(42)) self.assertFalse(check(object())) - From 17d60d15c8c47a29a3a15e1588938b7e0e20402b Mon Sep 17 00:00:00 2001 From: kalyanr Date: Tue, 31 Oct 2023 23:47:38 +0530 Subject: [PATCH 06/26] add 2 tests --- Lib/test/test_capi/test_list.py | 24 ++++++++++++++++++++++-- Modules/_testcapi/list.c | 20 ++++++++++++++++++-- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_capi/test_list.py b/Lib/test/test_capi/test_list.py index c9b273a755d757..132f3f3519a582 100644 --- a/Lib/test/test_capi/test_list.py +++ b/Lib/test/test_capi/test_list.py @@ -1,20 +1,40 @@ import unittest import sys from test.support import import_helper +from collections import UserList _testcapi = import_helper.import_module('_testcapi') NULL = None +class ListSubclass(list): + ... + class CAPITest(unittest.TestCase): def test_check(self): # Test PyList_Check() check = _testcapi.list_check self.assertTrue(check([1, 2])) self.assertTrue(check([])) + self.assertTrue(check(ListSubclass([1, 2]))) self.assertFalse(check({1: 2})) self.assertFalse(check((1, 2))) - self.assertFalse(check('abc')) - self.assertFalse(check(b'abc')) self.assertFalse(check(42)) self.assertFalse(check(object())) + + def test_list_check_exact(self): + check = _testcapi.list_check_exact + self.assertTrue(check([1])) + self.assertTrue(check([])) + self.assertFalse(check(ListSubclass([1]))) + self.assertFalse(check(UserList([1, 2]))) + self.assertFalse(check({1: 2})) + self.assertFalse(check(object())) + + def test_list_new(self): + list_new = _testcapi.list_new + lst = list_new() + self.assertEqual(lst, []) + self.assertIs(type(lst), list) + lst2 = list_new() + self.assertIsNot(lst2, lst) diff --git a/Modules/_testcapi/list.c b/Modules/_testcapi/list.c index 09382f067a5f16..c481fbf219f263 100644 --- a/Modules/_testcapi/list.c +++ b/Modules/_testcapi/list.c @@ -2,14 +2,30 @@ #include "util.h" static PyObject * -list_check(PyObject* self, PyObject *obj) +list_check(PyObject* Py_UNUSED(module), PyObject *obj) { NULLABLE(obj); return PyLong_FromLong(PyList_Check(obj)); } +static PyObject * +list_check_exact(PyObject* Py_UNUSED(module), PyObject *obj) +{ + NULLABLE(obj); + return PyLong_FromLong(PyList_CheckExact(obj)); +} + +static PyObject * +list_new(PyObject *self, PyObject *obj) +{ + return PyList_New(obj); +} + + static PyMethodDef test_methods[] = { - {"list_check", list_check, METH_O} + {"list_check", list_check, METH_O}, + {"list_check_exact", list_check_exact, METH_O}, + {"list_new", list_new, METH_NOARGS}, }; int From 322eab934fe5b00b077b76ae19c72e57515655c4 Mon Sep 17 00:00:00 2001 From: kalyanr Date: Wed, 1 Nov 2023 00:17:03 +0530 Subject: [PATCH 07/26] add test --- Lib/test/test_capi/test_list.py | 14 ++++++++++++-- Modules/_testcapi/list.c | 12 ++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_capi/test_list.py b/Lib/test/test_capi/test_list.py index 132f3f3519a582..992cdcb574b2ae 100644 --- a/Lib/test/test_capi/test_list.py +++ b/Lib/test/test_capi/test_list.py @@ -33,8 +33,18 @@ def test_list_check_exact(self): def test_list_new(self): list_new = _testcapi.list_new - lst = list_new() + lst = list_new(0) self.assertEqual(lst, []) self.assertIs(type(lst), list) - lst2 = list_new() + lst2 = list_new(0) self.assertIsNot(lst2, lst) + + def test_list_size(self): + size = _testcapi.list_size + self.assertEqual(size([1, 2]), 2) + self.assertEqual(size(ListSubclass([1, 2])), 2) + self.assertRaises(SystemError, size, UserList()) + self.assertRaises(SystemError, size, {}) + self.assertRaises(SystemError, size, 23) + self.assertRaises(SystemError, size, object()) + # CRASHES size(NULL) diff --git a/Modules/_testcapi/list.c b/Modules/_testcapi/list.c index c481fbf219f263..59d68984bb5b8a 100644 --- a/Modules/_testcapi/list.c +++ b/Modules/_testcapi/list.c @@ -18,14 +18,22 @@ list_check_exact(PyObject* Py_UNUSED(module), PyObject *obj) static PyObject * list_new(PyObject *self, PyObject *obj) { - return PyList_New(obj); + return PyList_New(PyLong_AsSsize_t(obj)); } +static PyObject * +list_size(PyObject *Py_UNUSED(module), PyObject *obj) +{ + NULLABLE(obj); + RETURN_SIZE(PyList_Size(obj)); +} static PyMethodDef test_methods[] = { {"list_check", list_check, METH_O}, {"list_check_exact", list_check_exact, METH_O}, - {"list_new", list_new, METH_NOARGS}, + {"list_new", list_new, METH_O}, + {"list_size", list_size, METH_O}, + }; int From 5c580226281406cd9689e83ae09aefe11684c534 Mon Sep 17 00:00:00 2001 From: kalyanr Date: Wed, 1 Nov 2023 02:06:38 +0530 Subject: [PATCH 08/26] add getitem test --- Lib/test/test_capi/test_list.py | 11 +++++++++++ Modules/_testcapi/list.c | 28 +++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_capi/test_list.py b/Lib/test/test_capi/test_list.py index 992cdcb574b2ae..2239e8024e68f1 100644 --- a/Lib/test/test_capi/test_list.py +++ b/Lib/test/test_capi/test_list.py @@ -48,3 +48,14 @@ def test_list_size(self): self.assertRaises(SystemError, size, 23) self.assertRaises(SystemError, size, object()) # CRASHES size(NULL) + + + def test_list_getitem(self): + getitem = _testcapi.list_getitem + lst = [1, 2, NULL] + self.assertEqual(getitem(lst, 0), 1) + self.assertRaises(IndexError, getitem, lst, -1) + self.assertRaises(IndexError, getitem, lst, 10) + self.assertRaises(SystemError, getitem, 42, 1) + + # CRASHES getitem(NULL, 1) diff --git a/Modules/_testcapi/list.c b/Modules/_testcapi/list.c index 59d68984bb5b8a..8dfbc5ccea8b24 100644 --- a/Modules/_testcapi/list.c +++ b/Modules/_testcapi/list.c @@ -16,7 +16,7 @@ list_check_exact(PyObject* Py_UNUSED(module), PyObject *obj) } static PyObject * -list_new(PyObject *self, PyObject *obj) +list_new(PyObject* Py_UNUSED(module), PyObject *obj) { return PyList_New(PyLong_AsSsize_t(obj)); } @@ -28,11 +28,37 @@ list_size(PyObject *Py_UNUSED(module), PyObject *obj) RETURN_SIZE(PyList_Size(obj)); } +static PyObject * +list_getitem(PyObject *Py_UNUSED(module), PyObject *args) +{ + PyObject *obj; + Py_ssize_t i; + if (!PyArg_ParseTuple(args, "On", &obj, &i)){ + return NULL; + } + NULLABLE(obj); + return PyList_GetItem(obj, i); +} + + + + + static PyMethodDef test_methods[] = { {"list_check", list_check, METH_O}, {"list_check_exact", list_check_exact, METH_O}, {"list_new", list_new, METH_O}, {"list_size", list_size, METH_O}, + {"list_getitem", list_getitem, METH_VARARGS}, + // {"list_setitem"}, + // {"list_insert"}, + // {"list_append"}, + // {"list_get_slice"}, + // {"list_set_slice"}, + // {"list_sort"}, + // {"list_reverse"}, + // {"list_as_tuple"}, + }; From 6b12f1f0b65ab3a773755c1837e3bac9a82e9902 Mon Sep 17 00:00:00 2001 From: kalyanr Date: Wed, 1 Nov 2023 02:18:45 +0530 Subject: [PATCH 09/26] test list_get_item --- Lib/test/test_capi/test_list.py | 10 ++++++++++ Modules/_testcapi/list.c | 16 +++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_capi/test_list.py b/Lib/test/test_capi/test_list.py index 2239e8024e68f1..c726662c665a86 100644 --- a/Lib/test/test_capi/test_list.py +++ b/Lib/test/test_capi/test_list.py @@ -59,3 +59,13 @@ def test_list_getitem(self): self.assertRaises(SystemError, getitem, 42, 1) # CRASHES getitem(NULL, 1) + + def test_list_get_item(self): + # Test PyList_GET_SIZE() + get_item = _testcapi.list_get_item + lst = [1, 2, NULL] + self.assertEqual(get_item(lst, 0), 1) + self.assertNotEqual(get_item(lst, 1), 12) + + # CRASHES for out of index: get_item(lst, 3) + # CRASHES get_item(21, 2) diff --git a/Modules/_testcapi/list.c b/Modules/_testcapi/list.c index 8dfbc5ccea8b24..81f6d9923b08f4 100644 --- a/Modules/_testcapi/list.c +++ b/Modules/_testcapi/list.c @@ -40,8 +40,17 @@ list_getitem(PyObject *Py_UNUSED(module), PyObject *args) return PyList_GetItem(obj, i); } - - +static PyObject * +list_get_item(PyObject *Py_UNUSED(module), PyObject *args) +{ + PyObject *obj; + Py_ssize_t i; + if (!PyArg_ParseTuple(args, "On", &obj, &i)){ + return NULL; + } + NULLABLE(obj); + return PyList_GET_ITEM(obj, i); +} static PyMethodDef test_methods[] = { @@ -50,7 +59,8 @@ static PyMethodDef test_methods[] = { {"list_new", list_new, METH_O}, {"list_size", list_size, METH_O}, {"list_getitem", list_getitem, METH_VARARGS}, - // {"list_setitem"}, + {"list_get_item", list_get_item, METH_VARARGS}, + // {"list_setitem", list_setitem, METH_VARARGS}, // {"list_insert"}, // {"list_append"}, // {"list_get_slice"}, From 70e385f247206986df1f24a3620351cafbd5116b Mon Sep 17 00:00:00 2001 From: kalyanr Date: Wed, 1 Nov 2023 02:40:27 +0530 Subject: [PATCH 10/26] add test for setitem --- Lib/test/test_capi/test_list.py | 14 ++++++++++++++ Modules/_testcapi/list.c | 16 +++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_capi/test_list.py b/Lib/test/test_capi/test_list.py index c726662c665a86..9f9e0ff7abe144 100644 --- a/Lib/test/test_capi/test_list.py +++ b/Lib/test/test_capi/test_list.py @@ -10,6 +10,7 @@ class ListSubclass(list): ... + class CAPITest(unittest.TestCase): def test_check(self): # Test PyList_Check() @@ -69,3 +70,16 @@ def test_list_get_item(self): # CRASHES for out of index: get_item(lst, 3) # CRASHES get_item(21, 2) + + + def test_list_setitem(self): + setitem = _testcapi.list_setitem + lst = [1, 2, 3] + setitem(lst, 1, 10) + self.assertEqual(lst[1], 10) + self.assertRaises(IndexError, setitem, [1], -1, 5) + self.assertRaises(TypeError, setitem, lst, 1.5, 10) + self.assertRaises(TypeError, setitem, 23, 'a', 5) + self.assertRaises(SystemError, setitem, {}, 0, 5) + + # CRASHES setitem(NULL, 'a', 5) diff --git a/Modules/_testcapi/list.c b/Modules/_testcapi/list.c index 81f6d9923b08f4..72d6462ac855c8 100644 --- a/Modules/_testcapi/list.c +++ b/Modules/_testcapi/list.c @@ -52,6 +52,20 @@ list_get_item(PyObject *Py_UNUSED(module), PyObject *args) return PyList_GET_ITEM(obj, i); } +static PyObject * +list_setitem(PyObject *Py_UNUSED(module), PyObject *args) +{ + PyObject *obj, *value; + Py_ssize_t i; + if ( !PyArg_ParseTuple(args, "OnO", &obj, &i, &value)){ + return NULL; + } + NULLABLE(obj); + NULLABLE(value); + RETURN_INT(PyList_SetItem(obj, i, value)); + +} + static PyMethodDef test_methods[] = { {"list_check", list_check, METH_O}, @@ -60,7 +74,7 @@ static PyMethodDef test_methods[] = { {"list_size", list_size, METH_O}, {"list_getitem", list_getitem, METH_VARARGS}, {"list_get_item", list_get_item, METH_VARARGS}, - // {"list_setitem", list_setitem, METH_VARARGS}, + {"list_setitem", list_setitem, METH_VARARGS}, // {"list_insert"}, // {"list_append"}, // {"list_get_slice"}, From 3166ba44bb6a176be161ce9a2434602982c337d7 Mon Sep 17 00:00:00 2001 From: kalyanr Date: Wed, 1 Nov 2023 02:40:27 +0530 Subject: [PATCH 11/26] add test for setitem Signed-off-by: kalyanr --- Lib/test/test_capi/test_list.py | 14 ++++++++++++++ Modules/_testcapi/list.c | 16 +++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_capi/test_list.py b/Lib/test/test_capi/test_list.py index c726662c665a86..9f9e0ff7abe144 100644 --- a/Lib/test/test_capi/test_list.py +++ b/Lib/test/test_capi/test_list.py @@ -10,6 +10,7 @@ class ListSubclass(list): ... + class CAPITest(unittest.TestCase): def test_check(self): # Test PyList_Check() @@ -69,3 +70,16 @@ def test_list_get_item(self): # CRASHES for out of index: get_item(lst, 3) # CRASHES get_item(21, 2) + + + def test_list_setitem(self): + setitem = _testcapi.list_setitem + lst = [1, 2, 3] + setitem(lst, 1, 10) + self.assertEqual(lst[1], 10) + self.assertRaises(IndexError, setitem, [1], -1, 5) + self.assertRaises(TypeError, setitem, lst, 1.5, 10) + self.assertRaises(TypeError, setitem, 23, 'a', 5) + self.assertRaises(SystemError, setitem, {}, 0, 5) + + # CRASHES setitem(NULL, 'a', 5) diff --git a/Modules/_testcapi/list.c b/Modules/_testcapi/list.c index 81f6d9923b08f4..72d6462ac855c8 100644 --- a/Modules/_testcapi/list.c +++ b/Modules/_testcapi/list.c @@ -52,6 +52,20 @@ list_get_item(PyObject *Py_UNUSED(module), PyObject *args) return PyList_GET_ITEM(obj, i); } +static PyObject * +list_setitem(PyObject *Py_UNUSED(module), PyObject *args) +{ + PyObject *obj, *value; + Py_ssize_t i; + if ( !PyArg_ParseTuple(args, "OnO", &obj, &i, &value)){ + return NULL; + } + NULLABLE(obj); + NULLABLE(value); + RETURN_INT(PyList_SetItem(obj, i, value)); + +} + static PyMethodDef test_methods[] = { {"list_check", list_check, METH_O}, @@ -60,7 +74,7 @@ static PyMethodDef test_methods[] = { {"list_size", list_size, METH_O}, {"list_getitem", list_getitem, METH_VARARGS}, {"list_get_item", list_get_item, METH_VARARGS}, - // {"list_setitem", list_setitem, METH_VARARGS}, + {"list_setitem", list_setitem, METH_VARARGS}, // {"list_insert"}, // {"list_append"}, // {"list_get_slice"}, From 392b3c6b981764165987e5baef39bb939e227e28 Mon Sep 17 00:00:00 2001 From: kalyanr Date: Wed, 1 Nov 2023 14:28:04 +0530 Subject: [PATCH 12/26] add tests Signed-off-by: kalyanr --- Lib/test/test_capi/test_list.py | 57 +++++++++++++++++++++++++++++++-- Modules/_testcapi/list.c | 55 ++++++++++++++++++++++++++++--- 2 files changed, 104 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_capi/test_list.py b/Lib/test/test_capi/test_list.py index 9f9e0ff7abe144..e7acd513717173 100644 --- a/Lib/test/test_capi/test_list.py +++ b/Lib/test/test_capi/test_list.py @@ -22,8 +22,12 @@ def test_check(self): self.assertFalse(check((1, 2))) self.assertFalse(check(42)) self.assertFalse(check(object())) - + + # CRASHES check(NULL) + + def test_list_check_exact(self): + # Test PyList_CheckExact() check = _testcapi.list_check_exact self.assertTrue(check([1])) self.assertTrue(check([])) @@ -31,16 +35,22 @@ def test_list_check_exact(self): self.assertFalse(check(UserList([1, 2]))) self.assertFalse(check({1: 2})) self.assertFalse(check(object())) + + # CRASHES check(NULL) def test_list_new(self): + # Test PyList_New() list_new = _testcapi.list_new lst = list_new(0) self.assertEqual(lst, []) self.assertIs(type(lst), list) lst2 = list_new(0) self.assertIsNot(lst2, lst) + self.assertRaises(SystemError, list_new, NULL) + self.assertRaises(SystemError, list_new, -1) def test_list_size(self): + # Test PyList_Size() size = _testcapi.list_size self.assertEqual(size([1, 2]), 2) self.assertEqual(size(ListSubclass([1, 2])), 2) @@ -52,6 +62,7 @@ def test_list_size(self): def test_list_getitem(self): + # Test PyList_GetItem() getitem = _testcapi.list_getitem lst = [1, 2, NULL] self.assertEqual(getitem(lst, 0), 1) @@ -62,17 +73,18 @@ def test_list_getitem(self): # CRASHES getitem(NULL, 1) def test_list_get_item(self): - # Test PyList_GET_SIZE() + # Test PyList_GET_ITEM() get_item = _testcapi.list_get_item lst = [1, 2, NULL] self.assertEqual(get_item(lst, 0), 1) self.assertNotEqual(get_item(lst, 1), 12) - # CRASHES for out of index: get_item(lst, 3) # CRASHES get_item(21, 2) + # CRASHES get_item(Null,1) def test_list_setitem(self): + # Test PyList_SET_ITEM() setitem = _testcapi.list_setitem lst = [1, 2, 3] setitem(lst, 1, 10) @@ -83,3 +95,42 @@ def test_list_setitem(self): self.assertRaises(SystemError, setitem, {}, 0, 5) # CRASHES setitem(NULL, 'a', 5) + + def test_list_insert(self): + # Test PyList_Insert() + insert = _testcapi.list_insert + lst = [1, 2, 3] + insert(lst, 1, 23) + self.assertEqual(lst[1], 23) + insert(lst, -1, 22) + self.assertEqual(lst[-2], 22) + self.assertRaises(TypeError, insert, lst, 1.5, 10) + self.assertRaises(TypeError, insert, 23, 'a', 5) + self.assertRaises(SystemError, insert, {}, 0, 5) + # CRASHES insert(NULL, 'a', 5) + + def test_list_append(self): + # Test PyList_Append() + append = _testcapi.list_append + lst = [1, 2, 3] + append(lst, 1) + self.assertEqual(lst[-1], 1) + append(lst, [4,5,6]) + self.assertEqual(lst[-1], [4,5,6]) + self.assertRaises(SystemError, append, lst, NULL) + # CRASHES append(NULL, 0) + + def test_list_getslice(self): + # Test PyList_GetSlice() + getslice = _testcapi.list_getslice + lst = [1,2,3,4,5,6,7,8,9,10] + self.assertEqual(getslice(lst, 0, 3), [1, 2, 3]) + self.assertEqual(getslice(lst, 4, 6), [5,6]) + self.assertEqual(getslice(lst, 6, 10), [7,8,9,10]) + self.assertEqual(getslice(lst, 6, 100), [7,8,9,10]) + self.assertNotEqual(getslice(lst, -2, -1), [9]) + self.assertRaises(TypeError, lst, 'a', '2') + + # CRASHES getslice(NULL, 1, 2) + + \ No newline at end of file diff --git a/Modules/_testcapi/list.c b/Modules/_testcapi/list.c index cce507032be39a..9742ceb4dca6fb 100644 --- a/Modules/_testcapi/list.c +++ b/Modules/_testcapi/list.c @@ -37,7 +37,7 @@ list_getitem(PyObject *Py_UNUSED(module), PyObject *args) return NULL; } NULLABLE(obj); - return PyList_GetItem(obj, i); + return Py_XNewRef(PyList_GetItem(obj, i)); } static PyObject * @@ -49,7 +49,7 @@ list_get_item(PyObject *Py_UNUSED(module), PyObject *args) return NULL; } NULLABLE(obj); - return PyList_GET_ITEM(obj, i); + return Py_XNewRef(PyList_GET_ITEM(obj, i)); } static PyObject * @@ -62,10 +62,54 @@ list_setitem(PyObject *Py_UNUSED(module), PyObject *args) } NULLABLE(obj); NULLABLE(value); + value = Py_XNewRef(value); RETURN_INT(PyList_SetItem(obj, i, value)); } +static PyObject * +list_insert(PyObject *Py_UNUSED(module), PyObject *args) +{ + PyObject *obj, *value; + Py_ssize_t where; + if ( !PyArg_ParseTuple(args, "OnO", &obj, &where, &value)){ + return NULL; + } + NULLABLE(obj); + NULLABLE(value); + value = Py_XNewRef(value); + RETURN_INT(PyList_Insert(obj, where, value)); + +} + +static PyObject * +list_append(PyObject *Py_UNUSED(module), PyObject *args) +{ + PyObject *obj, *value; + if ( !PyArg_ParseTuple(args, "OO", &obj, &value)){ + return NULL; + } + NULLABLE(obj); + NULLABLE(value); + value = Py_XNewRef(value); + RETURN_INT(PyList_Append(obj, value)); +} + +static PyObject * +list_getslice(PyObject *Py_UNUSED(module), PyObject *args) +{ + PyObject *obj; + Py_ssize_t ilow, ihigh; + if ( !PyArg_ParseTuple(args, "Onn", &obj, &ilow, &ihigh)){ + return NULL; + } + NULLABLE(obj); + return PyList_GetSlice(obj, ilow, ihigh); + +} + + + static PyMethodDef test_methods[] = { {"list_check", list_check, METH_O}, @@ -75,13 +119,14 @@ static PyMethodDef test_methods[] = { {"list_getitem", list_getitem, METH_VARARGS}, {"list_get_item", list_get_item, METH_VARARGS}, {"list_setitem", list_setitem, METH_VARARGS}, - // {"list_insert"}, - // {"list_append"}, - // {"list_get_slice"}, + {"list_insert", list_insert, METH_VARARGS}, + {"list_append", list_append, METH_VARARGS}, + {"list_getslice", list_getslice, METH_VARARGS}, // {"list_set_slice"}, // {"list_sort"}, // {"list_reverse"}, // {"list_as_tuple"}, + {NULL}, }; From eac8c671e2e77a75623d55798b1c68e00fcc572f Mon Sep 17 00:00:00 2001 From: kalyanr Date: Wed, 1 Nov 2023 14:28:20 +0530 Subject: [PATCH 13/26] lint fix Signed-off-by: kalyanr --- Lib/test/test_capi/test_list.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/Lib/test/test_capi/test_list.py b/Lib/test/test_capi/test_list.py index e7acd513717173..c37537c5bd9a44 100644 --- a/Lib/test/test_capi/test_list.py +++ b/Lib/test/test_capi/test_list.py @@ -22,10 +22,10 @@ def test_check(self): self.assertFalse(check((1, 2))) self.assertFalse(check(42)) self.assertFalse(check(object())) - + # CRASHES check(NULL) - - + + def test_list_check_exact(self): # Test PyList_CheckExact() check = _testcapi.list_check_exact @@ -35,7 +35,7 @@ def test_list_check_exact(self): self.assertFalse(check(UserList([1, 2]))) self.assertFalse(check({1: 2})) self.assertFalse(check(object())) - + # CRASHES check(NULL) def test_list_new(self): @@ -95,7 +95,7 @@ def test_list_setitem(self): self.assertRaises(SystemError, setitem, {}, 0, 5) # CRASHES setitem(NULL, 'a', 5) - + def test_list_insert(self): # Test PyList_Insert() insert = _testcapi.list_insert @@ -119,7 +119,7 @@ def test_list_append(self): self.assertEqual(lst[-1], [4,5,6]) self.assertRaises(SystemError, append, lst, NULL) # CRASHES append(NULL, 0) - + def test_list_getslice(self): # Test PyList_GetSlice() getslice = _testcapi.list_getslice @@ -130,7 +130,5 @@ def test_list_getslice(self): self.assertEqual(getslice(lst, 6, 100), [7,8,9,10]) self.assertNotEqual(getslice(lst, -2, -1), [9]) self.assertRaises(TypeError, lst, 'a', '2') - + # CRASHES getslice(NULL, 1, 2) - - \ No newline at end of file From 0f40f6da2c9936145be7802e838d9412aea059b6 Mon Sep 17 00:00:00 2001 From: kalyanr Date: Wed, 1 Nov 2023 15:36:27 +0530 Subject: [PATCH 14/26] add tests Signed-off-by: kalyanr --- Lib/test/test_capi/test_list.py | 11 ++++++ Modules/_testcapi/list.c | 69 +++++++++++++++++++++++++++++++-- 2 files changed, 76 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_capi/test_list.py b/Lib/test/test_capi/test_list.py index c37537c5bd9a44..5dce9c3b3e76ca 100644 --- a/Lib/test/test_capi/test_list.py +++ b/Lib/test/test_capi/test_list.py @@ -59,6 +59,17 @@ def test_list_size(self): self.assertRaises(SystemError, size, 23) self.assertRaises(SystemError, size, object()) # CRASHES size(NULL) + + def test_list_get_size(self): + # Test PyList_GET_SIZE() + size = _testcapi.list_get_size + self.assertEqual(size([1, 2]), 2) + self.assertEqual(size(ListSubclass([1, 2])), 2) + # CRASHES size(object()) + # CRASHES size(23) + # CRASHES size({}) + # CRASHES size(UserList()) + # CRASHES size(NULL) def test_list_getitem(self): diff --git a/Modules/_testcapi/list.c b/Modules/_testcapi/list.c index 9742ceb4dca6fb..22b1b2ef30fd65 100644 --- a/Modules/_testcapi/list.c +++ b/Modules/_testcapi/list.c @@ -28,6 +28,13 @@ list_size(PyObject *Py_UNUSED(module), PyObject *obj) RETURN_SIZE(PyList_Size(obj)); } +static PyObject * +list_get_size(PyObject *Py_UNUSED(module), PyObject *obj) +{ + NULLABLE(obj); + RETURN_SIZE(PyList_GET_SIZE(obj)); +} + static PyObject * list_getitem(PyObject *Py_UNUSED(module), PyObject *args) { @@ -67,6 +74,22 @@ list_setitem(PyObject *Py_UNUSED(module), PyObject *args) } +static PyObject * +list_set_item(PyObject *Py_UNUSED(module), PyObject *args) +{ + PyObject *obj, *value; + Py_ssize_t i; + if ( !PyArg_ParseTuple(args, "OnO", &obj, &i, &value)){ + return NULL; + } + NULLABLE(obj); + NULLABLE(value); + value = Py_XNewRef(value); + PyList_SET_ITEM(obj, i, value); + Py_RETURN_NONE; + +} + static PyObject * list_insert(PyObject *Py_UNUSED(module), PyObject *args) { @@ -108,6 +131,42 @@ list_getslice(PyObject *Py_UNUSED(module), PyObject *args) } +static PyObject * +list_setslice(PyObject *Py_UNUSED(module), PyObject *args) +{ + PyObject *obj, *value; + Py_ssize_t ilow, ihigh; + if ( !PyArg_ParseTuple(args, "OnnO", &obj, &ilow, &ihigh)){ + return NULL; + } + NULLABLE(obj); + NULLABLE(value); + value = Py_XNewRef(value); + return PyList_SetSlice(obj, ilow, ihigh, value); + +} + +static PyObject * +list_sort(PyObject* Py_UNUSED(module), PyObject *obj) +{ + NULLABLE(obj); + RETURN_INT(PyList_Sort(obj)); +} + +static PyObject * +list_reverse(PyObject* Py_UNUSED(module), PyObject *obj) +{ + NULLABLE(obj); + RETURN_INT(PyList_Reverse(obj)); +} + +static PyObject * +list_astuple(PyObject* Py_UNUSED(module), PyObject *obj) +{ + NULLABLE(obj); + return PyList_AsTuple(obj); +} + @@ -116,16 +175,18 @@ static PyMethodDef test_methods[] = { {"list_check_exact", list_check_exact, METH_O}, {"list_new", list_new, METH_O}, {"list_size", list_size, METH_O}, + {"list_get_size", list_get_size, METH_O}, {"list_getitem", list_getitem, METH_VARARGS}, {"list_get_item", list_get_item, METH_VARARGS}, {"list_setitem", list_setitem, METH_VARARGS}, + {"list_set_item", list_set_item, METH_VARARGS}, {"list_insert", list_insert, METH_VARARGS}, {"list_append", list_append, METH_VARARGS}, {"list_getslice", list_getslice, METH_VARARGS}, - // {"list_set_slice"}, - // {"list_sort"}, - // {"list_reverse"}, - // {"list_as_tuple"}, + {"list_setslice", list_setslice, METH_VARARGS}, + {"list_sort", list_sort, METH_O}, + {"list_reverse", list_reverse, METH_O}, + {"list_astuple", list_astuple, METH_O}, {NULL}, From 40e4022cdcf54e9a319b2264e74e05670f468322 Mon Sep 17 00:00:00 2001 From: Kalyan Date: Wed, 1 Nov 2023 15:37:00 +0530 Subject: [PATCH 15/26] Update Lib/test/test_capi/test_list.py Co-authored-by: Serhiy Storchaka --- Lib/test/test_capi/test_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_capi/test_list.py b/Lib/test/test_capi/test_list.py index 5dce9c3b3e76ca..bdba12c52f9b6d 100644 --- a/Lib/test/test_capi/test_list.py +++ b/Lib/test/test_capi/test_list.py @@ -8,7 +8,7 @@ NULL = None class ListSubclass(list): - ... + pass class CAPITest(unittest.TestCase): From d31fa57c70a5c3618d732af285089ccfd3600b41 Mon Sep 17 00:00:00 2001 From: Kalyan Date: Wed, 1 Nov 2023 15:41:12 +0530 Subject: [PATCH 16/26] Update Modules/_testcapi/list.c Co-authored-by: Serhiy Storchaka --- Modules/_testcapi/list.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Modules/_testcapi/list.c b/Modules/_testcapi/list.c index 22b1b2ef30fd65..24cbaa6d5225e9 100644 --- a/Modules/_testcapi/list.c +++ b/Modules/_testcapi/list.c @@ -114,8 +114,7 @@ list_append(PyObject *Py_UNUSED(module), PyObject *args) } NULLABLE(obj); NULLABLE(value); - value = Py_XNewRef(value); - RETURN_INT(PyList_Append(obj, value)); + RETURN_INT(PyList_Append(obj, Py_XNewRef(value))); } static PyObject * From 433351d7f502d7ac08798dc5a09f4f4123bd52c4 Mon Sep 17 00:00:00 2001 From: Kalyan Date: Wed, 1 Nov 2023 15:41:24 +0530 Subject: [PATCH 17/26] Update Lib/test/test_capi/test_list.py Co-authored-by: Serhiy Storchaka --- Lib/test/test_capi/test_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_capi/test_list.py b/Lib/test/test_capi/test_list.py index bdba12c52f9b6d..32df3a76cbe0d8 100644 --- a/Lib/test/test_capi/test_list.py +++ b/Lib/test/test_capi/test_list.py @@ -99,7 +99,7 @@ def test_list_setitem(self): setitem = _testcapi.list_setitem lst = [1, 2, 3] setitem(lst, 1, 10) - self.assertEqual(lst[1], 10) + self.assertEqual(lst, [1, 10, 3]) self.assertRaises(IndexError, setitem, [1], -1, 5) self.assertRaises(TypeError, setitem, lst, 1.5, 10) self.assertRaises(TypeError, setitem, 23, 'a', 5) From 34e915bec047150ab7e8439f174c706d5472ccfd Mon Sep 17 00:00:00 2001 From: Kalyan Date: Wed, 1 Nov 2023 15:41:41 +0530 Subject: [PATCH 18/26] Update Lib/test/test_capi/test_list.py Co-authored-by: Serhiy Storchaka --- Lib/test/test_capi/test_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_capi/test_list.py b/Lib/test/test_capi/test_list.py index 32df3a76cbe0d8..131fb63b13faea 100644 --- a/Lib/test/test_capi/test_list.py +++ b/Lib/test/test_capi/test_list.py @@ -91,7 +91,7 @@ def test_list_get_item(self): self.assertNotEqual(get_item(lst, 1), 12) # CRASHES for out of index: get_item(lst, 3) # CRASHES get_item(21, 2) - # CRASHES get_item(Null,1) + # CRASHES get_item(NULL, 0) def test_list_setitem(self): From 0e340a129c6e66b8ba6c3c15b0ddbdd63c1b8706 Mon Sep 17 00:00:00 2001 From: kalyanr Date: Wed, 1 Nov 2023 15:59:01 +0530 Subject: [PATCH 19/26] pr feedback fixes Signed-off-by: kalyanr --- Lib/test/test_capi/test_list.py | 18 +++++++++++++++--- Modules/_testcapi/list.c | 12 ++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/Lib/test/test_capi/test_list.py b/Lib/test/test_capi/test_list.py index 131fb63b13faea..39556a47eec8ca 100644 --- a/Lib/test/test_capi/test_list.py +++ b/Lib/test/test_capi/test_list.py @@ -91,21 +91,33 @@ def test_list_get_item(self): self.assertNotEqual(get_item(lst, 1), 12) # CRASHES for out of index: get_item(lst, 3) # CRASHES get_item(21, 2) - # CRASHES get_item(NULL, 0) + # CRASHES get_item(Null,1) def test_list_setitem(self): - # Test PyList_SET_ITEM() + # Test PyList_SetItem() setitem = _testcapi.list_setitem lst = [1, 2, 3] setitem(lst, 1, 10) - self.assertEqual(lst, [1, 10, 3]) + self.assertEqual(lst[1], 10) self.assertRaises(IndexError, setitem, [1], -1, 5) self.assertRaises(TypeError, setitem, lst, 1.5, 10) self.assertRaises(TypeError, setitem, 23, 'a', 5) self.assertRaises(SystemError, setitem, {}, 0, 5) # CRASHES setitem(NULL, 'a', 5) + + def test_list_set_item(self): + # Test PyList_SET_ITEM() + set_item = _testcapi.list_set_item + lst = [1, 2, 3] + set_item(lst, 1, 10) + # self.assertEqual(lst[1], 10) + # self.assertRaises(IndexError, set_item, [1], -1, 5) + # self.assertRaises(TypeError, set_item, lst, 1.5, 10) + # self.assertRaises(TypeError, set_item, 23, 'a', 5) + # self.assertRaises(SystemError, set_item, {}, 0, 5) + def test_list_insert(self): # Test PyList_Insert() diff --git a/Modules/_testcapi/list.c b/Modules/_testcapi/list.c index 24cbaa6d5225e9..a45faf0ee53be9 100644 --- a/Modules/_testcapi/list.c +++ b/Modules/_testcapi/list.c @@ -69,8 +69,7 @@ list_setitem(PyObject *Py_UNUSED(module), PyObject *args) } NULLABLE(obj); NULLABLE(value); - value = Py_XNewRef(value); - RETURN_INT(PyList_SetItem(obj, i, value)); + RETURN_INT(PyList_SetItem(obj, i, Py_XNewRef(value))); } @@ -84,8 +83,7 @@ list_set_item(PyObject *Py_UNUSED(module), PyObject *args) } NULLABLE(obj); NULLABLE(value); - value = Py_XNewRef(value); - PyList_SET_ITEM(obj, i, value); + PyList_SET_ITEM(obj, i, Py_XNewRef(value)); Py_RETURN_NONE; } @@ -100,8 +98,7 @@ list_insert(PyObject *Py_UNUSED(module), PyObject *args) } NULLABLE(obj); NULLABLE(value); - value = Py_XNewRef(value); - RETURN_INT(PyList_Insert(obj, where, value)); + RETURN_INT(PyList_Insert(obj, where, Py_XNewRef(value))); } @@ -140,8 +137,7 @@ list_setslice(PyObject *Py_UNUSED(module), PyObject *args) } NULLABLE(obj); NULLABLE(value); - value = Py_XNewRef(value); - return PyList_SetSlice(obj, ilow, ihigh, value); + return PyList_SetSlice(obj, ilow, ihigh, Py_XNewRef(value)); } From 8de9cf8397e552c0792313b689f1104938aeb019 Mon Sep 17 00:00:00 2001 From: kalyanr Date: Wed, 1 Nov 2023 19:58:47 +0530 Subject: [PATCH 20/26] fixes for the feedback Signed-off-by: kalyanr --- Lib/test/test_capi/test_list.py | 83 +++++++++++++++++++++++---------- 1 file changed, 59 insertions(+), 24 deletions(-) diff --git a/Lib/test/test_capi/test_list.py b/Lib/test/test_capi/test_list.py index 39556a47eec8ca..6a6ff6fafd5553 100644 --- a/Lib/test/test_capi/test_list.py +++ b/Lib/test/test_capi/test_list.py @@ -2,10 +2,12 @@ import sys from test.support import import_helper from collections import UserList - +from itertools import combinations _testcapi = import_helper.import_module('_testcapi') NULL = None +PY_SSIZE_T_MIN = _testcapi.PY_SSIZE_T_MIN +PY_SSIZE_T_MAX = _testcapi.PY_SSIZE_T_MAX class ListSubclass(list): pass @@ -59,7 +61,7 @@ def test_list_size(self): self.assertRaises(SystemError, size, 23) self.assertRaises(SystemError, size, object()) # CRASHES size(NULL) - + def test_list_get_size(self): # Test PyList_GET_SIZE() size = _testcapi.list_get_size @@ -75,10 +77,13 @@ def test_list_get_size(self): def test_list_getitem(self): # Test PyList_GetItem() getitem = _testcapi.list_getitem - lst = [1, 2, NULL] + lst = [1, 2, 3] self.assertEqual(getitem(lst, 0), 1) + self.assertEqual(getitem(lst, len(lst)-1), 3) self.assertRaises(IndexError, getitem, lst, -1) - self.assertRaises(IndexError, getitem, lst, 10) + self.assertRaises(IndexError, getitem, lst, PY_SSIZE_T_MIN) + self.assertRaises(IndexError, getitem, lst, PY_SSIZE_T_MAX) + self.assertRaises(IndexError, getitem, lst, len(lst)) self.assertRaises(SystemError, getitem, 42, 1) # CRASHES getitem(NULL, 1) @@ -86,9 +91,12 @@ def test_list_getitem(self): def test_list_get_item(self): # Test PyList_GET_ITEM() get_item = _testcapi.list_get_item - lst = [1, 2, NULL] + lst = [1, 2, [1, 2, 3]] self.assertEqual(get_item(lst, 0), 1) - self.assertNotEqual(get_item(lst, 1), 12) + self.assertEqual(get_item(lst, 2), [1, 2, 3]) + + # CRASHES for get_item(lst, PY_SSIZE_T_MIN) + # CRASHES for get_item(lst, PY_SSIZE_T_MAX) # CRASHES for out of index: get_item(lst, 3) # CRASHES get_item(21, 2) # CRASHES get_item(Null,1) @@ -98,38 +106,56 @@ def test_list_setitem(self): # Test PyList_SetItem() setitem = _testcapi.list_setitem lst = [1, 2, 3] - setitem(lst, 1, 10) - self.assertEqual(lst[1], 10) - self.assertRaises(IndexError, setitem, [1], -1, 5) + setitem(lst, 0, 10) + self.assertEqual(lst[0], 10) + setitem(lst, len(lst)-1, 12) + self.assertEqual(lst[-1], 12) + self.assertRaises(IndexError, setitem, lst, PY_SSIZE_T_MIN, 5) + self.assertRaises(IndexError, setitem, lst, PY_SSIZE_T_MAX, 5) + self.assertRaises(IndexError, setitem, lst, -1, 5) + self.assertRaises(IndexError, setitem, lst, len(lst) , 5) self.assertRaises(TypeError, setitem, lst, 1.5, 10) self.assertRaises(TypeError, setitem, 23, 'a', 5) self.assertRaises(SystemError, setitem, {}, 0, 5) # CRASHES setitem(NULL, 'a', 5) - + def test_list_set_item(self): # Test PyList_SET_ITEM() set_item = _testcapi.list_set_item lst = [1, 2, 3] set_item(lst, 1, 10) - # self.assertEqual(lst[1], 10) - # self.assertRaises(IndexError, set_item, [1], -1, 5) - # self.assertRaises(TypeError, set_item, lst, 1.5, 10) - # self.assertRaises(TypeError, set_item, 23, 'a', 5) - # self.assertRaises(SystemError, set_item, {}, 0, 5) + set_item(lst, 2, [1, 2, 3]) + self.assertEqual(lst[2], [1, 2, 3]) + + # CRASHES for set_item([1], PY_SSIZE_T_MIN, 5) + # CRASHES for set_item([1], PY_SSIZE_T_MAX, 5) + # CRASHES for set_item([1], -1, 5) + # CRASHES for set_item([], 0, 1) + # CRASHES for set_item(NULL, 0, 1) def test_list_insert(self): # Test PyList_Insert() insert = _testcapi.list_insert lst = [1, 2, 3] - insert(lst, 1, 23) - self.assertEqual(lst[1], 23) + insert(lst, 0, 23) + self.assertEqual(lst[0], 23) insert(lst, -1, 22) self.assertEqual(lst[-2], 22) + insert(lst, PY_SSIZE_T_MIN, 1) + self.assertEqual(lst[0], 1) + insert(lst, len(lst), 123) + self.assertEqual(lst[-1], 123) + insert(lst, len(lst)-1, 124) + self.assertEqual(lst[-2], 124) + insert(lst, PY_SSIZE_T_MAX, 223) + self.assertEqual(lst[-1], 223) + self.assertRaises(TypeError, insert, lst, 1.5, 10) self.assertRaises(TypeError, insert, 23, 'a', 5) self.assertRaises(SystemError, insert, {}, 0, 5) + # CRASHES insert(NULL, 'a', 5) def test_list_append(self): @@ -146,12 +172,21 @@ def test_list_append(self): def test_list_getslice(self): # Test PyList_GetSlice() getslice = _testcapi.list_getslice - lst = [1,2,3,4,5,6,7,8,9,10] - self.assertEqual(getslice(lst, 0, 3), [1, 2, 3]) - self.assertEqual(getslice(lst, 4, 6), [5,6]) - self.assertEqual(getslice(lst, 6, 10), [7,8,9,10]) - self.assertEqual(getslice(lst, 6, 100), [7,8,9,10]) - self.assertNotEqual(getslice(lst, -2, -1), [9]) + lst = [1,2,3] + + # empty + self.assertEqual(getslice(lst, PY_SSIZE_T_MIN, 0), []) + self.assertEqual(getslice(lst, -1, 0), []) + self.assertEqual(getslice(lst, 3, PY_SSIZE_T_MAX), []) + + # slice + self.assertEqual(getslice(lst, 1, 3), [2, 3]) + + # whole + self.assertEqual(getslice(lst, 0, len(lst)), lst) + self.assertEqual(getslice(lst, 0, 100), lst) + self.assertEqual(getslice(lst, -100, 100), lst) + self.assertRaises(TypeError, lst, 'a', '2') - # CRASHES getslice(NULL, 1, 2) + # CRASHES getslice(NULL, 0, 0) From 44dcf85f85ec4ed077732ef2a2297c01c2b9326e Mon Sep 17 00:00:00 2001 From: kalyanr Date: Wed, 1 Nov 2023 20:27:42 +0530 Subject: [PATCH 21/26] add tests from vstinner PR Signed-off-by: kalyanr --- Lib/test/test_capi/test_list.py | 75 +++++++++++++++++++++++++++++++++ Modules/_testcapi/list.c | 6 +-- 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_capi/test_list.py b/Lib/test/test_capi/test_list.py index 6a6ff6fafd5553..72826df8203e1b 100644 --- a/Lib/test/test_capi/test_list.py +++ b/Lib/test/test_capi/test_list.py @@ -190,3 +190,78 @@ def test_list_getslice(self): self.assertRaises(TypeError, lst, 'a', '2') # CRASHES getslice(NULL, 0, 0) + + def test_list_setslice(self): + # Test PyList_SetSlice() + def set_slice(lst, low, high, value): + lst = lst.copy() + self.assertEqual(_testcapi.list_setslice(lst, low, high, value), 0) + return lst + + # insert items + self.assertEqual(set_slice([], 0, 0, list("abc")), list("abc")) + self.assertEqual(set_slice([], PY_SSIZE_T_MIN, PY_SSIZE_T_MIN, list("abc")), list("abc")) + self.assertEqual(set_slice([], PY_SSIZE_T_MAX, PY_SSIZE_T_MAX, list("abc")), list("abc")) + lst = list("abc") + self.assertEqual(set_slice(lst, 0, 0, ["X"]), list("Xabc")) + self.assertEqual(set_slice(lst, 1, 1, list("XY")), list("aXYbc")) + self.assertEqual(set_slice(lst, len(lst), len(lst), ["X"]), list("abcX")) + # self.assertEqual(set_slice(lst, PY_SSIZE_T_MAX, PY_SSIZE_T_MAX, ["X"]), list("abcX")) + + # replace items + lst = list("abc") + self.assertEqual(set_slice(lst, -100, 1, list("X")), list("Xbc")) + self.assertEqual(set_slice(lst, 1, 2, list("X")), list("aXc")) + self.assertEqual(set_slice(lst, 1, 3, list("XY")), list("aXY")) + self.assertEqual(set_slice(lst, 0, 3, list("XYZ")), list("XYZ")) + + # delete items + lst = list("abcdef") + self.assertEqual(set_slice(lst, 0, len(lst), []), []) + self.assertEqual(set_slice(lst, -100, 100, []), []) + self.assertEqual(set_slice(lst, 1, 5, []), list("af")) + self.assertEqual(set_slice(lst, 3, len(lst), []), list("abc")) + + # delete items with NULL + lst = list("abcdef") + self.assertEqual(set_slice(lst, 0, len(lst), NULL), []) + self.assertEqual(set_slice(lst, 3, len(lst), NULL), list("abc")) + + # CRASHES PyList_SetSlice(NULL, 0, 0, ["x"]) + + def test_list_sort(self): + # Test PyList_Sort() + sort = _testcapi.list_sort + lst = [4, 6, 7, 3, 1, 5, 9, 2, 0, 8] + sort(lst) + self.assertEqual(lst, list(range(10))) + + lst2 = ListSubclass([4, 6, 7, 3, 1, 5, 9, 2, 0, 8]) + sort(lst2) + self.assertEqual(lst2, list(range(10))) + + self.assertRaises(SystemError, sort, object()) + self.assertRaises(SystemError, sort, NULL) + + + def test_list_reverse(self): + # Test PyList_Reverse() + reverse = _testcapi.list_reverse + def list_reverse(lst): + self.assertEqual(reverse(lst), 0) + return lst + + self.assertEqual(list_reverse([]), []) + self.assertEqual(list_reverse([2, 5, 10]), [10, 5, 2]) + + self.assertRaises(SystemError, reverse, object()) + self.assertRaises(SystemError, reverse, NULL) + + def test_list_astuple(self): + # Test PyList_AsTuple() + astuple = _testcapi.list_astuple + self.assertEqual(astuple([]), ()) + self.assertEqual(astuple([2, 5, 10]), (2, 5, 10)) + + self.assertRaises(SystemError, astuple, object()) + self.assertRaises(SystemError, astuple, NULL) diff --git a/Modules/_testcapi/list.c b/Modules/_testcapi/list.c index a45faf0ee53be9..28fc5698a18219 100644 --- a/Modules/_testcapi/list.c +++ b/Modules/_testcapi/list.c @@ -132,12 +132,12 @@ list_setslice(PyObject *Py_UNUSED(module), PyObject *args) { PyObject *obj, *value; Py_ssize_t ilow, ihigh; - if ( !PyArg_ParseTuple(args, "OnnO", &obj, &ilow, &ihigh)){ + if ( !PyArg_ParseTuple(args, "OnnO", &obj, &ilow, &ihigh, &value)){ return NULL; } NULLABLE(obj); NULLABLE(value); - return PyList_SetSlice(obj, ilow, ihigh, Py_XNewRef(value)); + RETURN_INT(PyList_SetSlice(obj, ilow, ihigh, Py_XNewRef(value))); } @@ -184,7 +184,7 @@ static PyMethodDef test_methods[] = { {"list_astuple", list_astuple, METH_O}, {NULL}, - + }; int From 2c6a41c75992187433df75e31524a3cf118c98ed Mon Sep 17 00:00:00 2001 From: kalyanr Date: Wed, 1 Nov 2023 20:33:26 +0530 Subject: [PATCH 22/26] remove unused import Signed-off-by: kalyanr --- Lib/test/test_capi/test_list.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_capi/test_list.py b/Lib/test/test_capi/test_list.py index 72826df8203e1b..e512dd2bed8f58 100644 --- a/Lib/test/test_capi/test_list.py +++ b/Lib/test/test_capi/test_list.py @@ -2,7 +2,6 @@ import sys from test.support import import_helper from collections import UserList -from itertools import combinations _testcapi = import_helper.import_module('_testcapi') NULL = None From e4864510114c68ed177b9aea8dc7e96d5a35f507 Mon Sep 17 00:00:00 2001 From: Kalyan Date: Wed, 1 Nov 2023 20:40:18 +0530 Subject: [PATCH 23/26] Update list.c --- Modules/_testcapi/list.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/Modules/_testcapi/list.c b/Modules/_testcapi/list.c index 28fc5698a18219..70a24ea6038866 100644 --- a/Modules/_testcapi/list.c +++ b/Modules/_testcapi/list.c @@ -183,8 +183,6 @@ static PyMethodDef test_methods[] = { {"list_reverse", list_reverse, METH_O}, {"list_astuple", list_astuple, METH_O}, {NULL}, - - }; int From ca07ac99831e9d3ebdf76888da24bdd3c6ad5fac Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 8 Nov 2023 15:56:42 +0100 Subject: [PATCH 24/26] Update Modules/_testcapi/list.c --- Modules/_testcapi/list.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_testcapi/list.c b/Modules/_testcapi/list.c index 70a24ea6038866..f269d5b6729043 100644 --- a/Modules/_testcapi/list.c +++ b/Modules/_testcapi/list.c @@ -137,7 +137,7 @@ list_setslice(PyObject *Py_UNUSED(module), PyObject *args) } NULLABLE(obj); NULLABLE(value); - RETURN_INT(PyList_SetSlice(obj, ilow, ihigh, Py_XNewRef(value))); + RETURN_INT(PyList_SetSlice(obj, ilow, ihigh, value)); } From dc039f1c62be326cd01fe4272aec63be64af0e03 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 8 Nov 2023 15:57:03 +0100 Subject: [PATCH 25/26] Update Modules/_testcapi/list.c --- Modules/_testcapi/list.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_testcapi/list.c b/Modules/_testcapi/list.c index f269d5b6729043..ff6f6b8ee69794 100644 --- a/Modules/_testcapi/list.c +++ b/Modules/_testcapi/list.c @@ -111,7 +111,7 @@ list_append(PyObject *Py_UNUSED(module), PyObject *args) } NULLABLE(obj); NULLABLE(value); - RETURN_INT(PyList_Append(obj, Py_XNewRef(value))); + RETURN_INT(PyList_Append(obj, value)); } static PyObject * From 42a2a54aefce3f5c11965d6bfe711aebc31bf162 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 8 Nov 2023 18:19:58 +0200 Subject: [PATCH 26/26] Minor fixes. --- Lib/test/test_capi/test_list.py | 67 +++++++++++++++++++-------------- Modules/_testcapi/list.c | 19 +++++----- 2 files changed, 48 insertions(+), 38 deletions(-) diff --git a/Lib/test/test_capi/test_list.py b/Lib/test/test_capi/test_list.py index e512dd2bed8f58..197da03e07fa27 100644 --- a/Lib/test/test_capi/test_list.py +++ b/Lib/test/test_capi/test_list.py @@ -78,12 +78,14 @@ def test_list_getitem(self): getitem = _testcapi.list_getitem lst = [1, 2, 3] self.assertEqual(getitem(lst, 0), 1) - self.assertEqual(getitem(lst, len(lst)-1), 3) + self.assertEqual(getitem(lst, 2), 3) + self.assertRaises(IndexError, getitem, lst, 3) self.assertRaises(IndexError, getitem, lst, -1) self.assertRaises(IndexError, getitem, lst, PY_SSIZE_T_MIN) self.assertRaises(IndexError, getitem, lst, PY_SSIZE_T_MAX) - self.assertRaises(IndexError, getitem, lst, len(lst)) self.assertRaises(SystemError, getitem, 42, 1) + self.assertRaises(SystemError, getitem, (1, 2, 3), 1) + self.assertRaises(SystemError, getitem, {1: 2}, 1) # CRASHES getitem(NULL, 1) @@ -94,11 +96,11 @@ def test_list_get_item(self): self.assertEqual(get_item(lst, 0), 1) self.assertEqual(get_item(lst, 2), [1, 2, 3]) + # CRASHES for out of index: get_item(lst, 3) # CRASHES for get_item(lst, PY_SSIZE_T_MIN) # CRASHES for get_item(lst, PY_SSIZE_T_MAX) - # CRASHES for out of index: get_item(lst, 3) # CRASHES get_item(21, 2) - # CRASHES get_item(Null,1) + # CRASHES get_item(NULL, 1) def test_list_setitem(self): @@ -106,16 +108,15 @@ def test_list_setitem(self): setitem = _testcapi.list_setitem lst = [1, 2, 3] setitem(lst, 0, 10) - self.assertEqual(lst[0], 10) - setitem(lst, len(lst)-1, 12) - self.assertEqual(lst[-1], 12) + self.assertEqual(lst, [10, 2, 3]) + setitem(lst, 2, 12) + self.assertEqual(lst, [10, 2, 12]) + self.assertRaises(IndexError, setitem, lst, 3 , 5) + self.assertRaises(IndexError, setitem, lst, -1, 5) self.assertRaises(IndexError, setitem, lst, PY_SSIZE_T_MIN, 5) self.assertRaises(IndexError, setitem, lst, PY_SSIZE_T_MAX, 5) - self.assertRaises(IndexError, setitem, lst, -1, 5) - self.assertRaises(IndexError, setitem, lst, len(lst) , 5) - self.assertRaises(TypeError, setitem, lst, 1.5, 10) - self.assertRaises(TypeError, setitem, 23, 'a', 5) - self.assertRaises(SystemError, setitem, {}, 0, 5) + self.assertRaises(SystemError, setitem, (1, 2, 3), 1, 5) + self.assertRaises(SystemError, setitem, {1: 2}, 1, 5) # CRASHES setitem(NULL, 'a', 5) @@ -125,11 +126,11 @@ def test_list_set_item(self): lst = [1, 2, 3] set_item(lst, 1, 10) set_item(lst, 2, [1, 2, 3]) - self.assertEqual(lst[2], [1, 2, 3]) + self.assertEqual(lst, [1, 10, [1, 2, 3]]) + # CRASHES for set_item([1], -1, 5) # CRASHES for set_item([1], PY_SSIZE_T_MIN, 5) # CRASHES for set_item([1], PY_SSIZE_T_MAX, 5) - # CRASHES for set_item([1], -1, 5) # CRASHES for set_item([], 0, 1) # CRASHES for set_item(NULL, 0, 1) @@ -139,9 +140,9 @@ def test_list_insert(self): insert = _testcapi.list_insert lst = [1, 2, 3] insert(lst, 0, 23) - self.assertEqual(lst[0], 23) + self.assertEqual(lst, [23, 1, 2, 3]) insert(lst, -1, 22) - self.assertEqual(lst[-2], 22) + self.assertEqual(lst, [23, 1, 2, 22, 3]) insert(lst, PY_SSIZE_T_MIN, 1) self.assertEqual(lst[0], 1) insert(lst, len(lst), 123) @@ -151,27 +152,28 @@ def test_list_insert(self): insert(lst, PY_SSIZE_T_MAX, 223) self.assertEqual(lst[-1], 223) - self.assertRaises(TypeError, insert, lst, 1.5, 10) - self.assertRaises(TypeError, insert, 23, 'a', 5) - self.assertRaises(SystemError, insert, {}, 0, 5) + self.assertRaises(SystemError, insert, (1, 2, 3), 1, 5) + self.assertRaises(SystemError, insert, {1: 2}, 1, 5) - # CRASHES insert(NULL, 'a', 5) + # CRASHES insert(NULL, 1, 5) def test_list_append(self): # Test PyList_Append() append = _testcapi.list_append lst = [1, 2, 3] - append(lst, 1) - self.assertEqual(lst[-1], 1) - append(lst, [4,5,6]) - self.assertEqual(lst[-1], [4,5,6]) + append(lst, 10) + self.assertEqual(lst, [1, 2, 3, 10]) + append(lst, [4, 5]) + self.assertEqual(lst, [1, 2, 3, 10, [4, 5]]) self.assertRaises(SystemError, append, lst, NULL) + self.assertRaises(SystemError, append, (), 0) + self.assertRaises(SystemError, append, 42, 0) # CRASHES append(NULL, 0) def test_list_getslice(self): # Test PyList_GetSlice() getslice = _testcapi.list_getslice - lst = [1,2,3] + lst = [1, 2, 3] # empty self.assertEqual(getslice(lst, PY_SSIZE_T_MIN, 0), []) @@ -186,15 +188,18 @@ def test_list_getslice(self): self.assertEqual(getslice(lst, 0, 100), lst) self.assertEqual(getslice(lst, -100, 100), lst) - self.assertRaises(TypeError, lst, 'a', '2') + self.assertRaises(SystemError, getslice, (1, 2, 3), 0, 0) + self.assertRaises(SystemError, getslice, 'abc', 0, 0) + self.assertRaises(SystemError, getslice, 42, 0, 0) # CRASHES getslice(NULL, 0, 0) def test_list_setslice(self): # Test PyList_SetSlice() + setslice = _testcapi.list_setslice def set_slice(lst, low, high, value): lst = lst.copy() - self.assertEqual(_testcapi.list_setslice(lst, low, high, value), 0) + self.assertEqual(setslice(lst, low, high, value), 0) return lst # insert items @@ -226,7 +231,10 @@ def set_slice(lst, low, high, value): self.assertEqual(set_slice(lst, 0, len(lst), NULL), []) self.assertEqual(set_slice(lst, 3, len(lst), NULL), list("abc")) - # CRASHES PyList_SetSlice(NULL, 0, 0, ["x"]) + self.assertRaises(SystemError, setslice, (), 0, 0, []) + self.assertRaises(SystemError, setslice, 42, 0, 0, []) + + # CRASHES setslice(NULL, 0, 0, []) def test_list_sort(self): # Test PyList_Sort() @@ -239,6 +247,7 @@ def test_list_sort(self): sort(lst2) self.assertEqual(lst2, list(range(10))) + self.assertRaises(SystemError, sort, ()) self.assertRaises(SystemError, sort, object()) self.assertRaises(SystemError, sort, NULL) @@ -253,6 +262,7 @@ def list_reverse(lst): self.assertEqual(list_reverse([]), []) self.assertEqual(list_reverse([2, 5, 10]), [10, 5, 2]) + self.assertRaises(SystemError, reverse, ()) self.assertRaises(SystemError, reverse, object()) self.assertRaises(SystemError, reverse, NULL) @@ -262,5 +272,6 @@ def test_list_astuple(self): self.assertEqual(astuple([]), ()) self.assertEqual(astuple([2, 5, 10]), (2, 5, 10)) + self.assertRaises(SystemError, astuple, ()) self.assertRaises(SystemError, astuple, object()) self.assertRaises(SystemError, astuple, NULL) diff --git a/Modules/_testcapi/list.c b/Modules/_testcapi/list.c index ff6f6b8ee69794..6ba0e7ab27c5d7 100644 --- a/Modules/_testcapi/list.c +++ b/Modules/_testcapi/list.c @@ -40,7 +40,7 @@ list_getitem(PyObject *Py_UNUSED(module), PyObject *args) { PyObject *obj; Py_ssize_t i; - if (!PyArg_ParseTuple(args, "On", &obj, &i)){ + if (!PyArg_ParseTuple(args, "On", &obj, &i)) { return NULL; } NULLABLE(obj); @@ -52,7 +52,7 @@ list_get_item(PyObject *Py_UNUSED(module), PyObject *args) { PyObject *obj; Py_ssize_t i; - if (!PyArg_ParseTuple(args, "On", &obj, &i)){ + if (!PyArg_ParseTuple(args, "On", &obj, &i)) { return NULL; } NULLABLE(obj); @@ -64,7 +64,7 @@ list_setitem(PyObject *Py_UNUSED(module), PyObject *args) { PyObject *obj, *value; Py_ssize_t i; - if ( !PyArg_ParseTuple(args, "OnO", &obj, &i, &value)){ + if (!PyArg_ParseTuple(args, "OnO", &obj, &i, &value)) { return NULL; } NULLABLE(obj); @@ -78,7 +78,7 @@ list_set_item(PyObject *Py_UNUSED(module), PyObject *args) { PyObject *obj, *value; Py_ssize_t i; - if ( !PyArg_ParseTuple(args, "OnO", &obj, &i, &value)){ + if (!PyArg_ParseTuple(args, "OnO", &obj, &i, &value)) { return NULL; } NULLABLE(obj); @@ -93,7 +93,7 @@ list_insert(PyObject *Py_UNUSED(module), PyObject *args) { PyObject *obj, *value; Py_ssize_t where; - if ( !PyArg_ParseTuple(args, "OnO", &obj, &where, &value)){ + if (!PyArg_ParseTuple(args, "OnO", &obj, &where, &value)) { return NULL; } NULLABLE(obj); @@ -106,7 +106,7 @@ static PyObject * list_append(PyObject *Py_UNUSED(module), PyObject *args) { PyObject *obj, *value; - if ( !PyArg_ParseTuple(args, "OO", &obj, &value)){ + if (!PyArg_ParseTuple(args, "OO", &obj, &value)) { return NULL; } NULLABLE(obj); @@ -119,7 +119,7 @@ list_getslice(PyObject *Py_UNUSED(module), PyObject *args) { PyObject *obj; Py_ssize_t ilow, ihigh; - if ( !PyArg_ParseTuple(args, "Onn", &obj, &ilow, &ihigh)){ + if (!PyArg_ParseTuple(args, "Onn", &obj, &ilow, &ihigh)) { return NULL; } NULLABLE(obj); @@ -132,13 +132,12 @@ list_setslice(PyObject *Py_UNUSED(module), PyObject *args) { PyObject *obj, *value; Py_ssize_t ilow, ihigh; - if ( !PyArg_ParseTuple(args, "OnnO", &obj, &ilow, &ihigh, &value)){ + if (!PyArg_ParseTuple(args, "OnnO", &obj, &ilow, &ihigh, &value)) { return NULL; } NULLABLE(obj); NULLABLE(value); RETURN_INT(PyList_SetSlice(obj, ilow, ihigh, value)); - } static PyObject * @@ -188,7 +187,7 @@ static PyMethodDef test_methods[] = { int _PyTestCapi_Init_List(PyObject *m) { - if (PyModule_AddFunctions(m, test_methods) < 0){ + if (PyModule_AddFunctions(m, test_methods) < 0) { return -1; }