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
@@ -291,6 +314,12 @@ def test_list_clear(self):
291
314
self .assertRaises (TypeError , list_clear , ())
292
315
self .assertRaises (TypeError , list_clear , object ())
293
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
+
294
323
# CRASHES list_clear(NULL)
295
324
296
325
def test_list_extend (self ):
0 commit comments