/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-10 01:35:53 UTC
  • mto: (6670.4.8 move-bzr)
  • mto: This revision was merged to the branch mainline in revision 6681.
  • Revision ID: jelmer@jelmer.uk-20170610013553-560y7mn3su4pp763
Fix remaining tests.

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
import sys
 
21
 
 
22
 
18
23
class BDecoder(object):
19
24
 
20
25
    def __init__(self, yield_tuples=False):
43
48
    def decode_int(self, x, f):
44
49
        f += 1
45
50
        newf = x.index('e', f)
46
 
        try:
47
 
            n = int(x[f:newf])
48
 
        except (OverflowError, ValueError):
49
 
            n = long(x[f:newf])
 
51
        n = int(x[f:newf])
50
52
        if x[f] == '-':
51
53
            if x[f + 1] == '0':
52
54
                raise ValueError
56
58
 
57
59
    def decode_string(self, x, f):
58
60
        colon = x.index(':', f)
59
 
        try:
60
 
            n = int(x[f:colon])
61
 
        except (OverflowError, ValueError):
62
 
            n = long(x[f:colon])
 
61
        n = int(x[f:colon])
63
62
        if x[f] == '0' and colon != f+1:
64
63
            raise ValueError
65
64
        colon += 1
86
85
        return (r, f + 1)
87
86
 
88
87
    def bdecode(self, x):
89
 
        if type(x) != str:
 
88
        if not isinstance(x, bytes):
90
89
            raise TypeError
91
90
        try:
92
91
            r, l = self.decode_func[x[0]](x, 0)
93
 
        except (IndexError, KeyError):
94
 
            raise ValueError
 
92
        except (IndexError, KeyError, OverflowError) as e:
 
93
            raise ValueError(str(e))
95
94
        if l != len(x):
96
95
            raise ValueError
97
96
        return r
104
103
bdecode_as_tuple = _tuple_decoder.bdecode
105
104
 
106
105
 
107
 
from types import StringType, IntType, LongType, DictType, ListType, TupleType
108
 
 
109
106
class Bencached(object):
110
107
    __slots__ = ['bencoded']
111
108
 
115
112
def encode_bencached(x,r):
116
113
    r.append(x.bencoded)
117
114
 
 
115
def encode_bool(x,r):
 
116
    encode_int(int(x), r)
 
117
 
118
118
def encode_int(x, r):
119
119
    r.extend(('i', str(x), 'e'))
120
120
 
129
129
 
130
130
def encode_dict(x,r):
131
131
    r.append('d')
132
 
    ilist = x.items()
133
 
    ilist.sort()
 
132
    ilist = sorted(x.items())
134
133
    for k, v in ilist:
135
134
        r.extend((str(len(k)), ':', k))
136
135
        encode_func[type(v)](v, r)
138
137
 
139
138
encode_func = {}
140
139
encode_func[type(Bencached(0))] = encode_bencached
141
 
encode_func[IntType] = encode_int
142
 
encode_func[LongType] = encode_int
143
 
encode_func[StringType] = encode_string
144
 
encode_func[ListType] = encode_list
145
 
encode_func[TupleType] = encode_list
146
 
encode_func[DictType] = encode_dict
147
 
 
148
 
try:
149
 
    from types import BooleanType
150
 
except ImportError:
151
 
    pass
152
 
else:
153
 
    def encode_bool(x,r):
154
 
        encode_int(int(x), r)
155
 
    encode_func[BooleanType] = encode_bool
156
 
 
157
 
from bzrlib._static_tuple_py import StaticTuple
 
140
encode_func[int] = encode_int
 
141
if sys.version_info < (3,):
 
142
    encode_func[long] = encode_int
 
143
encode_func[bytes] = encode_string
 
144
encode_func[list] = encode_list
 
145
encode_func[tuple] = encode_list
 
146
encode_func[dict] = encode_dict
 
147
encode_func[bool] = encode_bool
 
148
 
 
149
from breezy._static_tuple_py import StaticTuple
158
150
encode_func[StaticTuple] = encode_list
159
151
try:
160
 
    from bzrlib._static_tuple_c import StaticTuple
 
152
    from breezy._static_tuple_c import StaticTuple
161
153
except ImportError:
162
154
    pass
163
155
else: