Skip to content

Commit

Permalink
Bytes* fixed (allows to build from bytearrays) per #827
Browse files Browse the repository at this point in the history
  • Loading branch information
arekbulski committed Jan 21, 2020
1 parent 53c4c88 commit 9930e70
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion construct/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,8 @@ class Bytes(Construct):
Parses into a bytes (of given length). Builds into the stream directly (but checks that given object matches specified length). Can also build from an integer for convenience (although BytesInteger should be used instead). Size is the specified length.
Can also build from a bytearray.
:param length: integer or context lambda
:raises StreamError: requested reading negative amount, could not read enough bytes, requested writing different amount than actual data, or could not write all bytes
Expand Down Expand Up @@ -841,6 +843,7 @@ def _parse(self, stream, context, path):
def _build(self, obj, stream, context, path):
length = self.length(context) if callable(self.length) else self.length
data = integer2bytes(obj, length) if isinstance(obj, int) else obj
data = bytes(data) if type(data) is bytearray else data
stream_write(stream, data, length, path)
return data

Expand All @@ -864,6 +867,8 @@ class GreedyBytes(Construct):
Parses the stream to the end. Builds into the stream directly (without checks). Size is undefined.
Can also build from a bytearray.
:raises StreamError: stream failed when reading until EOF
:raises StringError: building from non-bytes value, perhaps unicode
Expand All @@ -879,7 +884,8 @@ def _parse(self, stream, context, path):
return stream_read_entire(stream, path)

def _build(self, obj, stream, context, path):
stream_write(stream, obj, len(obj), path)
data = bytes(obj) if type(obj) is bytearray else obj
stream_write(stream, data, len(data), path)
return obj

def _emitparse(self, code):
Expand Down
2 changes: 2 additions & 0 deletions docs/transition210.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Transition to 2.10
Overall
==========

Bytes GreedyBytes can build from bytearrays (not just bytes)

Embedded and EmbeddedSwitch were permanently removed

Exceptions always display path information
6 changes: 6 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def test_bytes():
def test_greedybytes():
common(GreedyBytes, b"1234", b"1234", SizeofError)

def test_bytes_issue_827():
d = Bytes(3)
assert d.build(bytearray(b'\x01\x02\x03')) == b'\x01\x02\x03'
d = GreedyBytes
assert d.build(bytearray(b'\x01\x02\x03')) == b'\x01\x02\x03'

def test_bitwise():
common(Bitwise(Bytes(8)), b"\xff", b"\x01\x01\x01\x01\x01\x01\x01\x01", 1)
common(Bitwise(Array(8,Bit)), b"\xff", [1,1,1,1,1,1,1,1], 1)
Expand Down

0 comments on commit 9930e70

Please sign in to comment.