Skip to content

Commit 33d2a4d

Browse files
preparing to tackle Issue #23
1 parent 91d2230 commit 33d2a4d

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

bsp_tool/lumps/__init__.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ class RawBspLump:
9696
offset: int # position in file where lump begins
9797
_changes: Dict[int, bytes]
9898
# ^ {index: new_byte}
99+
# NOTE: bytes objects don't support item assignment
100+
# -- but we want to allow light hex editting with slice overrides
99101
_length: int # number of indexable entries
100102

101103
def __init__(self, file: io.BufferedReader, lump_header: Any):
@@ -130,17 +132,14 @@ def __iadd__(self, other_bytes: bytes):
130132
self._length += len(other_bytes)
131133
self[start:] = other_bytes
132134

133-
def __setitem__(self, index: Union[int, slice], value: Any):
135+
def __setitem__(self, index: slice, value: Any):
136+
"""remapping slices is allowed, but only slices"""
137+
if not isinstance(index, slice):
138+
raise TypeError(f"list indices must be integers or slices, not {type(index)}")
139+
_slice = _remap_slice(index, self._length)
134140
# TODO: allow slice assignment to act like insert/extend
135-
if isinstance(index, int):
136-
index = _remap_negative_index(index, self._length)
141+
for i, v in zip(range(_slice.start, _slice.stop, _slice.step), value):
137142
self._changes[index] = value
138-
elif isinstance(index, slice):
139-
_slice = _remap_slice(index, self._length)
140-
for i, v in zip(range(_slice.start, _slice.stop, _slice.step), value):
141-
self[i] = v
142-
else:
143-
raise TypeError(f"list indices must be integers or slices, not {type(index)}")
144143

145144
def __iter__(self):
146145
return iter([self[i] for i in range(self._length)])

0 commit comments

Comments
 (0)