Changeset 00c2de63 in mainline for tools/xstruct.py


Ignore:
Timestamp:
2011-07-29T14:50:22Z (13 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
b6759f4
Parents:
6c69d19 (diff), 7ae249d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge libposix.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/xstruct.py

    r6c69d19 r00c2de63  
    11#
    22# Copyright (c) 2008 Martin Decky
     3# Copyright (c) 2011 Martin Sucha
    34# All rights reserved.
    45#
     
    3132
    3233import struct
     34import types
     35
     36ranges = {
     37        'B': ((int, long), 0x00, 0xff),
     38        'H': ((int, long), 0x0000, 0xffff),
     39        'L': ((int, long), 0x00000000, 0xffffffff),
     40        'Q': ((int, long), 0x0000000000000000, 0xffffffffffffffff),
     41        'b': ((int, long), -0x80, 0x7f),
     42        'h': ((int, long), -0x8000, 0x7fff),
     43        'l': ((int, long), -0x80000000, 0x7fffffff) ,
     44        'q': ((int, long), -0x8000000000000000, 0x7fffffffffffffff),
     45}
     46
     47def check_range(varname, fmt, value):
     48        if value == None:
     49                raise ValueError('Variable "%s" not set' % varname)
     50        if not fmt in ranges:
     51                return
     52        vartype, varmin, varmax = ranges[fmt]
     53        if not isinstance(value, vartype):
     54                raise ValueError('Variable "%s" is %s but should be %s' %
     55                                 (varname, str(type(value)), str(vartype)))
     56        if value < varmin or value > varmax:
     57                raise ValueError('Variable "%s" value %s out of range %s..%s' %
     58                                 (varname, repr(value), repr(varmin), repr(varmax)))
    3359
    3460class Struct:
     
    3864        def pack(self):
    3965                args = []
    40                 for variable in self._args_:
    41                         if (isinstance(self.__dict__[variable], list)):
    42                                 for item in self.__dict__[variable]:
     66                for variable, fmt, length in self._args_:
     67                        value = self.__dict__[variable]
     68                        if isinstance(value, list):
     69                                if length != None and length != len(value):
     70                                        raise ValueError('Variable "%s" length %u does not match %u' %
     71                                                      (variable, len(value), length))
     72                                for index, item in enumerate(value):
     73                                        check_range(variable + '[' + repr(index) + ']', fmt, item)
    4374                                        args.append(item)
    4475                        else:
    45                                 args.append(self.__dict__[variable])
    46                
     76                                check_range(variable, fmt, value)
     77                                args.append(value)             
    4778                return struct.pack(self._format_, *args)
     79       
     80        def unpack(self, data):
     81                values = struct.unpack(self._format_, data)
     82                i = 0
     83                for variable, fmt, length in self._args_:
     84                        self.__dict__[variable] = values[i]
     85                        i += 1
    4886
    4987def create(definition):
     
    77115                        subtokens = token.split("[")
    78116                       
     117                        length = None
    79118                        if (len(subtokens) > 1):
    80                                 format += "%d" % int(subtokens[1].split("]")[0])
     119                                length = int(subtokens[1].split("]")[0])
     120                                format += "%d" % length
    81121                       
    82122                        format += variable
    83123                       
    84124                        inst.__dict__[subtokens[0]] = None
    85                         args.append(subtokens[0])
     125                        args.append((subtokens[0], variable, length))
    86126                       
    87127                        variable = None
Note: See TracChangeset for help on using the changeset viewer.