/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 18:34:12 UTC
  • mto: This revision was merged to the branch mainline in revision 6690.
  • Revision ID: jelmer@jelmer.uk-20170610183412-s9fro6la0e1848x6
More moves.

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, OverflowError), e:
94
 
            import sys
95
 
            raise ValueError, ValueError(str(e)), sys.exc_info()[2]
 
92
        except (IndexError, KeyError, OverflowError) as e:
 
93
            raise ValueError(str(e))
96
94
        if l != len(x):
97
95
            raise ValueError
98
96
        return r
105
103
bdecode_as_tuple = _tuple_decoder.bdecode
106
104
 
107
105
 
108
 
from types import StringType, IntType, LongType, DictType, ListType, TupleType
109
 
 
110
106
class Bencached(object):
111
107
    __slots__ = ['bencoded']
112
108
 
116
112
def encode_bencached(x,r):
117
113
    r.append(x.bencoded)
118
114
 
 
115
def encode_bool(x,r):
 
116
    encode_int(int(x), r)
 
117
 
119
118
def encode_int(x, r):
120
119
    r.extend(('i', str(x), 'e'))
121
120
 
130
129
 
131
130
def encode_dict(x,r):
132
131
    r.append('d')
133
 
    ilist = x.items()
134
 
    ilist.sort()
 
132
    ilist = sorted(x.items())
135
133
    for k, v in ilist:
136
134
        r.extend((str(len(k)), ':', k))
137
135
        encode_func[type(v)](v, r)
139
137
 
140
138
encode_func = {}
141
139
encode_func[type(Bencached(0))] = encode_bencached
142
 
encode_func[IntType] = encode_int
143
 
encode_func[LongType] = encode_int
144
 
encode_func[StringType] = encode_string
145
 
encode_func[ListType] = encode_list
146
 
encode_func[TupleType] = encode_list
147
 
encode_func[DictType] = encode_dict
148
 
 
149
 
try:
150
 
    from types import BooleanType
151
 
except ImportError:
152
 
    pass
153
 
else:
154
 
    def encode_bool(x,r):
155
 
        encode_int(int(x), r)
156
 
    encode_func[BooleanType] = encode_bool
157
 
 
158
 
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
159
150
encode_func[StaticTuple] = encode_list
160
151
try:
161
 
    from bzrlib._static_tuple_c import StaticTuple
 
152
    from breezy._static_tuple_c import StaticTuple
162
153
except ImportError:
163
154
    pass
164
155
else: