/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/util/_bencode_py.py

  • Committer: Jelmer Vernooij
  • Date: 2017-06-11 01:28:08 UTC
  • mfrom: (6684 work)
  • mto: This revision was merged to the branch mainline in revision 6686.
  • Revision ID: jelmer@jelmer.uk-20170611012808-hw6uepwy9sjdpdl4
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
        """
31
31
        self.yield_tuples = yield_tuples
32
32
        decode_func = {}
33
 
        decode_func['l'] = self.decode_list
34
 
        decode_func['d'] = self.decode_dict
35
 
        decode_func['i'] = self.decode_int
36
 
        decode_func['0'] = self.decode_string
37
 
        decode_func['1'] = self.decode_string
38
 
        decode_func['2'] = self.decode_string
39
 
        decode_func['3'] = self.decode_string
40
 
        decode_func['4'] = self.decode_string
41
 
        decode_func['5'] = self.decode_string
42
 
        decode_func['6'] = self.decode_string
43
 
        decode_func['7'] = self.decode_string
44
 
        decode_func['8'] = self.decode_string
45
 
        decode_func['9'] = self.decode_string
 
33
        decode_func[b'l'] = self.decode_list
 
34
        decode_func[b'd'] = self.decode_dict
 
35
        decode_func[b'i'] = self.decode_int
 
36
        decode_func[b'0'] = self.decode_string
 
37
        decode_func[b'1'] = self.decode_string
 
38
        decode_func[b'2'] = self.decode_string
 
39
        decode_func[b'3'] = self.decode_string
 
40
        decode_func[b'4'] = self.decode_string
 
41
        decode_func[b'5'] = self.decode_string
 
42
        decode_func[b'6'] = self.decode_string
 
43
        decode_func[b'7'] = self.decode_string
 
44
        decode_func[b'8'] = self.decode_string
 
45
        decode_func[b'9'] = self.decode_string
46
46
        self.decode_func = decode_func
47
47
 
48
48
    def decode_int(self, x, f):
49
49
        f += 1
50
 
        newf = x.index('e', f)
 
50
        newf = x.index(b'e', f)
51
51
        n = int(x[f:newf])
52
 
        if x[f] == '-':
53
 
            if x[f + 1] == '0':
 
52
        if x[f] == b'-':
 
53
            if x[f + 1] == b'0':
54
54
                raise ValueError
55
 
        elif x[f] == '0' and newf != f+1:
 
55
        elif x[f] == b'0' and newf != f+1:
56
56
            raise ValueError
57
57
        return (n, newf+1)
58
58
 
59
59
    def decode_string(self, x, f):
60
 
        colon = x.index(':', f)
 
60
        colon = x.index(b':', f)
61
61
        n = int(x[f:colon])
62
 
        if x[f] == '0' and colon != f+1:
 
62
        if x[f] == b'0' and colon != f+1:
63
63
            raise ValueError
64
64
        colon += 1
65
65
        return (x[colon:colon+n], colon+n)
66
66
 
67
67
    def decode_list(self, x, f):
68
68
        r, f = [], f+1
69
 
        while x[f] != 'e':
 
69
        while x[f] != b'e':
70
70
            v, f = self.decode_func[x[f]](x, f)
71
71
            r.append(v)
72
72
        if self.yield_tuples:
76
76
    def decode_dict(self, x, f):
77
77
        r, f = {}, f+1
78
78
        lastkey = None
79
 
        while x[f] != 'e':
 
79
        while x[f] != b'e':
80
80
            k, f = self.decode_string(x, f)
81
81
            if lastkey >= k:
82
82
                raise ValueError
88
88
        if not isinstance(x, bytes):
89
89
            raise TypeError
90
90
        try:
91
 
            r, l = self.decode_func[x[0]](x, 0)
 
91
            r, l = self.decode_func[x[:1]](x, 0)
92
92
        except (IndexError, KeyError, OverflowError) as e:
93
93
            raise ValueError(str(e))
94
94
        if l != len(x):
116
116
    encode_int(int(x), r)
117
117
 
118
118
def encode_int(x, r):
119
 
    r.extend(('i', str(x), 'e'))
 
119
    r.extend((b'i', int_to_bytes(x), b'e'))
120
120
 
121
121
def encode_string(x, r):
122
 
    r.extend((str(len(x)), ':', x))
 
122
    r.extend((int_to_bytes(len(x)), b':', x))
 
123
 
 
124
def encode_unicode(x, r):
 
125
    r.extend((int_to_bytes(len(x)), b':', x))
123
126
 
124
127
def encode_list(x, r):
125
 
    r.append('l')
 
128
    r.append(b'l')
126
129
    for i in x:
127
130
        encode_func[type(i)](i, r)
128
 
    r.append('e')
 
131
    r.append(b'e')
129
132
 
130
133
def encode_dict(x,r):
131
 
    r.append('d')
 
134
    r.append(b'd')
132
135
    ilist = sorted(x.items())
133
136
    for k, v in ilist:
134
 
        r.extend((str(len(k)), ':', k))
 
137
        r.extend((int_to_bytes(len(k)), b':', k))
135
138
        encode_func[type(v)](v, r)
136
 
    r.append('e')
 
139
    r.append(b'e')
137
140
 
138
141
encode_func = {}
139
142
encode_func[type(Bencached(0))] = encode_bencached
140
143
encode_func[int] = encode_int
141
144
if sys.version_info < (3,):
142
145
    encode_func[long] = encode_int
 
146
    int_to_bytes = str
 
147
else:
 
148
    int_to_bytes = lambda n: b"%d" % n
143
149
encode_func[bytes] = encode_string
144
150
encode_func[list] = encode_list
145
151
encode_func[tuple] = encode_list
159
165
def bencode(x):
160
166
    r = []
161
167
    encode_func[type(x)](x, r)
162
 
    return ''.join(r)
 
168
    return b''.join(r)
163
169