Skip to content

Commit

Permalink
Merge pull request #717 from papperlapapp/bugfix/upstream-issue-709
Browse files Browse the repository at this point in the history
Fix issue #709: Hex() might not always work.
  • Loading branch information
arekbulski committed May 5, 2018
2 parents c30c74a + 32137b4 commit 8acadac
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions construct/lib/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ def __str__(self):
text.append("(enum) (unknown) %s" % (v, ))
elif v.__class__.__name__ == "EnumIntegerString":
text.append("(enum) %s %s" % (v, v.intvalue, ))
elif v.__class__.__name__ in ["HexDisplayedBytes", "HexDumpDisplayedBytes"]:
text.append(indentation.join(str(v).split("\n")))
elif isinstance(v, bytestringtype):
printingcap = 16
if len(v) <= printingcap or globalPrintFullStrings:
Expand Down
29 changes: 28 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ def test_peek():
assert d.sizeof() == 0
d = Peek(VarInt)
assert d.sizeof() == 0

d = Struct("a"/Peek(Int8ub), "b"/Int16ub)
common(d, b"\x01\x02", Container(a=0x01)(b=0x0102), 2)
d = Struct(Peek("a"/Byte), Peek("b"/Int16ub))
Expand Down Expand Up @@ -2048,3 +2048,30 @@ def test_greedybytes_issue_697():
"rest" / Bytewise(GreedyBytes),
)
d.parse(bytes(5))

def test_hex_issue_709():
# Make sure, the fix doesn't destroy already working code
d = Hex(Bytes(1))
obj = d.parse(b"\xff")
assert "unhexlify('ff')" in str(obj)

d = Struct("x" / Hex(Byte))
obj = d.parse(b"\xff")
assert "x = 0xFF" in str(obj)

d = HexDump(Bytes(1))
obj = d.parse(b"\xff")
assert "hexundump" in str(obj)

# The following checks only succeed after fixing the issue
d = Struct("x" / Hex(Bytes(1)))
obj = d.parse(b"\xff")
assert "x = unhexlify('ff')" in str(obj)

d = Struct("x" / HexDump(Bytes(1)))
obj = d.parse(b"\xff")
assert "x = hexundump" in str(obj)

d = Struct("x" / Struct("y" / Hex(Bytes(1))))
obj = d.parse(b"\xff")
assert "y = unhexlify('ff')" in str(obj)

0 comments on commit 8acadac

Please sign in to comment.