@@ -364,6 +364,85 @@ def test_uninitialized_list_repr(self):
364364 lst = _testlimitedcapi.list_new(3)
365365 self.assertEqual(repr(lst), '[<NULL>, <NULL>, <NULL>]')
366366
367+ def test_shrink(self):
368+ # Check list methods shrinking the list
369+ get_allocated = _testcapi._list_get_allocated
370+ LIST_SMALL_ALLOCATED = _testcapi.LIST_SMALL_ALLOCATED
371+
372+ def check_allocated(lst, expected_length):
373+ size = len(lst)
374+ self.assertEqual(size, expected_length)
375+
376+ # See py_list_resize() in Objects/listobject.c
377+ expected = max(size + (size >> 1) + 6, LIST_SMALL_ALLOCATED)
378+ self.assertLessEqual(get_allocated(lst), expected)
379+
380+ # del list[start:end]
381+ lst = list(range(1024))
382+ self.assertGreaterEqual(get_allocated(lst), 1024)
383+ del lst[128:]
384+ check_allocated(lst, 128)
385+ del lst[8:]
386+ check_allocated(lst, 8)
387+
388+ # del list[start:end:step] with step != 1
389+ lst = list(range(1024))
390+ self.assertGreaterEqual(get_allocated(lst), 1024)
391+ del lst[::2]
392+ del lst[::2]
393+ check_allocated(lst, 256)
394+ del lst[::2]
395+ del lst[::2]
396+ del lst[::2]
397+ del lst[::2]
398+ check_allocated(lst, 16)
399+
400+ # del list[index]
401+ lst = list(range(1024))
402+ self.assertGreaterEqual(get_allocated(lst), 1024)
403+ for _ in range(1024 - 128):
404+ del lst[-1]
405+ check_allocated(lst, 128)
406+ for _ in range(120):
407+ del lst[-1]
408+ check_allocated(lst, 8)
409+
410+ # list.pop()
411+ lst = list(range(1024))
412+ self.assertGreaterEqual(get_allocated(lst), 1024)
413+ for _ in range(1024 - 128):
414+ lst.pop()
415+ check_allocated(lst, 128)
416+ for _ in range(120):
417+ lst.pop()
418+ check_allocated(lst, 8)
419+
420+ # Iterator with a bad __length_hint__() estimation
421+ class Iterator:
422+ def __init__(self, items):
423+ self.data = tuple(items)
424+ self.pos = 0
425+
426+ def __iter__(self):
427+ return self
428+
429+ def __length_hint__(self):
430+ return len(self.data) * 8
431+
432+ def __next__(self):
433+ try:
434+ item = self.data[self.pos]
435+ except IndexError:
436+ raise StopIteration
437+ self.pos += 1
438+ return item
439+
440+ # list.extend(iterator) with bad __length_hint__() estimation
441+ lst = []
442+ items = list(range(8))
443+ lst.extend(Iterator(items))
444+ check_allocated(lst, len(items))
445+
367446
368447class ListBuilderTests(unittest.TestCase):
369448 def create_builder(self, size):
@@ -515,23 +594,49 @@ def test_empty(self):
515594 expected = size
516595 self.assertEqual(list_get_allocated(result), expected)
517596
597+ @staticmethod
598+ def compute_allocated(size):
599+ return ((size + (size >> 3) + 6) & ~3)
600+
601+ def test_no_shrink(self):
602+ # PyListBuilder methods must not shrink the list before Finish()
603+ get_allocated = _testcapi._list_get_allocated
604+
605+ def check_allocated():
606+ self.assertEqual(builder.get_allocated(), alloc)
607+
608+ alloc = 2048
609+ builder = self.create_builder(alloc)
610+ check_allocated()
611+
612+ builder.append('a')
613+ check_allocated()
614+ builder.append_steal('b')
615+ check_allocated()
616+ builder.extend(('c',))
617+ check_allocated()
618+ builder.extend_array(('d',))
619+ check_allocated()
620+ builder.extend_array(('e',))
621+ check_allocated()
622+
623+ result = builder.finish()
624+ self.list_equal(result, 'abcde')
625+ self.assertEqual(get_allocated(result),
626+ self.compute_allocated(len(result)))
627+
518628 def test_finish_shrink(self):
519629 # PyListBuilder_Finish() shrinks the list if its allocated size
520630 # is way too big
521631 list_get_allocated = _testcapi._list_get_allocated
522632
523- def compute_allocated(size):
524- return (size + (size >> 3) + 6) & ~3
525-
526633 def check_allocated(items, alloc, expected=None):
527634 builder = self.create_builder(alloc)
528- # Don't use PyListBuilder_Extend() since it can shrink the list
529- for item in items:
530- builder.append(item)
635+ builder.extend(items)
531636 result = builder.finish()
532637 self.list_equal(result, list(items))
533638 if expected is None:
534- expected = compute_allocated(len(items))
639+ expected = self. compute_allocated(len(items))
535640 self.assertEqual(list_get_allocated(result), expected)
536641
537642 # small list
0 commit comments