/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/bzr/_groupcompress_pyx.pyx

  • Committer: Jelmer Vernooij
  • Date: 2019-06-29 13:16:26 UTC
  • mto: This revision was merged to the branch mainline in revision 7376.
  • Revision ID: jelmer@jelmer.uk-20190629131626-qioafloyemhdbm4w
Remove Tree.get_root_id() in favour of Tree.path2id('').

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Compiled extensions for doing compression."""
18
18
 
19
 
#python2.4 support
 
19
from __future__ import absolute_import
 
20
 
 
21
 
20
22
cdef extern from "python-compat.h":
21
23
    pass
22
24
 
23
 
 
24
 
cdef extern from "Python.h":
25
 
    ctypedef int Py_ssize_t # Required for older pyrex versions
26
 
    int PyString_CheckExact(object)
27
 
    char * PyString_AS_STRING(object)
28
 
    Py_ssize_t PyString_GET_SIZE(object)
29
 
    object PyString_FromStringAndSize(char *, Py_ssize_t)
30
 
 
31
 
 
32
 
cdef extern from *:
33
 
    ctypedef unsigned long size_t
34
 
    void * malloc(size_t) nogil
35
 
    void * realloc(void *, size_t) nogil
36
 
    void free(void *) nogil
37
 
    void memcpy(void *, void *, size_t) nogil
 
25
from libc.stdlib cimport (
 
26
    free,
 
27
    )
 
28
from libc.string cimport (
 
29
    memcpy,
 
30
    )
 
31
 
 
32
from cpython.bytes cimport (
 
33
    PyBytes_AS_STRING,
 
34
    PyBytes_CheckExact,
 
35
    PyBytes_FromStringAndSize,
 
36
    PyBytes_GET_SIZE,
 
37
    )
 
38
from cpython.object cimport (
 
39
    PyObject,
 
40
    )
 
41
from cpython.mem cimport (
 
42
    PyMem_Free,
 
43
    PyMem_Malloc,
 
44
    )
38
45
 
39
46
 
40
47
cdef extern from "delta.h":
44
51
        unsigned long agg_offset
45
52
    struct delta_index:
46
53
        pass
47
 
    delta_index * create_delta_index(source_info *src, delta_index *old) nogil
48
 
    delta_index * create_delta_index_from_delta(source_info *delta,
49
 
                                                delta_index *old) nogil
 
54
    ctypedef enum delta_result:
 
55
        DELTA_OK
 
56
        DELTA_OUT_OF_MEMORY
 
57
        DELTA_INDEX_NEEDED
 
58
        DELTA_SOURCE_EMPTY
 
59
        DELTA_SOURCE_BAD
 
60
        DELTA_BUFFER_EMPTY
 
61
        DELTA_SIZE_TOO_BIG
 
62
    delta_result create_delta_index(source_info *src,
 
63
                                    delta_index *old,
 
64
                                    delta_index **fresh,
 
65
                                    int max_entries) nogil
 
66
    delta_result create_delta_index_from_delta(source_info *delta,
 
67
                                               delta_index *old,
 
68
                                               delta_index **fresh) nogil
50
69
    void free_delta_index(delta_index *index) nogil
51
 
    void *create_delta(delta_index *indexes,
52
 
             void *buf, unsigned long bufsize,
53
 
             unsigned long *delta_size, unsigned long max_delta_size) nogil
 
70
    delta_result create_delta(delta_index *indexes,
 
71
                              void *buf, unsigned long bufsize,
 
72
                              unsigned long *delta_size,
 
73
                              unsigned long max_delta_size,
 
74
                              void **delta_data) nogil
54
75
    unsigned long get_delta_hdr_size(unsigned char **datap,
55
76
                                     unsigned char *top) nogil
 
77
    unsigned long sizeof_delta_index(delta_index *index)
56
78
    Py_ssize_t DELTA_SIZE_MIN
57
 
 
58
 
 
59
 
cdef void *safe_malloc(size_t count) except NULL:
60
 
    cdef void *result
61
 
    result = malloc(count)
62
 
    if result == NULL:
63
 
        raise MemoryError('Failed to allocate %d bytes of memory' % (count,))
64
 
    return result
65
 
 
66
 
 
67
 
cdef void *safe_realloc(void * old, size_t count) except NULL:
68
 
    cdef void *result
69
 
    result = realloc(old, count)
70
 
    if result == NULL:
71
 
        raise MemoryError('Failed to reallocate to %d bytes of memory'
72
 
                          % (count,))
73
 
    return result
74
 
 
75
 
 
76
 
cdef int safe_free(void **val) except -1:
77
 
    assert val != NULL
78
 
    if val[0] != NULL:
79
 
        free(val[0])
80
 
        val[0] = NULL
 
79
    int get_hash_offset(delta_index *index, int pos, unsigned int *hash_offset)
 
80
    int get_entry_summary(delta_index *index, int pos,
 
81
                          unsigned int *global_offset, unsigned int *hash_val)
 
82
    unsigned int rabin_hash (unsigned char *data)
 
83
 
81
84
 
82
85
def make_delta_index(source):
83
86
    return DeltaIndex(source)
84
87
 
85
88
 
 
89
cdef object _translate_delta_failure(delta_result result):
 
90
    if result == DELTA_OUT_OF_MEMORY:
 
91
        return MemoryError("Delta function failed to allocate memory")
 
92
    elif result == DELTA_INDEX_NEEDED:
 
93
        return ValueError("Delta function requires delta_index param")
 
94
    elif result == DELTA_SOURCE_EMPTY:
 
95
        return ValueError("Delta function given empty source_info param")
 
96
    elif result == DELTA_SOURCE_BAD:
 
97
        return RuntimeError("Delta function given invalid source_info param")
 
98
    elif result == DELTA_BUFFER_EMPTY:
 
99
        return ValueError("Delta function given empty buffer params")
 
100
    return AssertionError("Unrecognised delta result code: %d" % result)
 
101
 
 
102
 
 
103
def _rabin_hash(content):
 
104
    if not PyBytes_CheckExact(content):
 
105
        raise ValueError('content must be a string')
 
106
    if len(content) < 16:
 
107
        raise ValueError('content must be at least 16 bytes long')
 
108
    # Try to cast it to an int, if it can fit
 
109
    return int(rabin_hash(<unsigned char*>(PyBytes_AS_STRING(content))))
 
110
 
 
111
 
86
112
cdef class DeltaIndex:
87
113
 
88
 
    # We need Pyrex 0.9.8+ to understand a 'list' definition, and this object
89
 
    # isn't performance critical
90
 
    # cdef readonly list _sources
91
 
    cdef readonly object _sources
 
114
    cdef readonly list _sources
92
115
    cdef source_info *_source_infos
93
116
    cdef delta_index *_index
 
117
    cdef public unsigned long _source_offset
94
118
    cdef readonly unsigned int _max_num_sources
95
 
    cdef public unsigned long _source_offset
 
119
    cdef public int _max_bytes_to_index
96
120
 
97
 
    def __init__(self, source=None):
 
121
    def __init__(self, source=None, max_bytes_to_index=None):
98
122
        self._sources = []
99
123
        self._index = NULL
100
124
        self._max_num_sources = 65000
101
 
        self._source_infos = <source_info *>safe_malloc(sizeof(source_info)
102
 
                                                        * self._max_num_sources)
 
125
        self._source_infos = <source_info *>PyMem_Malloc(
 
126
            sizeof(source_info) * self._max_num_sources)
 
127
        if self._source_infos == NULL:
 
128
            raise MemoryError('failed to allocate memory for DeltaIndex')
103
129
        self._source_offset = 0
 
130
        self._max_bytes_to_index = 0
 
131
        if max_bytes_to_index is not None:
 
132
            self._max_bytes_to_index = max_bytes_to_index
104
133
 
105
134
        if source is not None:
106
135
            self.add_source(source, 0)
107
136
 
 
137
    def __sizeof__(self):
 
138
        # We want to track the _source_infos allocations, but the referenced
 
139
        # void* are actually tracked in _sources itself.
 
140
        return (sizeof(DeltaIndex)
 
141
                + (sizeof(source_info) * self._max_num_sources)
 
142
                + sizeof_delta_index(self._index))
 
143
 
108
144
    def __repr__(self):
109
145
        return '%s(%d, %d)' % (self.__class__.__name__,
110
146
            len(self._sources), self._source_offset)
113
149
        if self._index != NULL:
114
150
            free_delta_index(self._index)
115
151
            self._index = NULL
116
 
        safe_free(<void **>&self._source_infos)
 
152
        PyMem_Free(self._source_infos)
117
153
 
118
154
    def _has_index(self):
119
155
        return (self._index != NULL)
120
156
 
 
157
    def _dump_index(self):
 
158
        """Dump the pointers in the index.
 
