1
+ import gc
2
+ import weakref
1
3
import unittest
2
- import sys
3
4
from test .support import import_helper
4
5
from collections import UserList
5
6
_testcapi = import_helper .import_module ('_testcapi' )
@@ -12,6 +13,15 @@ class ListSubclass(list):
12
13
pass
13
14
14
15
16
+ class DelAppend :
17
+ def __init__ (self , lst , item ):
18
+ self .lst = lst
19
+ self .item = item
20
+
21
+ def __del__ (self ):
22
+ self .lst .append (self .item )
23
+
24
+
15
25
class CAPITest (unittest .TestCase ):
16
26
def test_check (self ):
17
27
# Test PyList_Check()
@@ -196,10 +206,10 @@ def test_list_getslice(self):
196
206
197
207
def test_list_setslice (self ):
198
208
# Test PyList_SetSlice()
199
- setslice = _testcapi .list_setslice
209
+ list_setslice = _testcapi .list_setslice
200
210
def set_slice (lst , low , high , value ):
201
211
lst = lst .copy ()
202
- self .assertEqual (setslice (lst , low , high , value ), 0 )
212
+ self .assertEqual (list_setslice (lst , low , high , value ), 0 )
203
213
return lst
204
214
205
215
# insert items
@@ -231,8 +241,21 @@ def set_slice(lst, low, high, value):
231
241
self .assertEqual (set_slice (lst , 0 , len (lst ), NULL ), [])
232
242
self .assertEqual (set_slice (lst , 3 , len (lst ), NULL ), list ("abc" ))
233
243
234
- self .assertRaises (SystemError , setslice , (), 0 , 0 , [])
235
- self .assertRaises (SystemError , setslice , 42 , 0 , 0 , [])
244
+ self .assertRaises (SystemError , list_setslice , (), 0 , 0 , [])
245
+ self .assertRaises (SystemError , list_setslice , 42 , 0 , 0 , [])
246
+
247
+ # Item finalizer modify the list (clear the list)
248
+ lst = []
249
+ lst .append (DelAppend (lst , 'zombie' ))
250
+ self .assertEqual (list_setslice (lst , 0 , len (lst ), NULL ), 0 )
251
+ self .assertEqual (lst , ['zombie' ])
252
+
253
+ # Item finalizer modify the list (remove an list item)
254
+ lst = []
255
+ lst .append (DelAppend (lst , 'zombie' ))
256
+ lst .extend ("abc" )
257
+ self .assertEqual (list_setslice (lst , 0 , 1 , NULL ), 0 )
258
+ self .assertEqual (lst , ['a' , 'b' , 'c' , 'zombie' ])
236
259
237
260
# CRASHES setslice(NULL, 0, 0, [])
238
261
@@ -275,3 +298,47 @@ def test_list_astuple(self):
275
298
self .assertRaises (SystemError , astuple , ())
276
299
self .assertRaises (SystemError , astuple , object ())
277
300
self .assertRaises (SystemError , astuple , NULL )
301
+
302
+ def test_list_clear (self ):
303
+ # Test PyList_Clear()
304
+ list_clear = _testcapi .list_clear
305
+
306
+ lst = [1 , 2 , 3 ]
307
+ self .assertEqual (list_clear (lst ), 0 )
308
+ self .assertEqual (lst , [])
309
+
310
+ lst = []
311
+ self .assertEqual (list_clear (lst ), 0 )
312
+ self .assertEqual (lst , [])
313
+
314
+ self .assertRaises (SystemError , list_clear , ())
315
+ self .assertRaises (SystemError , list_clear , object ())
316
+
317
+ # Item finalizer modify the list
318
+ lst = []
319
+ lst .append (DelAppend (lst , 'zombie' ))
320
+ list_clear (lst )
321
+ self .assertEqual (lst , ['zombie' ])
322
+
323
+ # CRASHES list_clear(NULL)
324
+
325
+ def test_list_extend (self ):
326
+ # Test PyList_Extend()
327
+ list_extend = _testcapi .list_extend
328
+
329
+ for other_type in (list , tuple , str , iter ):
330
+ lst = list ("ab" )
331
+ arg = other_type ("def" )
332
+ self .assertEqual (list_extend (lst , arg ), 0 )
333
+ self .assertEqual (lst , list ("abdef" ))
334
+
335
+ # PyList_Extend(lst, lst)
336
+ lst = list ("abc" )
337
+ self .assertEqual (list_extend (lst , lst ), 0 )
338
+ self .assertEqual (lst , list ("abcabc" ))
339
+
340
+ self .assertRaises (TypeError , list_extend , [], object ())
341
+ self .assertRaises (SystemError , list_extend , (), list ("abc" ))
342
+
343
+ # CRASHES list_extend(NULL, [])
344
+ # CRASHES list_extend([], NULL)
0 commit comments