construct icon indicating copy to clipboard operation
construct copied to clipboard

Anchor requires key presence in container

Open ius opened this issue 13 years ago • 2 comments

When building a Struct which contains an Anchor, not providing the anchor's label as a key in the Container instance yields an AttributeError.

#!/usr/bin/python2
from construct import *

struct = Struct(None, Anchor('foo_anchor'))
struct.build(Container())

Running the testcase yields: AttributeError: 'Container' object has no attribute 'foo_anchor'

As an anchor's value is solely determined by it's position in the construct, it shouldn't have to be specified in the Container?

ius avatar Jun 30 '11 01:06 ius

Same thing happens with ConstAdapter and related macros.

There probably should be a flag that informs Struct to pass None if the field is not present. This flag would also make it possible to create constructs that have a default build value and computed build value.

nnemkin avatar Jul 19 '11 19:07 nnemkin

Hm. I haven't messed with Anchor much, but I'll look into this.

MostAwesomeDude avatar Mar 06 '12 02:03 MostAwesomeDude