159
 
 
160
        This is an arbitrary layout, used for testing. It is not meant to be
 
161
        used in production code.
 
162
 
 
163
        :return: (hash_list, entry_list)
 
164
            hash_list   A list of offsets, so hash[i] points to the 'hash
 
165
                        bucket' starting at the given offset and going until
 
166
                        hash[i+1]
 
167
            entry_list  A list of (text_offset, hash_val). text_offset is the
 
168
                        offset in the "source" texts, and hash_val is the RABIN
 
169
                        hash for that offset.
 
170
                        Note that the entry should be in the hash bucket
 
171
                        defined by
 
172
                        hash[(hash_val & mask)] && hash[(hash_val & mask) + 1]
 
173
        """
 
174
        cdef int pos
 
175
        cdef unsigned int text_offset
 
176
        cdef unsigned int hash_val
 
177
        cdef unsigned int hash_offset
 
178
        if self._index == NULL:
 
179
            return None
 
180
        hash_list = []
 
181
        pos = 0
 
182
        while get_hash_offset(self._index, pos, &hash_offset):
 
183
            hash_list.append(int(hash_offset))
 
184
            pos += 1
 
185
        entry_list = []
 
186
        pos = 0
 
187
        while get_entry_summary(self._index, pos, &text_offset, &hash_val):
 
188
            # Map back using 'int' so that we don't get Long everywhere, when
 
189
            # almost everything is <2**31.
 
190
            val = tuple(map(int, [text_offset, hash_val]))
 
191
            entry_list.append(val)
 
192
            pos += 1
 
193
        return hash_list, entry_list
 
194
 
121
195
    def add_delta_source(self, delta, unadded_bytes):
122
196
        """Add a new delta to the source texts.
