~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_bencode_c.pyx

  • Committer: Jelmer Vernooij
  • Date: 2009-05-26 09:50:19 UTC
  • mto: (4398.5.1 bencode_serializer)
  • mto: This revision was merged to the branch mainline in revision 4410.
  • Revision ID: jelmer@samba.org-20090526095019-35nkhcnyci25836v
Support bdecode_as_tuple.

Show diffs side-by-side

added added

removed removed

Lines of Context:
74
74
cdef class Decoder:
75
75
    """Bencode decoder"""
76
76
 
77
 
    cdef readonly object __s
 
77
    cdef readonly object _text
78
78
    cdef readonly char *tail
79
79
    cdef readonly int   size
80
80
 
81
81
    cdef readonly long   _MAXINT
82
82
    cdef readonly int    _MAXN
83
83
    cdef readonly object _longint
 
84
    cdef readonly int    _yield_tuples
84
85
 
85
 
    def __init__(self, s):
 
86
    def __init__(self, s, yield_tuples=0):
86
87
        """Initialize decoder engine.
87
88
        @param  s:  Python string.
88
89
        """
97
98
        if pstr == NULL:
98
99
            raise ValueError
99
100
 
100
 
        self.__s = s
 
101
        self._text = s
101
102
        self.tail = pstr
102
103
        self.size = <int>k
 
104
        self._yield_tuples = int(yield_tuples)
103
105
 
104
106
        self._MAXINT = PyInt_GetMax()
105
107
        self._MAXN = len(str(self._MAXINT))
106
108
        self._longint = long(0)
107
109
 
108
110
    def __repr__(self):
109
 
        return 'Decoder(%s)' % repr(self.__s)
 
111
        return 'Decoder(%s)' % repr(self._text)
110
112
 
111
113
    def decode(self):
112
114
        result = self.decode_object()
226
228
        while self.size > 0:
227
229
            if self.tail[0] == SMALL_E:
228
230
                self._update_tail(1)
229
 
                return result
 
231
                if self._yield_tuples:
 
232
                    return tuple(result)
 
233
                else:
 
234
                    return result
230
235
            else:
231
236
                result.append(self.decode_object())
232
237
 
263
268
    return Decoder(s).decode()
264
269
 
265
270
 
 
271
def bdecode_as_tuple(object s):
 
272
    """Decode string x to Python object, using tuples rather than lists."""
 
273
    return Decoder(s, True).decode()
 
274
 
 
275
 
266
276
class Bencached(object):
267
277
    __slots__ = ['bencoded']
268
278