Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion construct/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3873,7 +3873,17 @@ def _parse(self, stream, context, path):
def _build(self, obj, stream, context, path):
for sc in self.subcons:
try:
data = sc.build(obj, **context)
# Build into a temporary stream using a copy of the current
# context, mirroring how _parse tries each subcon in place.
# Going through sc.build(obj, **context) instead flattens
# the context into build keywords, which resets _params to
# the flattened dict and breaks this._params lookups in
# nested constructs (see issue #1038). The copy also keeps
# context changes made by a failed attempt from leaking
# into the next attempt or back into the caller.
stream2 = io.BytesIO()
sc._build(obj, stream2, Container(context), path)
data = stream2.getvalue()
except ExplicitError:
raise
except Exception:
Expand Down
29 changes: 28 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2441,19 +2441,46 @@ def test_compile_binexpr_bitwise_and_issue_1039():
common(d, b"\x01", {"a": 1, "cond": None})
common(d, b" \x05", {"a": 32, "cond": 5})

@xfail(reason="unknown problem with Select")
def test_select_issue_1038():
s = Struct(
"value" / Select(IfThenElse(this._params.ctx == 1, Byte, Short)),
)
assert s.build(dict(value=9), ctx=1) == b"\x09"
assert s.build(dict(value=9), ctx=2) == b"\x00\x09"
assert s.parse(b"\x09", ctx=1) == dict(value=9)
assert s.parse(b"\x00\x09", ctx=2) == dict(value=9)

def test_select_issue_1038_fixed():
s = Struct(
"value" / Select(If(this._.ctx == 1, Byte), If(this._.ctx == 2, Short)),
)
assert s.build(dict(value=9), ctx=1) == b"\x09"

def test_select_build_attempts_are_isolated():
# a failed attempt must not leak its context changes into the next
# attempt (nor back into the caller's context)
class MutatingFail(Subconstruct):
def _build(self, obj, stream, context, path):
context.leaked = True
self.subcon._build(obj, stream, context, path)
raise ValidationError("fail on purpose", path=path)
s = Struct(
"value" / Select(
MutatingFail(Byte),
IfThenElse(lambda ctx: ctx.get("leaked", False), Error, Byte),
),
)
assert s.build(dict(value=9)) == b"\x09"

def test_select_build_sees_sibling_fields():
# subcons inside Select still see fields built before them
s = Struct(
"kind" / Byte,
"value" / Select(IfThenElse(this.kind == 1, Byte, Short)),
)
assert s.build(dict(kind=1, value=9)) == b"\x01\x09"
assert s.build(dict(kind=2, value=9)) == b"\x02\x00\x09"

def test_unicode_error():
d = Select(PaddedString(255, "ascii"), CString("ascii"), PascalString(Byte, "ascii"), GreedyString("ascii"), Pass)
data = u"Афон".encode()
Expand Down