Package pike :: Module core :: Class Cursor
[hide private]
[frames] | no frames]

Class Cursor

source code

object --+
         |
        Cursor

Byte array cursor

Represents a position within a byte array.

encode_* operations write data at the current position and advance it by the number of bytes written.

decode_* operations read data at the current position, advance it by the number of bytes read, and return the result.

Cursors support in-place addition and subtraction (+=,-=) of integer values to manually adjust position, as well as ordinary addition and subtraction, which return a new cursor without modifiying the original.

Ordinary subtraction of two cursors will yield the difference between their positions as an integer. ==, !=, <, <=, >, and >= operators will also work to compare positions. The results will only be meaningful for cursors referencing the same underlying array.

For a given cursor, cursor.hole.encode_* will perform the same operation as cursor.encode_*, but will return a hole object. Calling this hole object with the same type of arguments as the original encode method will overwrite the original value. This mechanism is useful for backpatching values into fields that aren't known until later, such as internal packet lengths and offsets or checksum fields. For example:

   hole = cursor.hole.encode_uint32le(0)
   # Encode rest of packet
   ...
   # Overwrite value with calculated checksum
   hole(sum)

Cursors support slicing to extract sections of the underlying array. For example:

   # Extract array slice between cur1 and cur2
   subarray = cur1[:cur2]

Cursors also support establishing boundaries outside of which decoding will raise exceptions:

   with cur.bounded(startcur, endcur):
       # Within this block, attempts to decode data outside of
       # the range starting with startcur (inclusive) and ending
       # with endcur (exclusive) will raise BufferOverrun().
       # If the start and end paremeters are numbers rather than
       # other cursors, they will be taken to be relative to
       # cur itself.
       ...
Nested Classes [hide private]
  Hole
  Bounds
Instance Methods [hide private]
 
__init__(self, arr, offset, bounds=(None, None))
Create a Cursor for the given array at the given offset.
source code
 
__eq__(self, o) source code
 
__ne__(self, o) source code
 
__lt__(self, o) source code
 
__gt__(self, o) source code
 
__le__(self, o) source code
 
__ge__(self, o) source code
 
__add__(self, o) source code
 
__sub__(self, o) source code
 
__iadd__(self, o) source code
 
__isub__(self, o) source code
 
_getindex(self, ind) source code
 
__getitem__(self, index) source code
 
__repr__(self)
repr(x)
source code
 
copy(self)
Create copy of cursor.
source code
 
_expand_to(self, size) source code
 
encode_bytes(self, val)
Encode bytes.
source code
 
encode_struct(self, fmt, *args) source code
 
encode_uint8be(self, val) source code
 
encode_uint16be(self, val) source code
 
encode_uint32be(self, val) source code
 
encode_uint64be(self, val) source code
 
encode_uint8le(self, val) source code
 
encode_uint16le(self, val) source code
 
encode_uint32le(self, val) source code
 
encode_uint64le(self, val) source code
 
encode_int64le(self, val) source code
 
encode_utf16le(self, val) source code
 
trunc(self) source code
 
_check_bounds(self, start, end) source code
 
decode_bytes(self, size) source code
 
decode_struct(self, fmt) source code
 
decode_uint8be(self) source code
 
decode_uint16be(self) source code
 
decode_uint32be(self) source code
 
decode_uint64be(self) source code
 
decode_uint8le(self) source code
 
decode_uint16le(self) source code
 
decode_uint32le(self) source code
 
decode_int32le(self) source code
 
decode_uint64le(self) source code
 
decode_int64le(self) source code
 
decode_utf16le(self, size) source code
 
align(self, base, val) source code
 
seekto(self, o, lowerbound=None, upperbound=None) source code
 
advanceto(self, o, bound=None) source code
 
reverseto(self, o, bound=None) source code
 
bounded(self, lower, upper) source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __str__, __subclasshook__

Instance Variables [hide private]
  array
Array referenced by cursor
  bounds
Pair of lower and upper bound on offset
  offset
Offset within the array
Properties [hide private]
  lowerbound
  upperbound

Inherited from object: __class__

Method Details [hide private]

__init__(self, arr, offset, bounds=(None, None))
(Constructor)

source code 

Create a Cursor for the given array at the given offset.

Parameters:
  • arr (array.array('B', ...)) - The array
  • offset (number) - The offset from the start of the array
  • bounds - A pair of a lower and upper bound on valid offsets
Overrides: object.__init__

__repr__(self)
(Representation operator)

source code 

repr(x)

Overrides: object.__repr__
(inherited documentation)

encode_bytes(self, val)

source code 

Encode bytes. Accepts byte arrays, strings, and integer lists.


Property Details [hide private]

lowerbound

Get Method:
unreachable.lowerbound(self)

upperbound

Get Method:
unreachable.upperbound(self)