123
197
 
128
202
        cdef char *c_delta
129
203
        cdef Py_ssize_t c_delta_size
130
204
        cdef delta_index *index
 
205
        cdef delta_result res
131
206
        cdef unsigned int source_location
132
207
        cdef source_info *src
133
208
        cdef unsigned int num_indexes
134
209
 
135
 
        if not PyString_CheckExact(delta):
136
 
            raise TypeError('delta is not a str')
 
210
        if not PyBytes_CheckExact(delta):
 
211
            raise TypeError('delta is not a bytestring')
137
212
 
138
213
        source_location = len(self._sources)
139
214
        if source_location >= self._max_num_sources:
140
215
            self._expand_sources()
141
216
        self._sources.append(delta)
142
 
        c_delta = PyString_AS_STRING(delta)
143
 
        c_delta_size = PyString_GET_SIZE(delta)
 
217
        c_delta = PyBytes_AS_STRING(delta)
 
218
        c_delta_size = PyBytes_GET_SIZE(delta)
144
219
        src = self._source_infos + source_location
145
220
        src.buf = c_delta
146
221
        src.size = c_delta_size
147
222
        src.agg_offset = self._source_offset + unadded_bytes
148
223
        with nogil:
149
 
            index = create_delta_index_from_delta(src, self._index)
 
224
            res = create_delta_index_from_delta(src, self._index, &index)
 
225
        if res != DELTA_OK:
 
226
            raise _translate_delta_failure(res)
150
227
        self._source_offset = src.agg_offset + src.size
151
 
        if index != NULL:
 
228
        if index != self._index:
152
229
            free_delta_index(self._index)
153
230
            self._index = index
154
231
 
158
235
        :param source: The text in question, this must be a byte string
159
236
        :param unadded_bytes: Assume there are this many bytes that didn't get
160
237
            added between this source and the end of the previous source.
 
238
        :param max_pointers: Add no more than this many entries to the index.
 
239
            By default, we sample every 16 bytes, if that would require more
 
240
            than max_entries, we will reduce the sampling rate.
 
241
            A value of 0 means unlimited, None means use the default limit.
