/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: 2019-03-04 00:16:27 UTC
  • mfrom: (7293 work)
  • mto: This revision was merged to the branch mainline in revision 7318.
  • Revision ID: jelmer@jelmer.uk-20190304001627-v6u7o6pf97tukhek
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
        f += 1
50
50
        newf = x.index(b'e', f)
51
51
        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)
 
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)
57
57
 
58
58
    def decode_string(self, x, f):
59
59
        colon = x.index(b':', f)
60
60
        n = int(x[f:colon])
61
 
        if x[f:f+1] == b'0' and colon != f+1:
 
61
        if x[f:f + 1] == b'0' and colon != f + 1:
62
62
            raise ValueError
63
63
        colon += 1
64
 
        return (x[colon:colon+n], colon+n)
 
64
        return (x[colon:colon + n], colon + n)
65
65
 
66
66
    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)
 
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)
70
70
            r.append(v)
71
71
        if self.yield_tuples:
72
72
            r = tuple(r)
73
73
        return (r, f + 1)
74
74
 
75
75
    def decode_dict(self, x, f):
76
 
        r, f = {}, f+1
 
76
        r, f = {}, f + 1
77
77
        lastkey = None
78
 
        while x[f:f+1] != b'e':
 
78
        while x[f:f + 1] != b'e':
79
79
            k, f = self.decode_string(x, f)
80
80
            if lastkey is not None and lastkey >= k:
81
81
                raise ValueError
82
82
            lastkey = k
83
 
            r[k], f = self.decode_func[x[f:f+1]](x, f)
 
83
            r[k], f = self.decode_func[x[f:f + 1]](x, f)
84
84
        return (r, f + 1)
85
85
 
86
86
    def bdecode(self, x):
112
112
def encode_bencached(x, r):
113
113
    r.append(x.bencoded)
114
114
 
 
115
 
115
116
def encode_bool(x, r):
116
117
    encode_int(int(x), r)
117
118
 
 
119
 
118
120
def encode_int(x, r):
119
121
    r.extend((b'i', int_to_bytes(x), b'e'))
120
122
 
 
123
 
121
124
def encode_string(x, r):
122
125
    r.extend((int_to_bytes(len(x)), b':', x))
123
126
 
 
127
 
124
128
def encode_list(x, r):
125
129
    r.append(b'l')
126
130
    for i in x:
127
131
        encode_func[type(i)](i, r)
128
132
    r.append(b'e')
129
133
 
 
134
 
130
135
def encode_dict(x, r):
131
136
    r.append(b'd')
132
137
    ilist = sorted(x.items())
135
140
        encode_func[type(v)](v, r)
136
141
    r.append(b'e')
137
142
 
 
143
 
138
144
encode_func = {}
139
145
encode_func[type(Bencached(0))] = encode_bencached
140
146
encode_func[int] = encode_int
164
170
    r = []
165
171
    encode_func[type(x)](x, r)
166
172
    return b''.join(r)
167