Skip to content

Commit

Permalink
pythongh-116417: Move limited C API list.c tests to _testlimitedcapi (
Browse files Browse the repository at this point in the history
…python#116602)

Split list.c and set.c tests of _testcapi into two parts: limited C
API tests in _testlimitedcapi and non-limited C API tests in
_testcapi.
  • Loading branch information
vstinner authored and adorilson committed Mar 25, 2024
1 parent ac9d421 commit f7e5df1
Show file tree
Hide file tree
Showing 11 changed files with 404 additions and 351 deletions.
29 changes: 15 additions & 14 deletions Lib/test/test_capi/test_list.py
Expand Up @@ -4,6 +4,7 @@
from test.support import import_helper
from collections import UserList
_testcapi = import_helper.import_module('_testcapi')
_testlimitedcapi = import_helper.import_module('_testlimitedcapi')

NULL = None
PY_SSIZE_T_MIN = _testcapi.PY_SSIZE_T_MIN
Expand All @@ -25,7 +26,7 @@ def __del__(self):
class CAPITest(unittest.TestCase):
def test_check(self):
# Test PyList_Check()
check = _testcapi.list_check
check = _testlimitedcapi.list_check
self.assertTrue(check([1, 2]))
self.assertTrue(check([]))
self.assertTrue(check(ListSubclass([1, 2])))
Expand All @@ -39,7 +40,7 @@ def test_check(self):

def test_list_check_exact(self):
# Test PyList_CheckExact()
check = _testcapi.list_check_exact
check = _testlimitedcapi.list_check_exact
self.assertTrue(check([1]))
self.assertTrue(check([]))
self.assertFalse(check(ListSubclass([1])))
Expand All @@ -51,7 +52,7 @@ def test_list_check_exact(self):

def test_list_new(self):
# Test PyList_New()
list_new = _testcapi.list_new
list_new = _testlimitedcapi.list_new
lst = list_new(0)
self.assertEqual(lst, [])
self.assertIs(type(lst), list)
Expand All @@ -62,7 +63,7 @@ def test_list_new(self):

def test_list_size(self):
# Test PyList_Size()
size = _testcapi.list_size
size = _testlimitedcapi.list_size
self.assertEqual(size([1, 2]), 2)
self.assertEqual(size(ListSubclass([1, 2])), 2)
self.assertRaises(SystemError, size, UserList())
Expand Down Expand Up @@ -98,11 +99,11 @@ def check_list_get_item(self, getitem, exctype):

def test_list_getitem(self):
# Test PyList_GetItem()
self.check_list_get_item(_testcapi.list_getitem, SystemError)
self.check_list_get_item(_testlimitedcapi.list_getitem, SystemError)

def test_list_get_item_ref(self):
# Test PyList_GetItemRef()
self.check_list_get_item(_testcapi.list_get_item_ref, TypeError)
self.check_list_get_item(_testlimitedcapi.list_get_item_ref, TypeError)

def test_list_get_item(self):
# Test PyList_GET_ITEM()
Expand All @@ -119,7 +120,7 @@ def test_list_get_item(self):

def test_list_setitem(self):
# Test PyList_SetItem()
setitem = _testcapi.list_setitem
setitem = _testlimitedcapi.list_setitem
lst = [1, 2, 3]
setitem(lst, 0, 10)
self.assertEqual(lst, [10, 2, 3])
Expand Down Expand Up @@ -151,7 +152,7 @@ def test_list_set_item(self):

def test_list_insert(self):
# Test PyList_Insert()
insert = _testcapi.list_insert
insert = _testlimitedcapi.list_insert
lst = [1, 2, 3]
insert(lst, 0, 23)
self.assertEqual(lst, [23, 1, 2, 3])
Expand All @@ -173,7 +174,7 @@ def test_list_insert(self):

def test_list_append(self):
# Test PyList_Append()
append = _testcapi.list_append
append = _testlimitedcapi.list_append
lst = [1, 2, 3]
append(lst, 10)
self.assertEqual(lst, [1, 2, 3, 10])
Expand All @@ -186,7 +187,7 @@ def test_list_append(self):

def test_list_getslice(self):
# Test PyList_GetSlice()
getslice = _testcapi.list_getslice
getslice = _testlimitedcapi.list_getslice
lst = [1, 2, 3]

# empty
Expand All @@ -210,7 +211,7 @@ def test_list_getslice(self):

def test_list_setslice(self):
# Test PyList_SetSlice()
list_setslice = _testcapi.list_setslice
list_setslice = _testlimitedcapi.list_setslice
def set_slice(lst, low, high, value):
lst = lst.copy()
self.assertEqual(list_setslice(lst, low, high, value), 0)
Expand Down Expand Up @@ -265,7 +266,7 @@ def set_slice(lst, low, high, value):

def test_list_sort(self):
# Test PyList_Sort()
sort = _testcapi.list_sort
sort = _testlimitedcapi.list_sort
lst = [4, 6, 7, 3, 1, 5, 9, 2, 0, 8]
sort(lst)
self.assertEqual(lst, list(range(10)))
Expand All @@ -281,7 +282,7 @@ def test_list_sort(self):

def test_list_reverse(self):
# Test PyList_Reverse()
reverse = _testcapi.list_reverse
reverse = _testlimitedcapi.list_reverse
def list_reverse(lst):
self.assertEqual(reverse(lst), 0)
return lst
Expand All @@ -295,7 +296,7 @@ def list_reverse(lst):

def test_list_astuple(self):
# Test PyList_AsTuple()
astuple = _testcapi.list_astuple
astuple = _testlimitedcapi.list_astuple
self.assertEqual(astuple([]), ())
self.assertEqual(astuple([2, 5, 10]), (2, 5, 10))

Expand Down
32 changes: 17 additions & 15 deletions Lib/test/test_capi/test_set.py
Expand Up @@ -2,8 +2,10 @@

from test.support import import_helper

# Skip this test if the _testcapi or _testinternalcapi modules aren't available.
# Skip this test if the _testcapi, _testlimitedcapi or _testinternalcapi
# modules aren't available.
_testcapi = import_helper.import_module('_testcapi')
_testlimitedcapi = import_helper.import_module('_testlimitedcapi')
_testinternalcapi = import_helper.import_module('_testinternalcapi')

class set_subclass(set):
Expand All @@ -23,7 +25,7 @@ def assertImmutable(self, action, *args):

class TestSetCAPI(BaseSetTests, unittest.TestCase):
def test_set_check(self):
check = _testcapi.set_check
check = _testlimitedcapi.set_check
self.assertTrue(check(set()))
self.assertTrue(check({1, 2}))
self.assertFalse(check(frozenset()))
Expand All @@ -33,7 +35,7 @@ def test_set_check(self):
# CRASHES: check(NULL)

def test_set_check_exact(self):
check = _testcapi.set_checkexact
check = _testlimitedcapi.set_checkexact
self.assertTrue(check(set()))
self.assertTrue(check({1, 2}))
self.assertFalse(check(frozenset()))
Expand All @@ -43,7 +45,7 @@ def test_set_check_exact(self):
# CRASHES: check(NULL)

def test_frozenset_check(self):
check = _testcapi.frozenset_check
check = _testlimitedcapi.frozenset_check
self.assertFalse(check(set()))
self.assertTrue(check(frozenset()))
self.assertTrue(check(frozenset({1, 2})))
Expand All @@ -53,7 +55,7 @@ def test_frozenset_check(self):
# CRASHES: check(NULL)

def test_frozenset_check_exact(self):
check = _testcapi.frozenset_checkexact
check = _testlimitedcapi.frozenset_checkexact
self.assertFalse(check(set()))
self.assertTrue(check(frozenset()))
self.assertTrue(check(frozenset({1, 2})))
Expand All @@ -63,7 +65,7 @@ def test_frozenset_check_exact(self):
# CRASHES: check(NULL)

def test_anyset_check(self):
check = _testcapi.anyset_check
check = _testlimitedcapi.anyset_check
self.assertTrue(check(set()))
self.assertTrue(check({1, 2}))
self.assertTrue(check(frozenset()))
Expand All @@ -74,7 +76,7 @@ def test_anyset_check(self):
# CRASHES: check(NULL)

def test_anyset_check_exact(self):
check = _testcapi.anyset_checkexact
check = _testlimitedcapi.anyset_checkexact
self.assertTrue(check(set()))
self.assertTrue(check({1, 2}))
self.assertTrue(check(frozenset()))
Expand All @@ -85,7 +87,7 @@ def test_anyset_check_exact(self):
# CRASHES: check(NULL)

def test_set_new(self):
set_new = _testcapi.set_new
set_new = _testlimitedcapi.set_new
self.assertEqual(set_new().__class__, set)
self.assertEqual(set_new(), set())
self.assertEqual(set_new((1, 1, 2)), {1, 2})
Expand All @@ -98,7 +100,7 @@ def test_set_new(self):
set_new((1, {}))

def test_frozenset_new(self):
frozenset_new = _testcapi.frozenset_new
frozenset_new = _testlimitedcapi.frozenset_new
self.assertEqual(frozenset_new().__class__, frozenset)
self.assertEqual(frozenset_new(), frozenset())
self.assertEqual(frozenset_new((1, 1, 2)), frozenset({1, 2}))
Expand All @@ -111,7 +113,7 @@ def test_frozenset_new(self):
frozenset_new((1, {}))

def test_set_size(self):
get_size = _testcapi.set_size
get_size = _testlimitedcapi.set_size
self.assertEqual(get_size(set()), 0)
self.assertEqual(get_size(frozenset()), 0)
self.assertEqual(get_size({1, 1, 2}), 2)
Expand All @@ -134,7 +136,7 @@ def test_set_get_size(self):
# CRASHES: get_size(object())

def test_set_contains(self):
contains = _testcapi.set_contains
contains = _testlimitedcapi.set_contains
for cls in (set, frozenset, set_subclass, frozenset_subclass):
with self.subTest(cls=cls):
instance = cls((1, 2))
Expand All @@ -147,7 +149,7 @@ def test_set_contains(self):
# CRASHES: contains(NULL, NULL)

def test_add(self):
add = _testcapi.set_add
add = _testlimitedcapi.set_add
for cls in (set, set_subclass):
with self.subTest(cls=cls):
instance = cls((1, 2))
Expand All @@ -165,7 +167,7 @@ def test_add(self):
# CRASHES: add(NULL, NULL)

def test_discard(self):
discard = _testcapi.set_discard
discard = _testlimitedcapi.set_discard
for cls in (set, set_subclass):
with self.subTest(cls=cls):
instance = cls((1, 2))
Expand All @@ -187,7 +189,7 @@ def test_discard(self):
# CRASHES: discard(NULL, NULL)

def test_pop(self):
pop = _testcapi.set_pop
pop = _testlimitedcapi.set_pop
orig = (1, 2)
for cls in (set, set_subclass):
with self.subTest(cls=cls):
Expand All @@ -204,7 +206,7 @@ def test_pop(self):
# CRASHES: pop(NULL)

def test_clear(self):
clear = _testcapi.set_clear
clear = _testlimitedcapi.set_clear
for cls in (set, set_subclass):
with self.subTest(cls=cls):
instance = cls((1, 2))
Expand Down
2 changes: 1 addition & 1 deletion Modules/Setup.stdlib.in
Expand Up @@ -163,7 +163,7 @@
@MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c _testinternalcapi/test_lock.c _testinternalcapi/pytime.c _testinternalcapi/set.c _testinternalcapi/test_critical_sections.c
@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/list.c _testcapi/tuple.c _testcapi/getargs.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/complex.c _testcapi/numbers.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyatomic.c _testcapi/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/gc.c _testcapi/hash.c _testcapi/time.c
@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/bytearray.c _testlimitedcapi/bytes.c _testlimitedcapi/heaptype_relative.c _testlimitedcapi/pyos.c _testlimitedcapi/sys.c _testlimitedcapi/vectorcall_limited.c
@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/bytearray.c _testlimitedcapi/bytes.c _testlimitedcapi/heaptype_relative.c _testlimitedcapi/list.c _testlimitedcapi/pyos.c _testlimitedcapi/set.c _testlimitedcapi/sys.c _testlimitedcapi/vectorcall_limited.c
@MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c
@MODULE__TESTCLINIC_LIMITED_TRUE@_testclinic_limited _testclinic_limited.c

Expand Down

0 comments on commit f7e5df1

Please sign in to comment.