161
242
        """
162
243
        cdef char *c_source
163
244
        cdef Py_ssize_t c_source_size
164
245
        cdef delta_index *index
 
246
        cdef delta_result res
165
247
        cdef unsigned int source_location
166
248
        cdef source_info *src
167
249
        cdef unsigned int num_indexes
 
250
        cdef int max_num_entries
168
251
 
169
 
        if not PyString_CheckExact(source):
170
 
            raise TypeError('source is not a str')
 
252
        if not PyBytes_CheckExact(source):
 
253
            raise TypeError('source is not a bytestring')
171
254
 
172
255
        source_location = len(self._sources)
173
256
        if source_location >= self._max_num_sources:
176
259
            # We were lazy about populating the index, create it now
177
260
            self._populate_first_index()
178
261
        self._sources.append(source)
179
 
        c_source = PyString_AS_STRING(source)
180
 
        c_source_size = PyString_GET_SIZE(source)
 
262
        c_source = PyBytes_AS_STRING(source)
 
263
        c_source_size = PyBytes_GET_SIZE(source)
181
264
        src = self._source_infos + source_location
182
265
        src.buf = c_source
183
266
        src.size = c_source_size
187
270
        # We delay creating the index on the first insert
188
271
        if source_location != 0:
189
272
            with nogil:
190
 
                index = create_delta_index(src, self._index)
191
 
            if index != NULL:
 
273
                res = create_delta_index(src, self._index, &index,
 
274
                                         self._max_bytes_to_index)
 
275
            if res != DELTA_OK:
 
276
                raise _translate_delta_failure(res)
 
277
            if index != self._index:
192
278
                free_delta_index(self._index)
193
279
                self._index = index
194
280
 
195
281
    cdef _populate_first_index(self):
196
282
        cdef delta_index *index
 
283
        cdef delta_result res
197
284
        if len(self._sources) != 1 or self._index != NULL:
198
285
            raise AssertionError('_populate_first_index should only be'
199
286
                ' called when we have a single source and no index yet')
200
287
 
201
 
        # We know that self._index is already NULL, so whatever
202
 
        # create_delta_index returns is fine
 
288
        # We know that self._index is already NULL, so create_delta_index
 
289
        # will always create a new index unless there's a malloc failure
203
290
        with nogil:
204
 
            self._index = create_delta_index(&self._source_infos[0], NULL)
205
 
        assert self._index != NULL
 
291
            res = create_delta_index(&self._source_infos[0], NULL, &index,
 
292
                                     self._max_bytes_to_index)
 
293
        if res != DELTA_OK:
 
294
            raise _translate_delta_failure(res)
 
295
        self._index = index
206
296
 
207
297
    cdef _expand_sources(self):
208
298
        raise RuntimeError('if we move self._source_infos, then we need to'
209
299
                           ' change all of the index pointers as well.')
210
 
        self._max_num_sources = self._max_num_sources * 2
211
 
        self._source_infos = <source_info *>safe_realloc(self._source_infos,
212
 
                                                sizeof(source_info)
213
 
                                                * self._max_num_sources)
214
300
 
215
301
    def make_delta(self, target_bytes, max_delta_size=0):
216
302
        """Create a delta from the current source to the target bytes."""
219
305
        cdef void * delta
220
306
        cdef unsigned long delta_size
221
307
        cdef unsigned long c_max_delta_size
 
308
        cdef delta_result res
222
309
 
223
310
        if self._index == NULL:
224
311
            if len(self._sources) == 0:
226
313
            # We were just lazy about generating the index
227
314
            self._populate_first_index()
228
315
 
229
 
        if not PyString_CheckExact(target_bytes):
230
 
            raise TypeError('target is not a str')
 
316
        if not PyBytes_CheckExact(target_bytes):
 
317
            raise TypeError('target is not a bytestring')
231
318
 
232
 
        target = PyString_AS_STRING(target_bytes)
233
 
        target_size = PyString_GET_SIZE(target_bytes)
 
319
        target = PyBytes_AS_STRING(target_bytes)
 
320
        target_size = PyBytes_GET_SIZE(target_bytes)
234
321
 
235
322
        # TODO: inline some of create_delta so we at least don't have to double
236
 
        #       malloc, and can instead use PyString_FromStringAndSize, to
 
323
        #       malloc, and can instead use PyBytes_FromStringAndSize, to
237
324
        #       allocate the bytes into the final string
238
325
        c_max_delta_size = max_delta_size
239
326
        with nogil:
240
 
            delta = create_delta(self._index,
241
 
                                 target, target_size,
242
 
                                 &delta_size, c_max_delta_size)
 
327
            res = create_delta(self._index, target, target_size,
 
328
                               &delta_size, c_max_delta_size, &delta)
243
329
        result = None
244
 
        if delta:
245
 
            result = PyString_FromStringAndSize(<char *>delta, delta_size)
 
330
        if res == DELTA_OK:
 
331
            result = PyBytes_FromStringAndSize(<char *>delta, delta_size)
246
332
            free(delta)
 
333
        elif res != DELTA_SIZE_TOO_BIG:
 
334
            raise _translate_delta_failure(res)
247
335
        return result
248
336
 
249
337
 
260
348
    cdef char *delta
261
349
    cdef Py_ssize_t delta_size
262
350
 
263
 
    if not PyString_CheckExact(source_bytes):
264
 
        raise TypeError('source is not a str')
265
 
    if not PyString_CheckExact(delta_bytes):
266
 
        raise TypeError('delta is not a str')
267
 
    source = PyString_AS_STRING(source_bytes)
268
 
    source_size = PyString_GET_SIZE(source_bytes)
269
 
    delta = PyString_AS_STRING(delta_bytes)
270
 
    delta_size = PyString_GET_SIZE(delta_bytes)
 
351
    if not PyBytes_CheckExact(source_bytes):
 
352
        raise TypeError('source is not a bytestring')
 
353
    if not PyBytes_CheckExact(delta_bytes):
 
354
        raise TypeError('delta is not a bytestring')
 
355
    source = PyBytes_AS_STRING(source_bytes)
 
356
    source_size = PyBytes_GET_SIZE(source_bytes)
 
357
    delta = PyBytes_AS_STRING(delta_bytes)
 
358
    delta_size = PyBytes_GET_SIZE(delta_bytes)
271
359
    # Code taken from patch-delta.c, only brought here to give better error
272
360
    # handling, and to avoid double allocating memory
273
361
    if (delta_size < DELTA_SIZE_MIN):
278
366
    return _apply_delta(source, source_size, delta, delta_size)
279
367
 
280
368
 
281
 
cdef unsigned char *_decode_copy_instruction(unsigned char *bytes,
 
369
cdef unsigned char *_decode_copy_instruction(unsigned char *data,
282
370
    unsigned char cmd, unsigned int *offset,
283
371
    unsigned int *length) nogil: # cannot_raise
284
372
    """Decode a copy instruction from the next few bytes.
