/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2009, 2010 Canonical Ltd
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
4241.6.4 by Robert Collins
Really build on old pyrex.
18
#python2.4 support
19
cdef extern from "python-compat.h":
4679.9.14 by John Arbash Meinel
Revert previous change.
20
    pass
4241.6.4 by Robert Collins
Really build on old pyrex.
21
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
22
cdef extern from *:
23
    ctypedef unsigned int size_t
24
    int memcmp(void *, void*, size_t)
25
    void memcpy(void *, void*, size_t)
26
    void *memchr(void *s, int c, size_t len)
27
    long strtol(char *, char **, int)
28
    void sprintf(char *, char *, ...)
29
30
cdef extern from "Python.h":
4265.1.3 by John Arbash Meinel
restore the old Py_ssize_t import in the pyrex files.
31
    ctypedef int Py_ssize_t # Required for older pyrex versions
4679.9.12 by John Arbash Meinel
Tweak some of the internals of _chk_map_pyx.pyx
32
    ctypedef struct PyObject:
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
33
        pass
34
    int PyTuple_CheckExact(object p)
35
    Py_ssize_t PyTuple_GET_SIZE(object t)
36
    int PyString_CheckExact(object)
37
    char *PyString_AS_STRING(object s)
5218.2.1 by John Arbash Meinel
Implement a compiled extension for parsing the text key out of a CHKInventory value.
38
    PyObject *PyString_FromStringAndSize_ptr "PyString_FromStringAndSize" (char *, Py_ssize_t)
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
39
    Py_ssize_t PyString_GET_SIZE(object)
5218.2.1 by John Arbash Meinel
Implement a compiled extension for parsing the text key out of a CHKInventory value.
40
    void PyString_InternInPlace(PyObject **)
5165.1.2 by John Arbash Meinel
Simplify further, if this isn't going to be hot enough to matter, make the
41
    unsigned long PyInt_AsUnsignedLongMask(object) except? -1
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
42
43
    int PyDict_SetItem(object d, object k, object v) except -1
44
45
    object PyTuple_New(Py_ssize_t count)
46
    void PyTuple_SET_ITEM(object t, Py_ssize_t offset, object)
47
48
    void Py_INCREF(object)
5218.2.1 by John Arbash Meinel
Implement a compiled extension for parsing the text key out of a CHKInventory value.
49
    void Py_DECREF_ptr "Py_DECREF" (PyObject *)
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
50
51
    PyObject * PyTuple_GET_ITEM_ptr "PyTuple_GET_ITEM" (object t,
52
                                                        Py_ssize_t offset)
53
    int PyString_CheckExact_ptr "PyString_CheckExact" (PyObject *p)
54
    Py_ssize_t PyString_GET_SIZE_ptr "PyString_GET_SIZE" (PyObject *s)
55
    char *PyString_AS_STRING_ptr "PyString_AS_STRING" (PyObject *s)
56
    object PyString_FromStringAndSize(char*, Py_ssize_t)
57
4679.9.16 by John Arbash Meinel
More cleanups and clarifications.
58
# cimport all of the definitions we will need to access
4679.9.12 by John Arbash Meinel
Tweak some of the internals of _chk_map_pyx.pyx
59
from _static_tuple_c cimport StaticTuple,\
60
    import_static_tuple_c, StaticTuple_New, \
61
    StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact
62
63
cdef extern from "_static_tuple_c.h":
4679.9.16 by John Arbash Meinel
More cleanups and clarifications.
64
    # Defined explicitly rather than cimport-ing. Trying to use cimport, the
65
    # type for PyObject is a different class that happens to have the same
66
    # name...
4679.9.12 by John Arbash Meinel
Tweak some of the internals of _chk_map_pyx.pyx
67
    PyObject * StaticTuple_GET_ITEM_ptr "StaticTuple_GET_ITEM" (StaticTuple,
68
                                                                Py_ssize_t)
69
5165.1.1 by Martin
Use python builtin crc32 function rather than zlib C api for _chk_map_pyx
70
cdef object crc32
5165.1.4 by John Arbash Meinel
zlib.crc32 seems to be quite a bit faster than binascii.crc32
71
from zlib import crc32
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
72
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
73
4679.9.16 by John Arbash Meinel
More cleanups and clarifications.
74
# Set up the StaticTuple C_API functionality
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
75
import_static_tuple_c()
76
77
cdef object _LeafNode
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
78
_LeafNode = None
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
79
cdef object _InternalNode
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
80
_InternalNode = None
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
81
cdef object _unknown
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
82
_unknown = None
83
4459.2.1 by Vincent Ladeuil
Use a consistent scheme for naming pyrex source files.
84
# We shouldn't just copy this from _dirstate_helpers_pyx
4634.117.10 by John Arbash Meinel
Change 'no except' to 'cannot_raise'
85
cdef void* _my_memrchr(void *s, int c, size_t n): # cannot_raise
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
86
    # memrchr seems to be a GNU extension, so we have to implement it ourselves
