Skip to content

Commit

Permalink
building: add __setitem__, __iadd__ to TOC()
Browse files Browse the repository at this point in the history
  • Loading branch information
bpteague authored and bwoodsend committed Jan 12, 2022
1 parent cad0725 commit 14949d3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
26 changes: 26 additions & 0 deletions PyInstaller/building/datastruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ def __radd__(self, other):
result.extend(self)
return result

def __iadd__(self, other):
for entry in other:
self.append(entry)
return self

def extend(self, other):
# TODO: look if this can be done more efficient with out the loop, e.g. by not using a list as base at all.
for entry in other:
Expand All @@ -116,6 +121,27 @@ def __rsub__(self, other):
result = TOC(other)
return result.__sub__(self)

def __setitem__(self, key, value):
if isinstance(key, slice):
if key == slice(None, None, None):
# special case: set the entire list
self.filenames = set()
self.clear()
self.extend(value)
return
else:
raise KeyError("TOC.__setitem__ doesn't handle slices")

else:
old_value = self[key]
old_name = unique_name(old_value)
self.filenames.remove(old_name)

new_name = unique_name(value)
if new_name not in self.filenames:
self.filenames.add(new_name)
super(TOC, self).__setitem__(key, value)


class Target:
invcnum = 0
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/test_TOC.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,38 @@ def test_rsub_non_existing():
assert result == expected


def test_sub_after_setitem():
toc = TOC(ELEMS1)
toc[1] = ('lib-dynload/_random', '/usr/lib/python2.7/lib-dynload/_random.so', 'EXTENSION')
toc -= []
assert len(toc) == 3


def test_setitem_1():
toc = TOC()
toc[:] = ELEMS1
for e in ELEMS1:
assert e in toc
assert e[0] in toc.filenames


def test_setitem_2():
toc = TOC(ELEMS1)
toc[1] = ELEMS3[0]

assert ELEMS1[0] in toc
assert ELEMS1[0][0] in toc.filenames

assert ELEMS3[0] in toc
assert ELEMS3[0][0] in toc.filenames

assert ELEMS1[2] in toc
assert ELEMS1[2][0] in toc.filenames

for e in toc:
assert e[0] in toc.filenames


# The following tests verify that case-insensitive comparisons are used on Windows and only for
# appropriate TOC entry types

Expand Down

0 comments on commit 14949d3

Please sign in to comment.