-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IfThenElse
inside Select
using context not working as intended
#1038
Comments
Not sure why it is so but you cannot use Bit class outside of Bitwise context. |
I added the Bit to make sure that the subcon would trigger an exception, removing it still shows the issue : from construct import *
a = Struct(
"1" / Int8ul,
"2" / Select(IfThenElse(lambda ctx: ctx._params.get('ctx') == 1, Int8ul, Int16ul))
)
context = {"ctx": 1}
b = a.build({"1": 1, "2": 2}, **context)
print(len(b)) # Output: 3 |
I cannot for the love of god find what is wrong with it. I added following test case. I also managed to workaround the issue by just using different construction: @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"
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" |
I looked at it, and using the compile feature yielded the same issue. This is expected as the compiled code just calls the uncompiled feature. I added compilation feature.: https://github.com/franzhaas/construct and with that it works. The changes are quite significant (among other things you cant use "1" as a name for a field anymore), and currently I have no idea why the testsuite doesn't run at all on github actions... but just for this bug... it looks promising. from construct import *
a = Struct(
"a" / Int8ul,
"b" / Select(IfThenElse(lambda ctx: ctx._params.get('ctx') == 1, Int8ul, Int16ul))
)
context = {"ctx": 1}
s = a.compile()
b = s.build({"a": 1, "b": 2}, **context)
print(len(b)) # Output: 2
context = {"ctx": 0}
s = a.compile()
b = s.build({"a": 1, "b": 2}, **context)
print(len(b)) # Output: 3 Update.:
|
while my aproach changed a lot, the statements here remain valid... |
Hello, I looked for a bit but was unable to find anything related to this.
I have this code snippet :
I would expect to have the length of
b
to be 2 as my ctx provided is 1 but not it goes to the else part of theIfThenElse
statement.The behavior is correct when I remove the
Select
.The text was updated successfully, but these errors were encountered: