Core API: Debugging

construct.Probe(into=None, lookahead=None)

Probe that dumps the context, and some stream content (peeks into it) to the screen to aid the debugging process. It can optionally limit itself to a single context entry, instead of printing entire context.

Parameters:
  • into – optional, None by default, or context lambda

  • lookahead – optional, integer, number of bytes to dump from the stream

Example:

>>> d = Struct(
...     "count" / Byte,
...     "items" / Byte[this.count],
...     Probe(lookahead=32),
... )
>>> d.parse(b"\x05abcde\x01\x02\x03")

--------------------------------------------------
Probe, path is (parsing), into is None
Stream peek: (hexlified) b'010203'...
Container: 
    count = 5
    items = ListContainer: 
        97
        98
        99
        100
        101
--------------------------------------------------
>>> d = Struct(
...     "count" / Byte,
...     "items" / Byte[this.count],
...     Probe(this.count),
... )
>>> d.parse(b"\x05abcde\x01\x02\x03")

--------------------------------------------------
Probe, path is (parsing), into is this.count
5
--------------------------------------------------
construct.setGlobalPrintFullStrings(enabled=False)

When enabled, Container __str__ produces full content of bytes and unicode strings, otherwise and by default, it produces truncated output (16 bytes and 32 characters).

Parameters:

enabled – bool

construct.setGlobalPrintFalseFlags(enabled=False)

When enabled, Container __str__ that was produced by FlagsEnum parsing prints all values, otherwise and by default, it prints only the values that are True.

Parameters:

enabled – bool

construct.setGlobalPrintPrivateEntries(enabled=False)

When enabled, Container __str__ shows keys like _ _index _etc, otherwise and by default, it hides those keys. __repr__ never shows private entries.

Parameters:

enabled – bool

construct.Debugger(subcon)

PDB-based debugger. When an exception occurs in the subcon, a debugger will appear and allow you to debug the error (and even fix it on-the-fly).

Parameters:

subcon – Construct instance, subcon to debug

Example:

>>> Debugger(Byte[3]).build([])

--------------------------------------------------
Debugging exception of <Array: None>
path is (building)
  File "/media/arkadiusz/MAIN/GitHub/construct/construct/debug.py", line 192, in _build
    return self.subcon._build(obj, stream, context, path)
  File "/media/arkadiusz/MAIN/GitHub/construct/construct/core.py", line 2149, in _build
    raise RangeError("expected %d elements, found %d" % (count, len(obj)))
construct.core.RangeError: expected 3 elements, found 0

> /media/arkadiusz/MAIN/GitHub/construct/construct/core.py(2149)_build()
-> raise RangeError("expected %d elements, found %d" % (count, len(obj)))
(Pdb) q
--------------------------------------------------