/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-07-23 22:06:41 UTC
  • mfrom: (6738 trunk)
  • mto: This revision was merged to the branch mainline in revision 6739.
  • Revision ID: jelmer@jelmer.uk-20170723220641-69eczax9bmv8d6kk
Merge trunk, address review comments.

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] == b'-':
53
 
            if x[f + 1] == b'0':
54
 
                raise ValueError
55
 
        elif x[f] == b'0' and newf != f+1:
 
52
        if x[f:f+2] == b'-0':
 
53
            raise ValueError
 
54
        elif x[f:f+1] == b'0' and newf != f+1:
56
55
            raise ValueError
57
56
        return (n, newf+1)
58
57
 
59
58
    def decode_string(self, x, f):
60
59
        colon = x.index(b':', f)
61
60
        n = int(x[f:colon])
62
 
        if x[f] == b'0' and colon != f+1:
 
61
        if x[f:f+1] == b'0' and colon != f+1:
63
62
            raise ValueError
64
63
        colon += 1
65
64
        return (x[colon:colon+n], colon+n)
66
65
 
67
66
    def decode_list(self, x, f):
68
67
        r, f = [], f+1
69
 
        while x[f] != b'e':
70
 
            v, f = self.decode_func[x[f]](x, f)
 
68
        while x[f:f+1] != b'e':
 
69
            v, f = self.decode_func[x[f:f+1]](x, f)
71
70
            r.append(v)
72
71
        if self.yield_tuples:
73
72
            r = tuple(r)
76
75
    def decode_dict(self, x, f):
77
76
        r, f = {}, f+1
78
77
        lastkey = None
79
 
        while x[f] != b'e':
 
78
        while x[f:f+1] != b'e':
80
79
            k, f = self.decode_string(x, f)
81
 
            if lastkey >= k:
 
80
            if lastkey is not None and lastkey >= k:
82
81
                raise ValueError
83
82
            lastkey = k
84
 
            r[k], f = self.decode_func[x[f]](x, f)
 
83
            r[k], f = self.decode_func[x[f:f+1]](x, f)
85
84
        return (r, f + 1)
86
85
 
87
86
    def bdecode(self, x):
109
108
    def __init__(self, s):
110
109
        self.bencoded = s
111
110
 
 
111
 
112
112
def encode_bencached(x,r):
113
113
    r.append(x.bencoded)
114
114
 
121
121
def encode_string(x, r):
122
122
    r.extend((int_to_bytes(len(x)), b':', x))
123
123
 
124
 
def encode_unicode(x, r):
125
 
    r.extend((int_to_bytes(len(x)), b':', x))
126
 
 
127
124
def encode_list(x, r):
128
125
    r.append(b'l')
129
126
    for i in x:
145
142
    encode_func[long] = encode_int
146
143
    int_to_bytes = str
147
144
else:
148
 
    int_to_bytes = lambda n: b"%d" % n
 
145
    def int_to_bytes(n):
 
146
        return b'%d' % n
149
147
encode_func[bytes] = encode_string
150
148
encode_func[list] = encode_list
151
149
encode_func[tuple] = encode_list