Skip to content

Commit

Permalink
Tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
arekbulski committed Aug 30, 2016
1 parent db6a924 commit a3c94a9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
24 changes: 17 additions & 7 deletions construct/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1427,12 +1427,22 @@ class Padding(Construct):
r"""
A padding field (adds bytes when building, discards bytes when parsing).
:param length: the length of the field. the length can be either an integer,
or a function that takes the context as an argument and returns the length
:param pattern: the padding pattern (b-string character), default is b"\x00"
:param strict: whether to verify during parsing that the stream contains the pattern,
raises an exception if actual padding differs from the pattern.
default is False.
:param length: length of the field. can be either an integer or a function
that takes the context as an argument and returns the length
:param pattern: the padding pattern (b-string character). default is b"\x00"
:param strict: whether to verify during parsing that the stream contains
the pattern. raises an exception if actual padding differs
from the pattern. default is False.
Example::
Struct("struct",
Byte("num"),
Padding(4),
)
parse(b"\xff\x00\x00\x00\x00") -> Container(num=255)
build(Container(num=255)) -> b"\xff\x00\x00\x00\x00"
"""
__slots__ = ["length", "pattern", "strict"]
def __init__(self, length, pattern=b"\x00", strict=False):
Expand Down Expand Up @@ -1491,7 +1501,7 @@ def _parse(self, stream, context):
raise ConstError("expected %r but parsed %r" % (self.value,obj))
return obj
def _build(self, obj, stream, context):
return self.subcon._build(self.value, stream, context)
self.subcon._build(self.value, stream, context)
def _sizeof(self, context):
return self.subcon._sizeof(context)

8 changes: 3 additions & 5 deletions construct/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,16 +353,14 @@ def Aligned(subcon, modulus=4, pattern=b"\x00"):
"""
if modulus < 2:
raise ValueError("modulus must be >= 2", modulus)
if len(pattern) != 1:
raise ValueError("expected b-string character, given %r" % pattern)
def padlength(ctx):
return -subcon._sizeof(ctx) % modulus
return SeqOfOne(subcon.name,
subcon,
# ??????
# ??????
# ??????
# ??????
Padding(padlength, pattern),
nested = False,
nested=False,
)

def SeqOfOne(name, *args, **kw):
Expand Down

0 comments on commit a3c94a9

Please sign in to comment.