Skip to content

Commit

Permalink
Index class updated, accordingly to Struct copying _index entry
Browse files Browse the repository at this point in the history
  • Loading branch information
arekbulski committed Apr 8, 2018
1 parent 5706e41 commit b8c1b64
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
14 changes: 4 additions & 10 deletions construct/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2618,9 +2618,9 @@ class Index(Construct):
r"""
Indexes a field inside outer :class:`~construct.core.Array` :class:`~construct.core.GreedyRange` :class:`~construct.core.RepeatUntil` context.
Note that you can use this class, or use `this._index` or `this._._index` expressions instead, depending on how its used. See the examples.
Note that you can use this class, or use `this._index` expression instead, depending on how its used. See the examples.
Parsing and building pulls _index or _._index key from the context, in that order. Size is 0 because stream is unaffected.
Parsing and building pulls _index key from the context. Size is 0 because stream is unaffected.
:raises IndexFieldError: did not find either key in context
Expand All @@ -2646,17 +2646,11 @@ def __init__(self):
self.flagbuildnone = True

def _parse(self, stream, context, path):
if "_index" in context:
return context._index
if "_" in context and "_index" in context._:
return context._._index
return context.get("_index", None)
raise IndexFieldError("did not find either key in context")

def _build(self, obj, stream, context, path):
if "_index" in context:
return context._index
if "_" in context and "_index" in context._:
return context._._index
return context.get("_index", None)
raise IndexFieldError("did not find either key in context")

def _sizeof(self, context, path):
Expand Down
4 changes: 2 additions & 2 deletions docs/misc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ b'[\x86\xcc\xf1b\xd9\x10\x0f?\x1a'
Index
-------

Fields that are inside Array GreedyRange RepeatUntil can reference their index within the outer list. This is being effectuated by repeater class maintaining a context entry "_index" and updating it between each iteration. Note that some classes do context nesting (like Struct) so the key is then "_._index". You can access either key using Index class, or refer to the context entry directly, using `this._index` and `this._._index` expressions. Some constructions are only possible with direct method, when you want to use the index as parameter of a construct, like in `Bytes(this._index+1)`.
Fields that are inside Array GreedyRange RepeatUntil can reference their index within the outer list. This is being effectuated by repeater class maintaining a context entry "_index" and updating it between each iteration. Note that some classes do context nesting (like Struct), but they do copy the key over. You can access the key using Index class, or refer to the context entry directly, using `this._index` expression. Some constructions are only possible with direct method, when you want to use the index as parameter of a construct, like in `Bytes(this._index+1)`.


>>> d = Array(3, Index)
Expand All @@ -94,7 +94,7 @@ Fields that are inside Array GreedyRange RepeatUntil can reference their index w
>>> d = Array(3, Computed(this._index+1))
>>> d.parse(b"")
[1, 2, 3]
>>> d = Array(3, Struct("i" / Computed(this._._index+1)))
>>> d = Array(3, Struct("i" / Computed(this._index+1)))
>>> d.parse(b"")
[Container(i=1), Container(i=2), Container(i=3)]

Expand Down

0 comments on commit b8c1b64

Please sign in to comment.