@@ -189,5 +189,56 @@ def test_double_pointer(self):
189189 self .assertIn ("pp" , struct_type .__annotations__ )
190190
191191
192+ class BytearrayMemoryBytesTest (unittest .TestCase ):
193+ """Issue #6: __bytes__ fails on Python 3.13+ when backing memory is bytearray."""
194+
195+ def test_resolve_returns_bytes (self ):
196+ from libdestruct .backing .memory_resolver import MemoryResolver
197+
198+ # MemoryResolver.resolve() should always return bytes, even when
199+ # the backing memory is a bytearray
200+ resolver = MemoryResolver (bytearray (b"\x01 \x02 \x03 \x04 " ), 0 )
201+ result = resolver .resolve (4 , 0 )
202+ self .assertIsInstance (result , bytes )
203+
204+ def test_bytes_on_bytearray_backed_c_int (self ):
205+ lib = inflater (bytearray (b"\x2a \x00 \x00 \x00 " ))
206+ obj = lib .inflate (c_int , 0 )
207+
208+ result = bytes (obj )
209+ self .assertIsInstance (result , bytes )
210+ self .assertEqual (len (result ), 4 )
211+
212+ def test_bytes_on_bytearray_backed_c_str (self ):
213+ lib = inflater (bytearray (b"Hello\x00 " ))
214+ s = lib .inflate (c_str , 0 )
215+
216+ result = bytes (s )
217+ self .assertIsInstance (result , bytes )
218+
219+ def test_bytes_on_bytearray_backed_ptr (self ):
220+ class test_t (struct ):
221+ p : ptr = ptr_to_self ()
222+
223+ memory = bytearray (b"\x00 " * 8 )
224+ test = test_t .from_bytes (memory )
225+
226+ result = test .p .to_bytes ()
227+ self .assertIsInstance (result , bytes )
228+ self .assertEqual (len (result ), 8 )
229+
230+ def test_c_str_get_returns_bytes (self ):
231+ lib = inflater (bytearray (b"Hello\x00 " ))
232+ s = lib .inflate (c_str , 0 )
233+
234+ # get() without index returns the full string
235+ result = s .get ()
236+ self .assertIsInstance (result , bytes )
237+
238+ # get() with index returns a single byte
239+ result = s .get (0 )
240+ self .assertIsInstance (result , bytes )
241+
242+
192243if __name__ == "__main__" :
193244 unittest .main ()
0 commit comments