AdithyaSK's picture
AdithyaSK HF Staff
Publish 100 swe_next Harbor tasks
d40299b verified
|
Raw History Blame Contribute Delete
2.66 kB

BarrelList has three index-boundary bugs: out-of-bounds __getitem__, wrong insert position, and pop() crash on drained tail

Description

After a BarrelList has been split into multiple internal sublists (which happens after mutating operations trigger _balance_list), several boundary conditions are handled incorrectly.

Here's a helper to get a multi-sublist BarrelList:

from boltons.listutils import BarrelList

def make_bl(n=30000):
    bl = BarrelList(range(n))
    bl.pop(0)
    bl.insert(0, 0)
    # now bl.lists has more than one sublist
    return bl

Bug 1: bl[len(bl)] does not raise IndexError

Accessing an index equal to the length of the list should raise IndexError (matching standard Python list behavior), but instead it silently returns a value from somewhere in the middle of the list:

bl = make_bl()
bl[len(bl)]  # should raise IndexError, but returns an integer

Bug 2: insert at or past the end doesn't append

Python's list.insert(i, x) clamps out-of-bounds indices: insert(len(lst), x) appends to the end. BarrelList.insert does not do this — it inserts at the wrong position:

bl = make_bl()
bl.insert(len(bl), 'end')
print(bl[-1])  # prints 29999, expected 'end'

Bug 3: pop() raises IndexError after tail sublist is drained

If you remove all elements from the last internal sublist via indexed pops, the tail sublist becomes empty. A subsequent bl.pop() (no-arg, should pop the last element) then crashes with IndexError: pop from empty list even though the BarrelList still has elements:

bl = make_bl()
for _ in range(len(bl.lists[-1])):
    bl.pop(len(bl) - 1)  # drain the tail sublist
bl.pop()  # IndexError: pop from empty list

Expected behavior

  • bl[len(bl)] raises IndexError
  • bl.insert(len(bl), x) appends x to the end; bl[-1] == x
  • bl.insert(len(bl) + 100, x) also appends; bl.insert(-len(bl) - 100, x) prepends
  • bl.pop() returns the last element even when the tail internal sublist has been emptied by prior indexed removals

Actual behavior

  • bl[len(bl)] returns a mid-list element instead of raising IndexError
  • bl.insert(len(bl), 'end') inserts somewhere in the middle; bl[-1] is still 29999
  • bl.pop() raises IndexError: pop from empty list on a non-empty BarrelList

Work in /workspace. Submit your fix in the existing Python source files under boltons. Preserve the other public behavior. The environment is offline; dependencies are preinstalled. Grading runs the repository's test suite in a fresh environment, using your submitted source files.