296
384
    size = 0
297
385
    count = 0
298
386
    if (cmd & 0x01):
299
 
        off = bytes[count]
 
387
        off = data[count]
300
388
        count = count + 1
301
389
    if (cmd & 0x02):
302
 
        off = off | (bytes[count] << 8)
 
390
        off = off | (data[count] << 8)
303
391
        count = count + 1
304
392
    if (cmd & 0x04):
305
 
        off = off | (bytes[count] << 16)
 
393
        off = off | (data[count] << 16)
306
394
        count = count + 1
307
395
    if (cmd & 0x08):
308
 
        off = off | (bytes[count] << 24)
 
396
        off = off | (data[count] << 24)
309
397
        count = count + 1
310
398
    if (cmd & 0x10):
311
 
        size = bytes[count]
 
399
        size = data[count]
312
400
        count = count + 1
313
401
    if (cmd & 0x20):
314
 
        size = size | (bytes[count] << 8)
 
402
        size = size | (data[count] << 8)
315
403
        count = count + 1
316
404
    if (cmd & 0x40):
317
 
        size = size | (bytes[count] << 16)
 
405
        size = size | (data[count] << 16)
318
406
        count = count + 1
319
407
    if (size == 0):
320
408
        size = 0x10000
321
409
    offset[0] = off
322
410
    length[0] = size
323
 
    return bytes + count
 
411
    return data + count
324
412
 
325
413
 
326
414
cdef object _apply_delta(char *source, Py_ssize_t source_size,
327
415
                         char *delta, Py_ssize_t delta_size):
328
416
    """common functionality between apply_delta and apply_delta_to_source."""
329
 
    cdef unsigned char *data, *top
330
 
    cdef unsigned char *dst_buf, *out, cmd
 
417
    cdef unsigned char *data
 
418
    cdef unsigned char *top
 
