Skip to content
This repository has been archived by the owner on Jan 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #13 from alexras/alexras/fix_bread_new_with_data
Browse files Browse the repository at this point in the history
Fix bug passing data to bread.new()
  • Loading branch information
alexras authored Dec 9, 2018
2 parents b65bb7b + 22f244a commit 139da25
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ MANIFEST
build/
dist/
.tox
.mypy_cache
2 changes: 2 additions & 0 deletions bread/lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def new(spec, type_name='bread_struct', data=None):

if data is None:
data = BitArray(bytearray(int(math.ceil(len(struct) / 8.0))))
elif type(data) == bytearray:
data = BitArray(data)

struct._set_data(data)
struct._offset = 0
Expand Down
2 changes: 2 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[mypy]
ignore_errors = True
10 changes: 9 additions & 1 deletion test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,10 +1068,18 @@ def test_get_slice():


def test_new():
_test_new()


def test_new_with_data():
_test_new(bytearray(8 * 5 + 4))


def _test_new(data=None):
format_spec = [("greeting", b.string(5)),
("age", b.nibble)]

empty_struct = b.new(format_spec)
empty_struct = b.new(format_spec, data=data)

assert len(empty_struct) == 8 * 5 + 4

Expand Down

0 comments on commit 139da25

Please sign in to comment.