87
    cdef char *pos
88
    cdef char *start
89
90
    start = <char*>s
91
    pos = start + n - 1
92
    while pos >= start:
93
        if pos[0] == c:
94
            return <void*>pos
95
        pos = pos - 1
96
    return NULL
97
98
5218.2.1 by John Arbash Meinel
Implement a compiled extension for parsing the text key out of a CHKInventory value.
99
cdef object safe_interned_string_from_size(char *s, Py_ssize_t size):
100
    cdef PyObject *py_str
101
    if size < 0:
102
        raise AssertionError(
103
            'tried to create a string with an invalid size: %d @0x%x'
104
            % (size, <int>s))
105
    py_str = PyString_FromStringAndSize_ptr(s, size)
106
    PyString_InternInPlace(&py_str)
107
    result = <object>py_str
108
    # Casting a PyObject* to an <object> triggers an INCREF from Pyrex, so we
109
    # DECREF it to avoid geting immortal strings
110
    Py_DECREF_ptr(py_str)
111
    return result
112
113
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
114
def _search_key_16(key):
115
    """See chk_map._search_key_16."""
116
    cdef Py_ssize_t num_bits
117
    cdef Py_ssize_t i, j
118
    cdef Py_ssize_t num_out_bytes
5165.1.1 by Martin
Use python builtin crc32 function rather than zlib C api for _chk_map_pyx
119
    cdef unsigned long crc_val
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
120
    cdef Py_ssize_t out_off
121
    cdef char *c_out
122
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
123
    num_bits = len(key)
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
124
    # 4 bytes per crc32, and another 1 byte between bits
125
    num_out_bytes = (9 * num_bits) - 1
126
    out = PyString_FromStringAndSize(NULL, num_out_bytes)
127
    c_out = PyString_AS_STRING(out)
128
    for i from 0 <= i < num_bits:
129
        if i > 0:
130
            c_out[0] = c'\x00'
131
            c_out = c_out + 1
5165.1.2 by John Arbash Meinel
Simplify further, if this isn't going to be hot enough to matter, make the
132
        crc_val = PyInt_AsUnsignedLongMask(crc32(key[i]))
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
133
        # Hex(val) order
134
        sprintf(c_out, '%08X', crc_val)
135
        c_out = c_out + 8
136
    return out
137
138
139
def _search_key_255(key):
140
    """See chk_map._search_key_255."""
141
    cdef Py_ssize_t num_bits
142
    cdef Py_ssize_t i, j
143
    cdef Py_ssize_t num_out_bytes
5165.1.1 by Martin
Use python builtin crc32 function rather than zlib C api for _chk_map_pyx
144
    cdef unsigned long crc_val
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
145
    cdef Py_ssize_t out_off
146
    cdef char *c_out
147
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
148
    num_bits = len(key)
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
149
    # 4 bytes per crc32, and another 1 byte between bits
150
    num_out_bytes = (5 * num_bits) - 1
151
    out = PyString_FromStringAndSize(NULL, num_out_bytes)
152
    c_out = PyString_AS_STRING(out)
153
    for i from 0 <= i < num_bits:
154
        if i > 0:
155
            c_out[0] = c'\x00'
156
            c_out = c_out + 1
5165.1.2 by John Arbash Meinel
Simplify further, if this isn't going to be hot enough to matter, make the
157
        crc_val = PyInt_AsUnsignedLongMask(crc32(key[i]))
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
158
        # MSB order
159
        c_out[0] = (crc_val >> 24) & 0xFF
160
        c_out[1] = (crc_val >> 16) & 0xFF
161
        c_out[2] = (crc_val >> 8) & 0xFF
162
        c_out[3] = (crc_val >> 0) & 0xFF
163
        for j from 0 <= j < 4:
164
            if c_out[j] == c'\n':
165
                c_out[j] = c'_'
166
        c_out = c_out + 4
167
    return out
168
169
170
cdef int _get_int_from_line(char **cur, char *end, char *message) except -1:
171
    """Read a positive integer from the data stream.
172
173
    :param cur: The start of the data, this will be moved to after the
174
        trailing newline when done.
175
    :param end: Do not parse any data past this byte.
176
    :return: The integer stored in those bytes
177
    """
