From 105c82d74a2e9ef65badfdcba927484836867d4d Mon Sep 17 00:00:00 2001 From: projms-intive <263402810+projms@users.noreply.github.com> Date: Tue, 24 Feb 2026 14:08:03 +0100 Subject: [PATCH] Adds conversion to bytearray within Segment Previously, depending on how a Segment was created, the internal data may have been stored as an immutable bytes object. This caused issues with modifying operations. --- bincopy.py | 2 +- tests/test_bincopy.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/bincopy.py b/bincopy.py index c7d911d..adcc0c0 100755 --- a/bincopy.py +++ b/bincopy.py @@ -354,7 +354,7 @@ class Segment: def __init__(self, minimum_address, maximum_address, data, word_size_bytes): self.minimum_address = minimum_address self.maximum_address = maximum_address - self.data = data + self.data = bytearray(data) self.word_size_bytes = word_size_bytes @property diff --git a/tests/test_bincopy.py b/tests/test_bincopy.py index eadf292..7391bbd 100644 --- a/tests/test_bincopy.py +++ b/tests/test_bincopy.py @@ -1810,6 +1810,15 @@ def test_add_elf_keil(self): with open('tests/files/elf/keil.bin', 'rb') as fin: self.assertEqual(bf.as_binary(), fin.read()) + def test_add_elf_modify_overwrite(self): + bf = bincopy.BinFile() + bf.add_elf_file('tests/files/elf.out') + + data = b'test' + address = bf.minimum_address + bf.add_binary(data, address=address, overwrite=True) + self.assertEqual(data, bf.as_binary(minimum_address=address, maximum_address=address+len(data))) + def test_exclude_edge_cases(self): binfile = bincopy.BinFile() binfile.add_binary(b'1234', address=10)