/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 bzrlib/_chk_map_pyx.pyx

  • Committer: Martin
  • Date: 2010-05-16 15:18:43 UTC
  • mfrom: (5235 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5239.
  • Revision ID: gzlist@googlemail.com-20100516151843-lu53u7caehm3ie3i
Merge bzr.dev to resolve conflicts in NEWS and _chk_map_pyx

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
    Py_ssize_t PyTuple_GET_SIZE(object t)
36
36
    int PyString_CheckExact(object)
37
37
    char *PyString_AS_STRING(object s)
 
38
    PyObject *PyString_FromStringAndSize_ptr "PyString_FromStringAndSize" (char *, Py_ssize_t)
38
39
    Py_ssize_t PyString_GET_SIZE(object)
 
40
    void PyString_InternInPlace(PyObject **)
39
41
    long PyInt_AS_LONG(object)
40
42
 
41
43
    int PyDict_SetItem(object d, object k, object v) except -1
42
44
 
43
45
    void Py_INCREF(object)
 
46
    void Py_DECREF_ptr "Py_DECREF" (PyObject *)
44
47
 
45
48
    object PyString_FromStringAndSize(char*, Py_ssize_t)
46
49
 
47
50
# cimport all of the definitions we will need to access
48
51
from _static_tuple_c cimport StaticTuple,\
49
52
    import_static_tuple_c, StaticTuple_New, \
50
 
    StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact
 
53
    StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact, \
 
54
    StaticTuple_GET_SIZE
51
55
 
52
56
cdef object crc32
53
57
from zlib import crc32
78
82
    return NULL
79
83
 
80
84
 
 
85
cdef object safe_interned_string_from_size(char *s, Py_ssize_t size):
 
86
    cdef PyObject *py_str
 
87
    if size < 0:
 
88
        raise AssertionError(
 
89
            'tried to create a string with an invalid size: %d @0x%x'
 
90
            % (size, <int>s))
 
91
    py_str = PyString_FromStringAndSize_ptr(s, size)
 
92
    PyString_InternInPlace(&py_str)
 
93
    result = <object>py_str
 
94
    # Casting a PyObject* to an <object> triggers an INCREF from Pyrex, so we
 
95
    # DECREF it to avoid geting immortal strings
 
96
    Py_DECREF_ptr(py_str)
 
97
    return result
 
98
 
 
99
 
81
100
def _search_key_16(key):
82
101
    """See chk_map._search_key_16."""
83
102
    cdef Py_ssize_t num_bits
156
175
    return value
157
176
 
158
177
 
 
178
cdef _import_globals():
 
179
    """Set the global attributes. Done lazy to avoid recursive import loops."""
 
180
    global _LeafNode, _InternalNode, _unknown
 
181
 
 
182
    from bzrlib import chk_map
 
183
    _LeafNode = chk_map.LeafNode
 
184
    _InternalNode = chk_map.InternalNode
 
185
    _unknown = chk_map._unknown
 
186
 
 
187
 
159
188
def _deserialise_leaf_node(bytes, key, search_key_func=None):
160
189
    """Deserialise bytes, with key key, into a LeafNode.
161
190
 
173
202
    cdef StaticTuple entry_bits
174
203
 
175
204
    if _LeafNode is None:
176
 
        from bzrlib import chk_map
177
 
        _LeafNode = chk_map.LeafNode
178
 
        _InternalNode = chk_map.InternalNode
179
 
        _unknown = chk_map._unknown
 
205
        _import_globals()
180
206
 
181
207
    result = _LeafNode(search_key_func=search_key_func)
182
208
    # Splitlines can split on '\r' so don't use it, split('\n') adds an
280
306
                                               next_null - entry_start)
281
307
            Py_INCREF(entry)
282
308
            StaticTuple_SET_ITEM(entry_bits, i, entry)
283
 
        if len(entry_bits) != width:
 
309
        if StaticTuple_GET_SIZE(entry_bits) != width:
284
310
            raise AssertionError(
285
311
                'Incorrect number of elements (%d vs %d)'
286
312
                % (len(entry_bits)+1, width + 1))
316
342
    cdef char *prefix, *line_prefix, *next_null, *c_item_prefix
317
343
 
318
344
    if _InternalNode is None:
319
 
        from bzrlib import chk_map
320
 
        _LeafNode = chk_map.LeafNode
321
 
        _InternalNode = chk_map.InternalNode
322
 
        _unknown = chk_map._unknown
 
345
        _import_globals()
323
346
    result = _InternalNode(search_key_func=search_key_func)
324
347
 
325
348
    if not StaticTuple_CheckExact(key):
380
403
    result._node_width = len(item_prefix)
381
404
    result._search_prefix = PyString_FromStringAndSize(prefix, prefix_length)
382
405
    return result
 
406
 
 
407
 
 
408
def _bytes_to_text_key(bytes):
 
409
    """Take a CHKInventory value string and return a (file_id, rev_id) tuple"""
 
410
    cdef StaticTuple key
 
411
    cdef char *byte_str, *cur_end, *file_id_str, *byte_end
 
412
    cdef char *revision_str
 
413
    cdef Py_ssize_t byte_size, pos, file_id_len
 
414
 
 
415
    if not PyString_CheckExact(bytes):
 
416
        raise TypeError('bytes must be a string')
 
417
    byte_str = PyString_AS_STRING(bytes)
 
418
    byte_size = PyString_GET_SIZE(bytes)
 
419
    byte_end = byte_str + byte_size
 
420
    cur_end = <char*>memchr(byte_str, c':', byte_size)
 
421
    if cur_end == NULL:
 
422
        raise ValueError('No kind section found.')
 
423
    if cur_end[1] != c' ':
 
424
        raise ValueError('Kind section should end with ": "')
 
425
    file_id_str = cur_end + 2
 
426
    # file_id is now the data up until the next newline
 
427
    cur_end = <char*>memchr(file_id_str, c'\n', byte_end - file_id_str)
 
428
    if cur_end == NULL:
 
429
        raise ValueError('no newline after file-id')
 
430
    file_id = safe_interned_string_from_size(file_id_str,
 
431
                                             cur_end - file_id_str)
 
432
    # this is the end of the parent_str
 
433
    cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
 
434
    if cur_end == NULL:
 
435
        raise ValueError('no newline after parent_str')
 
436
    # end of the name str
 
437
    cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
 
438
    if cur_end == NULL:
 
439
        raise ValueError('no newline after name str')
 
440
    # the next section is the revision info
 
441
    revision_str = cur_end + 1
 
442
    cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
 
443
    if cur_end == NULL:
 
444
        # This is probably a dir: entry, which has revision as the last item
 
445
        cur_end = byte_end
 
446
    revision = safe_interned_string_from_size(revision_str,
 
447
        cur_end - revision_str)
 
448
    key = StaticTuple_New(2)
 
449
    Py_INCREF(file_id)
 
450
    StaticTuple_SET_ITEM(key, 0, file_id) 
 
451
    Py_INCREF(revision)
 
452
    StaticTuple_SET_ITEM(key, 1, revision) 
 
453
    return StaticTuple_Intern(key)