Skip to content
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

Using custom functions in Rebuild #1065

Open
franzhaas opened this issue Jan 18, 2024 · 1 comment · May be fixed by #1069
Open

Using custom functions in Rebuild #1065

franzhaas opened this issue Jan 18, 2024 · 1 comment · May be fixed by #1069

Comments

@franzhaas
Copy link

franzhaas commented Jan 18, 2024

This minimal test shows how using a custom function to rebuild fails in the compiled case, but works in the interpreted case.

def getlen(this):
    return 2

template = Struct("count" / Rebuild(Byte, getlen), "my_items" / Byte[this.count])
for d  in [template, template.compile()]:
    assert d.parse(b"\x02ab") == Container(count=2, my_items=[97,98])
    assert d.build(dict(count=None,my_items=[255,255])) == b"\x02\xff\xff"
    assert d.build(dict(count=2,my_items=[255,255])) == b"\x02\xff\xff"
    assert d.build(dict(my_items=[255,255])) == b"\x02\xff\xff"
@franzhaas
Copy link
Author

franzhaas commented Jan 28, 2024

I learned more about how to use the "this" mechanics. If you encounter this problem, check if you can make do without a function, just a this expression. In my case it was possible.

I find it more readable, the compiled code is faster...

I still believe that this improvement is worth it. Alternatively, we can detect the situation and raise a helpful Exception.

@franzhaas franzhaas linked a pull request Feb 4, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant