test pylistcheck crashes checknull test pylistcheckexact crashes checknull test pylistnew test pylistsize crashes sizenull test pylistgetsize crashes sizeobject crashes size23 crashes size crashes sizeuserlist crashes sizenull test pylistgetitem crashes getitemnull 1 test pylistgetitem crashes for out of index getitemlst 3 crashes for getitemlst pyssizetmin crashes for getitemlst pyssizetmax crashes getitem21 2 crashes getitemnull 1 test pylistsetitem crashes setitemnull a 5 test pylistsetitem crashes for setitem1 1 5 crashes for setitem1 pyssizetmin 5 crashes for setitem1 pyssizetmax 5 crashes for setitem 0 1 crashes for setitemnull 0 1 test pylistinsert crashes insertnull 1 5 test pylistappend crashes appendnull 0 test pylistgetslice empty slice whole crashes getslicenull 0 0 test pylistsetslice insert items self assertequalsetslicelst pyssizetmax pyssizetmax x listabcx replace items delete items delete items with null item finalizer modify the list clear the list item finalizer modify the list remove an list item crashes setslicenull 0 0 test pylistsort test pylistreverse test pylistastuple test pylistclear item finalizer modify the list crashes listclearnull test pylistextend pylistextendlst lst crashes listextendnull crashes listextend null test pylist_check crashes check null test pylist_checkexact crashes check null test pylist_new test pylist_size crashes size null test pylist_get_size crashes size object crashes size 23 crashes size crashes size userlist crashes size null test pylist_getitem crashes getitem null 1 test pylist_get_item 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 get_item 21 2 crashes get_item null 1 test pylist_setitem crashes setitem null a 5 test pylist_set_item 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 0 1 crashes for set_item null 0 1 test pylist_insert crashes insert null 1 5 test pylist_append crashes append null 0 test pylist_getslice empty slice whole crashes getslice null 0 0 test pylist_setslice insert items self assertequal set_slice lst py_ssize_t_max py_ssize_t_max x list abcx replace items delete items delete items with null item finalizer modify the list clear the list item finalizer modify the list remove an list item crashes setslice null 0 0 test pylist_sort test pylist_reverse test pylist_astuple test pylist_clear item finalizer modify the list crashes list_clear null test pylist_extend pylist_extend lst lst crashes list_extend null crashes list_extend null
import gc
import weakref
import unittest
from test.support import import_helper
from collections import UserList
_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


class DelAppend:
    def __init__(self, lst, item):
        self.lst = lst
        self.item = item

    def __del__(self):
        self.lst.append(self.item)


class CAPITest(unittest.TestCase):
    def test_check(self):
        
        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(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(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):
        
        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())
        

    def test_list_get_size(self):
        
        size = _testcapi.list_get_size
        self.assertEqual(size([1, 2]), 2)
        self.assertEqual(size(ListSubclass([1, 2])), 2)
        
        
        
        
        


    def test_list_getitem(self):
        
        getitem = _testcapi.list_getitem
        lst = [1, 2, 3]
        self.assertEqual(getitem(lst, 0), 1)
        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(SystemError, getitem, 42, 1)
        self.assertRaises(SystemError, getitem, (1, 2, 3), 1)
        self.assertRaises(SystemError, getitem, {1: 2}, 1)

        

    def test_list_get_item(self):
        
        get_item = _testcapi.list_get_item
        lst = [1, 2, [1, 2, 3]]
        self.assertEqual(get_item(lst, 0), 1)
        self.assertEqual(get_item(lst, 2), [1, 2, 3])

        
        
        
        
        


    def test_list_setitem(self):
        
        setitem = _testcapi.list_setitem
        lst = [1, 2, 3]
        setitem(lst, 0, 10)
        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(SystemError, setitem, (1, 2, 3), 1, 5)
        self.assertRaises(SystemError, setitem, {1: 2}, 1, 5)

        

    def test_list_set_item(self):
        
        set_item = _testcapi.list_set_item
        lst = [1, 2, 3]
        set_item(lst, 1, 10)
        set_item(lst, 2, [1, 2, 3])
        self.assertEqual(lst, [1, 10, [1, 2, 3]])

        
        
        
        
        


    def test_list_insert(self):
        
        insert = _testcapi.list_insert
        lst = [1, 2, 3]
        insert(lst, 0, 23)
        self.assertEqual(lst, [23, 1, 2, 3])
        insert(lst, -1, 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)
        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(SystemError, insert, (1, 2, 3), 1, 5)
        self.assertRaises(SystemError, insert, {1: 2}, 1, 5)

        

    def test_list_append(self):
        
        append = _testcapi.list_append
        lst = [1, 2, 3]
        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)
        

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

        
        self.assertEqual(getslice(lst, PY_SSIZE_T_MIN, 0), [])
        self.assertEqual(getslice(lst, -1, 0), [])
        self.assertEqual(getslice(lst, 3, PY_SSIZE_T_MAX), [])

        
        self.assertEqual(getslice(lst, 1, 3), [2, 3])

        
        self.assertEqual(getslice(lst, 0, len(lst)), lst)
        self.assertEqual(getslice(lst, 0, 100), lst)
        self.assertEqual(getslice(lst, -100, 100), lst)

        self.assertRaises(SystemError, getslice, (1, 2, 3), 0, 0)
        self.assertRaises(SystemError, getslice, 'abc', 0, 0)
        self.assertRaises(SystemError, getslice, 42, 0, 0)

        

    def test_list_setslice(self):
        
        list_setslice = _testcapi.list_setslice
        def set_slice(lst, low, high, value):
            lst = lst.copy()
            self.assertEqual(list_setslice(lst, low, high, value), 0)
            return lst

        
        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"))
        

        
        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"))

        
        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"))

        
        lst = list("abcdef")
        self.assertEqual(set_slice(lst, 0, len(lst), NULL), [])
        self.assertEqual(set_slice(lst, 3, len(lst), NULL), list("abc"))

        self.assertRaises(SystemError, list_setslice, (), 0, 0, [])
        self.assertRaises(SystemError, list_setslice, 42, 0, 0, [])

        
        lst = []
        lst.append(DelAppend(lst, 'zombie'))
        self.assertEqual(list_setslice(lst, 0, len(lst), NULL), 0)
        self.assertEqual(lst, ['zombie'])

        
        lst = []
        lst.append(DelAppend(lst, 'zombie'))
        lst.extend("abc")
        self.assertEqual(list_setslice(lst, 0, 1, NULL), 0)
        self.assertEqual(lst, ['a', 'b', 'c', 'zombie'])

        

    def test_list_sort(self):
        
        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, ())
        self.assertRaises(SystemError, sort, object())
        self.assertRaises(SystemError, sort, NULL)


    def test_list_reverse(self):
        
        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, ())
        self.assertRaises(SystemError, reverse, object())
        self.assertRaises(SystemError, reverse, NULL)

    def test_list_astuple(self):
        
        astuple = _testcapi.list_astuple
        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)

    def test_list_clear(self):
        
        list_clear = _testcapi.list_clear

        lst = [1, 2, 3]
        self.assertEqual(list_clear(lst), 0)
        self.assertEqual(lst, [])

        lst = []
        self.assertEqual(list_clear(lst), 0)
        self.assertEqual(lst, [])

        self.assertRaises(SystemError, list_clear, ())
        self.assertRaises(SystemError, list_clear, object())

        
        lst = []
        lst.append(DelAppend(lst, 'zombie'))
        list_clear(lst)
        self.assertEqual(lst, ['zombie'])

        

    def test_list_extend(self):
        
        list_extend = _testcapi.list_extend

        for other_type in (list, tuple, str, iter):
            lst = list("ab")
            arg = other_type("def")
            self.assertEqual(list_extend(lst, arg), 0)
            self.assertEqual(lst, list("abdef"))

        
        lst = list("abc")
        self.assertEqual(list_extend(lst, lst), 0)
        self.assertEqual(lst, list("abcabc"))

        self.assertRaises(TypeError, list_extend, [], object())
        self.assertRaises(SystemError, list_extend, (), list("abc"))