178
    cdef int value
179
    cdef char *next_line, *next
180
181
    next_line = <char *>memchr(cur[0], c'\n', end - cur[0])
182
    if next_line == NULL:
183
        raise ValueError("Missing %s line\n" % message)
184
185
    value = strtol(cur[0], &next, 10)
186
    if next != next_line:
187
        raise ValueError("%s line not a proper int\n" % message)
188
    cur[0] = next_line + 1
189
    return value
190
191
192
def _deserialise_leaf_node(bytes, key, search_key_func=None):
193
    """Deserialise bytes, with key key, into a LeafNode.
194
195
    :param bytes: The bytes of the node.
196
    :param key: The key that the serialised node has.
197
    """
198
    cdef char *c_bytes, *cur, *next, *end
199
    cdef char *next_line
200
    cdef Py_ssize_t c_bytes_len, prefix_length, items_length
201
    cdef int maximum_size, width, length, i, prefix_tail_len
202
    cdef int num_value_lines, num_prefix_bits
203
    cdef char *prefix, *value_start, *prefix_tail
204
    cdef char *next_null, *last_null, *line_start
205
    cdef char *c_entry, *entry_start
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
206
    cdef StaticTuple entry_bits
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
207
208
    if _LeafNode is None:
209
        from bzrlib import chk_map
210
        _LeafNode = chk_map.LeafNode
211
        _InternalNode = chk_map.InternalNode
212
        _unknown = chk_map._unknown
213
214
    result = _LeafNode(search_key_func=search_key_func)
215
    # Splitlines can split on '\r' so don't use it, split('\n') adds an
216
    # extra '' if the bytes ends in a final newline.
217
    if not PyString_CheckExact(bytes):
218
        raise TypeError('bytes must be a plain string not %s' % (type(bytes),))
219
220
    c_bytes = PyString_AS_STRING(bytes)
221
    c_bytes_len = PyString_GET_SIZE(bytes)
222
223
    if c_bytes_len < 9 or memcmp(c_bytes, "chkleaf:\n", 9) != 0:
224
        raise ValueError("not a serialised leaf node: %r" % bytes)
225
    if c_bytes[c_bytes_len - 1] != c'\n':
226
        raise ValueError("bytes does not end in a newline")
227
228
    end = c_bytes + c_bytes_len
229
    cur = c_bytes + 9
230
    maximum_size = _get_int_from_line(&cur, end, "maximum_size")
231
    width = _get_int_from_line(&cur, end, "width")
232
    length = _get_int_from_line(&cur, end, "length")
233
234
    next_line = <char *>memchr(cur, c'\n', end - cur)
235
    if next_line == NULL:
236
        raise ValueError('Missing the prefix line\n')
237
    prefix = cur
238
    prefix_length = next_line - cur
239
    cur = next_line + 1
240
241
    prefix_bits = []
242
    prefix_tail = prefix
243
    num_prefix_bits = 0
244
    next_null = <char *>memchr(prefix, c'\0', prefix_length)
245
    while next_null != NULL:
246
        num_prefix_bits = num_prefix_bits + 1
247
        prefix_bits.append(
248
            PyString_FromStringAndSize(prefix_tail, next_null - prefix_tail))
249
        prefix_tail = next_null + 1
250
        next_null = <char *>memchr(prefix_tail, c'\0', next_line - prefix_tail)
251
    prefix_tail_len = next_line - prefix_tail
252
253
    if num_prefix_bits >= width:
254
        raise ValueError('Prefix has too many nulls versus width')
255
256
    items_length = end - cur
257
    items = {}
258
    while cur < end:
259
        line_start = cur
260
        next_line = <char *>memchr(cur, c'\n', end - cur)
261
        if next_line == NULL:
262
            raise ValueError('null line\n')
263
        last_null = <char *>_my_memrchr(cur, c'\0', next_line - cur)
264
        if last_null == NULL:
265
            raise ValueError('fail to find the num value lines null')
266
        next_null = last_null + 1 # move past NULL
267
        num_value_lines = _get_int_from_line(&next_null, next_line + 1,
268
                                             "num value lines")
269
        cur = next_line + 1
270
        value_start = cur
271
        # Walk num_value_lines forward
272
        for i from 0 <= i < num_value_lines:
273
            next_line = <char *>memchr(cur, c'\n', end - cur)
274
            if next_line == NULL:
275
                raise ValueError('missing trailing newline')
