context missing '_' HELP!

93 views
Skip to first unread message

Anthony Asterisk

unread,
Mar 5, 2013, 10:07:20 PM3/5/13
to const...@googlegroups.com
In all of my testing I have never seen the alleged _ in the context.  I need access to the parent container to make this code work but _ just isn't there.  Whats wrong ?
I'm using Construct 2.5

Here is my example code:
#PrintContext taken from the Construct documentation

from construct import *
class PrintContext(Construct):
     def _parse(self, stream, context):
         print contex

event_opcode = Enum(Byte('event_opcode'),
                    Event1=0x3e,
                    Event2 = 0xff
                    )
param_len = OneOf(Byte("param_len"), range(256))

test_end = Struct("LE_Test_End_Command",
                  Embed(event_status_struct),
                  ULInt16("pkt_count"),
                  )

hci_event = Struct("hci_event",
                   event_opcode,
                   param_len,
                   Switch("event_params", lambda ctx: ctx["event_opcode"],
                          {
                              "Event1": Struct("event1",
                               PrintContext("abc"),
                                 Byte("flow_control"),
                                 cmd_opcode,
                                 Switch("cmd_response", lambda ctx: ctx["cmd_opcode"],
                                        {
                                            "test_end": test_end,
                                        },
                                        default = Struct("generic_event",
                                  Embed(event_status_struct),
                          Bytes("resp_params", do_this),
                                  )   
                                        ),
                                 ),
                              "vendor_specific": vendor_event_struct,
                              }
                          ),
                   )

Emanuele Ziglioli

unread,
Mar 7, 2013, 8:37:40 PM3/7/13
to const...@googlegroups.com
The case of Switch is particular because it inherits the flags of the constructs within.
Since you;ve put and Embedded Struct inside, the Switch becomes embedded, that means its context is at the same level as its parent's.

I've bumped into this problem myself and since then I never use embedded structs in Switch
Reply all
Reply to author
Forward
0 new messages