~martin-decky/helenos/rcu

« back to all changes in this revision

Viewing changes to tools/xstruct.py

  • Committer: Vojtech Horky
  • Date: 2012-05-23 12:03:26 UTC
  • mfrom: (1443.1.19 misc)
  • mto: This revision was merged to the branch mainline in revision 1479.
  • Revision ID: vojtechhorky@users.sourceforge.net-20120523120326-jv50stjymxmh598s
Merge GSOC-originated patches 

Merge from lp:~vojtech-horky/helenos/misc.

The merge includes:
 * Switching to previous directory with `cd -' in Bdsh
 * Interactive mode for cp module in Bdsh
 * Implementation of sleep command
 * printf and echo for Bdsh
 * Rewrite of the mkdir module
 * Ctrl-arrow jumps over words in the editor
 * The scripts work with Python 3

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
"""
32
32
 
33
33
import struct
 
34
import sys
34
35
import types
35
36
 
 
37
# Handle long integer conversions nicely in both Python 2 and Python 3
 
38
integer_types = (int, long) if sys.version < '3' else (int,)
 
39
 
 
40
# Ensure that 's' format for struct receives correct data type depending
 
41
# on Python version (needed due to different way to encode into bytes) 
 
42
ensure_string = \
 
43
        (lambda value: value if type(value) is str else bytes(value)) \
 
44
                if sys.version < '3' else \
 
45
        (lambda value: bytes(value, 'ascii') if type(value) is str else value)
 
46
 
36
47
ranges = {
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),
 
48
        'B': (integer_types, 0x00, 0xff),
 
49
        'H': (integer_types, 0x0000, 0xffff),
 
50
        'L': (integer_types, 0x00000000, 0xffffffff),
 
51
        'Q': (integer_types, 0x0000000000000000, 0xffffffffffffffff),
 
52
        'b': (integer_types, -0x80, 0x7f),
 
53
        'h': (integer_types, -0x8000, 0x7fff),
 
54
        'l': (integer_types, -0x80000000, 0x7fffffff) ,
 
55
        'q': (integer_types, -0x8000000000000000, 0x7fffffffffffffff),
45
56
}
46
57
 
47
58
def check_range(varname, fmt, value):
73
84
                                        check_range(variable + '[' + repr(index) + ']', fmt, item)
74
85
                                        args.append(item)
75
86
                        else:
 
87
                                if (fmt == "s"):
 
88
                                        value = ensure_string(value)
76
89
                                check_range(variable, fmt, value)
77
90
                                args.append(value)              
78
91
                return struct.pack(self._format_, *args)