276
            cur = next_line + 1
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
277
        entry_bits = StaticTuple_New(width)
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
278
        for i from 0 <= i < num_prefix_bits:
4679.9.4 by John Arbash Meinel
A bit broken, but getting there.
279
            # TODO: Use PyList_GetItem, or turn prefix_bits into a
280
            #       tuple/StaticTuple
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
281
            entry = prefix_bits[i]
282
            # SET_ITEM 'steals' a reference
283
            Py_INCREF(entry)
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
284
            StaticTuple_SET_ITEM(entry_bits, i, entry)
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
285
        value = PyString_FromStringAndSize(value_start, next_line - value_start)
286
        # The next entry bit needs the 'tail' from the prefix, and first part
287
        # of the line
288
        entry_start = line_start
289
        next_null = <char *>memchr(entry_start, c'\0',
290
                                   last_null - entry_start + 1)
291
        if next_null == NULL:
292
            raise ValueError('bad no null, bad')
293
        entry = PyString_FromStringAndSize(NULL,
294
                    prefix_tail_len + next_null - line_start)
295
        c_entry = PyString_AS_STRING(entry)
296
        if prefix_tail_len > 0:
297
            memcpy(c_entry, prefix_tail, prefix_tail_len)
298
        if next_null - line_start > 0:
299
            memcpy(c_entry + prefix_tail_len, line_start, next_null - line_start)
300
        Py_INCREF(entry)
301
        i = num_prefix_bits
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
302
        StaticTuple_SET_ITEM(entry_bits, i, entry)
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
303
        while next_null != last_null: # We have remaining bits
304
            i = i + 1
305
            if i > width:
306
                raise ValueError("Too many bits for entry")
307
            entry_start = next_null + 1
308
            next_null = <char *>memchr(entry_start, c'\0',
309
                                       last_null - entry_start + 1)
310
            if next_null == NULL:
311
                raise ValueError('bad no null')
312
            entry = PyString_FromStringAndSize(entry_start,
313
                                               next_null - entry_start)
314
            Py_INCREF(entry)
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
315
            StaticTuple_SET_ITEM(entry_bits, i, entry)
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
316
        if len(entry_bits) != width:
317
            raise AssertionError(
318
                'Incorrect number of elements (%d vs %d)'
319
                % (len(entry_bits)+1, width + 1))
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
320
        entry_bits = StaticTuple_Intern(entry_bits)
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
321
        PyDict_SetItem(items, entry_bits, value)
322
    if len(items) != length:
323
        raise ValueError("item count (%d) mismatch for key %s,"
324
                         " bytes %r" % (length, entry_bits, bytes))
325
    result._items = items
326
    result._len = length
327
    result._maximum_size = maximum_size
328
    result._key = key
329
    result._key_width = width
330
    result._raw_size = items_length + length * prefix_length
331
    if length == 0:
332
        result._search_prefix = None
333
        result._common_serialised_prefix = None
334
    else:
335
        result._search_prefix = _unknown
336
        result._common_serialised_prefix = PyString_FromStringAndSize(prefix,
337
                                                prefix_length)
338
    if c_bytes_len != result._current_size():
339
        raise AssertionError('_current_size computed incorrectly %d != %d',
340
            c_bytes_len, result._current_size())
341
    return result
342
343
344
def _deserialise_internal_node(bytes, key, search_key_func=None):
345
    cdef char *c_bytes, *cur, *next, *end
346
    cdef char *next_line
347
    cdef Py_ssize_t c_bytes_len, prefix_length
348
    cdef int maximum_size, width, length, i, prefix_tail_len
349
    cdef char *prefix, *line_prefix, *next_null, *c_item_prefix
350
351
    if _InternalNode is None:
352
        from bzrlib import chk_map
353
        _LeafNode = chk_map.LeafNode
354
        _InternalNode = chk_map.InternalNode
355
        _unknown = chk_map._unknown
356
    result = _InternalNode(search_key_func=search_key_func)
357
4679.9.4 by John Arbash Meinel
A bit broken, but getting there.
358
    if not StaticTuple_CheckExact(key):
359
        raise TypeError('key %r is not a StaticTuple' % (key,))
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
360
    if not PyString_CheckExact(bytes):
361
        raise TypeError('bytes must be a plain string not %s' % (type(bytes),))
362
363
    c_bytes = PyString_AS_STRING(bytes)
364
    c_bytes_len = PyString_GET_SIZE(bytes)
365
366
    if c_bytes_len < 9 or memcmp(c_bytes, "chknode:\n", 9) != 0:
367
        raise ValueError("not a serialised internal node: %r" % bytes)
