Skip to content

Commit

Permalink
Merge pull request #749 from matan1008/patch/allowing-seek-to-same-po…
Browse files Browse the repository at this point in the history
…sition-in-restreamed

Patch Optional() when restreamed issue
  • Loading branch information
arekbulski committed Jan 17, 2020
2 parents f75f8a2 + 94fee45 commit c31dce8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 4 additions & 1 deletion construct/lib/bitstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ def close(self):
raise ValueError("closing stream but %d unwritten bytes remain, %d is encoded unit" % (len(self.wbuffer), self.encoderunit))

def seek(self, at, whence=0):
raise IOError
if whence == 0 and at == self.sincereadwritten:
pass
else:
raise IOError

def seekable(self):
return False
Expand Down
21 changes: 20 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,10 +758,29 @@ def test_optional():
assert d.parse(b"\x01\x00\x00\x00") == 1
assert d.build(1) == b"\x01\x00\x00\x00"
assert d.parse(b"???") == None
assert d.parse(b"") == None
assert d.build(None) == b""
assert raises(d.sizeof) == SizeofError

def test_select_buildfromnone():
def test_optional_in_struct_issue_747():
d = Struct("field" / Optional(Int32ul))
assert d.parse(b"\x01\x00\x00\x00") == {"field": 1}
assert d.build({"field": 1}) == b"\x01\x00\x00\x00"
assert d.parse(b"???") == {"field": None}
assert d.build({"field": None}) == b""
assert d.parse(b"") == {"field": None}
assert raises(d.sizeof) == SizeofError

def test_optional_in_bit_struct_issue_747():
d = BitStruct("field" / Optional(Octet))
assert d.parse(b"\x01") == {"field": 1}
assert d.build({"field": 1}) == b"\x01"
assert d.parse(b"???") == {"field": ord("?")}
assert d.build({"field": None}) == b""
assert d.parse(b"") == {"field": None}
assert raises(d.sizeof) == SizeofError

def test_select_buildfromnone_issue_747():
d = Struct("select" / Select(Int32ub, Default(Bytes(3), b"abc")))
assert d.parse(b"def") == dict(select=b"def")
assert d.parse(b"\x01\x02\x03\x04") == dict(select=0x01020304)
Expand Down

0 comments on commit c31dce8

Please sign in to comment.