/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: 2020-03-22 01:35:14 UTC
  • mfrom: (7490.7.6 work)
  • mto: This revision was merged to the branch mainline in revision 7499.
  • Revision ID: jelmer@jelmer.uk-20200322013514-7vw1ntwho04rcuj3
merge lp:brz/3.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
#
16
16
# Modifications copyright (C) 2008 Canonical Ltd
17
17
 
18
 
from __future__ import absolute_import
19
 
 
20
18
import sys
21
19
 
22
20
 
49
47
        f += 1
50
48
        newf = x.index(b'e', f)
51
49
        n = int(x[f:newf])
52
 
        if x[f:f+2] == b'-0':
53
 
            raise ValueError
54
 
        elif x[f:f+1] == b'0' and newf != f+1:
55
 
            raise ValueError
56
 
        return (n, newf+1)
 
50
        if x[f:f + 2] == b'-0':
 
51
            raise ValueError
 
52
        elif x[f:f + 1] == b'0' and newf != f + 1:
 
53
            raise ValueError
 
54
        return (n, newf + 1)
57
55
 
58
56
    def decode_string(self, x, f):
59
57
        colon = x.index(b':', f)
60
58
        n = int(x[f:colon])
61
 
        if x[f:f+1] == b'0' and colon != f+1:
 
59
        if x[f:f + 1] == b'0' and colon != f + 1:
62
60
            raise ValueError
63
61
        colon += 1
64
 
        return (x[colon:colon+n], colon+n)
 
62
        return (x[colon:colon + n], colon + n)
65
63
 
66
64
    def decode_list(self, x, f):
67
 
        r, f = [], f+1
68
 
        while x[f:f+1] != b'e':
69
 
            v, f = self.decode_func[x[f:f+1]](x, f)
 
65
        r, f = [], f + 1
 
66
        while x[f:f + 1] != b'e':
 
67
            v, f = self.decode_func[x[f:f + 1]](x, f)
70
68
            r.append(v)
71
69
        if self.yield_tuples:
72
70
            r = tuple(r)
73
71
        return (r, f + 1)
74
72
 
75
73
    def decode_dict(self, x, f):
76
 
        r, f = {}, f+1
 
74
        r, f = {}, f + 1
77
75
        lastkey = None
78
 
        while x[f:f+1] != b'e':
 
76
        while x[f:f + 1] != b'e':
79
77
            k, f = self.decode_string(x, f)
80
78
            if lastkey is not None and lastkey >= k:
81
79
                raise ValueError
82
80
            lastkey = k
83
 
            r[k], f = self.decode_func[x[f:f+1]](x, f)
 
81
            r[k], f = self.decode_func[x[f:f + 1]](x, f)
84
82
        return (r, f + 1)
85
83
 
86
84
    def bdecode(self, x):
112
110
def encode_bencached(x, r):
113
111
    r.append(x.bencoded)
114
112
 
 
113
 
115
114
def encode_bool(x, r):
116
115
    encode_int(int(x), r)
117
116
 
 
117
 
118
118
def encode_int(x, r):
119
119
    r.extend((b'i', int_to_bytes(x), b'e'))
120
120
 
 
121
 
121
122
def encode_string(x, r):
122
123
    r.extend((int_to_bytes(len(x)), b':', x))
123
124
 
 
125
 
124
126
def encode_list(x, r):
125
127
    r.append(b'l')
126
128
    for i in x:
127
129
        encode_func[type(i)](i, r)
128
130
    r.append(b'e')
129
131
 
 
132
 
130
133
def encode_dict(x, r):
131
134
    r.append(b'd')
132
135
    ilist = sorted(x.items())
135
138
        encode_func[type(v)](v, r)
136
139
    r.append(b'e')
137
140
 
 
141
 
138
142
encode_func = {}
139
143
encode_func[type(Bencached(0))] = encode_bencached
140
144
encode_func[int] = encode_int
141
 
if sys.version_info < (3,):
142
 
    encode_func[long] = encode_int
143
 
    int_to_bytes = str
144
 
else:
145
 
    def int_to_bytes(n):
146
 
        return b'%d' % n
 
145
def int_to_bytes(n):
 
146
    return b'%d' % n
147
147
encode_func[bytes] = encode_string
148
148
encode_func[list] = encode_list
149
149
encode_func[tuple] = encode_list
164
164
    r = []
165
165
    encode_func[type(x)](x, r)
166
166
    return b''.join(r)
167