Пример #1
0
 def _before_unpack(self, opts):
     CField._before_unpack(self, opts)
     try:
         opts['length'] = opts['data'].index('\0', opts['offset']) - opts['offset'] + 1
         if "max_length" in opts:
             opts['length'] = min(opts['max_length'], opts['length'])
     except ValueError:
         raise UnpackException("Unterminated null string occured.")
Пример #2
0
 def __init__(self, idx, default=0, **kwargs):
     CField.__init__(self, idx, default, **kwargs)
     self.add_constraint(const.ValueTypeConstraint(numbers.Integral))
     self.__ctype = kwargs.get("ctype", "int")
Пример #3
0
    def set_value(self, obj, value):
        if not isinstance(value, bytes) or value[-1] != b'\0':
            raise ValueError("NullStringField value must a string with last character == '\\0'.")

        return CField.set_value(self, obj, value)
Пример #4
0
 def _retrieve_value(self, opts):
     (v, offset) = CField._retrieve_value(self, opts)
     return (v[0], offset)
Пример #5
0
 def pack(self, obj, offset, **opts):
     value = getattr(obj, self.name)
     return CField.pack(self, obj, offset, length=len(value), **opts)
Пример #6
0
 def __init__(self, idx, default='', length=0, **kwargs):
     CField.__init__(self, idx, default, **dict(kwargs, length=length))
Пример #7
0
 def set_value(self, obj, value):
     wrapper = ListItemWrapper(value)
     wrapper._set_action = self.item_set_value
     wrapper._get_action = self.item_get_value
     return CField.set_value(self, obj, wrapper)
Пример #8
0
 def __init__(self, idx, subfield, default=[], length=0, **kwargs):
     CField.__init__(self, idx, default, **dict(kwargs, length=length))
     self.__subfield = subfield
Пример #9
0
 def __init__(self, idx, struct, default=None, **kwargs):
     CField.__init__(self, idx, default, **kwargs)
     self._struct_klass = struct