368
    if c_bytes[c_bytes_len - 1] != c'\n':
369
        raise ValueError("bytes does not end in a newline")
370
371
    items = {}
372
    cur = c_bytes + 9
373
    end = c_bytes + c_bytes_len
374
    maximum_size = _get_int_from_line(&cur, end, "maximum_size")
375
    width = _get_int_from_line(&cur, end, "width")
376
    length = _get_int_from_line(&cur, end, "length")
377
378
    next_line = <char *>memchr(cur, c'\n', end - cur)
379
    if next_line == NULL:
380
        raise ValueError('Missing the prefix line\n')
381
    prefix = cur
382
    prefix_length = next_line - cur
383
    cur = next_line + 1
384
385
    while cur < end:
386
        # Find the null separator
387
        next_line = <char *>memchr(cur, c'\n', end - cur)
388
        if next_line == NULL:
389
            raise ValueError('missing trailing newline')
390
        next_null = <char *>_my_memrchr(cur, c'\0', next_line - cur)
391
        if next_null == NULL:
392
            raise ValueError('bad no null')
393
        item_prefix = PyString_FromStringAndSize(NULL,
394
            prefix_length + next_null - cur)
395
        c_item_prefix = PyString_AS_STRING(item_prefix)
396
        if prefix_length:
397
            memcpy(c_item_prefix, prefix, prefix_length)
398
        memcpy(c_item_prefix + prefix_length, cur, next_null - cur)
399
        flat_key = PyString_FromStringAndSize(next_null + 1,
400
                                              next_line - next_null - 1)
4679.9.1 by John Arbash Meinel
Merge in the static-tuple-no-use branch, and bring back the chk_map use.
401
        flat_key = StaticTuple(flat_key).intern()
402
        PyDict_SetItem(items, item_prefix, flat_key)
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
403
        cur = next_line + 1
404
    assert len(items) > 0
405
    result._items = items
406
    result._len = length
407
    result._maximum_size = maximum_size
408
    result._key = key
409
    result._key_width = width
410
    # XXX: InternalNodes don't really care about their size, and this will
411
    #      change if we add prefix compression
412
    result._raw_size = None # len(bytes)
413
    result._node_width = len(item_prefix)
414
    result._search_prefix = PyString_FromStringAndSize(prefix, prefix_length)
415
    return result
5218.2.1 by John Arbash Meinel
Implement a compiled extension for parsing the text key out of a CHKInventory value.
416
417
418
def _bytes_to_text_key(bytes):
419
    """Take a CHKInventory value string and return a (file_id, rev_id) tuple"""
420
    cdef StaticTuple key
421
    cdef char *byte_str, *cur_end, *file_id_str, *byte_end
422
    cdef char *revision_str
423
    cdef Py_ssize_t byte_size, pos, file_id_len
424
425
    if not PyString_CheckExact(bytes):
426
        raise TypeError('bytes must be a string')
427
    byte_str = PyString_AS_STRING(bytes)
428
    byte_size = PyString_GET_SIZE(bytes)
429
    byte_end = byte_str + byte_size
430
    cur_end = <char*>memchr(byte_str, c':', byte_size)
431
    if cur_end == NULL:
432
        raise ValueError('No kind section found.')
433
    if cur_end[1] != ' ':
434
        raise ValueError('Kind section should end with ": "')
435
    file_id_str = cur_end + 2
436
    # file_id is now the data up until the next newline
437
    cur_end = <char*>memchr(file_id_str, c'\n', byte_end - file_id_str)
438
    if cur_end == NULL:
439
        raise ValueError('no newline after file-id')
440
    file_id = safe_interned_string_from_size(file_id_str,
441
                                             cur_end - file_id_str)
442
    # this is the end of the parent_str
443
    cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
444
    if cur_end == NULL:
445
        raise ValueError('no newline after parent_str')
446
    # end of the name str
447
    cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
448
    if cur_end == NULL:
449
        raise ValueError('no newline after name str')
450
    # the next section is the revision info
451
    revision_str = cur_end + 1
452
    cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
453
    if cur_end == NULL:
454
        # This is probably a dir: entry, which has revision as the last item
455
        cur_end = byte_end
456
    revision = safe_interned_string_from_size(revision_str,
457
        cur_end - revision_str)
458
    key = StaticTuple_New(2)
459
    Py_INCREF(file_id)
460
    StaticTuple_SET_ITEM(key, 0, file_id) 
461
    Py_INCREF(revision)
462
    StaticTuple_SET_ITEM(key, 1, revision) 
463
    return StaticTuple_Intern(key)