419
    cdef unsigned char *dst_buf
 
420
    cdef unsigned char *out
 
421
    cdef unsigned char cmd
331
422
    cdef Py_ssize_t size
332
423
    cdef unsigned int cp_off, cp_size
333
424
    cdef int failed
337
428
 
338
429
    # now the result size
339
430
    size = get_delta_hdr_size(&data, top)
340
 
    result = PyString_FromStringAndSize(NULL, size)
341
 
    dst_buf = <unsigned char*>PyString_AS_STRING(result)
 
431
    result = PyBytes_FromStringAndSize(NULL, size)
 
432
    dst_buf = <unsigned char*>PyBytes_AS_STRING(result)
342
433
 
343
434
    failed = 0
344
435
    with nogil:
350
441
                # Copy instruction
351
442
                data = _decode_copy_instruction(data, cmd, &cp_off, &cp_size)
352
443
                if (cp_off + cp_size < cp_size or
353
 
                    cp_off + cp_size > source_size or
354
 
                    cp_size > size):
 
444
                    cp_off + cp_size > <unsigned int>source_size or
 
445
                    cp_size > <unsigned int>size):
355
446
                    failed = 1
356
447
                    break
357
448
                memcpy(out, source + cp_off, cp_size)
389
480
        raise RuntimeError('Did not extract the number of bytes we expected'
390
481
            ' we were left with %d bytes in "size", and top - data = %d'
391
482
            % (size, <int>(top - data)))
392
 
        return None
393
483
 
394
484
    # *dst_size = out - dst_buf;
395
 
    if (out - dst_buf) != PyString_GET_SIZE(result):
 
485
    if (out - dst_buf) != PyBytes_GET_SIZE(result):
396
486
        raise RuntimeError('Number of bytes extracted did not match the'
397
487
            ' size encoded in the delta header.')
398
488
    return result
406
496
    cdef Py_ssize_t c_delta_size
407
497
    cdef Py_ssize_t c_delta_start, c_delta_end
408
498
 
409
 
    if not PyString_CheckExact(source):
 
499
    if not PyBytes_CheckExact(source):
410
500
        raise TypeError('source is not a str')
411
 
    c_source_size = PyString_GET_SIZE(source)
 
501
    c_source_size = PyBytes_GET_SIZE(source)
412
502
    c_delta_start = delta_start
413
503
    c_delta_end = delta_end
414
504
    if c_delta_start >= c_source_size:
419
509
        raise ValueError('delta starts after it ends')
420
510
 
421
511
    c_delta_size = c_delta_end - c_delta_start
422
 
    c_source = PyString_AS_STRING(source)
 
512
    c_source = PyBytes_AS_STRING(source)
423
513
    c_delta = c_source + c_delta_start
424
514
    # We don't use source_size, because we know the delta should not refer to
425
515
    # any bytes after it starts
443
533
        raise ValueError('encode_base128_int overflowed the buffer')
444
534
    c_bytes[count] = <unsigned char>(c_val & 0xFF)
445
535
    count = count + 1
446
 
    return PyString_FromStringAndSize(<char *>c_bytes, count)
447
 
 
448
 
 
449
 
def decode_base128_int(bytes):
 
536
    return PyBytes_FromStringAndSize(<char *>c_bytes, count)
 
537
 
 
538
 
 
539
def decode_base128_int(data):
450
540
    """Decode an integer from a 7-bit lsb encoding."""
451
541
    cdef int offset
452
542
    cdef int val
458
548
    offset = 0
459
549
    val = 0
460
550
    shift = 0
461
 
    if not PyString_CheckExact(bytes):
 
551
    if not PyBytes_CheckExact(data):
462
552
        raise TypeError('bytes is not a string')
463
 
    c_bytes = <unsigned char*>PyString_AS_STRING(bytes)
 
553
    c_bytes = <unsigned char*>PyBytes_AS_STRING(data)
464
554
    # We take off 1, because we have to be able to decode the non-expanded byte
465
 
    num_low_bytes = PyString_GET_SIZE(bytes) - 1
 
555
    num_low_bytes = PyBytes_GET_SIZE(data) - 1
466
556
    while (c_bytes[offset] & 0x80) and offset < num_low_bytes:
467
557
        val = val | ((c_bytes[offset] & 0x7F) << shift)
468
558
        shift = shift + 7